-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-branchdir-manager.sh
509 lines (450 loc) · 14.5 KB
/
git-branchdir-manager.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
alias b='git-branchdir-manager'
alias gbm='git-branchdir-manager'
complete -F _gb_complete b
complete -F _gb_complete gbm
complete -F _gb_complete git-branchdir-manager
SOURCE=${BASH_SOURCE[0]}
while readlink $SOURCE; do
SOURCE=$(readlink $SOURCE)
done
export GB_VERSION=$(cat ${SOURCE/git-branchdir-manager.sh/VERSION} 2> /dev/null || echo 0)
export GB_GIT_NEW_WORKDIR="$(cd -P "${SOURCE/git-branchdir-manager.sh/}" && pwd)/git-new-workdir"
function _gb_env {
[ -z "$GB_BASE_DIR" ] && GB_BASE_DIR="$HOME/gbm"
[ -z "$GB_DEV_BRANCH" ] && GB_DEV_BRANCH="master"
[ -z "$GB_DEV_REMOTE" ] && GB_DEV_REMOTE="origin"
[ -z "$GB_MASTER_DIR_NAME" ] && GB_MASTER_DIR_NAME=".gb_master"
[ -z "$GB_MASTER_BRANCH" ] && GB_MASTER_BRANCH="gb_master"
[ -z "$GB_DEV_REMOTE_REF" ] && GB_DEV_REMOTE_REF="$GB_DEV_REMOTE/$GB_DEV_BRANCH"
[ -z "$GB_WORKFLOW" ] && GB_WORKFLOW="rebase"
}
function _gb_help {
echo "Usage:"
echo " gbm <repo_name> init <repo_url>"
echo " gbm <repo_name> <branch_name>"
echo " gbm <repo_name> <branch_name> update"
echo " gbm <repo_name> <branch_name> publish"
echo " gbm <repo_name> <branch_name> rm"
echo " gbm <repo_name> <branch_name> lib"
}
function _gb_version_check {
local remote_version=$(curl -f "https://raw.github.com/nnutter/git-branchdir-manager/master/VERSION" 2> /dev/null)
if [ -n "$remote_version" ] && (( "$remote_version" > "$GB_VERSION" )); then
echo "Updated git-branchdir-manager version ($remote_version) available."
fi
}
function _gb_current_repo {
_gb_env
local SUBDIR="${PWD#$GB_BASE_DIR\/}" || return 255;
local REPO="${SUBDIR/\/*/}"
if [ -n "$REPO" ]; then
echo "$REPO"
else
return 255;
fi
}
function _gb_current_branch {
_gb_env
local REPO=$(_gb_current_repo) || return 255;
local SUBDIR="${PWD#$GB_BASE_DIR\/$REPO\/}" || return 255;
local BRANCH="${SUBDIR/\/*/}"
if [ -n "$BRANCH" ]; then
echo "$BRANCH"
else
return 255;
fi
}
function _gb_repos {
_gb_env
if [ -d "$GB_BASE_DIR" ]; then
find "$GB_BASE_DIR" -mindepth 1 -maxdepth 2 -type d -name $GB_MASTER_DIR_NAME | sed -e "s|$GB_BASE_DIR/||" -e "s|/$GB_MASTER_DIR_NAME||"
fi
}
function _gb_repo_master_dir {
_gb_env
local GB_REPO="$1"
local GB_BRANCH="$GB_MASTER_DIR_NAME"
local GB_BRANCH_DIR="$GB_BASE_DIR/$GB_REPO/$GB_BRANCH"
echo "$GB_BRANCH_DIR"
}
function _gb_branches {
_gb_env
local GB_REPO=$1
local GB_REPO_DIR="$GB_BASE_DIR/$GB_REPO"
if [ -d "$GB_REPO_DIR" ]; then
find "$GB_REPO_DIR" -mindepth 1 -maxdepth 2 -type d -name .git | sed -e "s|$GB_REPO_DIR/||" -e "s|/.git||" | grep -v "$GB_MASTER_DIR_NAME"
fi
}
function _gb_branch_dir {
_gb_env
local GB_REPO="$1"
local GB_BRANCH="$2"
local GB_BRANCH_DIR="$GB_BASE_DIR/$GB_REPO/$GB_BRANCH"
echo "$GB_BRANCH_DIR"
}
function _gb_lib_path {
_gb_env
local GB_REPO="$1"
local GB_BRANCH="$2"
local GB_BRANCH_DIR="$GB_BASE_DIR/$GB_REPO/$GB_BRANCH"
local GB_LIB_DIR
[ -d "$GB_BRANCH_DIR/lib" ] && GB_LIB_DIR="$GB_BRANCH_DIR/lib"
[ -d "$GB_BRANCH_DIR/lib/perl" ] && GB_LIB_DIR="$GB_BRANCH_DIR/lib/perl"
echo "$GB_LIB_DIR"
}
function _gb_cd_lib_dir {
local GB_BRANCH_DIR=$1
if [ -d "$GB_BRANCH_DIR/lib/perl/Genome" ]; then
cd "$GB_BRANCH_DIR/lib/perl/Genome"
else
[ -d "$GB_BRANCH_DIR/lib" ] && cd "$GB_BRANCH_DIR/lib"
fi
return 0
}
function _gb_cd_branch {
_gb_env
GB_REPO="$1"
GB_BRANCH="$2"
local GB_BRANCH_DIR="$GB_BASE_DIR/$GB_REPO/$GB_BRANCH"
if [ ! -d "$GB_BRANCH_DIR" ]; then
echo "ERROR: Branch directory does not exist ($GB_BRANCH_DIR)."
echo -n "Do you wish to create the branch (y/n)? "
local response
read response
if [ "$response" == "y" ]; then
_gb_start_branch "$GB_REPO" "$GB_BRANCH"
else
return 255
fi
fi
cd "$GB_BRANCH_DIR"
GB_BRANCH="$(_git_current_branch)"
_gb_cd_lib_dir "$GB_BRANCH_DIR"
}
function _git_tracking_ref {
_gb_env
local GB_UPSTREAM_BRANCH=$(git config --get branch.$GB_BRANCH.merge | sed 's/^refs\/heads\///')
local GB_UPSTREAM_REMOTE=$(git config --get branch.$GB_BRANCH.remote)
if [ -z "$GB_UPSTREAM_BRANCH" ] || [ -z "$GB_UPSTREAM_REMOTE" ]; then
return 255
else
echo "$GB_UPSTREAM_REMOTE/$GB_UPSTREAM_BRANCH"
fi
}
function _gb_rm_branch {
_gb_env
local GB_REPO="$1"
local GB_BRANCH="$2"
local GB_BRANCH_DIR="$GB_BASE_DIR/$GB_REPO/$GB_BRANCH"
if [ ! -d "$GB_BRANCH_DIR" ]; then
echo "ERROR: Branch directory does not exist ($GB_BRANCH_DIR)."
return 255
fi
PRIOR_DIR=$(pwd)
[[ -d "$GB_BRANCH_DIR" ]] && cd "$GB_BRANCH_DIR"
local TRACKING_REF=$(_git_tracking_ref "$GB_BRANCH")
[[ -z "$TRACKING_REF" ]] && return 255
local COUNT="$(git log --oneline $TRACKING_REF..HEAD | wc -l | sed 's/^ *//')"
if [ "$COUNT" != "0" ]; then
echo "ERROR: $COUNT unpushed commits in repo:"
git log --oneline $TRACKING_REF..$GB_BRANCH | cat
echo -n "Do you wish to remove this branch (y/n)? "
local response
read response
if [ "$response" != "y" ]; then
return 255;
fi
fi
COUNT="$(git status -s | wc -l | sed 's/^ *//')"
if [ "$COUNT" != "0" ]; then
echo "ERROR: Uncommitted changes in repo:"
git status -s
echo -n "Do you wish to remove this branch (y/n)? "
local response
read response
if [ "$response" != "y" ]; then
return 255;
fi
fi
cd "$GB_BASE_DIR/$GB_REPO/$GB_MASTER_DIR_NAME"
echo "Removing '$GB_BRANCH_DIR'..."
sleep 1
rm -rf "$GB_BRANCH_DIR"
git branch -D "$GB_BRANCH"
if [ -d "$PRIOR_DIR" ]; then
cd $PRIOR_DIR
else
if [ -d $(_gb_branch_dir "$GB_REPO" "$GB_DEV_BRANCH") ]; then
_gb_cd_branch "$GB_REPO" "$GB_DEV_BRANCH"
else
cd
fi
fi
}
function _gb_refresh_master {
_gb_env
local GB_REPO="$1"
if [ -z "$GB_REPO" ]; then
return 255
fi
local ORIG_DIR="$PWD"
local ORIG_BRANCH="$GB_BRANCH"
local GB_MASTER_DIR="$(_gb_branch_dir "$GB_REPO" "$GB_MASTER_DIR_NAME")"
cd "$GB_MASTER_DIR"
if [ "$(_git_current_branch)" != "$GB_MASTER_BRANCH" ]; then
git checkout "$GB_MASTER_BRANCH"
fi
git fetch -q
git merge -q "$GB_DEV_REMOTE_REF"
git reset -q --hard "$GB_DEV_REMOTE_REF"
git clean -qxdf
cd "$ORIG_DIR"
}
function _gb_start_branch {
_gb_env
local GB_REPO="$1"
local GB_BRANCH="$2"
local GB_MASTER_DIR="$GB_BASE_DIR/$GB_REPO/$GB_MASTER_DIR_NAME"
local GB_BRANCH_DIR="$GB_BASE_DIR/$GB_REPO/$GB_BRANCH"
if [ ! -d "$GB_MASTER_DIR" ]; then
echo "ERROR: git-branchdir-manager master directory does not exist ($GB_MASTER_DIR)."
return 255
fi
if [ -d "$GB_BRANCH_DIR" ]; then
echo "ERROR: Branch directory already exists ($GB_BRANCH_DIR)."
return 255
fi
_gb_refresh_master "$GB_REPO" || return 255
$GB_GIT_NEW_WORKDIR "$GB_MASTER_DIR" "$GB_BRANCH_DIR"
cd "$GB_BRANCH_DIR"
if _git_branch_exists "$GB_BRANCH"; then
git checkout -q "$GB_BRANCH"
local TRACKING_REF=$(_git_tracking_ref "$GB_BRANCH")
if [ -n "$TRACKING_REF" ] && [ "$TRACKING_REF" != "$GB_DEV_REMOTE/$GB_DEV_BRANCH" ]; then
echo -n "Would you like to track the remote $GB_BRANCH branch (y/n)? "
local response
read response
if [ "$response" == "y" ]; then
git reset --hard "$TRACKING_REF"
else
git branch --set-upstream "$GB_BRANCH" "$GB_DEV_REMOTE_REF"
git $GB_WORKFLOW "$GB_DEV_REMOTE_REF"
fi
fi
else
git checkout -q -b "$GB_BRANCH" -t "$GB_DEV_REMOTE_REF"
fi
_gb_cd_lib_dir "$GB_BRANCH_DIR"
}
function _gb_update_branch {
_gb_env
local GB_REPO="$1"
local GB_BRANCH="$2"
_gb_cd_branch "$GB_REPO" "$GB_BRANCH"
local TRACKING_REF=$(_git_tracking_ref "$GB_BRANCH")
[[ -z "$TRACKING_REF" ]] && return 255
echo "Fetching..."
git fetch -q || return 255
git $GB_WORKFLOW "$TRACKING_REF" || return 255
}
function _gb_publish_branch {
_gb_env
local GB_REPO="$1"
local GB_BRANCH="$2"
_gb_update_branch "$GB_REPO" "$GB_BRANCH"
_gb_refresh_master "$GB_REPO" || return 255
git checkout -q "$GB_MASTER_BRANCH" || return 255
git merge -q --no-ff "$GB_BRANCH" || return 255
git push -q origin $GB_MASTER_BRANCH:$GB_DEV_BRANCH || return 255
git checkout -q "$GB_BRANCH" || return 255
git merge -q "$GB_MASTER_BRANCH" || return 255
}
function _gb_init_repo {
_gb_env
local GB_REPO="$1"
local GB_REPO_URL="$2"
local GB_REPO_BASE_DIR="$GB_BASE_DIR/$GB_REPO"
local GB_REPO_MASTER_DIR="$GB_REPO_BASE_DIR/$GB_MASTER_DIR_NAME"
if [ -z "$GB_REPO_URL" ]; then
_gb_help
return 255
fi
if [ -d "$GB_REPO_MASTER_DIR" ]; then
echo "ERROR: Repo master dir already exists ($GB_REPO_MASTER_DIR)."
return 255
fi
mkdir -p "$GB_REPO_BASE_DIR"
if echo $GB_REPO_URL | grep -qP "^/"; then
cp -a "$GB_REPO_URL" "$GB_REPO_MASTER_DIR"
cd "$GB_REPO_MASTER_DIR"
local CURRENT_BRANCH=$(_git_current_branch)
$GB_GIT_NEW_WORKDIR "$GB_REPO_MASTER_DIR" "$GB_REPO_BASE_DIR/$CURRENT_BRANCH"
rsync -a --delete --exclude '.git' "$GB_REPO_MASTER_DIR/" "$GB_REPO_BASE_DIR/$CURRENT_BRANCH/"
echo "Remove '$GB_REPO_URL' after you have verified nothing is missing."
else
git clone -q "$GB_REPO_URL" "$GB_REPO_MASTER_DIR"
fi
cd "$GB_REPO_MASTER_DIR"
git checkout -q -b "$GB_MASTER_BRANCH"
if echo $GB_REPO_URL | grep -qP "^/"; then
_gb_cd_branch "$GB_REPO" "$CURRENT_BRANCH"
else
_gb_start_branch "$GB_REPO" "master"
fi
}
function _gb_complete {
COMPREPLY=()
if (( ${#COMP_WORDS[@]} == 2 )); then
local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen -W "$(_gb_repos)" -- $cur))
fi
if (( ${#COMP_WORDS[@]} == 3 )); then
local cur=${COMP_WORDS[COMP_CWORD]}
local GB_REPO=${COMP_WORDS[1]}
if [ -d "$(_gb_repo_master_dir "$GB_REPO")" ]; then
COMPREPLY=($(compgen -W "$(_gb_branches $GB_REPO)" -- $cur))
else
COMPREPLY=($(compgen -W "init" -- $cur))
fi
fi
if (( ${#COMP_WORDS[@]} == 4 )); then
local cur=${COMP_WORDS[COMP_CWORD]}
local GB_REPO=${COMP_WORDS[1]}
local GB_BRANCH=${COMP_WORDS[2]}
if [ -d "$(_gb_branch_dir "$GB_REPO" "$GB_BRANCH")" ]; then
COMPREPLY=($(compgen -W "publish lib rm update" -- $cur))
fi
fi
}
function git-branchdir-manager {
_gb_env
_gb_version_check
GB_REPO="$1"
local GB_ACTION
case $# in
0)
GB_ACTION="help"
;;
1)
if [ "$GB_REPO" == "update" ] || [ "$GB_REPO" == "rm" ] || [ "$GB_REPO" == "publish" ]; then
GB_ACTION="$GB_REPO"
GB_REPO=$(_gb_current_repo)
if [ -z "$GB_REPO" ]; then
echo "ERROR: Could not determine current repo."
return 255;
fi
GB_BRANCH=$(_gb_current_branch)
if [ -z "$GB_BRANCH" ]; then
echo "ERROR: Could not determine current branchdir."
return 255;
fi
local GIT_BRANCH=$(_git_current_branch)
if [ "$GB_BRANCH" != "$GIT_BRANCH" ]; then
echo "ERROR: Git branch does not match branchdir."
return 255;
fi
else
GB_ACTION="ls"
fi
;;
2)
GB_BRANCH="$2"
GB_ACTION="cd"
;;
3)
GB_BRANCH="$2"
GB_ACTION="$3"
;;
*)
echo "ERROR: Too many arguments specified."
return 255
;;
esac
if [ "$GB_BRANCH" == "init" ]; then
GB_BRANCH="$3"
GB_ACTION="init"
fi
if [ "$GB_BRANCH" == "start" ] || [ "$GB_BRANCH" == "init" ] || [ "$GB_BRANCH" == "lib" ]; then
echo "ERROR: No branch argument specified."
return 255
fi
for item in $*; do
if [ "$item" == "--help" ] || [ "$item" == "-h" ]; then
GB_ACTION="help"
GB_REPO=$(_gb_current_repo)
fi
done
case $GB_ACTION in
help)
_gb_help
;;
ls)
_gb_branches "$GB_REPO"
;;
cd)
if ! _gb_cd_branch "$GB_REPO" "$GB_BRANCH"; then
echo "ERROR: Failed to cd to branch."
return 255
fi
;;
rm)
if ! _gb_rm_branch "$GB_REPO" "$GB_BRANCH"; then
echo "ERROR: Failed to remove branch."
return 255
fi
;;
start)
if ! _gb_start_branch "$GB_REPO" "$GB_BRANCH"; then
echo "ERROR: Failed to start branch."
return 255
fi
;;
update)
if ! _gb_update_branch "$GB_REPO" "$GB_BRANCH"; then
echo "ERROR: Failed to update branch."
return 255
fi
;;
publish)
if ! _gb_publish_branch "$GB_REPO" "$GB_BRANCH"; then
echo "ERROR: Failed to publish branch."
return 255
fi
;;
init)
local GB_REPO_URL="$GB_BRANCH"
if ! _gb_init_repo "$GB_REPO" "$GB_REPO_URL"; then
echo "ERROR: Failed to init repo."
return 255
fi
;;
lib)
_gb_lib_path "$GB_REPO" "$GB_BRANCH"
;;
*)
echo "ERROR: Unrecognized action ($GB_ACTION)."
;;
esac
}
function git-track {
local TRACK_BRANCH="$1"
local CURRENT_BRANCH=$(_git_current_branch)
echo -n "Are you sure you wish to replace the current working directory with $TRACK_BRANCH's (y/n)? "
local response
read response
if [ "$response" == "y" ]; then
git branch --set-upstream "$CURRENT_BRANCH" "$TRACK_BRANCH"
git reset --hard "$TRACK_BRANCH"
fi
}
function _git_current_branch {
git branch | grep ^\* | sed 's/^\* //'
}
function _git_branch_exists {
git show-ref --quiet "$1"
}
function _git_has_changes {
if git status -s | grep -q '^ M'; then
echo "1"
fi
}