difftool: use Git::* functions instead of passing around state

Call Git::command() and friends directly wherever possible. This makes it clear that these operations can be invoked directly without needing to manage the current directory and related GIT_* environment variables. Eliminate find_repository() since we can now use wc_path() and not worry about side-effects involving environment variables. Signed-off-by: David Aguilar <davvid@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Aguilar committed Jul 18, 2016 at 20:57 UTC 32b8c581ecf35e73bebe2c6e9f6de617807b7f91
1 file changed +21 -31
git-difftool.perl
+21 -31
@@ -37,14 +37,6 @@ USAGE
37 exit($exitcode);
38 }
39
40 -sub find_worktree
41 -{
42 - # Git->repository->wc_path() does not honor changes to the working
43 - # tree location made by $ENV{GIT_WORK_TREE} or the 'core.worktree'
44 - # config variable.
45 - return Git::command_oneline('rev-parse', '--show-toplevel');
46 -}
47 -
40 sub print_tool_help
41 {
42 # See the comment at the bottom of file_diff() for the reason behind
@@ -67,14 +59,14 @@ sub exit_cleanup
59
60 sub use_wt_file
61 {
70 - my ($repo, $workdir, $file, $sha1) = @_;
62 + my ($workdir, $file, $sha1) = @_;
63 my $null_sha1 = '0' x 40;
64
65 if (-l "$workdir/$file" || ! -e _) {
66 return (0, $null_sha1);
67 }
68
77 - my $wt_sha1 = $repo->command_oneline('hash-object', "$workdir/$file");
69 + my $wt_sha1 = Git::command_oneline('hash-object', "$workdir/$file");
70 my $use = ($sha1 eq $null_sha1) || ($sha1 eq $wt_sha1);
71 return ($use, $wt_sha1);
72 }
@@ -108,11 +100,9 @@ sub changed_files
100
101 sub setup_dir_diff
102 {
111 - my ($repo, $workdir, $symlinks) = @_;
112 -
113 - my $repo_path = $repo->repo_path();
103 + my ($workdir, $symlinks) = @_;
104 my @gitargs = ('diff', '--raw', '--no-abbrev', '-z', @ARGV);
115 - my $diffrtn = $repo->command_oneline(@gitargs);
105 + my $diffrtn = Git::command_oneline(@gitargs);
106 exit(0) unless defined($diffrtn);
107
108 # Build index info for left and right sides of the diff
@@ -164,12 +154,12 @@ EOF
154
155 if ($lmode eq $symlink_mode) {
156 $symlink{$src_path}{left} =
167 - $repo->command_oneline('show', "$lsha1");
157 + Git::command_oneline('show', $lsha1);
158 }
159
160 if ($rmode eq $symlink_mode) {
161 $symlink{$dst_path}{right} =
172 - $repo->command_oneline('show', "$rsha1");
162 + Git::command_oneline('show', $rsha1);
163 }
164
165 if ($lmode ne $null_mode and $status !~ /^C/) {
@@ -181,8 +171,8 @@ EOF
171 if ($working_tree_dups{$dst_path}++) {
172 next;
173 }
184 - my ($use, $wt_sha1) = use_wt_file($repo, $workdir,
185 - $dst_path, $rsha1);
174 + my ($use, $wt_sha1) =
175 + use_wt_file($workdir, $dst_path, $rsha1);
176 if ($use) {
177 push @working_tree, $dst_path;
178 $wtindex .= "$rmode $wt_sha1\t$dst_path\0";
@@ -203,27 +193,27 @@ EOF
193 my ($inpipe, $ctx);
194 $ENV{GIT_INDEX_FILE} = "$tmpdir/lindex";
195 ($inpipe, $ctx) =
206 - $repo->command_input_pipe(qw(update-index -z --index-info));
196 + Git::command_input_pipe('update-index', '-z', '--index-info');
197 print($inpipe $lindex);
208 - $repo->command_close_pipe($inpipe, $ctx);
198 + Git::command_close_pipe($inpipe, $ctx);
199
200 my $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/");
201 exit_cleanup($tmpdir, $rc) if $rc != 0;
202
203 $ENV{GIT_INDEX_FILE} = "$tmpdir/rindex";
204 ($inpipe, $ctx) =
215 - $repo->command_input_pipe(qw(update-index -z --index-info));
205 + Git::command_input_pipe('update-index', '-z', '--index-info');
206 print($inpipe $rindex);
217 - $repo->command_close_pipe($inpipe, $ctx);
207 + Git::command_close_pipe($inpipe, $ctx);
208
209 $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/");
210 exit_cleanup($tmpdir, $rc) if $rc != 0;
211
212 $ENV{GIT_INDEX_FILE} = "$tmpdir/wtindex";
213 ($inpipe, $ctx) =
224 - $repo->command_input_pipe(qw(update-index --info-only -z --index-info));
214 + Git::command_input_pipe('update-index', '--info-only', '-z', '--index-info');
215 print($inpipe $wtindex);
226 - $repo->command_close_pipe($inpipe, $ctx);
216 + Git::command_close_pipe($inpipe, $ctx);
217
218 # If $GIT_DIR was explicitly set just for the update/checkout
219 # commands, then it should be unset before continuing.
@@ -393,9 +383,9 @@ sub dir_diff
383 my $rc;
384 my $error = 0;
385 my $repo = Git->repository();
396 - my $workdir = find_worktree();
397 - my ($a, $b, $tmpdir, @worktree) =
398 - setup_dir_diff($repo, $workdir, $symlinks);
386 + my $repo_path = $repo->repo_path();
387 + my $workdir = $repo->wc_path();
388 + my ($a, $b, $tmpdir, @worktree) = setup_dir_diff($workdir, $symlinks);
389
390 if (defined($extcmd)) {
391 $rc = system($extcmd, $a, $b);
@@ -421,10 +411,10 @@ sub dir_diff
411 next if ! -f "$b/$file";
412
413 if (!$indices_loaded) {
424 - %wt_modified = changed_files($repo->repo_path(),
425 - "$tmpdir/wtindex", "$workdir");
426 - %tmp_modified = changed_files($repo->repo_path(),
427 - "$tmpdir/wtindex", "$b");
414 + %wt_modified = changed_files(
415 + $repo_path, "$tmpdir/wtindex", $workdir);
416 + %tmp_modified = changed_files(
417 + $repo_path, "$tmpdir/wtindex", $b);
418 $indices_loaded = 1;
419 }
420