Skip to content

Commit 1e2f94c

Browse files
Handle edge cases
- When not in a git repo - When the number of changes ends with a zero and is not zero - Clarify rebase apply vs rebase merge. These are two different rebase backends. Apply uses git am and merge uses git cherry-pick. The merge backend is the newer and is being migrated to. Rebase apply is being deprecated.
1 parent e0367e9 commit 1e2f94c

1 file changed

Lines changed: 32 additions & 14 deletions

File tree

scripts/git-prompt

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,51 @@
11
#!/bin/env sh
22

3-
# <https://stackoverflow.com/a/59115583>
4-
rebasing-branch() {
5-
for location in rebase-merge rebase-apply; do
6-
path=$(git rev-parse --git-path ${location})
7-
if test -d ${path}; then
8-
revision=$(<${path}/head-name)
9-
echo ${revision##refs/heads/}
10-
return 0
11-
fi
12-
done
3+
is_git_repo() {
4+
git rev-parse --is-inside-work-tree > /dev/null 2>&1
135
}
146

157
branch() {
168
git symbolic-ref HEAD --short 2>/dev/null
179
}
1810

11+
# <https://stackoverflow.com/a/59115583>
12+
rebase-branch-old() {
13+
path=$(git rev-parse --git-path rebase-apply) # rebase.backend is apply
14+
if test -d ${path}; then
15+
revision=$(<${path}/head-name)
16+
echo ${revision##refs/heads/}
17+
fi
18+
}
19+
20+
# <https://stackoverflow.com/a/59115583>
21+
rebase-branch-new() {
22+
path=$(git rev-parse --git-path rebase-merge) # rebase.backend is merge
23+
if test -d ${path}; then
24+
revision=$(<${path}/head-name)
25+
echo ${revision##refs/heads/}
26+
fi
27+
}
28+
1929
changes() {
2030
STAGED="$(git diff --cached --name-only 2>/dev/null | wc --lines)s"
2131
WORKTREE="$(git diff --name-only 2>/dev/null | wc --lines)w"
2232
UNTRACKED="$(git ls-files --others --exclude-standard 2>/dev/null | wc --lines)u"
23-
echo "${STAGED}${WORKTREE}${UNTRACKED}" | sed "s/0.//g"
33+
echo "${STAGED}${WORKTREE}${UNTRACKED}" | sed -E ':1; s/(^|[^0-9])0[swu]/\1/g; t1;'
2434
}
2535

36+
if ! is_git_repo; then
37+
exit
38+
fi
39+
2640
PROMPT=$(branch)
27-
if git rev-parse --quiet --verify MERGE_HEAD > /dev/null; then
41+
REBASE_BRANCH=$(rebase-branch-new)
42+
REBASE_BRANCH_OLD=$(rebase-branch-old)
43+
if [[ -n "$REBASE_BRANCH" ]]; then
44+
PROMPT="$REBASE_BRANCH [rebasing]"
45+
elif [[ -n "$REBASE_BRANCH_OLD" ]]; then
46+
PROMPT="$REBASE_BRANCH_OLD [*rebasing]"
47+
elif git rev-parse --quiet --verify MERGE_HEAD > /dev/null 2>&1; then
2848
PROMPT="$PROMPT [merging]"
29-
elif git rev-parse --quiet --verify REBASE_HEAD > /dev/null; then
30-
PROMPT="$(rebasing-branch) [rebasing]"
3149
fi
3250
CHANGES=$(changes)
3351
if [[ -n "$CHANGES" ]]; then

0 commit comments

Comments
 (0)