Skip to contentSkip to conversation

diff overhaul

PATCH In review Junio C Hamano 2 participants 2 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 diff overhaul

Diff overhaul.

This patch cleans up the way calls are made into the diff core from diff-tree family and diff-helper. Earlier, these programs had "if (generating_patch)" sprinkled all over the place, but those ugliness are gone and handled uniformly from the diff core, even when not generating patch format.

This also allowed diff-cache and diff-files to acquire -R (reverse) option to generate diff in reverse. Users of diff-tree can swap two trees easily so I did not add -R there.

Also -M<digits-as-mantissa> suggestion made by Linus has been implemented.

It also fixes minor permission problems with some of the test scripts (although it should not matter since they are explicitly run by being the first argument to "sh"), but this being a patch that may not convey well.

Documentation updates are also included.

---

Documentation/git-diff-cache.txt |    5 +
Documentation/git-diff-files.txt |    5 +
diff-cache.c                     |   42 ++++-------
diff-files.c                     |   44 ++++--------
diff-helper.c                    |   19 +++--
diff-tree.c                      |   46 ++++--------
diff.c                           |  142 +++++++++++++++++++++++++++++++--------
diff.h                           |    4 -
t/t0000-basic.sh                 |    2 
t/t2002-checkout-cache-u.sh      |    2 
t/t4001-diff-rename.sh           |    6 +
11 files changed, 192 insertions(+), 125 deletions(-)

mode changed (100644 --> 100755): t/t0110-environment-names-old.sh mode changed (100644 --> 100755): t/t1000-read-tree-m-3way.sh mode changed (100644 --> 100755): t/t2002-checkout-cache-u.sh mode changed (100644 --> 100755): t/t4000-diff-format.sh mode changed (100644 --> 100755): t/t4001-diff-rename.sh

Documentation/git-diff-cache.txt +4 -1
@@ -9,7 +9,7 @@ git-diff-cache - Compares content and mo
 
 SYNOPSIS
 --------
-'git-diff-cache' [-p] [-r] [-z] [-m] [-M] [--cached] <tree-ish>
+'git-diff-cache' [-p] [-r] [-z] [-m] [-M] [-R] [--cached] <tree-ish>
 
 DESCRIPTION
 -----------
@@ -36,6 +36,9 @@ OPTIONS
 -M::
 	Detect renames; implies -p.
 
+-R::
+	Output diff in reverse.
+
 --cached::
 	do not consider the on-disk file at all
 
Documentation/git-diff-files.txt +4 -1
@@ -9,7 +9,7 @@ git-diff-files - Compares files in the w
 
 SYNOPSIS
 --------
-'git-diff-files' [-p] [-q] [-r] [-z] [-M] [<pattern>...]
+'git-diff-files' [-p] [-q] [-r] [-z] [-M] [-R] [<pattern>...]
 
 DESCRIPTION
 -----------
@@ -26,6 +26,9 @@ OPTIONS
 -q::
 	Remain silent even on nonexisting files
 
+-R::
+	Output diff in reverse.
+
 -M::
 	Detect renames; implies -p.
 
diff-cache.c +17 -25

100 lines not on this page · raw message

diff-files.c +16 -28

97 lines not on this page · raw message

diff-helper.c +11 -8
@@ -7,6 +7,7 @@
 #include "diff.h"
 
 static int detect_rename = 0;
+static int diff_score_opt = 0;
 
 static int parse_oneside_change(const char *cp, int *mode,
 				unsigned char *sha1, char *path)
@@ -19,15 +20,15 @@ static int parse_oneside_change(const ch
 		cp++;
 	}
 	*mode = m;
-	if (strncmp(cp, "\tblob\t", 6))
+	if (strncmp(cp, "\tblob\t", 6) && strncmp(cp, " blob ", 6))
 		return -1;
 	cp += 6;
 	if (get_sha1_hex(cp, sha1))
 		return -1;
 	cp += 40;
