Skip to content

Commit 78a6937

Browse files
committed
Refactor git-follow script
1 parent 06cbf38 commit 78a6937

File tree

1 file changed

+66
-71
lines changed

1 file changed

+66
-71
lines changed

git-follow

Lines changed: 66 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,42 @@ use Getopt::Long qw(
1616
Configure
1717
GetOptions
1818
);
19-
use GitFollow::Core;
19+
use GitFollow::Cli::OptionsNormalizer qw(
20+
format
21+
normalize
22+
);
2023
use GitFollow::Config qw(
2124
get_config
2225
has_config
2326
);
24-
use GitFollow::Metadata qw(print_version);
27+
use GitFollow::Log qw(
28+
print_total
29+
set_refspec
30+
$DEFAULT_LOG_FMT
31+
$INVALID_PATH_ERR
32+
$INVALID_PATH_WITHIN_RANGE_ERR
33+
$INVALID_REPO_ERR
34+
$INVALID_REPO_HINT
35+
);
36+
use GitFollow::Environment qw($GIT_PATH);
37+
use GitFollow::Metadata qw(
38+
print_usage
39+
print_version
40+
);
2541
use GitFollow::Repository::ObjectUtils qw(
2642
is_object
2743
is_repo
2844
);
2945

3046
# If --version (or -V) was given as an option,
3147
# print the current release version and exit.
32-
&print_version() if grep { $_ eq "--version" or $_ eq "-V" } @ARGV;
48+
print_version() if grep { $_ eq "--version" or $_ eq "-V" } @ARGV;
3349

34-
our ($DEFAULT_LOG_FMT, $INVALID_PATH_ERR, $INVALID_PATH_WITHIN_RANGE_ERR);
35-
our ($INVALID_REPO_ERR, $INVALID_REPO_HINT);
36-
my ($pathspec, $refspec, @refs);
50+
# If git-follow was executed without arguments,
51+
# print usage information and exit.
52+
print_usage() unless @ARGV;
53+
54+
my ($pathspec, $refspec, @refs) = undef;
3755

