-
Notifications
You must be signed in to change notification settings - Fork 6
/
git-rebase-head
executable file
·36 lines (31 loc) · 1.13 KB
/
git-rebase-head
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
#!/usr/bin/env bash
#
# While a rebase or cherry-pick is paused due to conflicts, this helper prints the SHA of the
# commit being rebased or cherry-picked.
#
# In such a conflicted state, HEAD will typically point to the most recently applied commit,
# which does not exist in the original branch, and corresponds to the parent of the commit
# currently being applied.
set -eo pipefail
git_dir="$(git dir)"
rebase_head="$git_dir/REBASE_HEAD"
if [ -f "$rebase_head" ]; then
cat "$rebase_head"
exit 0
fi
cherry_pick_head="$git_dir/CHERRY_PICK_HEAD"
if [ -f "$cherry_pick_head" ]; then
cat "$cherry_pick_head"
exit 0
fi
# During an `exec` phase (e.g. via `git rebase -x <cmd> …`), REBASE_HEAD is not present, but we can
# parse the last `pick`ed SHA from `.git/rebase-merge-done`.
rebase_done="$git_dir/rebase-merge/done"
if ! [ -f "$rebase_done" ]; then
echo "No REBASE_HEAD, CHERRY_PICK_HEAD, or rebase-merge-done found in Git dir $git_dir" >&2
exit 11
fi
# Extract the SHA from the last `pick` line.
pick_line="$(cat "$rebase_done" | grep '^pick' | tail -n1)"
sha="$(echo "$pick_line" | awk -F' ' '{print $2}')"
echo "$sha"