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

Re: [RFC] Possible strategy cleanup for git add/remove/diff etc.

From
Junio C Hamano <junkio@cox.net>
Date
Apr 21, 2005, 04:04 UTC
Message-ID
<7vsm1kg4gz.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<Pine.LNX.4.58.0504192102140.6467@ppc970.osdl.org>
>>>>> "LT" == Linus Torvalds <torvalds@osdl.org> writes:

LT> And as you can see, the output matches "diff-tree -r" output (we always do LT> "-r", since the index is always fully populated). All the same rules: "+" LT> means added file, "-" means removed file, and "*" means changed file. You LT> can trivially see that the above is a rename.

I do not know if Pasky tools already have something like this already, or not; but just FIY, here is what I use to extract a "patch" out of a working tree.

Usage:
    $ diff-tree -z [-r] ... | jit-diff-tree-helper [ | less ]
    $ diff-cache -z ...     | jit-diff-tree-helper [ | less ]

This would be useful for the merge I described in my initial message in this thread to take a snapshot of what the user has done since the last commit, to be applied on the result of the merge.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---

--- jit-diff-tree-helper 2005-03-19 15:28:25.000000000 -0800 +++ jit-diff-tree-helper 2005-04-20 19:15:32.000000000 -0700

@@ -0,0 +1,63 @@
+#!/usr/bin/perl -w
+
+use strict;
+use File::Temp qw(mkstemp);
+
+sub cat_file {
+    my ($sha1, $file) = @_;
+    unless (defined $sha1) { return "/dev/null"; }
+    if ($sha1 =~ /^0{40}$/) {
+	open I, '<', $file;
+    } else {
+	local $/; # slurp mode
+	open I, "-|", "cat-file", "blob", $sha1
+	    or die "$0: cannot read $sha1";
+    }
+    my ($o, $filename) = mkstemp(",,jit-diff-tree-helperXXXXXX");
+    print $o join("",<I>);
+    close I
+	or die "$0: closing cat-file pipe from $sha1";
+    close $o
+	or die "$0: closing write fd to $filename";
+    return $filename;
+}
+$/ = "\0";
+my $rM = "[0-7]+";
+my $rI = "[0-9a-f]{40}";
+while (<STDIN>) {
+    my ($old, $new, $file);
+    chomp;
+    if (/^\+$rM\tblob\t($rI)\t(.*)$/os) {
+	($old, $new, $file) = (undef, $1, $2);
+    }
+    elsif (/^-$rM\tblob\t($rI)\t(.*)$/os) {
+	($old, $new, $file) = ($1, undef, $2);
+    }
+    elsif (/^\*$rM->$rM\tblob\t($rI)->($rI)\t(.*)$/os) {
+	($old, $new, $file) = ($1, $2, $3);
+    }
+    else {
+	chomp;
+	print STDERR "warning: $0: ignoring $_\n";
+	next;
+    }
+    if (@ARGV) {
+	my $matches = 0;
+	for (@ARGV) {
+	    my $l = length($_);
+	    if ($file eq $_ ||
+		(substr($file, 0, $l) eq $_ &&
+		 substr($file, $l, 1) eq "/")) {
+		$matches = 1;
+		last;
+	    }
+	}
+	next unless $matches;
+    }
+    $old = cat_file $old, $file;
+    $new = cat_file $new, $file;
+    system "diff", "-L", "l/$file", "-L", "k/$file", "-pu", $old, $new;
+    for ($old, $new) {
+	unlink $_ if $_ ne '/dev/null';
+    }
+}
Previous: Junio C Hamano
Message 10 of 10 in “"True" git merge in git-pasky”
  1. Petr BaudisApr 19, 2005
  2. bert hubertApr 19, 2005
  3. Petr BaudisApr 19, 2005
  4. Francois RomieuApr 19, 2005
  5. [RFC] Possible strategy cleanup for git add/remove/diff etc.Junio C Hamano, Apr 20, 2005
  6. Linus TorvaldsApr 20, 2005
  7. Junio C HamanoApr 20, 2005
  8. Linus TorvaldsApr 20, 2005
  9. Junio C HamanoApr 20, 2005
  10. Junio C HamanoApr 21, 2005

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.