Skip to content

Commit 0e504b0

Browse files
authored
Improve atomic push script to support worktrees (elastic#68873)
1 parent b213d8c commit 0e504b0

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

dev-tools/atomic_push.sh

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ if [ "$#" -eq 0 ]; then
77
fi
88

99
REMOTE="$1"
10+
WORKING_DIR=$(pwd)
1011

1112
if ! git ls-remote --exit-code "${REMOTE}" > /dev/null 2>&1; then
1213
echo >&2 "Remote '${REMOTE}' is not valid."
1314
exit 1;
1415
fi
1516

1617
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."
1819
exit 1;
1920
fi
2021

@@ -28,27 +29,39 @@ ATOMIC_COMMIT_DATE="$(date)"
2829
for BRANCH in "${@:2}"
2930
do
3031
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
3334
if ! git show-ref --verify --quiet "refs/heads/${BRANCH}"; then
3435
echo >&2 "No such branch named '${BRANCH}'."
3536
exit 1;
3637
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+
3848
# Pull and rebase all branches to ensure we've incorporated any new upstream commits
3949
git checkout --quiet ${BRANCH}
4050
git pull "${REMOTE}" "${BRANCH}" --rebase --quiet
41-
51+
4252
PENDING_COMMITS=$(git log ${REMOTE}/${BRANCH}..HEAD --oneline | grep "^.*$" -c || true)
43-
53+
4454
# Ensure that there is exactly 1 unpushed commit in the branch
4555
if [ "${PENDING_COMMITS}" -ne 1 ]; then
4656
echo >&2 "Expected exactly 1 pending commit for branch '${BRANCH}' but ${PENDING_COMMITS} exist."
4757
exit 1;
4858
fi
49-
59+
5060
# Amend HEAD commit to ensure all branch commit dates are the same
5161
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}
5265
done
5366

5467
echo "Pushing to remote '${REMOTE}'..."

0 commit comments

Comments
 (0)