3856
# Diff modes and their git-log(1) option counterparts.
3957
my %diffopts = (
@@ -43,12 +61,11 @@ my %diffopts = (
4361
);
4462

4563
my %git_log = (
46-
"exec" => "/usr/bin/git",
4764
"pager_mode" => "--paginate",
4865
"command" => "log",
4966
);
5067

51-
my %git_log_options = (
68+
my %git_log_opts = (
5269
"diff" => "--word-diff=%s",
5370
"m" => "-m",
5471
"follow" => "--follow",
@@ -58,122 +75,100 @@ my %git_log_options = (
5875
);
5976

6077
# follow.pager.disable configuration. Replace --paginate with --no-pager if set to true.
61-
$git_log{"pager_mode"} = "--no-pager" if &has_config("pager", "disable") and &get_config("pager", "disable") eq "true";
78+
$git_log{"pager_mode"} = "--no-pager" if has_config("pager", "disable") and get_config("pager", "disable") eq "true";
6279

6380
# follow.diff.mode configuration.
64-
if (&has_config("diff", "mode")) {
65-
my $diffmode = &get_config("diff", "mode");
81+
if (has_config("diff", "mode")) {
82+
my $diffmode = get_config("diff", "mode");
6683

6784
die sprintf("Invalid value '%s' specified for follow.diff.mode\n", $diffmode) unless grep { $_ eq $diffmode } keys %diffopts;
6885

6986
# Set corresponding --word-diff config value.
70-
$git_log_options{"diff"} = sprintf($git_log_options{"diff"}, $diffopts{$diffmode});
87+
$git_log_opts{"diff"} = sprintf($git_log_opts{"diff"}, $diffopts{$diffmode});
7188
}
7289

7390
# follow.log.format configuration.
74-
$git_log_options{"format"} = sprintf($git_log_options{"format"}, (&has_config("log", "format") ? &get_config("log", "format") : $DEFAULT_LOG_FMT));
75-
76-
# Options and their conflicting counterparts.
77-
my %copts = (
78-
"no-merges" => ["m"],
79-
"no-patch" => ["patch"],
80-
"no-renames" => ["follow"],
81-
"reverse" => [
82-
"graph",
83-
"follow",
84-
],
91+
$git_log_opts{"format"} = sprintf(
92+
$git_log_opts{"format"},
93+
(has_config("log", "format") ? get_config("log", "format") : $DEFAULT_LOG_FMT)
8594
);
8695

87-
# Default argument values for options that accept arguments.
88-
my %dargs = (
89-
"last" => 1,
90-
"lines" => 1,
91-
);
92-
93-
die "$USAGE_SYNOPSIS" unless @ARGV;
96+
print_usage() unless @ARGV;
9497

9598
# Validate we're inside a Git repository.
96-
die sprintf("%s\n%s", sprintf($INVALID_REPO_ERR, getcwd), $INVALID_REPO_HINT) unless &is_repo();
99+
die sprintf("%s\n%s", sprintf($INVALID_REPO_ERR, getcwd), $INVALID_REPO_HINT) unless is_repo();
97100

98101
$pathspec = ($ARGV[$#ARGV] eq ".")
99-
? &basename(getcwd) : $ARGV[$#ARGV];
102+
? basename(getcwd) : $ARGV[$#ARGV];
100103

101-
# @todo: Utilize $dispatch{"show_total"} instead.
102-
# ------------------------------------------------
103104
# If --total (or -T) was given as an option, print
104105
# total number of revisions for pathspec and exit.
105-
&show_total($pathspec) if grep { $_ eq "--total" or $_ eq "-T" } @ARGV;
106+
print_total($pathspec) if grep { $_ eq "--total" or $_ eq "-T" } @ARGV;
106107

107108
my @apts = ();
108109
my %dispatch = (
109110
# Set alias, passthrough options and option arguments.
110-
"set_apt_opt" => sub {
111-
push @apts, &get_format_apt(($pathspec, @_));
111+
"set_option" => sub {
112+
push @apts, &format('log', $pathspec, @_);
112113
},
113-
"set_pager" => sub {
114+
"set_pager" => sub {
114115
$git_log{"pager_mode"} = "--paginate";
115116
},
116-
"set_refspec" => sub {
117-
&set_refspec((@_, \$refspec));
117+
"set_refspec" => sub {
118+
set_refspec((@_, \$refspec));
118119
},
119-
"set_unary_opt" => sub {
120+
"set_flag" => sub {
120121
my $option = shift;
121122

122123
# Get formatted git-log(1) option from the unary option given.
123-
$git_log_options{$option} = &get_format_apt($pathspec, $option);
124-
125-
# Remove any conflicting options from %git_log_options.
126-
&rm_copts($option, \%copts, \%git_log_options);
127-
},
128-
"show_total" => sub {
129-
&show_total($pathspec);
130-
},
131-
"show_usage" => sub {
132-
print "$USAGE_SYNOPSIS";
133-
exit 0;
124+
$git_log_opts{$option} = &format('log', $pathspec, $option);
125+
normalize(\%git_log_opts, $option);
134126
},
135-
"print_version" => sub {
136-
&print_version();
127+
"print_total" => sub {
128+
print_total($pathspec)
137129
},
130+
"print_usage" => \&print_usage,
131+
"print_version" => \&print_version,
138132
);
139133

140-
&Configure(
134+
Configure(
141135
"no_auto_abbrev",
142136
"no_ignore_case",
143137
"require_order",
144138
);
145139

146-
&GetOptions(
140+
GetOptions(
147141
'branch|b=s{1,1}' => $dispatch{"set_refspec"},
148-
'first|f' => $dispatch{"set_apt_opt"},
149-
'func|F=s{1,1}' => $dispatch{"set_apt_opt"},
150-
'last|l=i{0,1}' => $dispatch{"set_apt_opt"},
151-
'lines|L=s{1,1}' => $dispatch{"set_apt_opt"},
152-
'no-merges|M' => $dispatch{"set_unary_opt"},
153-
'no-patch|N' => $dispatch{"set_unary_opt"},
154-
'no-renames|O' => $dispatch{"set_unary_opt"},
142+
'first|f' => $dispatch{"set_option"},
143+
'func|F=s{1,1}' => $dispatch{"set_option"},
144+
'last|l=i{0,1}' => $dispatch{"set_option"},
145+
'lines|L=s{1,1}' => $dispatch{"set_option"},
146+
'no-merges|M' => $dispatch{"set_flag"},
147+
'no-patch|N' => $dispatch{"set_flag"},
148+
'no-renames|O' => $dispatch{"set_flag"},
155149
'pager|p' => $dispatch{"set_pager"},
156-
'pickaxe|P=s{1,1}' => $dispatch{"set_apt_opt"},
150+
'pickaxe|P=s{1,1}' => $dispatch{"set_option"},
157151
'range|r=s{1,1}' => $dispatch{"set_refspec"},
158-
'reverse|R' => $dispatch{"set_unary_opt"},
152+
'reverse|R' => $dispatch{"set_flag"},
159153
'tag|t=s{1,1}' => $dispatch{"set_refspec"},
160-
'total|T' => $dispatch{"show_total"},
161-
'usage|help|h' => $dispatch{"show_usage"},
154+
'total|T' => $dispatch{"print_total"},
155+
'usage|help|h' => $dispatch{"print_usage"},
162156
'version|V' => $dispatch{"print_version"},
163-
) or &on_error();
157+
) or print_usage();
164158

165-
# Set default ref if not given explicitly via --branch, --range, or --tag.
159+
# Set default ref to HEAD if not given explicitly
160+
# via option --branch, --range, or --tag.
166161
$refspec = "HEAD" unless defined $refspec;
167162

168163
# Attempt split at .. range delimiter.
169164
@refs = split /\.{2}/, $refspec;
170165

171166
# Verify pathspec is valid given each ref in @refs.
172167
foreach my $ref (@refs) {
173-
die sprintf($INVALID_PATH_WITHIN_RANGE_ERR, $pathspec, $refspec) unless &is_object($ref, $pathspec);
168+
die sprintf($INVALID_PATH_WITHIN_RANGE_ERR, $pathspec, $refspec) unless is_object($ref, $pathspec);
174169
}
175170

176-
system $git_log{"exec"}, $git_log{"pager_mode"}, $git_log{"command"}, @apts, values %git_log_options, $refspec, "--", $pathspec;
171+
system $GIT_PATH, $git_log{"pager_mode"}, $git_log{"command"}, @apts, values %git_log_opts, $refspec, "--", $pathspec;
177172

178173
1;
179174

0 commit comments

Comments
 (0)