|
1 | 1 | #!/bin/env sh |
2 | 2 |
|
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 |
13 | 5 | } |
14 | 6 |
|
15 | 7 | branch() { |
16 | 8 | git symbolic-ref HEAD --short 2>/dev/null |
17 | 9 | } |
18 | 10 |
|
| 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 | + |
19 | 29 | changes() { |
20 | 30 | STAGED="$(git diff --cached --name-only 2>/dev/null | wc --lines)s" |
21 | 31 | WORKTREE="$(git diff --name-only 2>/dev/null | wc --lines)w" |
22 | 32 | 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;' |
24 | 34 | } |
25 | 35 |
|
| 36 | +if ! is_git_repo; then |
| 37 | + exit |
| 38 | +fi |
| 39 | + |
26 | 40 | 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 |
28 | 48 | PROMPT="$PROMPT [merging]" |
29 | | -elif git rev-parse --quiet --verify REBASE_HEAD > /dev/null; then |
30 | | - PROMPT="$(rebasing-branch) [rebasing]" |
31 | 49 | fi |
32 | 50 | CHANGES=$(changes) |
33 | 51 | if [[ -n "$CHANGES" ]]; then |
|
0 commit comments