qg 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 /tmp/.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. }' /tmp/.gt-temp.txt > /tmp/.gt-temp1.txt
  79. if test $1 = 1; then
  80. cat /tmp/.gt-temp1.txt
  81. rm /tmp/.gt-temp1.txt
  82. else
  83. awk '{ gsub("\x1B[(([0-9]{1,2})?(;)?([0-9]{1,2})?)?[m,K,H,f,J]", ""); print $0 }' /tmp/.gt-temp1.txt > /tmp/.gt-temp2.txt
  84. cat /tmp/.gt-temp2.txt > /tmp/.gt-temp1.txt
  85. rm /tmp/.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. ' /tmp/.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 /tmp/.gt-temp.txt | awk '$0 ~ "git push --set-upstream" { print $0 }'`
  166. if test "$cmd" = ""; then
  167. cat /tmp/.gt-temp.txt
  168. rm /tmp/.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. shift
  187. ;;
  188. l)
  189. if test $IN_STASH = 1; then
  190. git stash list $@
  191. else
  192. git log
  193. fi
  194. ;;
  195. M)
  196. git merge $1
  197. shift
  198. ;;
  199. b)
  200. if test $IN_STASH = 1; then
  201. git stash branch $@
  202. else
  203. git branch $1
  204. shift
  205. fi
  206. ;;
  207. d)
  208. if test $IN_STASH = 1; then
  209. git stash drop $@
  210. else
  211. git diff $1
  212. fi
  213. ;;
  214. r)
  215. status 0
  216. extract $i
  217. make_file_list "restore" $filenums
  218. git restore $files
  219. status 1
  220. ;;
  221. R)
  222. next=`echo $cmds | cut -d\ -f${i+2}`
  223. if test "$next" = ""; then
  224. removetemp
  225. git rm $@
  226. else
  227. status 0
  228. extract $i
  229. make_file_list "add" $filenums
  230. git rm $files
  231. removetemp
  232. fi
  233. ;;
  234. g)
  235. git grep -n "$1"
  236. ;;
  237. q)
  238. break
  239. ;;
  240. *)
  241. ;;
  242. esac
  243. done
  244. exit 0