@@ -19,26 +19,27 @@ if [ -z "${__GIT_PROMPT_DIR}" ]; then
19
19
__GIT_PROMPT_DIR=" $( cd -P " $( dirname " ${SOURCE} " ) " && pwd ) "
20
20
fi
21
21
22
- gitsym= ` git symbolic- ref HEAD`
22
+ branch= $( git rev-parse --abbrev- ref HEAD )
23
23
24
- # if "fatal: Not a git repo .., then exit
25
- case " $gitsym " in fatal * ) exit 0 ;; esac
24
+ # If error code we are not in a repo - exit
25
+ if [[ $? != 0 ]] ; then exit 0; fi
26
26
27
- # the current branch is the tail end of the symbolic reference
28
- branch=" ${gitsym## refs/ heads/ } " # get the basename after "refs/heads/"
27
+ # If we are detached, branch name will be HEAD
28
+ if [[ " $branch " == " HEAD" ]]; then
29
+ unset branch
30
+ fi
29
31
30
- gitstatus=` git diff --name-status 2>&1 `
32
+ gitstatus=$( git diff --name-status 2>&1 )
31
33
32
34
# if the diff is fatal, exit now
33
- case " $gitstatus " in fatal* ) exit 0 ;; esac
34
-
35
+ if [[ $? != 0 ]]; then exit 0; fi
35
36
36
- staged_files=` git diff --staged --name-status`
37
+ staged_files=$( git diff --staged --name-status )
37
38
38
- num_changed=$(( ` all_lines "$gitstatus "` - ` count_lines "$gitstatus " U` ))
39
- num_conflicts=` count_lines " $staged_files " U`
40
- num_staged=$(( ` all_lines "$staged_files "` - num_conflicts ))
41
- num_untracked=` git ls-files --others --exclude-standard $( git rev-parse --show-cdup) | wc -l`
39
+ num_changed=$(( $( all_lines "$gitstatus " ) - $( count_lines "$gitstatus " U ) ))
40
+ num_conflicts=$( count_lines " $staged_files " U )
41
+ num_staged=$(( $( all_lines "$staged_files " ) - num_conflicts ))
42
+ num_untracked=$( git ls-files --others --exclude-standard $( git rev-parse --show-cdup) | wc -l )
42
43
43
44
num_stashed=0
44
45
if [[ " $__GIT_PROMPT_IGNORE_STASH " != " 1" ]]; then
@@ -56,19 +57,20 @@ if (( num_changed == 0 && num_staged == 0 && num_U == 0 && num_untracked == 0 &&
56
57
fi
57
58
58
59
remote=
60
+ upstream=
59
61
60
62
if [[ -z " $branch " ]]; then
61
- tag=` git describe --exact-match`
63
+ tag=$( git describe --exact-match 2> /dev/null )
62
64
if [[ -n " $tag " ]]; then
63
65
branch=" $tag "
64
66
else
65
- branch=" _PREHASH_` git rev-parse --short HEAD` "
67
+ branch=" _PREHASH_$( git rev-parse --short HEAD ) "
66
68
fi
67
69
else
68
- remote_name=` git config branch.${branch} .remote`
70
+ remote_name=$( git config branch.${branch} .remote )
69
71
70
72
if [[ -n " $remote_name " ]]; then
71
- merge_name=` git config branch.${branch} .merge`
73
+ merge_name=$( git config branch.${branch} .merge )
72
74
else
73
75
remote_name=' origin'
74
76
merge_name=" refs/heads/${branch} "
81
83
fi
82
84
83
85
# detect if the local branch have a remote tracking branch
84
- cmd_output =$( git rev-parse --abbrev-ref ${branch} @{upstream} 2>&1 > /dev/null )
86
+ upstream =$( git rev-parse --abbrev-ref ${branch} @{upstream} 2>&1 )
85
87
86
88
if [[ $? == 0 ]]; then
87
89
has_remote_tracking=1
88
90
else
89
91
has_remote_tracking=0
92
+ unset upstream
90
93
fi
91
94
92
95
# get the revision list, and count the leading "<" and ">"
93
- revgit=` git rev-list --left-right ${remote_ref} ...HEAD`
94
- num_revs=` all_lines " $revgit " `
95
- num_ahead=` count_lines " $revgit " " ^>" `
96
- num_behind=$(( num_revs - num_ahead ))
97
- if (( num_behind > 0 )) ; then
98
- remote=" ${remote} _BEHIND_${num_behind} "
99
- fi
100
- if (( num_ahead > 0 )) ; then
101
- remote=" ${remote} _AHEAD_${num_ahead} "
96
+ revgit=$( git rev-list --left-right ${remote_ref} ...HEAD 2> /dev/null )
97
+ if [[ $? == 0 ]]; then
98
+ num_revs=$( all_lines " $revgit " )
99
+ num_ahead=$( count_lines " $revgit " " ^>" )
100
+ num_behind=$(( num_revs - num_ahead ))
101
+ if (( num_behind > 0 )) ; then
102
+ remote=" ${remote} _BEHIND_${num_behind} "
103
+ fi
104
+ if (( num_ahead > 0 )) ; then
105
+ remote=" ${remote} _AHEAD_${num_ahead} "
106
+ fi
102
107
fi
103
108
fi
104
109
@@ -110,9 +115,14 @@ if [[ "$has_remote_tracking" == "0" ]] ; then
110
115
remote=' _NO_REMOTE_TRACKING_'
111
116
fi
112
117
113
- printf " %s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" \
118
+ if [[ -z " $upstream " ]] ; then
119
+ upstream=' ^'
120
+ fi
121
+
122
+ printf " %s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" \
114
123
" $branch " \
115
124
" $remote " \
125
+ " $upstream " \
116
126
$num_staged \
117
127
$num_conflicts \
118
128
$num_changed \
0 commit comments