-
Notifications
You must be signed in to change notification settings - Fork 4
/
git-follow
executable file
·303 lines (226 loc) · 8 KB
/
git-follow
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env perl
###
### git-follow - Follow lifetime changes of a pathspec in Git.
###
### Copyright (C) 2017 Nickolas Burr <nickolasburr@gmail.com>
###
use 5.008;
use strict;
use warnings;
use lib "/usr/local/opt/git-follow/src";
use Cwd qw(getcwd);
use File::Basename qw(basename);
use Getopt::Long qw(
Configure
GetOptions
);
use GitFollow::Cli::OptionsNormalizer qw(
format
normalize
);
use GitFollow::Config qw(
get_config
has_config
);
use GitFollow::Log qw(
print_total
set_refspec
$DEFAULT_LOG_FMT
$INVALID_PATH_ERR
$INVALID_PATH_WITHIN_RANGE_ERR
$INVALID_REPO_ERR
$INVALID_REPO_HINT
);
use GitFollow::Environment qw($GIT_PATH);
use GitFollow::Metadata qw(
print_usage
print_version
);
use GitFollow::Repository::ObjectUtils qw(
is_object
is_repo
);
# If --version (or -V) was given as an option,
# print the current release version and exit.
print_version() if grep { $_ eq "--version" or $_ eq "-V" } @ARGV;
# If git-follow was executed without arguments,
# print usage information and exit.
print_usage() unless @ARGV;
my ($pathspec, $refspec, @refs) = undef;
# Diff modes and their git-log(1) option counterparts.
my %diffopts = (
"inline" => "none",
"sxs" => "plain",
"colorsxs" => "color",
);
my %git_log = (
"pager_mode" => "--paginate",
"command" => "log",
);
my %git_log_opts = (
"diff" => "--word-diff=%s",
"m" => "-m",
"follow" => "--follow",
"format" => "--format=%s",
"graph" => "--graph",
"patch" => "--patch-with-stat",
);
# follow.pager.disable configuration. Replace --paginate with --no-pager if set to true.
$git_log{"pager_mode"} = "--no-pager" if has_config("pager", "disable") and get_config("pager", "disable") eq "true";
# follow.diff.mode configuration.
if (has_config("diff", "mode")) {
my $diffmode = get_config("diff", "mode");
die sprintf("Invalid value '%s' specified for follow.diff.mode\n", $diffmode) unless grep { $_ eq $diffmode } keys %diffopts;
# Set corresponding --word-diff config value.
$git_log_opts{"diff"} = sprintf($git_log_opts{"diff"}, $diffopts{$diffmode});
}
# follow.log.format configuration.
$git_log_opts{"format"} = sprintf(
$git_log_opts{"format"},
(has_config("log", "format") ? get_config("log", "format") : $DEFAULT_LOG_FMT)
);
print_usage() unless @ARGV;
# Validate we're inside a Git repository.
die sprintf("%s\n%s", sprintf($INVALID_REPO_ERR, getcwd), $INVALID_REPO_HINT) unless is_repo();
$pathspec = ($ARGV[$#ARGV] eq ".")
? basename(getcwd) : $ARGV[$#ARGV];
# If --total (or -T) was given as an option, print
# total number of revisions for pathspec and exit.
print_total($pathspec) if grep { $_ eq "--total" or $_ eq "-T" } @ARGV;
my @apts = ();
my %dispatch = (
# Set alias, passthrough options and option arguments.
"set_option" => sub {
push @apts, &format('log', $pathspec, @_);
},
"set_pager" => sub {
$git_log{"pager_mode"} = "--paginate";
},
"set_refspec" => sub {
set_refspec((@_, \$refspec));
},
"set_flag" => sub {
my $option = shift;
# Get formatted git-log(1) option from the unary option given.
$git_log_opts{$option} = &format('log', $pathspec, $option);
normalize(\%git_log_opts, $option);
},
"print_total" => sub {
print_total($pathspec)
},
"print_usage" => \&print_usage,
"print_version" => \&print_version,
);
Configure(
"no_auto_abbrev",
"no_ignore_case",
"require_order",
);
GetOptions(
'branch|b=s{1,1}' => $dispatch{"set_refspec"},
'first|f' => $dispatch{"set_option"},
'func|F=s{1,1}' => $dispatch{"set_option"},
'last|l=i{0,1}' => $dispatch{"set_option"},
'lines|L=s{1,1}' => $dispatch{"set_option"},
'no-merges|M' => $dispatch{"set_flag"},
'no-patch|N' => $dispatch{"set_flag"},
'no-renames|O' => $dispatch{"set_flag"},
'pager|p' => $dispatch{"set_pager"},
'pickaxe|P=s{1,1}' => $dispatch{"set_option"},
'range|r=s{1,1}' => $dispatch{"set_refspec"},
'reverse|R' => $dispatch{"set_flag"},
'tag|t=s{1,1}' => $dispatch{"set_refspec"},
'total|T' => $dispatch{"print_total"},
'usage|help|h' => $dispatch{"print_usage"},
'version|V' => $dispatch{"print_version"},
) or print_usage();
# Set default ref to HEAD if not given explicitly
# via option --branch, --range, or --tag.
$refspec = "HEAD" unless defined $refspec;
# Attempt split at .. range delimiter.
@refs = split /\.{2}/, $refspec;
# Verify pathspec is valid given each ref in @refs.
foreach my $ref (@refs) {
die sprintf($INVALID_PATH_WITHIN_RANGE_ERR, $pathspec, $refspec) unless is_object($ref, $pathspec);
}
system $GIT_PATH, $git_log{"pager_mode"}, $git_log{"command"}, @apts, values %git_log_opts, $refspec, "--", $pathspec;
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
git-follow - Follow lifetime changes of a pathspec in Git.
=head1 VERSION
version 1.1.5
=head1 DESCRIPTION
Follow lifetime changes of a pathspec in Git. git-follow(1) makes analyzing changes of a pathspec trivial with robust options and simplified log output.
=head1 CONFIGURATION
Configuration values set via git-config(1) can be used to customize the behavior of git-follow.
follow.diff.mode
Diff mode. Choices include inline, sxs, and colorsxs. See --word-diff, --color-words, et al. of git-log(1).
follow.log.format
Log format. See --format of git-log(1) for syntax.
follow.pager.disable
Disable pager. Defaults to false. Set to true to disable pager. See --no-pager of git(1).
=head1 OPTIONS
-b, --branch <branchref>
Show commits specific to a branch.
-f, --first
Show first commit where Git initiated tracking of pathspec.
-F, --func <funcname>
Show commits which affected function <funcname> in pathspec. See -L of git-log(1).
-l, --last [<count>]
Show last <count> commits which affected pathspec. Omitting <count> defaults to last commit.
-L, --lines <start>[,<end>]
Show commits which affected lines <start> to <end> in pathspec. Omitting <end> defaults to EOF.
-M, --no-merges
Show commits which have a maximum of one parent. See --no-merges of git-log(1).
-N, --no-patch
Suppress diff output. See --no-patch of git-log(1).
-O, --no-renames
Disable rename detection. See --no-renames of git-log(1).
-p, --pager
Force pager when invoking git-log(1). Overrides follow.pager.disable config value.
-P, --pickaxe <string>
Show commits which change the number of occurrences of <string> in pathspec. See -S of git-log(1).
-r, --range <start>[,<end>]
Show commits in range <start> to <end>. Omitting <endref> defaults to HEAD. See git-revisions(1).
-R, --reverse
Show commits in reverse chronological order. See --walk-reflogs of git-log(1).
-t, --tag <tagref>
Show commits specific to a tag.
-T, --total
Show total number of commits for pathspec.
-V, --version
Show current release version.
=head1 NOTES
Like standard Git builtins, git-follow supports an optional pathspec delimiter [--] to help disambiguate options, option arguments, and refs from pathspecs.
=head1 EXAMPLES
Display commits on branch 'topic'
git follow --branch topic -- blame.c
Display first commit where Git initiated tracking
git follow --first -- branch.c
Display last 5 commits
git follow --last 5 -- Makefile
Display last commit where lines 5 through EOF were affected
git follow --last --lines 5 -- apply.c
Display last 3 commits where lines 10 through 15 were affected
git follow --last 3 --lines 10,15 -- bisect.c
Display commits where function `funcname' was affected
git follow --func funcname -- archive.c
Display commits in range from aa03428 to b354ef9
git follow --range aa03428,b354ef9 -- worktree.c
Display commits in range from v1.5.3 to v1.5.4
git follow --range v1.5.3,v1.5.4 -- apply.c
Display commits up to tag v1.5.3
git follow --tag v1.5.3 -- graph.c
Display total number of commits
git follow --total -- rebase.c
=head1 BUGS
https://github.com/nickolasburr/git-follow/issues
=head1 AUTHOR
Written by Nickolas Burr <nickolasburr@gmail.com>
=head1 SEE ALSO
git(1), git-branch(1), git-check-ref-format(1), git-config(1), git-diff(1), git-log(1), git-remote(1), git-revisions(1), git-tag(1)
=cut
# vim: syntax=perl