-	if (*cp++ != '\t')
+	if ((*cp != '\t') && *cp != ' ')
 		return -1;
-	strcpy(path, cp);
+	strcpy(path, ++cp);
 	return 0;
 }
 
@@ -63,7 +64,7 @@ static int parse_diff_raw_output(const c
 			new_mode = (new_mode << 3) | (ch - '0');
 			cp++;
 		}
-		if (strncmp(cp, "\tblob\t", 6))
+		if (strncmp(cp, "\tblob\t", 6) && strncmp(cp, " blob ", 6))
 			return -1;
 		cp += 6;
 		if (get_sha1_hex(cp, old_sha1))
@@ -75,9 +76,9 @@ static int parse_diff_raw_output(const c
 		if (get_sha1_hex(cp, new_sha1))
 			return -1;
 		cp += 40;
-		if (*cp++ != '\t')
+		if ((*cp != '\t') && *cp != ' ')
 			return -1;
-		strcpy(path, cp);
+		strcpy(path, ++cp);
 		diff_change(old_mode, new_mode, old_sha1, new_sha1, path, 0);
 		break;
 	default:
@@ -101,15 +102,17 @@ int main(int ac, const char **av) {
 			reverse = 1;
 		else if (av[1][1] == 'z')
 			line_termination = 0;
-		else if (av[1][1] == 'M')
+		else if (av[1][1] == 'M') {
 			detect_rename = 1;
+			diff_score_opt = diff_scoreopt_parse(av[1]);
+		}
 		else
 			usage(diff_helper_usage);
 		ac--; av++;
 	}
 	/* the remaining parameters are paths patterns */
 
-	diff_setup(detect_rename, 0, reverse, av+1, ac-1);
+	diff_setup(detect_rename, diff_score_opt, reverse, -1, av+1, ac-1);
 
 	while (1) {
 		int status;
diff-tree.c +15 -31

100 lines not on this page · raw message

diff.c +113 -29

301 lines not on this page · raw message

diff.h +3 -1
@@ -17,8 +17,10 @@ extern void diff_change(unsigned mode1, 
 
 extern void diff_unmerge(const char *path);
 
+extern int diff_scoreopt_parse(const char *opt);
+
 extern void diff_setup(int detect_rename, int minimum_score,
-		       int reverse,
+		       int reverse, int raw_output,
 		       const char **spec, int cnt);
 
 extern void diff_flush(void);
t/t0000-basic.sh +1 -1
@@ -166,7 +166,7 @@ cat >expected <<\EOF
 EOF
 test_expect_success \
     'validate git-diff-files output for a know cache/work tree state.' \
-    'git-diff-files >current && cmp -s current expected'
+    'git-diff-files >current && diff >/dev/null -b current expected'
 
 test_expect_success \
     'git-update-cache --refresh should succeed.' \
t/t0110-environment-names-old.sh +0 -0
t/t1000-read-tree-m-3way.sh +0 -0
t/t2002-checkout-cache-u.sh +2 -0
@@ -29,3 +29,5 @@ rm -f path0 &&
 git-read-tree $t &&
 git-checkout-cache -u -f -a &&
 git-diff-files | diff - /dev/null'
+
+test_done
t/t4000-diff-format.sh +0 -0
t/t4001-diff-rename.sh +6 -0
@@ -58,3 +58,9 @@ rename new path1
  Line 13
  Line 14
 EOF
+
+test_expect_success \
+    'validate the output.' \
+    'diff -I "rename similarity.*" >/dev/null current expected'
+
+test_done

------------------------------------------------

#1 lore raw
Quoted 4 lines · Junio C Hamano

This also allowed diff-cache and diff-files to acquire -R (reverse) option to generate diff in reverse. Users of diff-tree can swap two trees easily so I did not add -R there.

Actually, diff-tree would want it too.

You can only swap the trees easily if you give two trees, not if you give a commit.

Linus