/
git-get
executable file
·200 lines (168 loc) · 4.52 KB
/
git-get
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
#!/usr/bin/perl
use strict;
my ($what, @args) = @ARGV;
# For shell completion
if ($what eq "--list") {
print join "\n", _command_names(), "";
exit 0;
}
_usage() unless defined $what;
$what =~ tr/-/_/;
{ no strict 'refs';
if (defined &$what) { print $what->(@args), $ENV{GIT_GET_NO_NEWLINE} ? "" : "\n" }
else {
_usage($what);
}
}
sub repo_rootdir_absolute {
_run("git rev-parse --show-toplevel");
}
sub repo_rootdir_relative {
my $rel = _run("git rev-parse --show-cdup");
$rel =~ s{/$}{};
$rel eq "" ? "." : $rel;
}
sub cwd_relative_to_repo {
my $rel = _run("git rev-parse --show-prefix");
$rel =~ s{/$}{};
$rel eq "" ? "." : $rel;
}
# Oops, duplicated this by mistake
sub pwd_relative {
goto &cwd_relative_to_repo;
}
sub current_branch_name {
if (_head_detached()) { return "" }
_run("git rev-parse --symbolic-full-name --abbrev-ref HEAD");
}
sub is_head_detached {
exit (_head_detached() ? 0 : 1);
}
sub _head_detached {
system("git symbolic-ref -q HEAD > /dev/null") != 0;
}
sub branch_remote {
my ($branch) = @_;
$branch ||= current_branch_name();
_run("git config --get 'branch.$branch.remote'");
}
sub branch_tracked_branch {
my ($branch) = @_;
$branch ||= current_branch_name();
my $remote = branch_remote($branch) or exit 1;
my $remote_ref = _run("git config --get 'branch.$branch.merge'");
$remote_ref =~ s{^refs/heads/}{};
$remote_ref or exit 1;
return $remote eq "." ? $remote_ref : "$remote/$remote_ref";
}
sub is_working_tree_clean {
exit (_working_tree_dirty() ? 1 : 0);
}
sub is_working_tree_dirty {
exit (_working_tree_dirty() ? 0 : 1);
}
sub _working_tree_dirty {
my ($arg) = @_;
for my $cmd ("git rev-parse --verify HEAD > /dev/null",
"git update-index --ignore-submodules --refresh > /dev/null",
"git diff-files --quiet --ignore-submodules",
"git diff-index --cached --quiet HEAD --ignore-submodules") {
system($cmd) == 0 or return 1;
}
return 0;
}
sub heads {
my @heads;
for (qx{git branch}) {
s/^..//; # Trim off leading '*' and/or spaces
push @heads, $_;
}
chomp $heads[-1]; # wrapper will append an extra newline on output
@heads;
}
sub is_remote {
my ($in) = @_;
$in .= "\n";
if (grep $_ eq $in, qx{git remote}) { exit 0 } else { exit 1 }
}
sub is_ancestor_of {
my ($A, $B) = @_;
is_same_object($A, _run("git", "merge-base", $A, $B));
}
sub is_same_object {
my ($A, $B) = @_;
$A = _run("git", "rev-parse", $A);
$B = _run("git", "rev-parse", $B);
exit($A eq $B ? 0 : 1);
}
sub config_keys {
my ($pat) = @_ ? $_[0] : '.';
my @keys;
open my ($fh), "-|", "git config --null --list"
or die "couldn't run git-config: $!";
{ local $/ = "\0";
while (my $line = <$fh>) {
chomp $line;
my ($k, $v) = split /\n/, $line, 2;
push @keys, "$k\n" if $k =~ /$pat/o;
}
}
chomp $keys[-1]; # wrapper will append an extra newline on output
return @keys;
}
# This is the complete pager command git uses, suitable for passing to
# "sh -c", and including the environment setting to specify the
# default pager flags. Supply the -n flag to omit the environment
# setting, if you need to pass to execve or something. Supply the -v
# flag if you want environment variables replaced by their values.
sub pager_command {
my $pager = _run(qw(git config core.pager));
my %opt;
for (@_) {
next unless s/^-//;
$opt{$_} = 1 for split //;
}
$pager =~ s/\$(\w+)/$ENV{$1}/g if $opt{v};
return $opt{n} ? $pager : join(" ", "LESS=FRSX", $pager);
}
sub author_email {
_run(qw(git log -1 --format=%ae), _temporary_commit());
}
sub author_username {
my $email = author_email();
$email =~ s/\@.*\z//sr;
}
sub author_name {
_run(qw(git log -1 --format=%an), _temporary_commit());
}
# This insanity creates a new, unattached commit and returns its commit ID.
# It does not change the index or any of the refs.
sub _temporary_commit {
my $commit_id = qx{git commit-tree HEAD^{tree} < /dev/null};
chomp($commit_id);
return $commit_id;
}
sub in_any_repo {
exit (_in_any_repo() ? 0 : 1);
}
sub _in_any_repo {
system("git rev-parse HEAD >> /dev/null 2>&1") == 0;
}
sub _run {
my @cmd = @_;
open my($p), "-|", @cmd or die "Couldn't run '@cmd': $!";
my $res = join "", <$p>;
chomp($res);
return $res;
}
sub _usage {
my ($what) = @_;
warn "Don't know how to get '$what'; try:\n" if defined $what;
print STDERR join "\n", _command_names(), "";
exit;
}
sub _command_names {
my @names = sort grep !/^_/ && defined &{$_}, keys %main::;
tr/_/-/ for @names;
return @names;
}