-
Notifications
You must be signed in to change notification settings - Fork 76
/
git-output.awk
executable file
·220 lines (184 loc) · 5.55 KB
/
git-output.awk
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/awk -f
#
# (c) 2011 Brian C. Milco
#
# This script parses the output of several git commands and presents the
# information in one line that can be added to a command prompt (PS1).
#
# If you use -v separator="|" separator2="/" when calling this script you can use other field separators.
#
# Sample output:
#
# | master | ▲ 1 ▼ 1 | 1/2/3/4 |1
#
# What the output means:
#
# | - field separator
# master - the branch name or (no branch) or (bare repository)
# ▲ 1 - how many commits on this branch not in the remote branch. You need to push changes to remote.
# ▼ 1 - how many commits on the remote branch not in this branch. You need to pull changes from remote.
# 1/2/3/4 - The count of changes in the repository:
# 1 - the number of staged changes. (green)
# 2 - the number of unstaged changes. (yellow)
# 3 - the number of unmerged changes. (magenta)
# 4 - the number of untracked changes. (red)
# |1 - the number of stashed changes. (yellow)
#
# original code from:
# https://gist.github.com/1266869
# old branch symbol: └├
function cmd( c )
{
while( (c|getline foo) > 0 )
continue;
close( c );
return foo;
}
BEGIN {
#colors:
black="\033[30m";
dark_gray="\033[01;30m";
red="\033[31m";
bright_red="\033[1;31m";
green="\033[32m";
bright_green="\033[1;32m";
yellow="\033[33m";
bright_yellow="\033[1;33m";
blue="\033[34m";
bright_blue="\033[1;34m";
violet="\033[35m";
bright_violet="\033[1;35m";
cyan="\033[036m";
bright_cyan="\033[1;36m";
white="\033[37m";
light_gray="\033[00;37m";
end_color="\033[0m";
if(separator == "") {
separator = "|";
}
if(separator2 == "") {
separator2 = "-";
}
separator = dark_gray separator end_color;
separator2 = dark_gray separator2 end_color;
isRepo = 0;
output = cmd("git rev-parse --git-dir 2> /dev/null");
if(output) {
bareTest = cmd("cat " output "/config | grep \"bare\" 2> /dev/null");
if(bareTest ~ "true")
bareRepo = 1;
stashCount = cmd("git stash list | wc -l 2> /dev/null");
gsub(/ /,"",stashCount);
isRepo = 1;
}
changes["staged"] = 0;
changes["unstaged"] = 0;
changes["untracked"] = 0;
changes["unmerged"] = 0;
}
{
#only process lines that have data.
if(skip > 0) {
skip--;
next;
}
test=$1 " " $2 " " $3;
if(test == "# On branch") {
branch = $4;
next;
} else if(test == "# Changes to") { #staged
skip = 1;
staged = 1;
tracked = 1;
merged = 1;
next;
} else if(test == "# Changes not") { #unstaged
skip = 3;
staged = 0;
tracked = 1;
merged = 1;
next;
} else if(test == "# Untracked files:") {#untracked
skip = 2;
staged = 0;
tracked = 0;
merged = 1;
next;
} else if(test == "# Unmerged paths:") {#unmerged
skip = 2;
staged = 0;
tracked = 1;
merged = 0;
next;
} else if($1 != "#") {
next;
} else if(test == "# Initial commit") {
next;
} else if(test == "# Your branch") { #branch is ahead/behind
if($5 == "ahead") {
ahead = $9;
} else if($5 == "behind") {
behind = $8;
}
next;
} else if(test == "# and have") { #branches have diverged
ahead = $4;
behind = $6;
next;
} else if(test == "# Not currently") {#detached HEAD
branch = "(no branch)";
}
#Don't count blank lines
if($0 == "#")
next;
if(staged == 1)
changes["staged"] += 1;
else if(staged == 0 && tracked == 1 && merged == 1)
changes["unstaged"] += 1;
else if(tracked == 0)
changes["untracked"] += 1;
else if(merged == 0)
changes["unmerged"] += 1;
}
END {
output = "";
if(isRepo == 1) {
branchOutput = separator " ";
if(bareRepo == 1) {
branchOutput = branchOutput bright_cyan "(bare repository)";
} else {
branchOutput = branchOutput bright_cyan " " branch;
}
output = output branchOutput " " separator " ";
if(bareRepo != 1) {
if(ahead > 0 || behind > 0) {
if(ahead > 0) {
output = output bright_yellow "▲ " end_color ahead;
}
if (behind > 0) {
output = output bright_yellow "▼ " end_color behind;
}
output = output " " separator " ";
}
#if there are changes show the output.
if(changes["staged"] > 0 || changes["unstaged"] > 0 || changes["untracked"] > 0 || changes["unmerged"] > 0) {
output = output bright_green changes["staged"] end_color;
output = output separator2;
output = output bright_yellow changes["unstaged"] end_color;
output = output separator2;
output = output violet changes["unmerged"] end_color;
output = output separator2;
output = output bright_red changes["untracked"] end_color;
output = output " ";
} else {
output = output bright_green "√ ☺ " end_color;
}
if(stashCount > 0) {
output = output separator bright_yellow " {≡" stashCount "} " end_color;
}
} else {
output = output "no branch ";
}
printf output end_color;
}
}