My dotfiles. Period.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. printf %s "${full#$git_root/}"
  38. }
  39. slurp1() {
  40. #
  41. # Slurp pathspec $1
  42. #
  43. local rel_pathspec # relative to git root
  44. rel_pathspec=$(git_relative "$1")
  45. git add "$rel_pathspec" || die
  46. git commit -m "WIP slurp, $date: $rel_pathspec" || die
  47. }
  48. go_slurp() {
  49. #
  50. # Do the slurp for pathspecs $@
  51. #
  52. $force_slurp || allowed_slurp || die "you don't want this."
  53. local date=$(date -Isec)
  54. local pathspec
  55. for pathspec in "$@";
  56. do
  57. slurp1 "$pathspec"
  58. done
  59. }
  60. go_push() {
  61. #
  62. # Do the push (if allowed)
  63. #
  64. if $force_push || allowed_push;
  65. then
  66. git push
  67. fi
  68. }
  69. main() {
  70. local git_root
  71. local ok_file
  72. local force_slurp=false
  73. local force_push=false
  74. while true; do case $1 in
  75. --force-slurp) force_slurp=true; shift ;;
  76. --force-push) force_push=true; shift ;;
  77. --) shift; break ;;
  78. -*) usage ;;
  79. *) break ;;
  80. esac done
  81. test -n "$1" || set -- .
  82. git_root="$(git rev-parse --show-toplevel)" || die
  83. ok_file="$git_root/.git-slurp-ok"
  84. go_slurp "$@"
  85. go_push
  86. }
  87. main "$@"