Update geoip files to match ipfire location db, 2023/11/03.
[tor.git] / scripts / git / git-install-tools.sh
blobd74f8475affa17c985662b5bc6a039bd883bd006
1 #!/usr/bin/env bash
3 SCRIPT_NAME=$(basename "$0")
4 SCRIPTS_DIR=$(dirname "$0")
6 TOOL_NAMES=(push-all pull-all merge-forward list-tor-branches resquash)
8 function usage()
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."
25 INSTALL_HOOKS=0
26 INSTALL_TOOLS=0
27 INSTALL_ALIASES=0
29 DRY_RUN=0
30 VERBOSE=0
31 FORCE=0
33 while getopts "hnfv" opt; do
34 case "$opt" in
35 h) usage
36 exit 0
38 n) DRY_RUN=1
40 v) VERBOSE=1
42 f) FORCE=1
44 *) echo
45 usage
46 exit 1
48 esac
49 done
51 for item in "${@:$OPTIND}"; do
52 case "$item" in
53 hooks) INSTALL_HOOKS=1
55 tools) INSTALL_TOOLS=1
57 aliases) INSTALL_ALIASES=1
59 all) INSTALL_HOOKS=1
60 INSTALL_TOOLS=1
61 INSTALL_ALIASES=1
63 *) echo "Unrecognized mode '$item'"
64 usage
65 exit 1
67 esac
68 done
70 if [[ $VERBOSE = 1 ]]; then
71 function note()
73 echo "$@"
75 else
76 function note()
78 true
82 function fail()
84 echo "$@" 1>&2
85 exit 1
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
93 if [[ $INSTALL_TOOLS = 1 || $INSTALL_ALIASES = 1 ]]; then
94 if [[ -z "$TOR_DEVTOOLS_DIR" ]] ; then
95 fail "\$TOR_DEVTOOLS_DIR was not set."
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.
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
116 else
117 note "It was not."
121 if [[ ! -d "$SCRIPTS_DIR" || ! -e "$SCRIPTS_DIR/git-push-all.sh" ]]; then
122 fail "Couldn't find scripts in '$SCRIPTS_DIR'"
125 if [[ $DRY_RUN = 1 ]]; then
126 echo "** DRY RUN **"
127 RUN="echo >>"
128 else
129 RUN=
132 set -e
134 # ======================================================================
135 if [[ $INSTALL_HOOKS = 1 ]]; then
136 HOOKS_DIR=$(git rev-parse --git-path hooks)
138 note "Looking for hooks directory"
140 if [[ -z "$HOOKS_DIR" || ! -d "$HOOKS_DIR" ]]; then
141 fail "Couldn't find git hooks directory."
144 note "Found hooks directory in $HOOKS_DIR"
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
154 # ======================================================================
155 if [[ $INSTALL_TOOLS = 1 ]]; then
156 note "Installing tools."
157 note "Looking for \$TOR_DEVTOOLS_DIR ($TOR_DEVTOOLS_DIR)"
159 if [[ ! -d "$TOR_DEVTOOLS_DIR" ]]; then
160 note "Creating directory"
161 $RUN mkdir -p "$TOR_DEVTOOLS_DIR"
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
170 # ======================================================================
171 if [[ $INSTALL_ALIASES = 1 ]]; then
172 note "Installing aliases."
173 note "Looking for \$TOR_DEVTOOLS_DIR ($TOR_DEVTOOLS_DIR)"
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"
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
189 note Done.