@@ -7,14 +7,15 @@ if [ "$#" -eq 0 ]; then
7
7
fi
8
8
9
9
REMOTE=" $1 "
10
+ WORKING_DIR=$( pwd)
10
11
11
12
if ! git ls-remote --exit-code " ${REMOTE} " > /dev/null 2>&1 ; then
12
13
echo >&2 " Remote '${REMOTE} ' is not valid."
13
14
exit 1;
14
15
fi
15
16
16
17
if [ " $# " -lt 3 ]; then
17
- echo >&2 " You must specify at least two branchs to push."
18
+ echo >&2 " You must specify at least two branches to push."
18
19
exit 1;
19
20
fi
20
21
@@ -28,27 +29,39 @@ ATOMIC_COMMIT_DATE="$(date)"
28
29
for BRANCH in " ${@: 2} "
29
30
do
30
31
echo " Validating branch '${BRANCH} '..."
31
-
32
- # Vailidate that script arguments are valid local branch names
32
+
33
+ # Validate that script arguments are valid local branch names
33
34
if ! git show-ref --verify --quiet " refs/heads/${BRANCH} " ; then
34
35
echo >&2 " No such branch named '${BRANCH} '."
35
36
exit 1;
36
37
fi
37
-
38
+
39
+ # Determine if the branch is checked out to a worktree directory
40
+ WORKTREE_DIR=$( git worktree list | grep -F [${BRANCH} ] | awk ' {print $1}' )
41
+
42
+ # If the branch is checked out to a worktree directory change directory to there
43
+ if [ -n " ${WORKTREE_DIR} " -a " ${WORKTREE_DIR} " != " ${WORKING_DIR} " ]; then
44
+ echo " Switching to ${BRANCH} worktree in ${WORKTREE_DIR} " ;
45
+ cd ${WORKTREE_DIR}
46
+ fi
47
+
38
48
# Pull and rebase all branches to ensure we've incorporated any new upstream commits
39
49
git checkout --quiet ${BRANCH}
40
50
git pull " ${REMOTE} " " ${BRANCH} " --rebase --quiet
41
-
51
+
42
52
PENDING_COMMITS=$( git log ${REMOTE} /${BRANCH} ..HEAD --oneline | grep " ^.*$" -c || true)
43
-
53
+
44
54
# Ensure that there is exactly 1 unpushed commit in the branch
45
55
if [ " ${PENDING_COMMITS} " -ne 1 ]; then
46
56
echo >&2 " Expected exactly 1 pending commit for branch '${BRANCH} ' but ${PENDING_COMMITS} exist."
47
57
exit 1;
48
58
fi
49
-
59
+
50
60
# Amend HEAD commit to ensure all branch commit dates are the same
51
61
GIT_COMMITTER_DATE=" ${ATOMIC_COMMIT_DATE} " git commit --amend --no-edit --quiet
62
+
63
+ # Change back to original working directory if necessary
64
+ cd ${WORKING_DIR}
52
65
done
53
66
54
67
echo " Pushing to remote '${REMOTE} '..."
0 commit comments