My dotfiles. Period.

slurp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/bash
  2. #
  3. # Slurp - add everything, slurp into a WIP commit and push
  4. #
  5. warn() {
  6. echo "$@" >&2
  7. }
  8. die() {
  9. warn "$@"
  10. exit 2
  11. }
  12. usage() {
  13. die "usage: git slurp [--force-slurp] [--force-push] [pathspec]..."
  14. }
  15. allowed_slurp() {
  16. #
  17. # Is slurp allowed in this repo?
  18. #
  19. test -f "$ok_file"
  20. }
  21. allowed_push() {
  22. #
  23. # Is push allowed for the remote branch??
  24. #
  25. # If this branch is tracking a remote branch, and the remote
  26. # branch name is listed in .git-slurp-ok after PUSH, we are
  27. # allowed to push
  28. #
  29. # The remote tracking branch is parsed; example output is here:
  30. #
  31. # $ git branch -vv
  32. # main aaf02f0 [main/master: ahead 25] Some other commit
  33. # * master add0a03 [jdsumsion/master] Some commit
  34. #
  35. local tbranch=$(
  36. git branch -vv \
  37. | grep '^\*' \
  38. | cut -d \] -f1 \
  39. | cut -d \[ -f2 \
  40. | cut -d : -f1
  41. )
  42. test -n "$tbranch" || return 0
  43. grep -qxF "PUSH $tbranch" "$ok_file" 2>/dev/null
  44. }
  45. git_relative() {
  46. #
  47. # Convert pathspec $1 to git-root-based pathspec
  48. #
  49. # Also try to clean up path a bit; i.e. remove double slashes
  50. # or unnecessary trailing dot.
  51. #
  52. local path="$1"
  53. test -n "$GIT_PREFIX" && path="$GIT_PREFIX/$path"
  54. local full=$(readlink -m "$path")
  55. test "$full" = "$git_root" && echo . && return
  56. printf %s "${full#$git_root/}"
  57. }
  58. slurp1() {
  59. #
  60. # Slurp pathspec $1
  61. #
  62. local rel_pathspec # relative to git root
  63. rel_pathspec=$(git_relative "$1")
  64. git add "$rel_pathspec" || die
  65. git commit -m "WIP slurp, $date: $rel_pathspec" || die
  66. }
  67. go_slurp() {
  68. #
  69. # Do the slurp for pathspecs $@
  70. #
  71. $force_slurp || allowed_slurp || die "you don't want this."
  72. local date=$(date -Isec)
  73. local pathspec
  74. for pathspec in "$@";
  75. do
  76. slurp1 "$pathspec"
  77. done
  78. }
  79. go_push() {
  80. #
  81. # Do the push (if allowed)
  82. #
  83. if $force_push || allowed_push;
  84. then
  85. git push -o ci.skip
  86. fi
  87. }
  88. main() {
  89. local git_root
  90. local ok_file
  91. local force_slurp=false
  92. local force_push=false
  93. while true; do case $1 in
  94. --force-slurp) force_slurp=true; shift ;;
  95. --force-push) force_push=true; shift ;;
  96. --) shift; break ;;
  97. -*) usage ;;
  98. *) break ;;
  99. esac done
  100. test -n "$1" || set -- .
  101. git_root="$(git rev-parse --show-toplevel)" || die
  102. ok_file="$git_root/.git-slurp-ok"
  103. go_slurp "$@"
  104. go_push
  105. }
  106. main "$@"