My dotfiles. Period.

slurp 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 in this repo?
  24. #
  25. grep -qxF PUSH "$ok_file" 2>/dev/null
  26. }
  27. git_relative() {
  28. #
  29. # Convert pathspec $1 to git-root-based pathspec
  30. #
  31. # Also try to clean up path a bit; i.e. remove double slashes
  32. # or unnecessary trailing dot.
  33. #
  34. local path="$1"
  35. test -n "$GIT_PREFIX" && path="$GIT_PREFIX/$path"
  36. local full=$(readlink -m "$path")
  37. test "$full" = "$git_root" && echo . && return
  38. printf %s "${full#$git_root/}"
  39. }
  40. slurp1() {
  41. #
  42. # Slurp pathspec $1
  43. #
  44. local rel_pathspec # relative to git root
  45. rel_pathspec=$(git_relative "$1")
  46. git add "$rel_pathspec" || die
  47. git commit -m "WIP slurp, $date: $rel_pathspec" || die
  48. }
  49. go_slurp() {
  50. #
  51. # Do the slurp for pathspecs $@
  52. #
  53. $force_slurp || allowed_slurp || die "you don't want this."
  54. local date=$(date -Isec)
  55. local pathspec
  56. for pathspec in "$@";
  57. do
  58. slurp1 "$pathspec"
  59. done
  60. }
  61. go_push() {
  62. #
  63. # Do the push (if allowed)
  64. #
  65. if $force_push || allowed_push;
  66. then
  67. git push
  68. fi
  69. }
  70. main() {
  71. local git_root
  72. local ok_file
  73. local force_slurp=false
  74. local force_push=false
  75. while true; do case $1 in
  76. --force-slurp) force_slurp=true; shift ;;
  77. --force-push) force_push=true; shift ;;
  78. --) shift; break ;;
  79. -*) usage ;;
  80. *) break ;;
  81. esac done
  82. test -n "$1" || set -- .
  83. git_root="$(git rev-parse --show-toplevel)" || die
  84. ok_file="$git_root/.git-slurp-ok"
  85. go_slurp "$@"
  86. go_push
  87. }
  88. main "$@"