-
Notifications
You must be signed in to change notification settings - Fork 13
/
make-worktree.sh
executable file
·74 lines (63 loc) · 2.21 KB
/
make-worktree.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
#!/usr/bin/env bash
#/
#/ Creates a new Git worktree next to the current repo folder
#/
#/ Usage: git $KIT_ID make-worktree <WORKTREE-NAME> [-b <BRANCH-NAME> [<COMMITISH>]]
#/
#/ With default parameters, the command creates an empty worktree first,
#/ then runs 'git adsk make-branch' command and populates the
#/ worktree based on its results.
#/
#/ If optional param [-b <BRANCH-NAME>] is specified,
#/ it makes new worktree named <WORKTREE-NAME> and checkouts branch <BRANCH-NAME>.
#/ If <COMMITISH> is specified, starts <BRANCH-NAME> from it, otherwise defaults to HEAD.
#/
#/ E.g. git $KIT_ID make-worktree new-worktree -b new-branch origin/release/2019
#/ would create new-worktree and create new-branch starting at origin/release/2019.
#/
set -e
KIT_PATH="$(dirname "${BASH_SOURCE[0]}")"
. "$KIT_PATH/lib/setup_helpers.sh"
export ERROR_HELP_MESSAGE="Usage: git adsk make-worktree <WORKTREE-NAME> [-b <BRANCH-NAME> [<COMMITISH>]]"
if [ $# -lt 1 ]; then
error_exit "Please specify worktree name!"
fi
WORKTREE_NAME=$1
if [[ -z "$WORKTREE_NAME" ]]; then
error_exit "Worktree name can not be empty! Please, specify worktree name"
fi
BRANCH_NAME=""
COMMITISH=""
if [[ -n $2 ]]; then
if [[ $2 = "-b" ]]; then
if [[ -z $3 ]]; then
error_exit "if param '-b' was specified, specify branch name too"
fi
BRANCH_NAME=$3
COMMITISH=$4 # if not specified, Git will default to HEAD
else
error_exit "Unrecognized param $2"
fi
fi
ROOT=$(git rev-parse --show-toplevel)
WORKTREE_PATH="$ROOT/../$WORKTREE_NAME"
if [ -d "$WORKTREE_PATH" ]; then
error_exit "Worktree \"$WORKTREE_NAME\" already exists!"
fi
if [[ -n "$BRANCH_NAME" ]]
then
# create and populate worktree as specified
git worktree add -b $BRANCH_NAME "$WORKTREE_PATH" $COMMITISH
else
# create an empty worktree and checkout the branch interactively
git worktree add --no-checkout --detach "$WORKTREE_PATH"
pushd "$WORKTREE_PATH"
if ! git adsk make-branch --no-local-repo-checks; then
error_exit "Error during making branch"
fi
popd
fi
pushd "$WORKTREE_PATH"
git submodule update --init --recursive
popd
print_success "Worktree \"$WORKTREE_NAME\" successfully created!"