Skip to content

Commit e2a33e1

Browse files
committed
Refactor Core module
1 parent 836d1d7 commit e2a33e1

File tree

1 file changed

+25
-100
lines changed

1 file changed

+25
-100
lines changed

src/GitFollow/Core.pm

Lines changed: 25 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,21 @@
33
###
44
### Copyright (C) 2017 Nickolas Burr <nickolasburr@gmail.com>
55
###
6-
76
package GitFollow::Core;
7+
88
use 5.008;
99
use strict;
1010
use warnings;
1111
use Exporter qw(import);
12+
use GitFollow::Environment qw($GIT_PATH);
1213

1314
our @EXPORT = qw(
14-
get_config
1515
get_format_apt
16-
has_config
17-
is_pathspec
18-
is_repo
1916
on_error
2017
rm_copts
2118
set_refspec
2219
set_unary_opt
2320
show_total
24-
show_version
2521
$DEFAULT_LOG_FMT
2622
$INVALID_PATH_ERR
2723
$INVALID_PATH_WITHIN_RANGE_ERR
@@ -39,12 +35,9 @@ our %parts = (
3935
"time" => "%C(bold green)%cr%Creset",
4036
);
4137

42-
# Default git-log format.
38+
# Default git-log(1) format.
4339
our $DEFAULT_LOG_FMT = "$parts{'hash'} ($parts{'tree'}) - $parts{'entry'} - $parts{'name'} <$parts{'email'}> [$parts{'time'}]";
4440

45-
# Current release version.
46-
our $GIT_FOLLOW_VERSION = "1.1.5";
47-
4841
###
4942
### Environment variables.
5043
###
@@ -64,27 +57,27 @@ our $INVALID_REPO_ERR = "%s is not a Git repository.\n";
6457
our $INVALID_REPO_HINT = "FYI: If you don't want to change directories, you can run 'git -C /path/to/repository follow ...'\n";
6558
our $INVALID_PATH_ERR = "%s is not a valid pathspec.\n";
6659
our $INVALID_PATH_WITHIN_RANGE_ERR = "%s is not a valid pathspec within range %s.\n";
67-
our $USAGE_SYNOPSIS = <<"END_USAGE_SYNOPSIS";
60+
our $USAGE_SYNOPSIS = <<"END_USAGE_SYNOPSIS";
6861
69-
Usage: git follow [OPTIONS] [--] pathspec
62+
Usage: git follow [OPTIONS] [--] <pathspec>
7063
7164
Options:
7265
73-
--branch, -b <branchref> Show commits for pathspec, specific to a branch.
74-
--first, -f Show first commit where Git initiated tracking of pathspec.
75-
--func, -F <funcname> Show commits which affected function <funcname> in pathspec.
76-
--last, -l [<count>] Show last <count> commits which affected pathspec. Omitting <count> defaults to last commit.
77-
--lines, -L <start> [<end>] Show commits which affected lines <start> through <end> in pathspec. Omitting <end> defaults to EOF.
78-
--no-merges, -M Show commits which have a maximum of one parent. See --no-merges of git-log(1).
79-
--no-patch, -N Suppress diff output. See --no-patch of git-log(1).
80-
--no-renames, -O Disable rename detection. See --no-renames of git-log(1).
81-
--pager, -p Force pager when invoking git-log(1). Overrides follow.pager.disable config value.
82-
--pickaxe, -P <string> Show commits which change the number of occurrences of <string> in pathspec. See -S of git-log(1).
83-
--range, -r <startref> [<endref>] Show commits in range <startref> to <endref> which affected pathspec. Omitting <endref> defaults to HEAD. See gitrevisions(1).
84-
--reverse, -R Show commits in reverse chronological order. See --walk-reflogs of git-log(1).
85-
--tag, -t <tagref> Show commits for pathspec, specific to a tag.
86-
--total, -T Show total number of commits for pathspec.
87-
--version, -V Show current release version.
66+
-b, --branch <branchref> Show commits for pathspec, specific to a branch.
67+
-f, --first Show first commit where Git initiated tracking of pathspec.
68+
-F, --func <funcname> Show commits which affected function <funcname> in pathspec.
69+
-l, --last [<count>] Show last <count> commits which affected pathspec. Omitting <count> defaults to last commit.
70+
-L, --lines <start> [<end>] Show commits which affected lines <start> through <end> in pathspec. Omitting <end> defaults to EOF.
71+
-M, --no-merges Show commits which have a maximum of one parent. See --no-merges of git-log(1).
72+
-N, --no-patch Suppress diff output. See --no-patch of git-log(1).
73+
-O, --no-renames Disable rename detection. See --no-renames of git-log(1).
74+
-p, --pager Force pager when invoking git-log(1). Overrides follow.pager.disable config value.
75+
-P, --pickaxe <string> Show commits which change the number of occurrences of <string> in pathspec. See -S of git-log(1).
76+
-r, --range <startref> [<endref>] Show commits in range <startref> to <endref> which affected pathspec. Omitting <endref> defaults to HEAD. See gitrevisions(1).
77+
-R, --reverse Show commits in reverse chronological order. See --walk-reflogs of git-log(1).
78+
-t, --tag <tagref> Show commits for pathspec, specific to a tag.
79+
-T, --total Show total number of commits for pathspec.
80+
-V, --version Show current release version.
8881
8982
END_USAGE_SYNOPSIS
9083

@@ -94,57 +87,19 @@ END_USAGE_SYNOPSIS
9487

9588
sub get_format_apt;
9689
sub get_rev_range;
97-
sub get_config;
98-
sub has_config;
9990
sub is_int;
100-
sub is_pathspec;
101-
sub is_repo;
10291
sub on_error;
10392
sub rm_copts;
10493
sub set_refspec;
10594
sub set_unary_opt;
10695
sub show_total;
107-
sub show_version;
10896

10997
# Display usage information, and exit failure.
11098
sub on_error {
11199
print "$USAGE_SYNOPSIS";
112-
113100
exit 1;
114101
}
115102

116-
# Get git-config value.
117-
sub get_config {
118-
my ($key, $qual) = @_;
119-
my $config = undef;
120-
121-
system("git config follow.$key.$qual >/dev/null");
122-
123-
$config = (!$?)
124-
? `git config follow.$key.$qual`
125-
: `git config follow.$key$qual`;
126-
127-
# Strip trailing newline from config value.
128-
chomp $config;
129-
130-
return $config;
131-
}
132-
133-
# Check if git-config key exists.
134-
sub has_config {
135-
my ($key, $qual) = @_;
136-
137-
system("git config follow.$key.$qual >/dev/null");
138-
139-
if (!$?) {
140-
return 1;
141-
}
142-
143-
system("git config follow.$key$qual >/dev/null");
144-
145-
!($? >> 8);
146-
}
147-
148103
# Determine if value is an integer.
149104
sub is_int {
150105
my $num = shift;
@@ -156,31 +111,13 @@ sub is_int {
156111
}
157112
}
158113

159-
# Determine if pathspec is a valid file object.
160-
sub is_pathspec {
161-
my ($refspec, $pathspec) = @_;
162-
163-
# Validate pathspec via git-cat-file.
164-
system("git cat-file -e $refspec:$pathspec &>/dev/null");
165-
166-
!($? >> 8);
167-
}
168-
169-
# Determine if we're inside a Git repository.
170-
sub is_repo {
171-
system("git rev-parse --is-inside-work-tree &>/dev/null");
172-
173-
!($? >> 8);
174-
}
175-
176114
# Get revision range via start and end boundaries.
177115
sub get_rev_range {
178116
my $range = shift;
179117
my ($start, $end) = split ',', $range;
180118

181119
# If no end revision was given, default to HEAD.
182120
$end = "HEAD" unless defined $end;
183-
184121
return "$start..$end";
185122
}
186123

@@ -193,7 +130,6 @@ sub get_format_apt {
193130
return "--diff-filter=A";
194131
} elsif ($opt eq "func") {
195132
my $funcname = shift @args;
196-
197133
return "-L:$funcname:$pathspec";
198134
} elsif ($opt eq "last") {
199135
my $num = shift @args;
@@ -203,7 +139,6 @@ sub get_format_apt {
203139
}
204140

205141
die sprintf($INVALID_NUM_ARG, $num) unless &is_int($num);
206-
207142
return "--max-count=$num";
208143
} elsif ($opt eq "lines") {
209144
my $lines = shift @args;
@@ -216,7 +151,6 @@ sub get_format_apt {
216151
}
217152
} elsif ($opt eq "pickaxe") {
218153
my $subject = shift @args;
219-
220154
return "-S$subject";
221155
} else {
222156
return "--$opt";
@@ -245,8 +179,8 @@ sub set_refspec {
245179
if ($opt eq "range") {
246180
$$refspec = &get_rev_range($ref);
247181
} else {
248-
my $refs = `git $opt --list`;
249-
my $remotes = `git branch -r` if $opt eq "branch";
182+
my $refs = `$GIT_PATH $opt --list`;
183+
my $remotes = `$GIT_PATH branch -r` if $opt eq "branch";
250184
$refs = $refs . $remotes if defined $remotes;
251185

252186
# Filter asterisk, escape codes from `git {branch,tag} --list`.
@@ -274,29 +208,20 @@ sub show_total {
274208

275209
# Whether to use rename detection or stop at renames.
276210
my $fopt = (grep { $_ eq "--no-renames" || $_ eq "-O" } @ARGV)
277-
? "--no-renames"
278-
: "--follow";
211+
? "--no-renames" : "--follow";
279212

280213
# Use pathspec, if defined. Otherwise,
281214
# get the last element in @ARGV array.
282215
my $path = (defined $pathspec)
283-
? $pathspec
284-
: $ARGV[$#ARGV];
216+
? $pathspec : $ARGV[$#ARGV];
285217

286218
# Array of abbreviated commit hashes.
287-
my @hashes = `git log $fopt --format=\"%h\" -- $path`;
219+
my @hashes = `$GIT_PATH log $fopt --format=\"%h\" -- $path`;
288220

289221
print scalar @hashes;
290222
print "\n";
291223

292224
exit 0;
293225
}
294226

295-
# Show current release version.
296-
sub show_version {
297-
print "$GIT_FOLLOW_VERSION\n";
298-
299-
exit 0;
300-
}
301-
302227
1;

0 commit comments

Comments
 (0)