"git-diff-tree -R A B == git-diff-tree B A"?
I was preparing a set of tests for diff family because I wanted to have something that catches screwups I am going to inflict upon them during the coming couple of days. One of the tests accidentally found out that the above is not true in the current implementation. Just an excerpt of relevant lines [*1*]:
$ git-diff-tree -r $tree_B $tree_A
+100644 blob 7e426fb079479fd67f6d81f984e4ec649a44bc25 AN +100644 blob 68a6d8b91da11045cf4aa3a5ab9f2a781c701249 DF/DF -100644 blob 71420ab81e254145d26d6fc0cddee64c1acd4787 DF -100644 blob 3c4d8de5fbad08572bab8e10eef8dbb264cf0231 DM
$ git-diff-tree -R $tree_A $tree_B
+100644 blob 7e426fb079479fd67f6d81f984e4ec649a44bc25 AN -100644 blob 71420ab81e254145d26d6fc0cddee64c1acd4787 DF +100644 blob 68a6d8b91da11045cf4aa3a5ab9f2a781c701249 DF/DF -100644 blob 3c4d8de5fbad08572bab8e10eef8dbb264cf0231 DM
I personally don't care about the difference of the order these are output, but it affects the stability of the test, and also is inconsistent --- everybody else sorts the entries in the cache order.
Although I do not see practical problems in this inconsistency, I am asking you just in case if you care about it. If there is a downstream consumer that relies on the ordering of the entries this may cause problems (diff-helper is OK).
[Footnote]
*1* The trees are taken from t1000-read-tree-m-3way.sh test.
Quoted 14 lines · Junio C Hamano
I was preparing a set of tests for diff family because I wanted to have something that catches screwups I am going to inflict upon them during the coming couple of days. One of the tests accidentally found out that the above is not true in the current implementation. Just an excerpt of relevant lines [*1*]:
$ git-diff-tree -r $tree_B $tree_A+100644 blob
7e426fb079479fd67f6d81f984e4ec649a44bc25AN +100644 blob68a6d8b91da11045cf4aa3a5ab9f2a781c701249DF/DF -100644 blob71420ab81e254145d26d6fc0cddee64c1acd4787DF -100644 blob3c4d8de5fbad08572bab8e10eef8dbb264cf0231DM
Quoted 6 lines
$ git-diff-tree -R $tree_A $tree_B+100644 blob
7e426fb079479fd67f6d81f984e4ec649a44bc25AN -100644 blob71420ab81e254145d26d6fc0cddee64c1acd4787DF +100644 blob68a6d8b91da11045cf4aa3a5ab9f2a781c701249DF/DF -100644 blob3c4d8de5fbad08572bab8e10eef8dbb264cf0231DM
Ahh.. "diff-tree" uses the wrong algorithm for selecting name ordering.
It thinks that "DF" and "DF/" sort equally because it just looks at the name, not the type. So then, becuse the names sort the same, it will sort them according to where they come from, and you get the behaviour you see.
It doesn't really matter, but you're right, I should fix it to be consistent.
git-diff-cache and git-diff-files should automaticallu get it right thanks to reading the whole tree, so it's likely just git-diff-tree that can get confused.
Linus
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"LT" == Linus Torvalds <[email protected]> writes:
Quoted 14 lines · Junio C Hamano
Quoted 14 lines
I was preparing a set of tests for diff family because I wanted to have something that catches screwups I am going to inflict upon them during the coming couple of days. One of the tests accidentally found out that the above is not true in the current implementation. Just an excerpt of relevant lines [*1*]:
$ git-diff-tree -r $tree_B $tree_A+100644 blob
7e426fb079479fd67f6d81f984e4ec649a44bc25AN +100644 blob68a6d8b91da11045cf4aa3a5ab9f2a781c701249DF/DF -100644 blob71420ab81e254145d26d6fc0cddee64c1acd4787DF -100644 blob3c4d8de5fbad08572bab8e10eef8dbb264cf0231DM
Quoted 6 lines
Quoted 6 lines
$ git-diff-tree -R $tree_A $tree_B+100644 blob
7e426fb079479fd67f6d81f984e4ec649a44bc25AN -100644 blob71420ab81e254145d26d6fc0cddee64c1acd4787DF +100644 blob68a6d8b91da11045cf4aa3a5ab9f2a781c701249DF/DF -100644 blob3c4d8de5fbad08572bab8e10eef8dbb264cf0231DM
LT> Ahh.. "diff-tree" uses the wrong algorithm for selecting name ordering.
LT> It thinks that "DF" and "DF/" sort equally because it just looks at the LT> name, not the type. So then, becuse the names sort the same, it will sort LT> them according to where they come from, and you get the behaviour you see.
LT> It doesn't really matter, but you're right, I should fix it to be LT> consistent.
Don't bother. I already have a fix with the test case.
PATCH 1/1 Simplify "reverse-diff" logic in the diff core.
In reply to Linus TorvaldsInstead of swapping the arguments just before output, this patch makes the swapping happen on the input side of the diff core, when "reverse-diff" is in effect. This greatly simplifies the logic, but more importantly it is necessary for upcoming "copy detection" work.
---
diff.c | 38 +++++++++++++++-----------------------
1 files changed, 15 insertions(+), 23 deletions(-)
diff.c +15 -23
@@ -350,15 +350,6 @@ static void run_external_diff(const charint status;static int atexit_asked =0 ; -if (reverse_diff) { -struct diff_spec *tmp_spec; - tmp_spec = one; one = two; two = tmp_spec; -if (other) { -const char *tmp; - tmp = name; name = other; other = tmp; - } - } -if (!matches_pathspec (name) && (!other || !matches_pathspec (other)))return ;
@@ -765,11 +756,12 @@ void diff_addremove(int addremove, unsigchar concatpath[PATH_MAX ];struct diff_spec spec[2 ], *one, *two; +if (reverse_diff) + addremove = (addremove =='+' ?'-' :'+' ); +if (0 <= diff_raw_output) {if (!path) path ="" ; -if (reverse_diff) - addremove = (addremove =='+' ?'-' :'+' ); printf("%c%06o %s %s %s%s%c", addremove, mode,
@@ -806,24 +798,24 @@ void diff_change(unsigned old_mode, unsichar concatpath[PATH_MAX ];struct diff_spec spec[2 ]; +if (reverse_diff) { +unsigned tmp; +const unsigned char *tmp_c; + tmp = old_mode; old_mode = new_mode; new_mode = tmp; + tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c; + } +if (0 <= diff_raw_output) {char old_hex[41 ];strcpy (old_hex,sha1_to_hex (old_sha1));if (!path) path ="" ; -if (reverse_diff) -printf ("*%06o->%06o %s %s->%s %s%s%c" , - new_mode, old_mode, -git_object_type (new_mode), -sha1_to_hex (new_sha1), old_hex, - base, path, diff_raw_output); -else -printf ("*%06o->%06o %s %s->%s %s%s%c" , - old_mode, new_mode, -git_object_type (new_mode), - old_hex,sha1_to_hex (new_sha1), - base, path, diff_raw_output); +printf ("*%06o->%06o %s %s->%s %s%s%c" , + old_mode, new_mode, +git_object_type (new_mode), + old_hex,sha1_to_hex (new_sha1), + base, path, diff_raw_output);return ; }if (S_ISDIR (new_mode))
------------------------------------------------
Keep git-diff-tree output sorted in cache order on dir/file swap.
When one tree has a tree where the other has a blob, diff-tree always outputs delete/create pair for that entry; deletion of the entry in the first tree is output first and then creation in the second tree. This order results in entries in the subdirectories from the first tree reported before the plain blob is shown from the second tree, i.e. not in the cache order.
This inconsistency may break downstream tools that expect the entries are sorted, since the git-* tools that output paths in all other cases do so in the cache order. Also this is needed to make "diff-tree A B" === "diff-tree -R B A".
---
diff-tree.c | 16 +++ t/lib-read-tree-m-3way.sh | 144 +++++++++++++++++++++++++++++++++ t/t1000-read-tree-m-3way.sh | 171 ---------------------------------------- t/t4002-diff-basic.sh | 187 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 347 insertions(+), 171 deletions(-)
new file (100755): t/lib-read-tree-m-3way.sh
new file (100755): t/t4002-diff-basic.sh
diff-tree.c +15 -1
@@ -124,8 +124,22 @@ static int compare_tree_entry(void *tree* file, we need to consider it a remove and an add. */ if (S_ISDIR (mode1) !=S_ISDIR (mode2)) { -show_file ("-" , tree1, size1, base); +/* +* This "one_first" is needed for keeping the output in +* the cache entry order. Otherwise, "diff A B" becomes +* different from "diff -R B A". +* +* It may look counterintuitive that one_first is not +* defined as (!recursive || S_ISDIR(mode2)), but this +* is deliberate; again otherwise, "diff A B" becomes +* different from "diff -R B A". +*/ +int one_first =S_ISDIR (mode2); +if (one_first) +show_file ("-" , tree1, size1, base);show_file ("+" , tree2, size2, base); +if (!one_first) +show_file ("-" , tree1, size1, base);return 0 ; }
t/lib-read-tree-m-3way.sh new file +144 -0
144 lines not on this page · raw message
t/t1000-read-tree-m-3way.sh +1 -170
178 lines not on this page · raw message
t/t4002-diff-basic.sh new file +187 -0
187 lines not on this page · raw message
------------------------------------------------
Quoted 2 lines · Junio C Hamano
Keep git-diff-tree output sorted in cache order on dir/file swap.
No, this is wrong.
Or rather, it is incomplete - it gets only a small subset of the interesting cases right.
You need to really teach diff-tree about directory sorting, because it is more complicated than you think.
The sorting does _not_ just affect files/directories with the same names. It affects files and directories that have the same _beginning_.
The only correct way to do name sorting is the one that fsck does in "verify_order()". To realize why, you need to realize that "A" (the directory) sorts _after_ a filename "A file", because '/' sorts after ' '.
In contrast, "A" (the directory) sorts _before_ "ABBA" (the band), because '/' sorts before 'B'.
Signature · 11 lines
Side note: Btw, right now I depend on "memcmp()" doing comparisons as "unsigned char", which is supposed to be true, but I don't know whether it's entirely portable. So it might be that I should implement my own "namecmp()" entirely from scratch. Does anybody know if some broken system does "memcmp" ordering on signed chars? Linus
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"LT" == Linus Torvalds <[email protected]> writes:
LT> No, this is wrong.
Understood. Since I am going to be futzing with diff family, I'll come up with some more tests while I am at it.
Quoted 7 lines · Linus Torvalds
The only correct way to do name sorting is the one that fsck does in "
verify_order()". To realize why, you need to realize that "A" (the directory) sorts _after_ a filename "A file", because '/' sorts after ' '.In contrast, "A" (the directory) sorts _before_ "ABBA" (the band), because '/' sorts before 'B'.
Btw, in case you wonder: the real problem is that we use "cache_name_compare()" on a directory name. The function is (as the name says) designed for full pathnames, not the diff-tree kind of possibly partial basenames.
Linus
Quoted 3 lines · Linus Torvalds
The sorting does _not_ just affect files/directories with the same names. It affects files and directories that have the same _beginning_.
Ok, I just pushed out a fix which should get this right (it introduces a new "base_name_compare()" function that knows about these subtleties).
I guess we should add a proper ordering test for all these cases. There's:
- exact same name, directory vs file
- different length names, but same in the first characters
- this one has the subcases of the next character sorting with '/' or '\0' depending on the type of the shorter filename.
Your previous test handled only the simple "exact same name" case.
Linus