warbo-utilities: a2cc822004af49c27384007b3c106bb272dcefdb
1: #!/usr/bin/env bash
2: set -e
3:
4: ## Looks in the current directory for a git repo and integrates it with our git
5: ## hosting infrastructure. This script should be idempotent, so we can run it in
6: ## an existing repo to set up new parts of our infrastructure.
7:
8: REPOS="/home/chris/Programming/repos"
9:
10: # Ensure we're in an appropriate git repo, and get its top-level directory
11:
12: if echo "$PWD" | grep -q "$REPOS"
13: then
14: echo "trackGit should be called from a working copy, not a bare clone" 1>&2
15: exit 1
16: fi
17:
18: if ! ROOT=$(git rev-parse --show-toplevel)
19: then
20: echo "trackGit must be run from a git repo. Try 'git init' first." 1>&2
21: exit 1
22: fi
23:
24: # Working dirs can be called anything, so check upstream for a repo name
25:
26: origin=""
27: name=""
28: if git remote | grep -q '^origin$'
29: then
30: # We have an 'origin' remote. Check if it points to our $REPOS dir
31: origin=$(git remote get-url origin)
32: if echo "$origin" | grep -q "$REPOS"
33: then
34: name=$(basename "$origin" .git)
35: fi
36: fi
37:
38: # Repo name is important since it's used in paths, etc. so confirm with user
39:
40: if [[ -n "$name" ]]
41: then
42: read -r -p "Found repo name '$name', is this correct? (Y/n)? " answer
43: case "${answer:0:1}" in
44: n|N )
45: name=""
46: ;;
47: esac
48: fi
49:
50: if [[ -z "$name" ]]
51: then
52: echo "Enter project name (.git will be appended automatically)"
53: read -r name
54: fi
55:
56: # Make sure we have a local bare clone in $REPOS, following our naming scheme
57:
58: localRepo="$REPOS/$name.git"
59:
60: if [[ -d "$localRepo" ]]
61: then
62: echo "Found existing '$localRepo'" 1>&2
63: else
64: pushd "$REPOS" > /dev/null
65: echo "Cloning $ROOT to $localRepo" 1>&2
66: git clone --bare "$ROOT" "$name.git"
67: popd > /dev/null
68: pushd "$localRepo" > /dev/null
69: git remote rm origin # We're upstream, but default is downstream
70: popd > /dev/null
71: fi
72:
73: # Make sure the local bare clone is setup to push to a remote bare clone
74:
75: remoteRepo="chris@chriswarbo.net:/opt/repos/$name.git"
76: pushd "$localRepo" > /dev/null
77: rOrigin=""
78: if git remote | grep '^origin$'
79: then
80: rOrigin=$(git remote get-url origin)
81: fi
82: if [[ "$rOrigin" = "$remoteRepo" ]]
83: then
84: echo "$localRepo is setup to push to chriswarbo.net" 1>&2
85: else
86: if [[ -n "$rOrigin" ]]
87: then
88: echo "Expected $localRepo origin $remoteRepo not '$rOrigin'" 1>&2
89: read -r -p "Keep origin '$rOrigin' of $localRepo? (Y/n)? " answer
90: case "${answer:0:1}" in
91: n|N )
92: git remote rm origin
93: rOrigin=""
94: ;;
95: esac
96: fi
97: fi
98: if [[ -z "$rOrigin" ]]
99: then
100: echo "Pointing $localRepo to $remoteRepo" 1>&2
101: git remote add origin "$remoteRepo"
102: fi
103: popd > /dev/null
104:
105: # Make sure the working dir is setup to push to the local bare clone
106:
107: origin=""
108: if git remote | grep '^origin$' 1>&2
109: then
110: # We have an 'origin' remote, we need to see if it's our $REPOS dir
111: origin=$(git remote get-url origin)
112: fi
113: if [[ "$origin" = "$localRepo" ]]
114: then
115: echo "$ROOT is setup to push to $localRepo" 1>&2
116: else
117: if [[ -n "$origin" ]]
118: then
119: echo "Expected $ROOT origin $localRepo not '$origin'" 1>&2
120: read -r -p "Keep origin '$origin' of $ROOT? (Y/n)? " answer
121: case "${answer:0:1}" in
122: n|N )
123: git remote rm origin
124: origin=""
125: ;;
126: esac
127: fi
128: fi
129: if [[ -z "$origin" ]]
130: then
131: echo "Adding $localRepo as remote origin for $ROOT" 1>&2
132: git remote add origin "$localRepo"
133: fi
134:
135: # Check if we need to generate HTML pages for this repo
136: HTML="/opt/html/$name"
137: if ssh chriswarbo.net true
138: then
139: # shellcheck disable=SC2029
140: if ssh chriswarbo.net "test -e '$HTML/index.html'"
141: then
142: echo "Found existing HTML directory '$HTML' on chriswarbo.net" 1>&2
143: else
144: MAKEHTML=1
145: echo "No HTML '$HTML/index.html' found on chriswarbo.net" 1>&2
146: read -r -p "Should we create 'chriswarbo.net:$HTML'? (Y/n)? " answer
147: case "${answer:0:1}" in
148: n|N )
149: MAKEHTML=0
150: ;;
151: esac
152: [[ "$MAKEHTML" -eq 0 ]] || pushGitPages "$localRepo"
153: unset MAKEHTML
154: fi
155: else
156: echo "Couldn't ssh to chriswarbo.net, not checking for HTML pages" 1>&2
157: fi
158:
159: # Check if we have an asv.conf.json
160: ASV_CONF=0
161: # shellcheck disable=SC2034
162: while read -r F
163: do
164: ASV_CONF=1
165: done < <(find . -name 'asv.conf.json')
166:
167: # If we have no asv.conf.json, ask to make one
168: ASV_TEMPLATE="$HOME/.templates/asv.conf.json.template"
169: if [[ "$ASV_CONF" -eq 0 ]] && [[ -e "$ASV_TEMPLATE" ]]
170: then
171: read -r -p "No asv.conf.json, create from $ASV_TEMPLATE? (y/N)? " answer
172: case "${answer:0:1}" in
173: y|Y )
174: ASV_CONF=1
175: ;;
176: esac
177: if [[ "$ASV_CONF" -eq 1 ]]
178: then
179: sed -e "s/PROJECT_NAME/$name/g" < "$ASV_TEMPLATE" > asv.conf.json
180: fi
181: fi
182: unset ASV_TEMPLATE
183:
184: # If we have an asv.conf.json in the default location, see if its benchmark_dir
185: # already exists, and ask to create if not
186: ASV_CONF="$PWD/asv.conf.json"
187: if [[ -e "$ASV_CONF" ]]
188: then
189: BENCHMARK_DIR=$(grep -v '^ *//' < "$ASV_CONF" | jq -r '.benchmark_dir')
190: BENCHMARK_DIR="$PWD/$BENCHMARK_DIR"
191: [[ -e "$BENCHMARK_DIR" ]] || {
192: read -r -p "Dir $BENCHMARK_DIR not found, create (y/N)? " answer
193: case "${answer:0:1}" in
194: y|Y )
195: mkdir -p "$BENCHMARK_DIR"
196: ;;
197: esac
198: }
199:
200: # Benchmark dir should contain an __init__.py to avoid problems
201: [[ -e "$BENCHMARK_DIR" ]] && {
202: BENCHMARK_PY="$BENCHMARK_DIR/__init__.py"
203: [[ -e "$BENCHMARK_PY" ]] || {
204: read -r -p "No $BENCHMARK_PY found, create empty one (y/N)? " answer
205: case "${answer:0:1}" in
206: y|Y )
207: touch "$BENCHMARK_PY"
208: ;;
209: esac
210: }
211: unset BENCHMARK_PY
212: }
213:
214: # Should we ask to create a default.nix in the benchmark_dir?
215: BENCHMARK_TEMPLATE="$HOME/.templates/benchmark_default.template"
216: if [[ -e "$BENCHMARK_DIR" ]] && [[ -e "$BENCHMARK_TEMPLATE" ]]
217: then
218: BENCHMARK_NIX="$BENCHMARK_DIR/default.nix"
219: [[ -e "$BENCHMARK_NIX" ]] || {
220: read -r -p "No $BENCHMARK_NIX, create one? (y/N)? " answer
221: case "${answer:0:1}" in
222: y|Y )
223: cp -v "$BENCHMARK_TEMPLATE" "$BENCHMARK_NIX"
224: ;;
225: esac
226: }
227: unset BENCHMARK_NIX
228: fi
229: unset BENCHMARK_TEMPLATE
230: unset BENCHMARK_DIR
231: fi
232: unset ASV_CONF
233:
234: echo "Checking $REPOS in case we made changes which need propagating" 1>&2
235: cd "$REPOS" || exit 1
236: sh check.sh
Generated by git2html.