git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 2/3] t7800: fix tests when difftool uses --no-symlinks

From
John Keeping <john@keeping.me.uk>
Date
Mar 25, 2013, 10:42 UTC
Message-ID
<20130325104219.GD2286@serenity.lan>
In-Reply-To
<514FFFC7.3090004@viscovery.net>
On Mon, Mar 25, 2013 at 08:41:59AM +0100, Johannes Sixt wrote:
Show 103 quoted lines
> Am 3/24/2013 16:15, schrieb John Keeping:
> > Subject: [PATCH] difftool: don't overwrite modified files
> > 
> > After running the user's diff tool, git-difftool will copy any files
> > that differ between the working tree and the temporary tree.  This is
> > useful when the user edits the file in their diff tool but is wrong if
> > they edit the working tree file while examining the diff.
> > 
> > Instead of copying unconditionally when the files differ, store the
> > initial hash of the working tree file and only copy the temporary file
> > back if it was modified and the working tree file was not.  If both
> > files have been modified, print a warning and exit with an error.
> > 
> > Signed-off-by: John Keeping <john@keeping.me.uk>
> > ---
> >  git-difftool.perl   | 35 +++++++++++++++++++++--------------
> >  t/t7800-difftool.sh | 26 ++++++++++++++++++++++++++
> >  2 files changed, 47 insertions(+), 14 deletions(-)
> > 
> > diff --git a/git-difftool.perl b/git-difftool.perl
> > index c433e86..be82b5a 100755
> > --- a/git-difftool.perl
> > +++ b/git-difftool.perl
> > @@ -15,7 +15,6 @@ use strict;
> >  use warnings;
> >  use File::Basename qw(dirname);
> >  use File::Copy;
> > -use File::Compare;
> >  use File::Find;
> >  use File::stat;
> >  use File::Path qw(mkpath rmtree);
> > @@ -123,7 +122,7 @@ sub setup_dir_diff
> >  	my $rindex = '';
> >  	my %submodule;
> >  	my %symlink;
> > -	my @working_tree = ();
> > +	my %working_tree;
> >  	my @rawdiff = split('\0', $diffrtn);
> >  
> >  	my $i = 0;
> > @@ -175,7 +174,9 @@ EOF
> >  
> >  		if ($rmode ne $null_mode) {
> >  			if (use_wt_file($repo, $workdir, $dst_path, $rsha1, $symlinks)) {
> > -				push(@working_tree, $dst_path);
> > +				$working_tree{$dst_path} =
> > +					$repo->command_oneline('hash-object',
> > +						"$workdir/$dst_path");
> >  			} else {
> >  				$rindex .= "$rmode $rsha1\t$dst_path\0";
> >  			}
> > @@ -227,7 +228,7 @@ EOF
> >  	# not part of the index. Remove any trailing slash from $workdir
> >  	# before starting to avoid double slashes in symlink targets.
> >  	$workdir =~ s|/$||;
> > -	for my $file (@working_tree) {
> > +	for my $file (keys %working_tree) {
> >  		my $dir = dirname($file);
> >  		unless (-d "$rdir/$dir") {
> >  			mkpath("$rdir/$dir") or
> > @@ -278,7 +279,7 @@ EOF
> >  		exit_cleanup($tmpdir, 1) if not $ok;
> >  	}
> >  
> > -	return ($ldir, $rdir, $tmpdir, @working_tree);
> > +	return ($ldir, $rdir, $tmpdir, %working_tree);
> >  }
> >  
> >  sub write_to_file
> > @@ -376,7 +377,7 @@ sub dir_diff
> >  	my $error = 0;
> >  	my $repo = Git->repository();
> >  	my $workdir = find_worktree($repo);
> > -	my ($a, $b, $tmpdir, @worktree) =
> > +	my ($a, $b, $tmpdir, %worktree) =
> >  		setup_dir_diff($repo, $workdir, $symlinks);
> >  
> >  	if (defined($extcmd)) {
> > @@ -390,19 +391,25 @@ sub dir_diff
> >  	# should be copied back to the working tree.
> >  	# Do not copy back files when symlinks are used and the
> >  	# external tool did not replace the original link with a file.
> > -	for my $file (@worktree) {
> > +	for my $file (keys %worktree) {
> >  		next if $symlinks && -l "$b/$file";
> >  		next if ! -f "$b/$file";
> >  
> > -		my $diff = compare("$b/$file", "$workdir/$file");
> > -		if ($diff == 0) {
> > -			next;
> > -		} elsif ($diff == -1) {
> > -			my $errmsg = "warning: Could not compare ";
> > -			$errmsg += "'$b/$file' with '$workdir/$file'\n";
> > +		my $wt_hash = $repo->command_oneline('hash-object',
> > +			"$workdir/$file");
> > +		my $tmp_hash = $repo->command_oneline('hash-object',
> > +			"$b/$file");
> 
> This is gross. Can't we do much better here? Difftool already keeps a
> GIT_INDEX of the files in the temporary tree ($tmpdir/rindex). Running
> git-diff-files should be sufficient to tell which ones where edited via
> the users's diff-tool. Then you can restrict calling hash-object to only
> those worktree files where an "edit collision" needs to be checked for.

That's only the case for files that are not copied from the working tree, so the temporary index doesn't contain the files that are of interest here.

> You could also keep a parallel index that keeps the state of the same set
> of files in the worktree. Then another git-diff-files call could replace
> the other half of hash-object calls.

I like the idea of creating an index from the working tree files and using it here. If we create a "starting state" index for these files, we should be able to run git-diff-files against both the working tree and the temporary tree at this point and compare the output. I'll try this approach this evening.

Show 15 quoted lines
> > +		my $wt_modified = $wt_hash ne $worktree{$file};
> > +		my $tmp_modified = $tmp_hash ne $worktree{$file};
> > +
> > +		if ($wt_modified and $tmp_modified) {
> > +			my $errmsg = "warning: Both files modified: ";
> > +			$errmsg .= "'$workdir/$file' and '$b/$file'.\n";
> > +			$errmsg .= "warning: Working tree file has been left.\n";
> > +			$errmsg .= "warning:\n";
> >  			warn $errmsg;
> >  			$error = 1;
> > -		} elsif ($diff == 1) {
> > +		} elsif ($tmp_modified) {
> >  			my $mode = stat("$b/$file")->mode;
> >  			copy("$b/$file", "$workdir/$file") or
> >  			exit_cleanup($tmpdir, 1);
Previous: Johannes SixtNext: John Keeping
Message 34 of 55 in “difftool: silence uninitialized variable warning”
  1. 1/4 difftool: silence uninitialized variable warningDavid Aguilar, Feb 21, 2013
  2. 2/4 t7800: update copyright noticeDavid Aguilar, Feb 21, 2013
  3. 3/4 t7800: modernize testsDavid Aguilar, Feb 21, 2013
  4. 4/4 t7800: "defaults" is no longer a builtin tool nameDavid Aguilar, Feb 21, 2013
  5. Junio C HamanoFeb 21, 2013
  6. Junio C HamanoFeb 21, 2013
  7. David AguilarFeb 21, 2013
  8. Johannes SixtMar 20, 2013
  9. David AguilarMar 20, 2013
  10. Johannes SixtMar 21, 2013
  11. Johannes SixtMar 22, 2013
  12. John KeepingMar 22, 2013
  13. Johannes SixtMar 22, 2013
  14. John KeepingMar 22, 2013
  15. 0/3 Improve difftool --dir-diff testsJohn Keeping, Mar 22, 2013
  16. 1/3 t7800: don't hide grep outputJohn Keeping, Mar 22, 2013
  17. Johannes SixtMar 22, 2013
  18. Junio C HamanoMar 22, 2013
  19. 2/3 t7800: fix tests when difftool uses --no-symlinksJohn Keeping, Mar 22, 2013
  20. Johannes SixtMar 22, 2013
  21. Junio C HamanoMar 22, 2013
  22. John KeepingMar 22, 2013
  23. David AguilarMar 23, 2013
  24. 3/3 t7800: run --dir-diff tests with and without symlinksJohn Keeping, Mar 22, 2013
  25. 3/3 t7800: run --dir-diff tests with and without symlinksJohn Keeping, Mar 22, 2013
  26. 0/3 difftool --dir-diff test improvementsJohn Keeping, Mar 23, 2013
  27. 1/3 t7800: don't hide grep outputJohn Keeping, Mar 23, 2013
  28. 2/3 t7800: fix tests when difftool uses --no-symlinksJohn Keeping, Mar 23, 2013
  29. Junio C HamanoMar 24, 2013
  30. John KeepingMar 24, 2013
  31. Matt McClureMar 24, 2013
  32. John KeepingMar 24, 2013
  33. Johannes SixtMar 25, 2013
  34. John KeepingMar 25, 2013
  35. difftool: don't overwrite modified filesJohn Keeping, Mar 25, 2013
  36. Johannes SixtMar 26, 2013
  37. Johannes SixtMar 26, 2013
  38. John KeepingMar 26, 2013
  39. Johannes SixtMar 26, 2013
  40. John KeepingMar 26, 2013
  41. Matt McClureMar 26, 2013
  42. John KeepingMar 26, 2013
  43. Junio C HamanoMar 25, 2013
  44. David AguilarMar 24, 2013
  45. John KeepingMar 25, 2013
  46. Junio C HamanoMar 25, 2013
  47. Matt McClureMar 24, 2013
  48. Eric SunshineMar 24, 2013
  49. 3/3 t7800: run --dir-diff tests with and without symlinksJohn Keeping, Mar 23, 2013
  50. Johannes SixtMar 25, 2013
  51. John KeepingMar 25, 2013
  52. Johannes SixtMar 25, 2013
  53. John KeepingMar 25, 2013
  54. Junio C HamanoMar 25, 2013
  55. John KeepingMar 26, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.