-
Notifications
You must be signed in to change notification settings - Fork 3
/
git-prompt.pl
executable file
·286 lines (237 loc) · 7.03 KB
/
git-prompt.pl
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/perl
use strict;
use warnings;
use v5.16;
binmode(STDOUT, ":utf8");
################################################################
# Git status in your bash prompt
# https://github.com/scottchiefbaker/perl-git-prompt.git
#
# Version 0.2
#
# Put this in your ~/.bashrc to show git status in your prompt:
#
# GIT_PROMPT_PATH="$HOME/github/perl-git-prompt/git-prompt.pl"
# if [[ -f $GIT_PROMPT_PATH ]] && [[ -z $GIT_PROMPT ]]; then
# export GIT_PROMPT=$GIT_PROMPT_PATH
# export PS1="\$($GIT_PROMPT_PATH)"$PS1
# fi
################################################################
# 1 2 3 2 4 5 6 4
# Git Branch: (master) [+2 14]
# Color codes found in utils/term-colors.pl
my $PROMPT_COLOR = color(45); # Color of the header prompt #1
my $PAREN_COLOR = color(15); # Color of the parenthesis #2
my $DIRTY_COLOR = color(203); # Color when the branch is dirty #3
my $CLEAN_COLOR = color(83); # Color when the branch is clean #3
my $MERGE_COLOR = color(214); # Color when the branch has merge conflict #3
my $BRACE_COLOR = color(15); # Color of the file count braces #4
my $AHEAD_COLOR = color(117); # Color when the branch is ahead of the remote #5
my $BEHIND_COLOR = color(196); # Color when the branch is behind the remote #5
my $PENDING_COLOR = color(228); # Color of the uncommitted file count #6
my $RESET_COLOR = color(); # Reset the color
###################################################################
# Get the state of git in the current dir
my $i = get_git_info();
# A string of all the args passed in
my $args = argv();
# If the user requests debug dump out the data structure
if ($args->{debug}) {
kd($i);
}
# If they request JSON spit that out
if ($args->{json}) {
require JSON;
print JSON::encode_json($i) . "\n";
exit;
}
# If we're on a git enabled dir
if ($i && $i->{'branch'}) {
print $PROMPT_COLOR . "Git Branch: ";
my $branch_color = '';
if ($i->{'merge_conflict'} || $i->{rebasing}) {
$branch_color = $MERGE_COLOR;
} elsif ($i->{'dirty'}) {
$branch_color = $DIRTY_COLOR;
} else {
$branch_color = $CLEAN_COLOR;
}
# Print out the name of the branch we're currently on
my $branch_name = $i->{'branch'};
print $PAREN_COLOR . "(" . $branch_color . $branch_name . $PAREN_COLOR . ") ";
# If we're ahead/behind of the remote OR there are pending files
if ($i->{'position'} || $i->{'dirty'}) {
# Open paren
print $BRACE_COLOR . "[";
# Ahead/Behind of the remote server
if ($i->{'position'}) {
if ($i->{'ahead'}) {
print $AHEAD_COLOR;
} elsif ($i->{'behind'}) {
print $BEHIND_COLOR;
}
print $i->{'position'};
}
# If the repo is dirty AND we're ahead/behind put a space between the numbers
if ($i->{'dirty'} && $i->{'position'}) {
print " ";
}
# If there are pending files to be committed
#
# If none of the files are staged
if (($i->{'dirty'} >= 1) && ($i->{'staged'} == 0)) {
print $DIRTY_COLOR;
# If all the files are staged
} elsif ($i->{dirty} && ($i->{dirty} == $i->{staged})) {
print $CLEAN_COLOR;
# If some of the files are staged (but not all)
} elsif ($i->{dirty} && ($i->{dirty} != $i->{staged})) {
print $PENDING_COLOR;
}
# Print out the number of dirty files
if ($i->{dirty}) {
print $i->{'dirty'};
}
if ($i->{merge_conflict}) {
my $count = $i->{merge_conflict};
#my $merge_symbol = "\N{ALMOST EQUAL TO}";
my $merge_symbol = "\x{21C4}";
print $MERGE_COLOR . " " . $merge_symbol . $count;
}
# Close paren
print $BRACE_COLOR . "]";
}
print "\n" . $RESET_COLOR;
}
# Set the foreground color
sub color {
my $c = shift() || "";
my $bold = shift() || "";
my $ret = '';
if ($bold eq 'bold') {
$ret = "\e[1m";
} else {
$ret = "\e[0m";
}
# Reset the color
if (!defined($c)) {
$ret = "\e[0m";
} else {
$ret .= "\e[38;5;${c}m";
}
return $ret;
}
###################################################
sub is_git_dir {
# git branch is the fastest way to see if we're in a git dir or not
my $cmd = "git branch 2>&1 > /dev/null";
`$cmd`;
# If the exit code is 0 we're in a git enabled dir
if ($? == 0) {
return 1;
} else {
return 0;
}
}
sub get_git_info {
# See if we're in a git enabled dir
if (!is_git_dir()) {
return 0;
}
my $ret = {};
# Init some variables
$ret->{staged} = 0;
$ret->{unstaged} = 0;
$ret->{dirty} = 0;
# Git status has all the data we'll need to get the parts
my $cmd = "2>/dev/null git status";
my $out = `$cmd`;
# Find the branch we're on
if ($out =~ /On branch (.+?)\n/) {
$ret->{'branch'} = $1;
} elsif ($out =~ /rebasing branch '(.+?)'/) {
$ret->{'branch'} = $1;
$ret->{'rebasing'} = 1;
} elsif ($out =~ /Not currently on any branch|HEAD detached (at|from)/) {
$ret->{'branch'} = "DETACHED_HEAD";
}
if (my @m = $out =~ /both modified:/g) {
my $count = scalar(@m);
$ret->{merge_conflict} = $count;
}
#Your branch is behind 'origin/vader' by 2 commits
if ($out =~ /Your branch is (ahead|behind).*'(.+?)' by (\d+) commit/) {
my $sigil = "?";
if ($1 eq 'ahead') {
$sigil = "+";
$ret->{'ahead'} = 1;
} elsif ($1 eq 'behind') {
$sigil = "-";
$ret->{'behind'} = 1;
}
my $str = "${sigil}$3";
$ret->{'position'} = $str;
}
# If there is nothing to commit don't keep looking there is nothing there
if ($out =~ /^nothing to commit/m) {
$ret->{'clean'} = 1;
return $ret;
}
# Find the number of files in each given state
my $state;
foreach my $line (split(/\n/,$out)) {
# Files staged to be committed
if ($line =~ /Changes to be committed:/) {
$state = "staged";
# Files git is aware of but aren't going to be committed
} elsif ($line =~ /Changes not staged for commit:/) {
$state = "unstaged";
# Files gits sees but isn't tracking
} elsif ($line =~ /Untracked files:/) {
$state = "untracked";
} elsif ($line =~ /Unmerged paths:/) {
$state = "unmerged";
# If the line has a \t in it, it's a file name
} elsif ($line =~ /\t/) {
$ret->{$state}++;
$ret->{'dirty'}++;
}
}
return $ret;
}
sub argv {
my @args = @_ || @ARGV;
my $ret = {};
for (my $i = 0; $i < scalar(@args); $i++) {
# If the item starts with "-" it's a key
if (my ($key) = $args[$i] =~ /^--?(\S+)/) {
# If the next item does not start with "--" it's the value for this item
if (defined($args[$i + 1]) && ($args[$i + 1] !~ /^--?/)) {
$ret->{$key} = $args[$i + 1];
# Bareword like --verbose with no options
} else {
$ret->{$key}++;
}
}
}
return $ret;
}
# Add debug print krumo style
# Borrowed from: https://www.perturb.org/display/1097_Perl_detect_if_a_module_is_installed_before_using_it.html
sub AUTOLOAD {
our $AUTOLOAD; # keep 'use strict' happy
if ($AUTOLOAD eq 'main::k' || $AUTOLOAD eq 'main::kd') {
if (eval { require Data::Dump::Color }) {
*k = sub { Data::Dump::Color::dd(@_) };
} else {
require Data::Dumper;
*k = sub { print Data::Dumper::Dumper(@_) };
}
sub kd {
k(@_);
printf("Died at %2\$s line #%3\$s\n",caller());
exit(15);
}
eval($AUTOLOAD . '(@_)');
}
}