Skip to contentSkip to conversation

Diff-helper update

PATCH In review Junio C Hamano 3 participants 13 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

cover Diff-helper update

This is just a cover letter but the next patch implements the rename detection I told you about.

The output from the patched version is attached at the end of this message as a demonstration.

My plan is to get the documentation and the framework in place with this patch first. The basic strategy is to hold created and deleted files while we parse the incoming diff-tree output, and match them up at the end, looking for usefully similar pair.

The similarity evaluator included in this round of patch detects exact renames only, which is not very useful in practice, but that would be improved in the later round. It will probably be done with the same deltify code Nico is using.

$ git-diff-tree -r \
    13ab4462d2aefb252d7c916bd537151856b7c967 \
    99665af5c0be0fe4319b39183e84917993153576 | ./git-diff-helper -r
diff -git a/Documentation/diff-format.txt b/Documentation/diff-format.txt
--- a/Documentation/diff-format.txt
+++ b/Documentation/diff-format.txt
@@ -45,7 +45,7 @@ with a '-p' option, they do not produce 
 instead they produce a patch file.
 ...
diff -git a/diff.h b/diff.h
--- a/diff.h
+++ b/diff.h
@@ -17,7 +17,7 @@ extern void diff_change(unsigned mode1, 

extern void diff_unmerge(const char *path);

-/* These are for diff-tree-helper */
+/* These are for diff-helper */

struct diff_spec {
unsigned char blob_sha1[20];
diff -git a/diff-tree-helper.c b/diff-helper.c
rename old diff-tree-helper.c
rename new diff-helper.c
diff -git a/Documentation/git-diff-tree-helper.txt b/Documentation/git-diff-helper.txt rename old Documentation/git-diff-tree-helper.txt
rename new Documentation/git-diff-helper.txt

#1 lore raw
Quoted 3 lines · Junio C Hamano

This is just a cover letter but the next patch implements the rename detection I told you about.

Ok. I now have one more worry: git-diff-tree with "--stdin".

I actually find that thing to be surpremely useful, and I use it for not just my silly git-whatchanged script, but also to make release notes. I just do

	git-rev-tree HEAD ^LAST_RELEASE | cut -d' ' -f2 | git-diff-tree -v -s --stdin

and that's wonderful. And sometimes I use "-p" instead of "-s", because I'm not making release notes, but because I'm doing a "whatchanged" with full diffs.

In other words, try something like this on the kernel tree:

git-whatchanged -p include/asm-i386 arch/i386 | less -S

and stare in wonder at just how _useful_ this simple thing is.

Maybe it's just that I've not used all that many different SCM systems, and I've certainly not used all the features of the ones I _have_ used, so maybe I never knew, but dammit, I've never seen anybody else do something quite that useful (at least doing it fast enough that it _remains_ useful).

(Replace "arch/i386" with "drivers/usb" or "fs/ext3" or whatever, depending on just what your area of interest happens to be).

In other words, I'm very happy with git.

However, git-diff-helper doesn't understand these things, and the builtin diff doesn't do the rename thing. Yet it would be very very useful to do.

Now, if you do just a

git-whatchanged include/asm-i386 arch/i386

(or you can even use "-z" if you want to), it turns out hat git-diff-tree actually does output perfectly usable material. Each "set of diffs" is clearly separated, and there is no ambiguos material: all lines are either:
- empty or start with a whitespace
- start with "diff-tree ", "Author: " or "Date: "
- are valid input for diff-helper

So what I'd suggest is one (or both) of two possibilities:
- make the internal diff logic also able to do the same rename handling as the external diff-helper. This may or may not be complex, I've not looked at it.
- change diff-helper subtly: instead of printing "cannot parse %s", any nonrecognized line would be a "ignore this line, but process all pending potential renames".

The above would mean that I could either just do

git-whatchanged include/asm-i386 arch/i386 | git-diff-helper

or continue to use

git-whatchanged -p include/asm-i386 arch/i386

and still get the "nice" output (and it's a _feature_ that a rename within the arch/i386 then shows up as a rename, but a rename that crosses the boundary shows up as a "create" or "delete" event).

Comments?

Linus

#2 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"LT" == Linus Torvalds <[email protected]> writes:

LT> However, git-diff-helper doesn't understand these things, and the builtin LT> diff doesn't do the rename thing. Yet it would be very very useful to do.

It is unclear what you meant by "these things" in "doesn't understand these things", and what you meant by "it" in "it would be very very useful to do." Could you explain?

About the built-in diff not doing the rename , I have a bit longer term (knowing _my_ timescale I'd imagine you would understand that is not that long ;-) plan to have -p option for diff-* family to use the same rename detection logic that I added to diff-helper in the patch you are commenting on. It involves slight change to callers (three diff-* family main programs) to add a call to tell the diff driver "I've given you all the diffs, now go look for renames") at the end, and the rest is changes to what diff.c does internally.

LT> So what I'd suggest is one (or both) of two possibilities:
LT> - make the internal diff logic also able to do the same rename handling
LT> as the external diff-helper. This may or may not be complex, I've not
LT> looked at it.

Yes that is part of the plan. I wanted to do things in these steps:

- Put rename detect in helper so screwups there would not impact the diff-* family's built-in output, with the initial dumb rename detection.

- Improve rename detection still keeping the logic and machinery in diff-helper only. I expect a heuristics similar to the one you posted on the deltification thread would work nicely here as well.

- Straighten out the GIT_EXTERNAL_DIFF interface so that it can also express renames (the patch I sent currently punts there). They will get eighth argument (rename destination) only when they are being fed a rename patch. git-apply-patch-script needs to be adjusted for this change.

- Change diff-helper not to do the rename detection itself, but clean it up so it uses the same diff_addremove(), diff_change(), and diff_unmerge() interface the diff-* family use. Change the implementation of these three functions so that they do not directly call run_external_diff() but pool changes for later matching when rename detection is in effect. Add diff_finished() which would flush the rename candidate pools, and call that at the end of program from three diff-* family and diff-helper. The rename detection logic in diff-helper will be moved to this "inspect the pooled rename candidates, match them up and flush" part.

The patch I sent is the first step in the above sequence.

LT> - change diff-helper subtly: instead of printing "cannot parse %s", any
LT> nonrecognized line would be a "ignore this line, but process all
LT> pending potential renames".

Once the built-in diff driver is straightened out the way I outlined above, this change may turn out to be unnecessary, I need to look at the whatchanged output and think a bit more about this later today.

#3 lore raw
Quoted 8 lines · Junio C Hamano
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"LT" == Linus Torvalds <[email protected]> writes:

LT> However, git-diff-helper doesn't understand these things, and the builtin LT> diff doesn't do the rename thing. Yet it would be very very useful to do.

It is unclear what you meant by "these things" in "doesn't understand these things", and what you meant by "it" in "it would be very very useful to do." Could you explain?

"These things" being the extra output from "diff-tree" that is not a "diff line".

If diff-helper just passes the lines it doesn't understand through unmodified (_after_ having handled any pending rename logic), it will automatically do the right thing.

Quoted 5 lines

About the built-in diff not doing the rename , I have a bit longer term (knowing _my_ timescale I'd imagine you would understand that is not that long ;-) plan to have -p option for diff-* family to use the same rename detection logic that I added to diff-helper in the patch you are commenting on.

Goodie. I was hoping that was the case, but felt that the diff-helper thing should be pretty easy to do.

Linus

#4 lore raw

[preview] diff-helper rename detection.

Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

"LT" == Linus Torvalds <[email protected]> writes:

About the built-in diff not doing the rename , I have a bit longer term (knowing _my_ timescale I'd imagine you would understand that is not that long ;-) plan to have -p option for diff-* family to use the same rename detection logic that I added to diff-helper in the patch you are commenting on.

LT> Goodie. I was hoping that was the case, but felt that the diff-helper LT> thing should be pretty easy to do.

This is not yet a request for inclusion but is a preview requesting for testing and comments, especially from Linus (for general usability comments and suggestions for cut-off-points in heuristics) and Nico (in case I had blatantly misused the diff-delta interface).

I stole diff-delta part from the delta support patch (but I dropped the part that actually touches sha1_file part, hence this does not introduce the deltified object store), and used it as an "extent-of-damage estimator". The rename detector logic in the previous round was detecting exact renames only (and did not even look at the filesystem so the raw-diff could not come from diff-files or diff-cache without --cached), but this round it actually checks the content. The interim heuristics is:

- If exact match in SHA1, or exact match comparing the files,

    then that is a rename (trivial);

- If size changed more than 20% that cannot be a rename;

- If delta produced by the deltification stuff between two is

    more than 20% of the original, that cannot be a rename;

- Otherwise pick the deleted-created file pair that has the smallest delta between them.

I've only very lightly tested it but it seems to do the right thing. I fed soemthing like this when I had heavily modified diff-helper.c (diff-helpee.c was taken from the git-ls-files output for diff-helper.c from the cache) and lightly modified diff.c (diffo.c was from the cache), and diffo.c -> diff.c were reported as rename, while helpee -> helper was not.

-100644 blob cde27275fad8103084d7ed2d08d246ba4ce6eb9c Makefile
+100644 blob cde27275fad8103084d7ed2d08d246ba4ce6eb9c GNUMakefile
-100644 blob 2877ddc4df85179c03cdcc8c7a66831951ec5d97 diff-helpee.c
+100644 blob 0000000000000000000000000000000000000000 diff-helper.c
-100644 blob 74004e5a3f9fa491b30ab3d5f231826593e4eae4 diffo.c
+100644 blob 0000000000000000000000000000000000000000 diff.c

---
# - linus: merge-base: use the new lookup_commit_reference() helper function # + 9: diff-helper detects renames with Nico's delta stuff.

Makefile +1 -1
diff -git a/Makefile b/Makefile
@@ -36,7 +36,7 @@ install: $(PROG) $(SCRIPTS)
 	$(INSTALL) $(PROG) $(SCRIPTS) $(dest)$(bin)
 
 LIB_OBJS=read-cache.o sha1_file.o usage.o object.o commit.o tree.o blob.o \
-	 tag.o date.o
+	 tag.o date.o diff-delta.o
 LIB_FILE=libgit.a
 LIB_H=cache.h object.h blob.h tree.h commit.h tag.h
 

diff -git a/delta.h b/delta.h
new file mode 100644

delta.h new file +6 -0
@@ -0,0 +1,6 @@
+extern void *diff_delta(void *from_buf, unsigned long from_size,
+			void *to_buf, unsigned long to_size,
+		        unsigned long *delta_size);
+extern void *patch_delta(void *src_buf, unsigned long src_size,
+			 void *delta_buf, unsigned long delta_size,
+			 unsigned long *dst_size);

diff -git a/diff-delta.c b/diff-delta.c
new file mode 100644

diff-delta.c new file +330 -0

330 lines not on this page · raw message

diff-helper.c +177 -72

292 lines not on this page · raw message

diff.c +1 -1
diff -git a/diff.c b/diff.c
@@ -329,7 +329,7 @@ void run_external_diff(const char *name,
 		die("unable to fork");
 	if (!pid) {
 		const char *pgm = external_diff();
-		/* not passing rename patch to external ones */
+		/* NEEDSWORK: not passing rename patch to external ones */
 		if (!other && pgm) {
 			if (one && two)
 				execlp(pgm, pgm,
#5 lore raw

Re: [preview] diff-helper rename detection.

Quoted 6 lines · Junio C Hamano

This is not yet a request for inclusion but is a preview requesting for testing and comments, especially from Linus (for general usability comments and suggestions for cut-off-points in heuristics) and Nico (in case I had blatantly misused the diff-delta interface).

I think this is great, although I also bet that we'll tweak the heuristics later.

I suspect that one tweak may be to try to find the "best" rename first, rather than look at files in the order they were discovered (ie you'd create a _matrix_ of the delta scores, rather than walk through the newly created files in order). That would then potentially allow for a more relaxed definitions of "rename", without any possibility of losing sight of a better rename due to finding a bad one first.

But I think the simple heurstic is probably fine as a first cut.

So my only nit so far is that you declare "patch_delta()", even though you don't actually have it. Just remove it.

Linus

#6 lore raw

Re: [preview] diff-helper rename detection.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"LT" == Linus Torvalds <[email protected]> writes:

LT> I suspect that one tweak may be to try to find the "best" rename first, LT> rather than look at files in the order they were discovered (ie you'd LT> create a _matrix_ of the delta scores, rather than walk through the newly LT> created files in order). That would then potentially allow for a more LT> relaxed definitions of "rename", without any possibility of losing sight LT> of a better rename due to finding a bad one first.

I thought about that but I opted for simplicity of first picking the exact matches. I was unsure about the usefulness of relaxed matching to begin with. But I now realize that to see how useful that would be we need to experiment it and a matrix approach would become necessity for that.

LT> So my only nit so far is that you declare "patch_delta()", LT> even though you don't actually have it. Just remove it.

I'd rather keep that file as is for later merge with Nico. It came from the delitification patch and I tried to keep the new files as they are.

#7 lore raw
In reply to Linus Torvalds
Quoted 4 lines · Linus Torvalds

If diff-helper just passes the lines it doesn't understand through unmodified (_after_ having handled any pending rename logic), it will automatically do the right thing.

I took the liberty of doing just that. The only subtle issue was that the strbuf functions would consider an empty line to be EOF, which looked wrong and unintentional. Fixing that made the actual diff-helper changes totally trivial, and I can now do

	git-rev-list HEAD | git-diff-tree -r -v --stdin | ./git-diff-helper -r | less -S

and it does the right thing for me.

Linus

#8 lore raw

Hello,

Quoted 1 line
	git-rev-list HEAD | git-diff-tree -r -v --stdin | ./git-diff-helper -r | less -S

nice one!

Thomas

#9 lore raw
In reply to Linus Torvalds
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"LT" == Linus Torvalds <[email protected]> writes:

LT> I took the liberty of doing just that. The only subtle issue was that LT> the strbuf functions would consider an empty line to be EOF, which looked LT> wrong and unintentional. Fixing that made the actual diff-helper changes LT> totally trivial, and I can now do
LT> git-rev-list HEAD | git-diff-tree -r -v --stdin | ./git-diff-helper -r | less -S
LT> and it does the right thing for me.

Thanks for fixing up strbuf.

@@ -136,8 +268,12 @@ int main(int ac, const char **av) {
if (sb.eof)
break;
status = parse_diff_raw_output(sb.buf, av+1, ac-1, reverse);
- if (status)
- fprintf(stderr, "cannot parse %s\n", sb.buf);
+ if (status) {
+ flush_renames(av+1, ac-1, reverse);
+ printf("%s%c", sb.buf, line_termination);
+ }
}
+
+ flush_renames(av+1, ac-1, reverse);
return 0;
}

I suspect doing something like this might be saner instead, assuming non raw-diffs come at the end.

		if (status)
			break;
	}
	flush_renames(av+1, ac-1, reverse);
	if (!sb.eof) {
        	spit out what we have in sb.eof, sendfile ;-) the
                rest of the input to the output.
	}
	return 0;
#10 lore raw
Quoted 3 lines · Junio C Hamano

I suspect doing something like this might be saner instead, assuming non raw-diffs come at the end.

It won't ever trigger, since we only exit the loop once we see EOF.

So the non-diffs at the end will trigger by the exact same "oh, we didn't recognize it" logic.

Linus

#11 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"LT" == Linus Torvalds <[email protected]> writes:

Quoted 3 lines · Junio C Hamano
Quoted 3 lines

I suspect doing something like this might be saner instead, assuming non raw-diffs come at the end.

LT> It won't ever trigger, since we only exit the loop once we see EOF.

I was not talking about correctness, but the readability of the code. Breaking out from the loop to process raw-diff and switching to straight copy would make our intent more explicit.

#12 lore raw
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

"JCH" == Junio C Hamano <[email protected]> writes:

"LT" == Linus Torvalds <[email protected]> writes:

Quoted 3 lines · Junio C Hamano
Quoted 3 lines
Quoted 3 lines

I suspect doing something like this might be saner instead, assuming non raw-diffs come at the end.

LT> It won't ever trigger, since we only exit the loop once we see EOF.

JCH> I was not talking about correctness, but the readability of the JCH> code. Breaking out from the loop to process raw-diff and JCH> switching to straight copy would make our intent more explicit.

I was completely mistaken about what you were talking about. Your output (helper's input) is (<non-diff material>* <raw-diff material>*)*, so of course we should not break out of the main loop.