git-install-tools.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/env bash
  2. SCRIPT_NAME=$(basename "$0")
  3. SCRIPTS_DIR=$(dirname "$0")
  4. TOOL_NAMES=(push-all pull-all merge-forward list-tor-branches)
  5. function usage()
  6. {
  7. echo "$SCRIPT_NAME [-h] [-n] [-v] [-f] <all|hooks|tools|aliases>"
  8. echo
  9. echo " flags:"
  10. echo " -h: show this help text"
  11. echo " -n: dry-run"
  12. echo " -v: verbose mode"
  13. echo " -f: force-install even if \$TOR_DEVTOOLS_DIR looks fishy"
  14. echo
  15. echo " modes:"
  16. echo " hooks: install git hooks in this repository."
  17. echo " tools: install scripts in \$TOR_DEVTOOLS_DIR"
  18. echo " aliases: set up global git aliases for git tools in \$TOR_DEVTOOLS_DIR"
  19. echo " all: all of the above."
  20. }
  21. INSTALL_HOOKS=0
  22. INSTALL_TOOLS=0
  23. INSTALL_ALIASES=0
  24. DRY_RUN=0
  25. VERBOSE=0
  26. FORCE=0
  27. while getopts "hnfv" opt; do
  28. case "$opt" in
  29. h) usage
  30. exit 0
  31. ;;
  32. n) DRY_RUN=1
  33. ;;
  34. v) VERBOSE=1
  35. ;;
  36. f) FORCE=1
  37. ;;
  38. *) echo
  39. usage
  40. exit 1
  41. ;;
  42. esac
  43. done
  44. for item in "${@:$OPTIND}"; do
  45. case "$item" in
  46. hooks) INSTALL_HOOKS=1
  47. ;;
  48. tools) INSTALL_TOOLS=1
  49. ;;
  50. aliases) INSTALL_ALIASES=1
  51. ;;
  52. all) INSTALL_HOOKS=1
  53. INSTALL_TOOLS=1
  54. INSTALL_ALIASES=1
  55. ;;
  56. *) echo "Unrecognized mode '$item'"
  57. usage
  58. exit 1
  59. ;;
  60. esac
  61. done
  62. if [[ $VERBOSE = 1 ]]; then
  63. function note()
  64. {
  65. echo "$@"
  66. }
  67. else
  68. function note()
  69. {
  70. true
  71. }
  72. fi
  73. function fail()
  74. {
  75. echo "$@" 1>&2
  76. exit 1
  77. }
  78. if [[ $INSTALL_HOOKS = 0 && $INSTALL_TOOLS = 0 && $INSTALL_ALIASES = 0 ]]; then
  79. echo "Nothing to do. Try $SCRIPT_NAME -h for a list of commands."
  80. exit 0
  81. fi
  82. if [[ $INSTALL_TOOLS = 1 || $INSTALL_ALIASES = 1 ]]; then
  83. if [[ -z "$TOR_DEVTOOLS_DIR" ]] ; then
  84. fail "\$TOR_DEVTOOLS_DIR was not set."
  85. fi
  86. note "Checking whether \$TOR_DEVTOOLS_DIR ($TOR_DEVTOOLS_DIR) is a git repo..."
  87. GITDIR=$(cd "$TOR_DEVTOOLS_DIR" && git rev-parse --git-dir 2>/dev/null)
  88. note "GITDIR is $GITDIR"
  89. if [[ -n "$GITDIR" ]] ; then
  90. cat <<EOF
  91. You have asked me to install to \$TOR_DEVTOOLS_DIR ($TOR_DEVTOOLS_DIR).
  92. That is inside a git repository, so you might not want to install there:
  93. depending on what you pull or push, you might find yourself giving somebody
  94. else write access to your scripts. I think you should just use ~/bin or
  95. something.
  96. EOF
  97. echo
  98. if [[ "$FORCE" = 1 ]] ; then
  99. echo "I will install anyway, since you said '-f'."
  100. else
  101. echo "I will not install. You can tell me -f if you are really sure."
  102. exit 1
  103. fi
  104. else
  105. note "It was not."
  106. fi
  107. fi
  108. if [[ ! -d "$SCRIPTS_DIR" || ! -e "$SCRIPTS_DIR/git-push-all.sh" ]]; then
  109. fail "Couldn't find scripts in '$SCRIPTS_DIR'"
  110. fi
  111. if [[ $DRY_RUN = 1 ]]; then
  112. echo "** DRY RUN **"
  113. RUN="echo >>"
  114. else
  115. RUN=
  116. fi
  117. set -e
  118. # ======================================================================
  119. if [[ $INSTALL_HOOKS = 1 ]]; then
  120. HOOKS_DIR=$(git rev-parse --git-path hooks)
  121. note "Looking for hooks directory"
  122. if [[ -z "$HOOKS_DIR" || ! -d "$HOOKS_DIR" ]]; then
  123. fail "Couldn't find git hooks directory."
  124. fi
  125. note "Found hooks directory in $HOOKS_DIR"
  126. note "Installing hooks"
  127. for fn in "$SCRIPTS_DIR"/*.git-hook; do
  128. name=$(basename "$fn")
  129. $RUN install --backup "$fn" "${HOOKS_DIR}/${name%.git-hook}"
  130. done
  131. fi
  132. # ======================================================================
  133. if [[ $INSTALL_TOOLS = 1 ]]; then
  134. note "Installing tools."
  135. note "Looking for \$TOR_DEVTOOLS_DIR ($TOR_DEVTOOLS_DIR)"
  136. if [[ ! -d "$TOR_DEVTOOLS_DIR" ]]; then
  137. note "Creating directory"
  138. $RUN mkdir -p "$TOR_DEVTOOLS_DIR"
  139. fi
  140. note "Copying scripts"
  141. for tool in "${TOOL_NAMES[@]}"; do
  142. $RUN install --backup "${SCRIPTS_DIR}/git-${tool}.sh" "${TOR_DEVTOOLS_DIR}/"
  143. done
  144. fi
  145. # ======================================================================
  146. if [[ $INSTALL_ALIASES = 1 ]]; then
  147. note "Installing aliases."
  148. note "Looking for \$TOR_DEVTOOLS_DIR ($TOR_DEVTOOLS_DIR)"
  149. note "Checking for ${TOR_DEVTOOLS_DIR}/git-push-all.sh"
  150. if [[ ! -x "${TOR_DEVTOOLS_DIR}/git-push-all.sh" ]]; then
  151. if [[ $DRY_RUN = 0 ]]; then
  152. fail "Could not find scripts in \$TOR_DEVTOOLS_DIR"
  153. fi
  154. fi
  155. note "Setting aliases"
  156. for tool in "${TOOL_NAMES[@]}"; do
  157. $RUN git config --global "alias.$tool" \!"${TOR_DEVTOOLS_DIR}/git-${tool}.sh"
  158. done
  159. fi
  160. note Done.