qg 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/bin/sh
  2. extract()
  3. {
  4. filenums=`echo $cmds | awk -v idx=$1 '{
  5. split($0, a, " ");
  6. list = "";
  7. for(v in a) {
  8. if(v > idx) {
  9. if(a[v] ~ /[0-9]/ || a[v] ~ "-" || a[v] ~ ",") {
  10. list = list a[v];
  11. }
  12. }
  13. }
  14. split(list, arr, ",");
  15. final="";
  16. for(i in arr) {
  17. if(arr[i] ~ "-") {
  18. split(arr[i], inner, "-");
  19. for(k = inner[1]; k <= inner[2]; k++) {
  20. final=final "," k;
  21. }
  22. } else {
  23. final=final "," arr[i]
  24. }
  25. }
  26. printf("%s", substr(final, 2));
  27. }'`
  28. }
  29. removetemp()
  30. {
  31. filepaths=`git status | awk '
  32. $0 ~ ".gt-temp1.txt" || $0 ~ ".gt-temp.txt" {
  33. printf("%s ", $1);
  34. }
  35. '`
  36. if test "$filepaths" != ""; then
  37. rm $filepaths
  38. fi
  39. }
  40. status()
  41. {
  42. cmd=`git status 2>&1 | tee .gt-temp.txt`
  43. awk '
  44. BEGIN {
  45. num = 0;
  46. }
  47. $0 ~ "Untracked" {
  48. untracked = 1
  49. committed = 0;
  50. notcommitted = 0;
  51. first = 1;
  52. }
  53. $0 ~ "Changes not" {
  54. untracked = 0;
  55. committed = 0;
  56. notcommitted = 1;
  57. first = 1;
  58. }
  59. $0 ~ "Changes to" {
  60. untracked = 0;
  61. committed = 1;
  62. notcommitted = 0;
  63. first = 1;
  64. }
  65. {
  66. if($0 ~ "use \"git " || first) {
  67. printf("\033[38m%s\033[0m\n", $0);
  68. first = 0;
  69. } else if($0 ~ ".gt-temp") {
  70. } else if(committed && $0 ~ /[^\s\\]/) {
  71. num++;
  72. printf("\033[1m%d\033[0m.\033[32m%s\033[0m\n", num, $0)
  73. } else if((untracked || notcommitted) && $0 ~ /[^\s\\]/) {
  74. num++;
  75. printf("\033[1m%d\033[0m.\033[31m%s\033[0m\n", num, $0)
  76. } else
  77. printf("\033[38m%s\033[0m\n", $0)
  78. }' .gt-temp.txt > .gt-temp1.txt
  79. if test $1 = 1; then
  80. cat .gt-temp1.txt
  81. rm .gt-temp1.txt
  82. else
  83. awk '{ gsub("\x1B[(([0-9]{1,2})?(;)?([0-9]{1,2})?)?[m,K,H,f,J]", ""); print $0 }' .gt-temp1.txt > .gt-temp2.txt
  84. cat .gt-temp2.txt > .gt-temp1.txt
  85. rm .gt-temp2.txt
  86. fi
  87. }
  88. # TODO: work with file names and paths
  89. make_file_list()
  90. {
  91. files=`awk -v t=$1 -v arg=$2 '
  92. BEGIN {
  93. split(arg, nums, ",");
  94. ff = 0;
  95. }
  96. {
  97. if($0 ~ "--staged" && ff == 0 && t == "restore") {
  98. printf("--staged ");
  99. }
  100. if($1 ~ /[\.]/) {
  101. for(i in nums) {
  102. withp = nums[i]"."
  103. if(withp == $1) {
  104. printf("%s ", $NF);
  105. ff++;
  106. }
  107. }
  108. }
  109. }
  110. ' .gt-temp1.txt`
  111. }
  112. if test "$1" = ""; then
  113. status 1
  114. removetemp
  115. exit 1
  116. fi
  117. cmds=`echo $1 | sed 's/\(.\)/\1 /g'`
  118. shift
  119. IN_STASH=0
  120. i=0
  121. echo "$cmds" | tr ' ' '\n' | while read c;
  122. do
  123. i=${i+1}
  124. case $c in
  125. a)
  126. next=`echo $cmds | cut -d\ -f${i+2}`
  127. if test "$next" = "."; then
  128. removetemp
  129. git add .
  130. elif test "$next" = "a"; then
  131. git add $@
  132. else
  133. status 0
  134. extract $i
  135. make_file_list "add" $filenums
  136. git add $files
  137. removetemp
  138. fi
  139. ;;
  140. s)
  141. if test $IN_STASH = 1; then
  142. git stash show $@
  143. else
  144. status 1
  145. fi
  146. ;;
  147. S)
  148. next=`echo $cmds | cut -d\ -f${i+2}`
  149. if test $IN_STASH = 1; then
  150. git stash save $@
  151. elif test "$next" = "S"; then
  152. git stash
  153. else
  154. IN_STASH=1
  155. fi
  156. ;;
  157. p)
  158. if test $IN_STASH = 1; then
  159. git stash pop $@
  160. else
  161. git pull
  162. fi
  163. ;;
  164. P)
  165. cmd=`git push 2>&1 | tee .gt-temp.txt | awk '$0 ~ "git push --set-upstream" { print $0 }'`
  166. if test "$cmd" = ""; then
  167. cat .gt-temp.txt
  168. rm .gt-temp.txt
  169. exit 0
  170. else
  171. echo "Running: ${cmd}"
  172. removetemp
  173. eval $cmd
  174. exit 0
  175. fi
  176. ;;
  177. c)
  178. if test $IN_STASH = 1; then
  179. git stash clear $@
  180. else
  181. git commit
  182. fi
  183. ;;
  184. C)
  185. git checkout $1
  186. ;;
  187. l)
  188. if test $IN_STASH = 1; then
  189. git stash list $@
  190. else
  191. git log
  192. fi
  193. ;;
  194. M)
  195. git merge $1
  196. ;;
  197. b)
  198. if test $IN_STASH = 1; then
  199. git stash branch $@
  200. else
  201. git branch $1
  202. fi
  203. ;;
  204. d)
  205. if test $IN_STASH = 1; then
  206. git stash drop $@
  207. else
  208. git diff $1
  209. fi
  210. ;;
  211. r)
  212. status 0
  213. extract $i
  214. make_file_list "restore" $filenums
  215. git restore $files
  216. status 1
  217. ;;
  218. R)
  219. next=`echo $cmds | cut -d\ -f${i+2}`
  220. if test "$next" = ""; then
  221. removetemp
  222. git rm $@
  223. else
  224. status 0
  225. extract $i
  226. make_file_list "add" $filenums
  227. git rm $files
  228. removetemp
  229. fi
  230. ;;
  231. g)
  232. git grep -n "$1"
  233. ;;
  234. q)
  235. break
  236. ;;
  237. *)
  238. ;;
  239. esac
  240. done
  241. exit 0