-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgit-rebase-head
More file actions
executable file
·43 lines (38 loc) · 1.44 KB
/
git-rebase-head
File metadata and controls
executable file
·43 lines (38 loc) · 1.44 KB
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
#!/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`, `edit`, or `merge` line.
# For merge lines, the format is "merge -C <sha> <label>"
last_line="$(cat "$rebase_done" | grep -E '^(pick|edit|merge)' | tail -n1)"
if echo "$last_line" | grep -q '^merge'; then
# Extract SHA from merge line (format: "merge -C SHA label")
sha="$(echo "$last_line" | awk '{print $3}')"
else
# Extract SHA from pick/edit line (format: "pick SHA")
sha="$(echo "$last_line" | awk '{print $2}')"
fi
echo "$sha"