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