-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgitprompt.pl
executable file
·209 lines (188 loc) · 5.82 KB
/
gitprompt.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
#!/usr/bin/perl -w
eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
if 0; # not running under some shell
use strict;
=ignore
Copyright 2013 Synacor, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=cut
use IO::Handle;
use IPC::Open3;
use Time::HiRes qw(time);
### prechecks ###
my $ps0 = $ENV{LEADUP};
unless ($ps0) {
print "!define PS0!> ";
exit 1;
}
### global definitions ###
my %formatliteral = (
e => "\e",
'%' => '%',
'[' => "\\[",
']' => "\\]",
);
my %formatcondkeep = (
g => 1,
);
### definitions ###
my %opt = (
c => 'c',
u => 'u',
f => 'f',
A => 'A',
B => 'B',
F => 'F',
t => '?',
l => '?~',
n => '??',
g => '',
statuscount => 0,
keepempty => 1,
);
### read options ###
if (@ARGV) {
foreach (@ARGV) {
return {error=>"invalid parameter $_"} unless /^(\w+)\=(.*?)$/;
my ($key,$val) = ($1,$2);
$val =~ s/\%(.)/exists $formatliteral{$1} ? $formatliteral{$1} : ''/ge;
$opt{$key} = $val;
}
}
my %formatvalue = %{gitdata()};
my $output = "";
my @ps0 = split(/\%\{(.*?)\%\}/, $ps0);
my $conditional = 0;
foreach my $part (@ps0) {
if ($conditional) {
my $keep = 0;
my $formatter = sub {
my ($code) = @_;
if (exists $formatliteral{$code}) {
return $formatliteral{$code};
} elsif (exists $formatvalue{$code} && ($opt{keepempty} || length $formatvalue{$code} || $formatcondkeep{$code})) {
$keep = 1;
return $formatvalue{$code};
} else {
return '';
}
};
$part =~ s/\%(.)/$formatter->($1)/ge;
$output .= $part if $keep;
} else {
$part =~ s/\%(.)/exists $formatliteral{$1} ? $formatliteral{$1} : exists $formatvalue{$1} ? $formatvalue{$1} : ''/ge;
$output .= $part;
}
$conditional = !$conditional;
}
$output = "\\[\e[0;30;41m\\]! $formatvalue{error} !\\[\e[0m\\]$output" if exists $formatvalue{error};
print $output;
sub gitdata {
### prechecks ###
chomp(my $headref = `git symbolic-ref HEAD 2>&1`);
return {} if $headref =~ /fatal: Not a git repository|fatal: Unable to read current working directory/i;
### collect branch data ###
chomp(my $commitid = `git rev-parse --short HEAD 2>&1`);
my $branch = $commitid; #fallback value
if ($headref =~ /fatal: ref HEAD is not a symbolic ref/i) {
# find gitdir
chomp(my $gitdir = `git rev-parse --git-dir`);
# parse HEAD log
open(HEADLOG, "$gitdir/logs/HEAD");
my $lastrelevant = '';
while (<HEADLOG>) {
$lastrelevant = $_ if /^\s*\w+\s+$branch\w+\s/;
}
# if the log mentions switching to the commit id, use whatever it calls it
$branch = $1 if $lastrelevant =~ /\scheckout\:\s+moving\s+from\s+\S+\s+to\s+(\S+)\s*$/ || $lastrelevant =~ /\smerge\s+(\S+)\:\s+Fast\-forward\s*$/;
} elsif ($headref =~ /^refs\/heads\/(.+?)\s*$/) {
# normal branch
$branch = $1;
} else {
# unexpected input
$headref =~ s/[^\x20-\x7e]//g;
return {error=>$headref};
}
### collect status data ###
my ($statusexitcode, $statusout, @status);
$SIG{CHLD} = sub { wait(); $statusexitcode = $?>>8; };
my $statuspid = open3(undef,$statusout,undef,"git status");
$statusout->blocking(0);
my ($running, $waiting, $start, $valid) = (1, 1, time, 0);
while ($running && $waiting) {
while (<$statusout>) {
push @status, $_;
}
$running = kill 0 => $statuspid;
select undef, undef, undef, .001; #yield, actually
$waiting = time < $start + 1;
}
### parse status data ###
my %statuscount;
my %sectionmap = (
'Changes to be committed' => 'c',
'Changed but not updated' => 'u',
'Changes not staged for commit' => 'u',
'Untracked files' => 'f',
'Unmerged paths' => 'u',
);
$statuscount{$_} = 0 foreach values %sectionmap;
my $can_fast_forward = '';
if (!$running) {
# if it terminated, parse output
my ($section);
foreach (@status) {
if (/^(?:\# )?(\S.+?)\:\s*$/ && exists $sectionmap{$1}) {
$section = $sectionmap{$1};
} elsif ($section && /^\#?\t\S/) {
$statuscount{$section}++;
$valid = 1;
} elsif (/^nothing to commit\b/) {
$valid = 1;
} elsif (/\bis (ahead|behind) .+ by (\d+) commits?(\,? and can be fast\-forwarded)?/) {
$statuscount{($1 eq 'ahead') ? 'A' : 'B'} = $2;
$can_fast_forward = 1 if $3;
} elsif (/^(?:\# )?and have (\d+) and (\d+) different commit/) {
$statuscount{A} = $1;
$statuscount{B} = $2;
}
}
}
my $timeout = '';
if ($running) {
# it was running when we stopped caring
$timeout = $opt{t};
kill 2 => $statuspid;
} elsif (!$valid) {
#determine cause of failure
if ($status[0] =~ /\.git\/index\.lock/) {
$timeout = $opt{l};
} elsif ($status[0] =~ /must be run in a work tree/) {
$timeout = $opt{n};
} else {
print "\\[\e[41m\\]!! gitprompt.pl: \\`git status\' returned with exit code $statusexitcode and message:\n$status[0]\\[\e[0m\\]";
$timeout = "\\[\e[41m\\]!$statusexitcode!\\[\e[0m\\]";
}
}
### produce output ###
my %formatvalue = (
b => $branch,
i => $commitid,
t => $timeout,
g => $opt{g},
);
$formatvalue{F} = $opt{F} if $can_fast_forward;
foreach my $flag (keys %statuscount) {
$formatvalue{$flag} = $statuscount{$flag} ? ($opt{$flag}.($opt{statuscount} ? $statuscount{$flag} : '')) : '';
}
return \%formatvalue;
}