THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

503 lines
13 KiB

  1. #!/usr/bin/perl
  2. # Copyright (c) 2009, 2010 David Aguilar
  3. # Copyright (c) 2012 Tim Henigan
  4. #
  5. # This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
  6. # git-difftool--helper script.
  7. #
  8. # This script exports GIT_EXTERNAL_DIFF and GIT_PAGER for use by git.
  9. # The GIT_DIFF* variables are exported for use by git-difftool--helper.
  10. #
  11. # Any arguments that are unknown to this script are forwarded to 'git diff'.
  12. use 5.008;
  13. use strict;
  14. use warnings;
  15. use Error qw(:try);
  16. use File::Basename qw(dirname);
  17. use File::Copy;
  18. use File::Find;
  19. use File::stat;
  20. use File::Path qw(mkpath rmtree);
  21. use File::Temp qw(tempdir);
  22. use Getopt::Long qw(:config pass_through);
  23. use Git;
  24. sub usage
  25. {
  26. my $exitcode = shift;
  27. print << 'USAGE';
  28. usage: git difftool [-t|--tool=<tool>] [--tool-help]
  29. [-x|--extcmd=<cmd>]
  30. [-g|--gui] [--no-gui]
  31. [--prompt] [-y|--no-prompt]
  32. [-d|--dir-diff]
  33. ['git diff' options]
  34. USAGE
  35. exit($exitcode);
  36. }
  37. sub find_worktree
  38. {
  39. # Git->repository->wc_path() does not honor changes to the working
  40. # tree location made by $ENV{GIT_WORK_TREE} or the 'core.worktree'
  41. # config variable.
  42. return Git::command_oneline('rev-parse', '--show-toplevel');
  43. }
  44. sub print_tool_help
  45. {
  46. # See the comment at the bottom of file_diff() for the reason behind
  47. # using system() followed by exit() instead of exec().
  48. my $rc = system(qw(git mergetool --tool-help=diff));
  49. exit($rc | ($rc >> 8));
  50. }
  51. sub exit_cleanup
  52. {
  53. my ($tmpdir, $status) = @_;
  54. my $errno = $!;
  55. rmtree($tmpdir);
  56. if ($status and $errno) {
  57. my ($package, $file, $line) = caller();
  58. warn "$file line $line: $errno\n";
  59. }
  60. exit($status | ($status >> 8));
  61. }
  62. sub use_wt_file
  63. {
  64. my ($repo, $workdir, $file, $sha1) = @_;
  65. my $null_sha1 = '0' x 40;
  66. if (-l "$workdir/$file" || ! -e _) {
  67. return (0, $null_sha1);
  68. }
  69. my $wt_sha1 = $repo->command_oneline('hash-object', "$workdir/$file");
  70. my $use = ($sha1 eq $null_sha1) || ($sha1 eq $wt_sha1);
  71. return ($use, $wt_sha1);
  72. }
  73. sub changed_files
  74. {
  75. my ($repo_path, $index, $worktree) = @_;
  76. $ENV{GIT_INDEX_FILE} = $index;
  77. $ENV{GIT_WORK_TREE} = $worktree;
  78. my $must_unset_git_dir = 0;
  79. if (not defined($ENV{GIT_DIR})) {
  80. $must_unset_git_dir = 1;
  81. $ENV{GIT_DIR} = $repo_path;
  82. }
  83. my @refreshargs = qw/update-index --really-refresh -q --unmerged/;
  84. my @gitargs = qw/diff-files --name-only -z/;
  85. try {
  86. Git::command_oneline(@refreshargs);
  87. } catch Git::Error::Command with {};
  88. my $line = Git::command_oneline(@gitargs);
  89. my @files;
  90. if (defined $line) {
  91. @files = split('\0', $line);
  92. } else {
  93. @files = ();
  94. }
  95. delete($ENV{GIT_INDEX_FILE});
  96. delete($ENV{GIT_WORK_TREE});
  97. delete($ENV{GIT_DIR}) if ($must_unset_git_dir);
  98. return map { $_ => 1 } @files;
  99. }
  100. sub setup_dir_diff
  101. {
  102. my ($repo, $workdir, $symlinks) = @_;
  103. # Run the diff; exit immediately if no diff found
  104. # 'Repository' and 'WorkingCopy' must be explicitly set to insure that
  105. # if $GIT_DIR and $GIT_WORK_TREE are set in ENV, they are actually used
  106. # by Git->repository->command*.
  107. my $repo_path = $repo->repo_path();
  108. my %repo_args = (Repository => $repo_path, WorkingCopy => $workdir);
  109. my $diffrepo = Git->repository(%repo_args);
  110. my @gitargs = ('diff', '--raw', '--no-abbrev', '-z', @ARGV);
  111. my $diffrtn = $diffrepo->command_oneline(@gitargs);
  112. exit(0) unless defined($diffrtn);
  113. # Build index info for left and right sides of the diff
  114. my $submodule_mode = '160000';
  115. my $symlink_mode = '120000';
  116. my $null_mode = '0' x 6;
  117. my $null_sha1 = '0' x 40;
  118. my $lindex = '';
  119. my $rindex = '';
  120. my $wtindex = '';
  121. my %submodule;
  122. my %symlink;
  123. my @working_tree = ();
  124. my %working_tree_dups = ();
  125. my @rawdiff = split('\0', $diffrtn);
  126. my $i = 0;
  127. while ($i < $#rawdiff) {
  128. if ($rawdiff[$i] =~ /^::/) {
  129. warn << 'EOF';
  130. Combined diff formats ('-c' and '--cc') are not supported in
  131. directory diff mode ('-d' and '--dir-diff').
  132. EOF
  133. exit(1);
  134. }
  135. my ($lmode, $rmode, $lsha1, $rsha1, $status) =
  136. split(' ', substr($rawdiff[$i], 1));
  137. my $src_path = $rawdiff[$i + 1];
  138. my $dst_path;
  139. if ($status =~ /^[CR]/) {
  140. $dst_path = $rawdiff[$i + 2];
  141. $i += 3;
  142. } else {
  143. $dst_path = $src_path;
  144. $i += 2;
  145. }
  146. if ($lmode eq $submodule_mode or $rmode eq $submodule_mode) {
  147. $submodule{$src_path}{left} = $lsha1;
  148. if ($lsha1 ne $rsha1) {
  149. $submodule{$dst_path}{right} = $rsha1;
  150. } else {
  151. $submodule{$dst_path}{right} = "$rsha1-dirty";
  152. }
  153. next;
  154. }
  155. if ($lmode eq $symlink_mode) {
  156. $symlink{$src_path}{left} =
  157. $diffrepo->command_oneline('show', "$lsha1");
  158. }
  159. if ($rmode eq $symlink_mode) {
  160. $symlink{$dst_path}{right} =
  161. $diffrepo->command_oneline('show', "$rsha1");
  162. }
  163. if ($lmode ne $null_mode and $status !~ /^C/) {
  164. $lindex .= "$lmode $lsha1\t$src_path\0";
  165. }
  166. if ($rmode ne $null_mode) {
  167. # Avoid duplicate working_tree entries
  168. if ($working_tree_dups{$dst_path}++) {
  169. next;
  170. }
  171. my ($use, $wt_sha1) = use_wt_file($repo, $workdir,
  172. $dst_path, $rsha1);
  173. if ($use) {
  174. push @working_tree, $dst_path;
  175. $wtindex .= "$rmode $wt_sha1\t$dst_path\0";
  176. } else {
  177. $rindex .= "$rmode $rsha1\t$dst_path\0";
  178. }
  179. }
  180. }
  181. # Setup temp directories
  182. my $tmpdir = tempdir('git-difftool.XXXXX', CLEANUP => 0, TMPDIR => 1);
  183. my $ldir = "$tmpdir/left";
  184. my $rdir = "$tmpdir/right";
  185. mkpath($ldir) or exit_cleanup($tmpdir, 1);
  186. mkpath($rdir) or exit_cleanup($tmpdir, 1);
  187. # If $GIT_DIR is not set prior to calling 'git update-index' and
  188. # 'git checkout-index', then those commands will fail if difftool
  189. # is called from a directory other than the repo root.
  190. my $must_unset_git_dir = 0;
  191. if (not defined($ENV{GIT_DIR})) {
  192. $must_unset_git_dir = 1;
  193. $ENV{GIT_DIR} = $repo_path;
  194. }
  195. # Populate the left and right directories based on each index file
  196. my ($inpipe, $ctx);
  197. $ENV{GIT_INDEX_FILE} = "$tmpdir/lindex";
  198. ($inpipe, $ctx) =
  199. $repo->command_input_pipe(qw(update-index -z --index-info));
  200. print($inpipe $lindex);
  201. $repo->command_close_pipe($inpipe, $ctx);
  202. my $rc = system('git', 'checkout-index', '--all', "--prefix=$ldir/");
  203. exit_cleanup($tmpdir, $rc) if $rc != 0;
  204. $ENV{GIT_INDEX_FILE} = "$tmpdir/rindex";
  205. ($inpipe, $ctx) =
  206. $repo->command_input_pipe(qw(update-index -z --index-info));
  207. print($inpipe $rindex);
  208. $repo->command_close_pipe($inpipe, $ctx);
  209. $rc = system('git', 'checkout-index', '--all', "--prefix=$rdir/");
  210. exit_cleanup($tmpdir, $rc) if $rc != 0;
  211. $ENV{GIT_INDEX_FILE} = "$tmpdir/wtindex";
  212. ($inpipe, $ctx) =
  213. $repo->command_input_pipe(qw(update-index --info-only -z --index-info));
  214. print($inpipe $wtindex);
  215. $repo->command_close_pipe($inpipe, $ctx);
  216. # If $GIT_DIR was explicitly set just for the update/checkout
  217. # commands, then it should be unset before continuing.
  218. delete($ENV{GIT_DIR}) if ($must_unset_git_dir);
  219. delete($ENV{GIT_INDEX_FILE});
  220. # Changes in the working tree need special treatment since they are
  221. # not part of the index. Remove any trailing slash from $workdir
  222. # before starting to avoid double slashes in symlink targets.
  223. $workdir =~ s|/$||;
  224. for my $file (@working_tree) {
  225. my $dir = dirname($file);
  226. unless (-d "$rdir/$dir") {
  227. mkpath("$rdir/$dir") or
  228. exit_cleanup($tmpdir, 1);
  229. }
  230. if ($symlinks) {
  231. symlink("$workdir/$file", "$rdir/$file") or
  232. exit_cleanup($tmpdir, 1);
  233. } else {
  234. copy("$workdir/$file", "$rdir/$file") or
  235. exit_cleanup($tmpdir, 1);
  236. my $mode = stat("$workdir/$file")->mode;
  237. chmod($mode, "$rdir/$file") or
  238. exit_cleanup($tmpdir, 1);
  239. }
  240. }
  241. # Changes to submodules require special treatment. This loop writes a
  242. # temporary file to both the left and right directories to show the
  243. # change in the recorded SHA1 for the submodule.
  244. for my $path (keys %submodule) {
  245. my $ok = 0;
  246. if (defined($submodule{$path}{left})) {
  247. $ok = write_to_file("$ldir/$path",
  248. "Subproject commit $submodule{$path}{left}");
  249. }
  250. if (defined($submodule{$path}{right})) {
  251. $ok = write_to_file("$rdir/$path",
  252. "Subproject commit $submodule{$path}{right}");
  253. }
  254. exit_cleanup($tmpdir, 1) if not $ok;
  255. }
  256. # Symbolic links require special treatment. The standard "git diff"
  257. # shows only the link itself, not the contents of the link target.
  258. # This loop replicates that behavior.
  259. for my $path (keys %symlink) {
  260. my $ok = 0;
  261. if (defined($symlink{$path}{left})) {
  262. $ok = write_to_file("$ldir/$path",
  263. $symlink{$path}{left});
  264. }
  265. if (defined($symlink{$path}{right})) {
  266. $ok = write_to_file("$rdir/$path",
  267. $symlink{$path}{right});
  268. }
  269. exit_cleanup($tmpdir, 1) if not $ok;
  270. }
  271. return ($ldir, $rdir, $tmpdir, @working_tree);
  272. }
  273. sub write_to_file
  274. {
  275. my $path = shift;
  276. my $value = shift;
  277. # Make sure the path to the file exists
  278. my $dir = dirname($path);
  279. unless (-d "$dir") {
  280. mkpath("$dir") or return 0;
  281. }
  282. # If the file already exists in that location, delete it. This
  283. # is required in the case of symbolic links.
  284. unlink($path);
  285. open(my $fh, '>', $path) or return 0;
  286. print($fh $value);
  287. close($fh);
  288. return 1;
  289. }
  290. sub main
  291. {
  292. # parse command-line options. all unrecognized options and arguments
  293. # are passed through to the 'git diff' command.
  294. my %opts = (
  295. difftool_cmd => undef,
  296. dirdiff => undef,
  297. extcmd => undef,
  298. gui => undef,
  299. help => undef,
  300. prompt => undef,
  301. symlinks => $^O ne 'cygwin' &&
  302. $^O ne 'MSWin32' && $^O ne 'msys',
  303. tool_help => undef,
  304. trust_exit_code => undef,
  305. );
  306. GetOptions('g|gui!' => \$opts{gui},
  307. 'd|dir-diff' => \$opts{dirdiff},
  308. 'h' => \$opts{help},
  309. 'prompt!' => \$opts{prompt},
  310. 'y' => sub { $opts{prompt} = 0; },
  311. 'symlinks' => \$opts{symlinks},
  312. 'no-symlinks' => sub { $opts{symlinks} = 0; },
  313. 't|tool:s' => \$opts{difftool_cmd},
  314. 'tool-help' => \$opts{tool_help},
  315. 'trust-exit-code' => \$opts{trust_exit_code},
  316. 'no-trust-exit-code' => sub { $opts{trust_exit_code} = 0; },
  317. 'x|extcmd:s' => \$opts{extcmd});
  318. if (defined($opts{help})) {
  319. usage(0);
  320. }
  321. if (defined($opts{tool_help})) {
  322. print_tool_help();
  323. }
  324. if (defined($opts{difftool_cmd})) {
  325. if (length($opts{difftool_cmd}) > 0) {
  326. $ENV{GIT_DIFF_TOOL} = $opts{difftool_cmd};
  327. } else {
  328. print "No <tool> given for --tool=<tool>\n";
  329. usage(1);
  330. }
  331. }
  332. if (defined($opts{extcmd})) {
  333. if (length($opts{extcmd}) > 0) {
  334. $ENV{GIT_DIFFTOOL_EXTCMD} = $opts{extcmd};
  335. } else {
  336. print "No <cmd> given for --extcmd=<cmd>\n";
  337. usage(1);
  338. }
  339. }
  340. if ($opts{gui}) {
  341. my $guitool = Git::config('diff.guitool');
  342. if (defined($guitool) && length($guitool) > 0) {
  343. $ENV{GIT_DIFF_TOOL} = $guitool;
  344. }
  345. }
  346. if (!defined $opts{trust_exit_code}) {
  347. $opts{trust_exit_code} = Git::config_bool('difftool.trustExitCode');
  348. }
  349. if ($opts{trust_exit_code}) {
  350. $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE} = 'true';
  351. } else {
  352. $ENV{GIT_DIFFTOOL_TRUST_EXIT_CODE} = 'false';
  353. }
  354. # In directory diff mode, 'git-difftool--helper' is called once
  355. # to compare the a/b directories. In file diff mode, 'git diff'
  356. # will invoke a separate instance of 'git-difftool--helper' for
  357. # each file that changed.
  358. if (defined($opts{dirdiff})) {
  359. dir_diff($opts{extcmd}, $opts{symlinks});
  360. } else {
  361. file_diff($opts{prompt});
  362. }
  363. }
  364. sub dir_diff
  365. {
  366. my ($extcmd, $symlinks) = @_;
  367. my $rc;
  368. my $error = 0;
  369. my $repo = Git->repository();
  370. my $workdir = find_worktree();
  371. my ($a, $b, $tmpdir, @worktree) =
  372. setup_dir_diff($repo, $workdir, $symlinks);
  373. if (defined($extcmd)) {
  374. $rc = system($extcmd, $a, $b);
  375. } else {
  376. $ENV{GIT_DIFFTOOL_DIRDIFF} = 'true';
  377. $rc = system('git', 'difftool--helper', $a, $b);
  378. }
  379. # If the diff including working copy files and those
  380. # files were modified during the diff, then the changes
  381. # should be copied back to the working tree.
  382. # Do not copy back files when symlinks are used and the
  383. # external tool did not replace the original link with a file.
  384. #
  385. # These hashes are loaded lazily since they aren't needed
  386. # in the common case of --symlinks and the difftool updating
  387. # files through the symlink.
  388. my %wt_modified;
  389. my %tmp_modified;
  390. my $indices_loaded = 0;
  391. for my $file (@worktree) {
  392. next if $symlinks && -l "$b/$file";
  393. next if ! -f "$b/$file";
  394. if (!$indices_loaded) {
  395. %wt_modified = changed_files($repo->repo_path(),
  396. "$tmpdir/wtindex", "$workdir");
  397. %tmp_modified = changed_files($repo->repo_path(),
  398. "$tmpdir/wtindex", "$b");
  399. $indices_loaded = 1;
  400. }
  401. if (exists $wt_modified{$file} and exists $tmp_modified{$file}) {
  402. my $errmsg = "warning: Both files modified: ";
  403. $errmsg .= "'$workdir/$file' and '$b/$file'.\n";
  404. $errmsg .= "warning: Working tree file has been left.\n";
  405. $errmsg .= "warning:\n";
  406. warn $errmsg;
  407. $error = 1;
  408. } elsif (exists $tmp_modified{$file}) {
  409. my $mode = stat("$b/$file")->mode;
  410. copy("$b/$file", "$workdir/$file") or
  411. exit_cleanup($tmpdir, 1);
  412. chmod($mode, "$workdir/$file") or
  413. exit_cleanup($tmpdir, 1);
  414. }
  415. }
  416. if ($error) {
  417. warn "warning: Temporary files exist in '$tmpdir'.\n";
  418. warn "warning: You may want to cleanup or recover these.\n";
  419. exit(1);
  420. } else {
  421. exit_cleanup($tmpdir, $rc);
  422. }
  423. }
  424. sub file_diff
  425. {
  426. my ($prompt) = @_;
  427. if (defined($prompt)) {
  428. if ($prompt) {
  429. $ENV{GIT_DIFFTOOL_PROMPT} = 'true';
  430. } else {
  431. $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
  432. }
  433. }
  434. $ENV{GIT_PAGER} = '';
  435. $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
  436. # ActiveState Perl for Win32 does not implement POSIX semantics of
  437. # exec* system call. It just spawns the given executable and finishes
  438. # the starting program, exiting with code 0.
  439. # system will at least catch the errors returned by git diff,
  440. # allowing the caller of git difftool better handling of failures.
  441. my $rc = system('git', 'diff', @ARGV);
  442. exit($rc | ($rc >> 8));
  443. }
  444. main();