{"thread":{"id":"667","subject":"[PATCH] diff overhaul","startedAt":"2005-05-20T02:00:36Z","lastAt":"2005-05-20T05:30:34Z","messageCount":2,"participants":["Junio C Hamano","Linus Torvalds"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"3595","messageId":"7vll6awta3.fsf@assigned-by-dhcp.cox.net","threadId":"667","inReplyTo":null,"subject":"[PATCH] diff overhaul","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-05-20T02:00:36Z","receivedAt":"2005-05-20T02:00:36Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Diff overhaul.\n\nThis patch cleans up the way calls are made into the diff core\nfrom diff-tree family and diff-helper.  Earlier, these programs\nhad \"if (generating_patch)\" sprinkled all over the place, but\nthose ugliness are gone and handled uniformly from the diff\ncore, even when not generating patch format.\n\nThis also allowed diff-cache and diff-files to acquire -R\n(reverse) option to generate diff in reverse.  Users of\ndiff-tree can swap two trees easily so I did not add -R there.\n\nAlso -M<digits-as-mantissa> suggestion made by Linus has been\nimplemented.\n\nIt also fixes minor permission problems with some of the test\nscripts (although it should not matter since they are explicitly\nrun by being the first argument to \"sh\"), but this being a patch\nthat may not convey well.\n\nDocumentation updates are also included.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n\nDocumentation/git-diff-cache.txt |    5 +\nDocumentation/git-diff-files.txt |    5 +\ndiff-cache.c                     |   42 ++++-------\ndiff-files.c                     |   44 ++++--------\ndiff-helper.c                    |   19 +++--\ndiff-tree.c                      |   46 ++++--------\ndiff.c                           |  142 +++++++++++++++++++++++++++++++--------\ndiff.h                           |    4 -\nt/t0000-basic.sh                 |    2 \nt/t2002-checkout-cache-u.sh      |    2 \nt/t4001-diff-rename.sh           |    6 +\n11 files changed, 192 insertions(+), 125 deletions(-)\nmode changed (100644 --> 100755): t/t0110-environment-names-old.sh\nmode changed (100644 --> 100755): t/t1000-read-tree-m-3way.sh\nmode changed (100644 --> 100755): t/t2002-checkout-cache-u.sh\nmode changed (100644 --> 100755): t/t4000-diff-format.sh\nmode changed (100644 --> 100755): t/t4001-diff-rename.sh\n\ndiff --git a/Documentation/git-diff-cache.txt b/Documentation/git-diff-cache.txt\n--- a/Documentation/git-diff-cache.txt\n+++ b/Documentation/git-diff-cache.txt\n@@ -9,7 +9,7 @@ git-diff-cache - Compares content and mo\n \n SYNOPSIS\n --------\n-'git-diff-cache' [-p] [-r] [-z] [-m] [-M] [--cached] <tree-ish>\n+'git-diff-cache' [-p] [-r] [-z] [-m] [-M] [-R] [--cached] <tree-ish>\n \n DESCRIPTION\n -----------\n@@ -36,6 +36,9 @@ OPTIONS\n -M::\n \tDetect renames; implies -p.\n \n+-R::\n+\tOutput diff in reverse.\n+\n --cached::\n \tdo not consider the on-disk file at all\n \ndiff --git a/Documentation/git-diff-files.txt b/Documentation/git-diff-files.txt\n--- a/Documentation/git-diff-files.txt\n+++ b/Documentation/git-diff-files.txt\n@@ -9,7 +9,7 @@ git-diff-files - Compares files in the w\n \n SYNOPSIS\n --------\n-'git-diff-files' [-p] [-q] [-r] [-z] [-M] [<pattern>...]\n+'git-diff-files' [-p] [-q] [-r] [-z] [-M] [-R] [<pattern>...]\n \n DESCRIPTION\n -----------\n@@ -26,6 +26,9 @@ OPTIONS\n -q::\n \tRemain silent even on nonexisting files\n \n+-R::\n+\tOutput diff in reverse.\n+\n -M::\n \tDetect renames; implies -p.\n \ndiff --git a/diff-cache.c b/diff-cache.c\n--- a/diff-cache.c\n+++ b/diff-cache.c\n@@ -6,15 +6,13 @@ static int generate_patch = 0;\n static int match_nonexisting = 0;\n static int line_termination = '\\n';\n static int detect_rename = 0;\n+static int reverse_diff = 0;\n+static int diff_score_opt = 0;\n \n /* A file entry went away or appeared */\n static void show_file(const char *prefix, struct cache_entry *ce, unsigned char *sha1, unsigned int mode)\n {\n-\tif (generate_patch)\n-\t\tdiff_addremove(prefix[0], ntohl(mode), sha1, ce->name, NULL);\n-\telse\n-\t\tprintf(\"%s%06o\\tblob\\t%s\\t%s%c\", prefix, ntohl(mode),\n-\t\t       sha1_to_hex(sha1), ce->name, line_termination);\n+\tdiff_addremove(prefix[0], ntohl(mode), sha1, ce->name, NULL);\n }\n \n static int get_stat_data(struct cache_entry *ce, unsigned char **sha1p, unsigned int *modep)\n@@ -64,7 +62,6 @@ static int show_modified(struct cache_en\n {\n \tunsigned int mode, oldmode;\n \tunsigned char *sha1;\n-\tchar old_sha1_hex[60];\n \n \tif (get_stat_data(new, &sha1, &mode) < 0) {\n \t\tif (report_missing)\n@@ -79,15 +76,8 @@ static int show_modified(struct cache_en\n \tmode = ntohl(mode);\n \toldmode = ntohl(oldmode);\n \n-\tif (generate_patch)\n-\t\tdiff_change(oldmode, mode,\n-\t\t\t    old->sha1, sha1, old->name, NULL);\n-\telse {\n-\t\tstrcpy(old_sha1_hex, sha1_to_hex(old->sha1));\n-\t\tprintf(\"*%06o->%06o\\tblob\\t%s->%s\\t%s%c\", oldmode, mode,\n-\t\t       old_sha1_hex, sha1_to_hex(sha1),\n-\t\t       old->name, line_termination);\n-\t}\n+\tdiff_change(oldmode, mode,\n+\t\t    old->sha1, sha1, old->name, NULL);\n \treturn 0;\n }\n \n@@ -127,10 +117,7 @@ static int diff_cache(struct cache_entry\n \t\t\t\tbreak;\n \t\t\t/* fallthru */\n \t\tcase 3:\n-\t\t\tif (generate_patch)\n-\t\t\t\tdiff_unmerge(ce->name);\n-\t\t\telse\n-\t\t\t\tprintf(\"U %s%c\", ce->name, line_termination);\n+\t\t\tdiff_unmerge(ce->name);\n \t\t\tbreak;\n \n \t\tdefault:\n@@ -166,7 +153,7 @@ static void mark_merge_entries(void)\n }\n \n static char *diff_cache_usage =\n-\"git-diff-cache [-p] [-r] [-z] [-m] [-M] [--cached] <tree-ish>\";\n+\"git-diff-cache [-p] [-r] [-z] [-m] [-M] [-R] [--cached] <tree-ish>\";\n \n int main(int argc, char **argv)\n {\n@@ -188,14 +175,19 @@ int main(int argc, char **argv)\n \t\t\tgenerate_patch = 1;\n \t\t\tcontinue;\n \t\t}\n-\t\tif (!strcmp(arg, \"-M\")) {\n+\t\tif (!strncmp(arg, \"-M\", 2)) {\n \t\t\tgenerate_patch = detect_rename = 1;\n+\t\t\tdiff_score_opt = diff_scoreopt_parse(arg);\n \t\t\tcontinue;\n \t\t}\n \t\tif (!strcmp(arg, \"-z\")) {\n \t\t\tline_termination = '\\0';\n \t\t\tcontinue;\n \t\t}\n+\t\tif (!strcmp(arg, \"-R\")) {\n+\t\t\treverse_diff = 1;\n+\t\t\tcontinue;\n+\t\t}\n \t\tif (!strcmp(arg, \"-m\")) {\n \t\t\tmatch_nonexisting = 1;\n \t\t\tcontinue;\n@@ -210,8 +202,9 @@ int main(int argc, char **argv)\n \tif (argc != 2 || get_sha1(argv[1], tree_sha1))\n \t\tusage(diff_cache_usage);\n \n-\tif (generate_patch)\n-\t\tdiff_setup(detect_rename, 0, 0, 0, 0);\n+\tdiff_setup(detect_rename, diff_score_opt, reverse_diff,\n+\t\t   (generate_patch ? -1 : line_termination),\n+\t\t   0, 0);\n \n \tmark_merge_entries();\n \n@@ -222,7 +215,6 @@ int main(int argc, char **argv)\n \t\tdie(\"unable to read tree object %s\", argv[1]);\n \n \tret = diff_cache(active_cache, active_nr);\n-\tif (generate_patch)\n-\t\tdiff_flush();\n+\tdiff_flush();\n \treturn ret;\n }\ndiff --git a/diff-files.c b/diff-files.c\n--- a/diff-files.c\n+++ b/diff-files.c\n@@ -7,11 +7,13 @@\n #include \"diff.h\"\n \n static const char *diff_files_usage =\n-\"git-diff-files [-p] [-q] [-r] [-z] [-M] [paths...]\";\n+\"git-diff-files [-p] [-q] [-r] [-z] [-M] [-R] [paths...]\";\n \n static int generate_patch = 0;\n static int line_termination = '\\n';\n static int detect_rename = 0;\n+static int reverse_diff = 0;\n+static int diff_score_opt = 0;\n static int silent = 0;\n \n static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)\n@@ -31,36 +33,19 @@ static int matches_pathspec(struct cache\n \n static void show_unmerge(const char *path)\n {\n-\tif (generate_patch)\n-\t\tdiff_unmerge(path);\n-\telse\n-\t\tprintf(\"U %s%c\", path, line_termination);\n+\tdiff_unmerge(path);\n }\n \n static void show_file(int pfx, struct cache_entry *ce)\n {\n-\tif (generate_patch)\n-\t\tdiff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1,\n-\t\t\t       ce->name, NULL);\n-\telse\n-\t\tprintf(\"%c%06o\\t%s\\t%s\\t%s%c\",\n-\t\t       pfx, ntohl(ce->ce_mode), \"blob\",\n-\t\t       sha1_to_hex(ce->sha1), ce->name, line_termination);\n+\tdiff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL);\n }\n \n static void show_modified(int oldmode, int mode,\n \t\t\t  const unsigned char *old_sha1, const unsigned char *sha1,\n \t\t\t  char *path)\n {\n-\tchar old_sha1_hex[41];\n-\tstrcpy(old_sha1_hex, sha1_to_hex(old_sha1));\n-\n-\tif (generate_patch)\n-\t\tdiff_change(oldmode, mode, old_sha1, sha1, path, NULL);\n-\telse\n-\t\tprintf(\"*%06o->%06o\\tblob\\t%s->%s\\t%s%c\",\n-\t\t       oldmode, mode, old_sha1_hex, sha1_to_hex(sha1), path,\n-\t\t       line_termination);\n+\tdiff_change(oldmode, mode, old_sha1, sha1, path, NULL);\n }\n \n int main(int argc, char **argv)\n@@ -80,7 +65,10 @@ int main(int argc, char **argv)\n \t\t\t; /* no-op */\n \t\telse if (!strcmp(argv[1], \"-z\"))\n \t\t\tline_termination = 0;\n-\t\telse if (!strcmp(argv[1], \"-M\")) {\n+\t\telse if (!strcmp(argv[1], \"-R\"))\n+\t\t\treverse_diff = 1;\n+\t\telse if (!strncmp(argv[1], \"-M\", 2)) {\n+\t\t\tdiff_score_opt = diff_scoreopt_parse(argv[1]);\n \t\t\tdetect_rename = generate_patch = 1;\n \t\t}\n \t\telse\n@@ -96,8 +84,9 @@ int main(int argc, char **argv)\n \t\texit(1);\n \t}\n \n-\tif (generate_patch)\n-\t\tdiff_setup(detect_rename, 0, 0, 0, 0);\t\t\n+\tdiff_setup(detect_rename, diff_score_opt, reverse_diff,\n+\t\t   (generate_patch ? -1 : line_termination),\n+\t\t   0, 0);\n \n \tfor (i = 0; i < entries; i++) {\n \t\tstruct stat st;\n@@ -117,12 +106,12 @@ int main(int argc, char **argv)\n \t\t\ti--; /* compensate for loop control increments */\n \t\t\tcontinue;\n \t\t}\n- \n+\n \t\tif (lstat(ce->name, &st) < 0) {\n \t\t\tif (errno != ENOENT) {\n \t\t\t\tperror(ce->name);\n \t\t\t\tcontinue;\n-\t\t\t}\t\n+\t\t\t}\n \t\t\tif (silent)\n \t\t\t\tcontinue;\n \t\t\tshow_file('-', ce);\n@@ -139,7 +128,6 @@ int main(int argc, char **argv)\n \t\tshow_modified(oldmode, mode, ce->sha1, null_sha1,\n \t\t\t      ce->name);\n \t}\n-\tif (generate_patch)\n-\t\tdiff_flush();\n+\tdiff_flush();\n \treturn 0;\n }\ndiff --git a/diff-helper.c b/diff-helper.c\n--- a/diff-helper.c\n+++ b/diff-helper.c\n@@ -7,6 +7,7 @@\n #include \"diff.h\"\n \n static int detect_rename = 0;\n+static int diff_score_opt = 0;\n \n static int parse_oneside_change(const char *cp, int *mode,\n \t\t\t\tunsigned char *sha1, char *path)\n@@ -19,15 +20,15 @@ static int parse_oneside_change(const ch\n \t\tcp++;\n \t}\n \t*mode = m;\n-\tif (strncmp(cp, \"\\tblob\\t\", 6))\n+\tif (strncmp(cp, \"\\tblob\\t\", 6) && strncmp(cp, \" blob \", 6))\n \t\treturn -1;\n \tcp += 6;\n \tif (get_sha1_hex(cp, sha1))\n \t\treturn -1;\n \tcp += 40;\n-\tif (*cp++ != '\\t')\n+\tif ((*cp != '\\t') && *cp != ' ')\n \t\treturn -1;\n-\tstrcpy(path, cp);\n+\tstrcpy(path, ++cp);\n \treturn 0;\n }\n \n@@ -63,7 +64,7 @@ static int parse_diff_raw_output(const c\n \t\t\tnew_mode = (new_mode << 3) | (ch - '0');\n \t\t\tcp++;\n \t\t}\n-\t\tif (strncmp(cp, \"\\tblob\\t\", 6))\n+\t\tif (strncmp(cp, \"\\tblob\\t\", 6) && strncmp(cp, \" blob \", 6))\n \t\t\treturn -1;\n \t\tcp += 6;\n \t\tif (get_sha1_hex(cp, old_sha1))\n@@ -75,9 +76,9 @@ static int parse_diff_raw_output(const c\n \t\tif (get_sha1_hex(cp, new_sha1))\n \t\t\treturn -1;\n \t\tcp += 40;\n-\t\tif (*cp++ != '\\t')\n+\t\tif ((*cp != '\\t') && *cp != ' ')\n \t\t\treturn -1;\n-\t\tstrcpy(path, cp);\n+\t\tstrcpy(path, ++cp);\n \t\tdiff_change(old_mode, new_mode, old_sha1, new_sha1, path, 0);\n \t\tbreak;\n \tdefault:\n@@ -101,15 +102,17 @@ int main(int ac, const char **av) {\n \t\t\treverse = 1;\n \t\telse if (av[1][1] == 'z')\n \t\t\tline_termination = 0;\n-\t\telse if (av[1][1] == 'M')\n+\t\telse if (av[1][1] == 'M') {\n \t\t\tdetect_rename = 1;\n+\t\t\tdiff_score_opt = diff_scoreopt_parse(av[1]);\n+\t\t}\n \t\telse\n \t\t\tusage(diff_helper_usage);\n \t\tac--; av++;\n \t}\n \t/* the remaining parameters are paths patterns */\n \n-\tdiff_setup(detect_rename, 0, reverse, av+1, ac-1);\n+\tdiff_setup(detect_rename, diff_score_opt, reverse, -1, av+1, ac-1);\n \n \twhile (1) {\n \t\tint status;\ndiff --git a/diff-tree.c b/diff-tree.c\n--- a/diff-tree.c\n+++ b/diff-tree.c\n@@ -11,6 +11,7 @@ static int read_stdin = 0;\n static int line_termination = '\\n';\n static int generate_patch = 0;\n static int detect_rename = 0;\n+static int diff_score_opt = 0;\n static const char *header = NULL;\n static const char *header_prefix = \"\";\n \n@@ -84,21 +85,13 @@ static void show_file(const char *prefix\n \t\t\tdie(\"corrupt tree sha %s\", sha1_to_hex(sha1));\n \n \t\tshow_tree(prefix, tree, size, newbase);\n-\t\t\n+\n \t\tfree(tree);\n \t\tfree(newbase);\n \t\treturn;\n \t}\n \n-\tif (generate_patch) {\n-\t\tif (!S_ISDIR(mode))\n-\t\t\tdiff_addremove(prefix[0], mode, sha1, base, path);\n-\t}\n-\telse\n-\t\tprintf(\"%s%06o\\t%s\\t%s\\t%s%s%c\", prefix, mode,\n-\t\t       S_ISDIR(mode) ? \"tree\" : \"blob\",\n-\t\t       sha1_to_hex(sha1), base, path,\n-\t\t       line_termination);\n+\tdiff_addremove(prefix[0], mode, sha1, base, path);\n }\n \n static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)\n@@ -107,7 +100,6 @@ static int compare_tree_entry(void *tree\n \tconst char *path1, *path2;\n \tconst unsigned char *sha1, *sha2;\n \tint cmp, pathlen1, pathlen2;\n-\tchar old_sha1_hex[50];\n \n \tsha1 = extract(tree1, size1, &path1, &mode1);\n \tsha2 = extract(tree2, size2, &path2, &mode2);\n@@ -151,17 +143,7 @@ static int compare_tree_entry(void *tree\n \tif (silent)\n \t\treturn 0;\n \n-\tif (generate_patch) {\n-\t\tif (!S_ISDIR(mode1))\n-\t\t\tdiff_change(mode1, mode2, sha1, sha2, base, path1);\n-\t}\n-\telse {\n-\t\tstrcpy(old_sha1_hex, sha1_to_hex(sha1));\n-\t\tprintf(\"*%06o->%06o\\t%s\\t%s->%s\\t%s%s%c\", mode1, mode2,\n-\t\t       S_ISDIR(mode1) ? \"tree\" : \"blob\",\n-\t\t       old_sha1_hex, sha1_to_hex(sha2), base, path1,\n-\t\t       line_termination);\n-\t}\n+\tdiff_change(mode1, mode2, sha1, sha2, base, path1);\n \treturn 0;\n }\n \n@@ -287,11 +269,12 @@ static int diff_tree_sha1_top(const unsi\n \t\t\t      const unsigned char *new, const char *base)\n {\n \tint ret;\n-\tif (generate_patch)\n-\t\tdiff_setup(detect_rename, 0, 0, 0, 0);\n+\n+\tdiff_setup(detect_rename, diff_score_opt, 0,\n+\t\t   (generate_patch ? -1 : line_termination),\n+\t\t   0, 0);\n \tret = diff_tree_sha1(old, new, base);\n-\tif (generate_patch)\n-\t\tdiff_flush();\n+\tdiff_flush();\n \treturn ret;\n }\n \n@@ -301,15 +284,15 @@ static int diff_root_tree(const unsigned\n \tvoid *tree;\n \tunsigned long size;\n \n-\tif (generate_patch)\n-\t\tdiff_setup(detect_rename, 0, 0, 0, 0);\n+\tdiff_setup(detect_rename, diff_score_opt, 0,\n+\t\t   (generate_patch ? -1 : line_termination),\n+\t\t   0, 0);\n \ttree = read_object_with_reference(new, \"tree\", &size, 0);\n \tif (!tree)\n \t\tdie(\"unable to read root tree (%s)\", sha1_to_hex(new));\n \tretval = diff_tree(\"\", 0, tree, size, base);\n \tfree(tree);\n-\tif (generate_patch)\n-\t\tdiff_flush();\n+\tdiff_flush();\n \treturn retval;\n }\n \n@@ -485,8 +468,9 @@ int main(int argc, char **argv)\n \t\t\trecursive = generate_patch = 1;\n \t\t\tcontinue;\n \t\t}\n-\t\tif (!strcmp(arg, \"-M\")) {\n+\t\tif (!strncmp(arg, \"-M\", 2)) {\n \t\t\tdetect_rename = recursive = generate_patch = 1;\n+\t\t\tdiff_score_opt = diff_scoreopt_parse(arg);\n \t\t\tcontinue;\n \t\t}\n \t\tif (!strcmp(arg, \"-z\")) {\ndiff --git a/diff.c b/diff.c\n--- a/diff.c\n+++ b/diff.c\n@@ -11,6 +11,8 @@\n \n static const char *diff_opts = \"-pu\";\n static unsigned char null_sha1[20] = { 0, };\n+#define MAX_SCORE 10000\n+#define DEFAULT_MINIMUM_SCORE 5000\n \n static const char *external_diff(void)\n {\n@@ -55,7 +57,7 @@ static char *sq_expand(const char *src)\n \tconst char *cp;\n \tchar *bp;\n \n-\t/* count bytes needed to store the quoted string. */ \n+\t/* count bytes needed to store the quoted string. */\n \tfor (cnt = 1, cp = src; *cp; cnt++, cp++)\n \t\tif (*cp == '\\'')\n \t\t\tcnt += 3;\n@@ -93,7 +95,8 @@ struct diff_spec {\n \n static void builtin_diff(const char *name_a,\n \t\t\t const char *name_b,\n-\t\t\t struct diff_tempfile *temp)\n+\t\t\t struct diff_tempfile *temp,\n+\t\t\t int rename_score)\n {\n \tint i, next_at, cmd_size;\n \tconst char *diff_cmd = \"diff -L'%s%s' -L'%s%s'\";\n@@ -149,6 +152,10 @@ static void builtin_diff(const char *nam\n \t\t\tprintf(\"new mode %s\\n\", temp[1].mode);\n \t\t}\n \t\tif (strcmp(name_a, name_b)) {\n+\t\t\tif (0 < rename_score)\n+\t\t\t\tprintf(\"rename similarity index %d%%\\n\",\n+\t\t\t\t       (int)(0.5+\n+\t\t\t\t\t     rename_score*100.0/MAX_SCORE));\n \t\t\tprintf(\"rename old %s\\n\", name_a);\n \t\t\tprintf(\"rename new %s\\n\", name_b);\n \t\t}\n@@ -184,7 +191,7 @@ static int work_tree_matches(const char \n \t * file.  Practically, this code only helps when we are used\n \t * by diff-cache --cached, which does read the cache before\n \t * calling us.\n-\t */ \n+\t */\n \tif (!active_cache)\n \t\treturn 0;\n \n@@ -302,9 +309,10 @@ static void remove_tempfile_on_signal(in\n \n static int detect_rename;\n static int reverse_diff;\n+static int diff_raw_output = -1;\n static const char **pathspec;\n static int speccnt;\n-static int diff_rename_minimum_score;\n+static int minimum_score;\n \n static int matches_pathspec(const char *name)\n {\n@@ -334,7 +342,8 @@ static int matches_pathspec(const char *\n static void run_external_diff(const char *name,\n \t\t\t      const char *other,\n \t\t\t      struct diff_spec *one,\n-\t\t\t      struct diff_spec *two)\n+\t\t\t      struct diff_spec *two,\n+\t\t\t      int rename_score)\n {\n \tstruct diff_tempfile *temp = diff_temp;\n \tpid_t pid;\n@@ -395,7 +404,7 @@ static void run_external_diff(const char\n \t\t * otherwise we use the built-in one.\n \t\t */\n \t\tif (one && two)\n-\t\t\tbuiltin_diff(name, other ? : name, temp);\n+\t\t\tbuiltin_diff(name, other ? : name, temp, rename_score);\n \t\telse\n \t\t\tprintf(\"* Unmerged path %s\\n\", name);\n \t\texit(0);\n@@ -446,7 +455,7 @@ static void hold_diff(const char *name,\n \t\tdie(\"internal error\");\n \n \tif (!detect_rename) {\n-\t\trun_external_diff(name, NULL, one, two);\n+\t\trun_external_diff(name, NULL, one, two, -1);\n \t\treturn;\n \t}\n \telem = xmalloc(sizeof(*elem) + strlen(name));\n@@ -519,10 +528,10 @@ static void flush_remaining_diff(struct \n \t\t\tcontinue;\n \t\tif (on_created_list)\n \t\t\trun_external_diff(elem->path, NULL,\n-\t\t\t\t\t  &null_file_spec, &elem->it);\n+\t\t\t\t\t  &null_file_spec, &elem->it, -1);\n \t\telse\n \t\t\trun_external_diff(elem->path, NULL,\n-\t\t\t\t\t  &elem->it, &null_file_spec);\n+\t\t\t\t\t  &elem->it, &null_file_spec, -1);\n \t}\n }\n \n@@ -541,7 +550,6 @@ static int is_exact_match(struct diff_sp\n \treturn 0;\n }\n \n-#define MINIMUM_SCORE 5000\n int estimate_similarity(struct diff_spec_hold *src, struct diff_spec_hold *dst)\n {\n \t/* src points at a deleted file and dst points at a created\n@@ -549,20 +557,24 @@ int estimate_similarity(struct diff_spec\n \t * say src is renamed to dst.\n \t *\n \t * Compare them and return how similar they are, representing\n-\t * the score as an integer between 0 and 10000.  10000 is\n-\t * reserved for the case where they match exactly.\n+\t * the score as an integer between 0 and 10000, except\n+\t * where they match exactly it is considered better than anything\n+\t * else.\n \t */\n \tvoid *delta;\n \tunsigned long delta_size;\n+\tint score;\n \n \tdelta_size = ((src->size < dst->size) ?\n \t\t      (dst->size - src->size) : (src->size - dst->size));\n \n \t/* We would not consider rename followed by more than\n-\t * 20% edits; that is, delta_size must be smaller than\n-\t * (src->size + dst->size)/2 * 0.2, which means...\n+\t * minimum_score/MAX_SCORE edits; that is, delta_size must be smaller\n+\t * than (src->size + dst->size)/2 * minimum_score/MAX_SCORE,\n+\t * which means...\n \t */\n-\tif ((src->size + dst->size) < delta_size * 10)\n+\n+\tif ((src->size+dst->size)*minimum_score < delta_size*MAX_SCORE*2)\n \t\treturn 0;\n \n \tdelta = diff_delta(src->data, src->size,\n@@ -573,14 +585,17 @@ int estimate_similarity(struct diff_spec\n \t/* This \"delta\" is really xdiff with adler32 and all the\n \t * overheads but it is a quick and dirty approximation.\n \t *\n-\t * Now we will give some score to it.  Let's say 20% edit gets\n-\t * 5000 points and 0% edit gets 9000 points.  That is, every\n-\t * 1/20000 edit gets 1 point penalty.  The amount of penalty is:\n+\t * Now we will give some score to it.  100% edit gets\n+\t * 0 points and 0% edit gets MAX_SCORE points.  That is, every\n+\t * 1/MAX_SCORE edit gets 1 point penalty.  The amount of penalty is:\n \t *\n-\t * (delta_size * 2 / (src->size + dst->size)) * 20000\n+\t * (delta_size * 2 / (src->size + dst->size)) * MAX_SCORE\n \t *\n \t */\n-\treturn 9000 - (40000 * delta_size / (src->size+dst->size));\n+\tscore = MAX_SCORE-(MAX_SCORE*2*delta_size/(src->size+dst->size));\n+\tif (score < 0) return 0;\n+\tif (MAX_SCORE < score) return MAX_SCORE;\n+\treturn score;\n }\n \n struct diff_score {\n@@ -596,14 +611,15 @@ static int score_compare(const void *a_,\n }\n \n static void flush_rename_pair(struct diff_spec_hold *src,\n-\t\t\t      struct diff_spec_hold *dst)\n+\t\t\t      struct diff_spec_hold *dst,\n+\t\t\t      int rename_score)\n {\n \tsrc->flags |= MATCHED;\n \tdst->flags |= MATCHED;\n \tfree_data(src);\n \tfree_data(dst);\n \trun_external_diff(src->path, dst->path,\n-\t\t\t  &src->it, &dst->it);\n+\t\t\t  &src->it, &dst->it, rename_score);\n }\n \n static void free_held_diff(struct diff_spec_hold *list)\n@@ -637,7 +653,7 @@ void diff_flush(void)\n \t\t\t\tcontinue;\n \t\t\tif (! is_exact_match(src, dst))\n \t\t\t\tcontinue;\n-\t\t\tflush_rename_pair(src, dst);\n+\t\t\tflush_rename_pair(src, dst, MAX_SCORE);\n \t\t\tbreak;\n \t\t}\n \t}\n@@ -670,7 +686,7 @@ void diff_flush(void)\n \t\t}\n \t\tc++;\n \t}\n-\tqsort(mx, num_create*num_delete, sizeof(*mx), score_compare); \n+\tqsort(mx, num_create*num_delete, sizeof(*mx), score_compare);\n \n #if 0\n  \tfor (c = 0; c < num_create * num_delete; c++) {\n@@ -689,9 +705,9 @@ void diff_flush(void)\n \t\tdst = mx[c].dst;\n \t\tif ((src->flags & MATCHED) || (dst->flags & MATCHED))\n \t\t\tcontinue;\n-\t\tif (mx[c].score < diff_rename_minimum_score)\n+\t\tif (mx[c].score < minimum_score)\n \t\t\tbreak;\n-\t\tflush_rename_pair(src, dst);\n+\t\tflush_rename_pair(src, dst, mx[c].score);\n \t}\n \tfree(mx);\n \n@@ -703,7 +719,26 @@ void diff_flush(void)\n \tcreatedfile = deletedfile = NULL;\n }\n \n+int diff_scoreopt_parse(const char *opt)\n+{\n+\tint diglen, num, scale, i;\n+\tif (opt[0] != '-' || opt[1] != 'M')\n+\t\treturn -1; /* that is not -M option */\n+\tdiglen = strspn(opt+2, \"0123456789\");\n+\tif (diglen == 0 || strlen(opt+2) != diglen)\n+\t\treturn 0; /* use default */\n+\tsscanf(opt+2, \"%d\", &num);\n+\tfor (i = 0, scale = 1; i < diglen; i++)\n+\t\tscale *= 10;\n+\n+\t/* user says num divided by scale and we say internally that\n+\t * is MAX_SCORE * num / scale.\n+\t */\n+\treturn MAX_SCORE * num / scale;\n+}\n+\n void diff_setup(int detect_rename_, int minimum_score_, int reverse_diff_,\n+\t\tint diff_raw_output_,\n \t\tconst char **pathspec_, int speccnt_)\n {\n \tfree_held_diff(createdfile);\n@@ -713,8 +748,14 @@ void diff_setup(int detect_rename_, int \n \tdetect_rename = detect_rename_;\n \treverse_diff = reverse_diff_;\n \tpathspec = pathspec_;\n+\tdiff_raw_output = diff_raw_output_;\n \tspeccnt = speccnt_;\n-\tdiff_rename_minimum_score = minimum_score_ ? : MINIMUM_SCORE;\n+\tminimum_score = minimum_score_ ? : DEFAULT_MINIMUM_SCORE;\n+}\n+\n+static const char *git_object_type(unsigned mode)\n+{\n+\treturn S_ISDIR(mode) ? \"tree\" : \"blob\";\n }\n \n void diff_addremove(int addremove, unsigned mode,\n@@ -724,6 +765,21 @@ void diff_addremove(int addremove, unsig\n \tchar concatpath[PATH_MAX];\n \tstruct diff_spec spec[2], *one, *two;\n \n+\tif (0 <= diff_raw_output) {\n+\t\tif (!path)\n+\t\t\tpath = \"\";\n+\t\tif (reverse_diff)\n+\t\t\taddremove = (addremove == '+' ? '-' : '+');\n+\t\tprintf(\"%c%06o %s %s %s%s%c\",\n+\t\t       addremove,\n+\t\t       mode,\n+\t\t       git_object_type(mode), sha1_to_hex(sha1),\n+\t\t       base, path, diff_raw_output);\n+\t\treturn;\n+\t}\n+\tif (S_ISDIR(mode))\n+\t\treturn;\n+\n \tmemcpy(spec[0].blob_sha1, sha1, 20);\n \tspec[0].mode = mode;\n \tspec[0].sha1_valid = !!memcmp(sha1, null_sha1, 20);\n@@ -750,6 +806,29 @@ void diff_change(unsigned old_mode, unsi\n \tchar concatpath[PATH_MAX];\n \tstruct diff_spec spec[2];\n \n+\tif (0 <= diff_raw_output) {\n+\t\tchar old_hex[41];\n+\t\tstrcpy(old_hex, sha1_to_hex(old_sha1));\n+\n+\t\tif (!path)\n+\t\t\tpath = \"\";\n+\t\tif (reverse_diff)\n+\t\t\tprintf(\"*%06o->%06o %s %s->%s %s%s%c\",\n+\t\t\t       new_mode, old_mode,\n+\t\t\t       git_object_type(new_mode),\n+\t\t\t       sha1_to_hex(new_sha1), old_hex,\n+\t\t\t       base, path, diff_raw_output);\n+\t\telse\n+\t\t\tprintf(\"*%06o->%06o %s %s->%s %s%s%c\",\n+\t\t\t       old_mode, new_mode,\n+\t\t\t       git_object_type(new_mode),\n+\t\t\t       old_hex, sha1_to_hex(new_sha1),\n+\t\t\t       base, path, diff_raw_output);\n+\t\treturn;\n+\t}\n+\tif (S_ISDIR(new_mode))\n+\t\treturn;\n+\n \tif (path) {\n \t\tstrcpy(concatpath, base);\n \t\tstrcat(concatpath, path);\n@@ -766,10 +845,15 @@ void diff_change(unsigned old_mode, unsi\n \t/* We do not look at changed files as candidate for\n \t * rename detection ever.\n \t */\n-\trun_external_diff(path ? concatpath : base, NULL, &spec[0], &spec[1]);\n+\trun_external_diff(path ? concatpath : base, NULL,\n+\t\t\t  &spec[0], &spec[1], -1);\n }\n \n void diff_unmerge(const char *path)\n {\n-\trun_external_diff(path, NULL, NULL, NULL);\n+\tif (0 <= diff_raw_output) {\n+\t\tprintf(\"U %s%c\", path, diff_raw_output);\n+\t\treturn;\n+\t}\n+\trun_external_diff(path, NULL, NULL, NULL, -1);\n }\ndiff --git a/diff.h b/diff.h\n--- a/diff.h\n+++ b/diff.h\n@@ -17,8 +17,10 @@ extern void diff_change(unsigned mode1, \n \n extern void diff_unmerge(const char *path);\n \n+extern int diff_scoreopt_parse(const char *opt);\n+\n extern void diff_setup(int detect_rename, int minimum_score,\n-\t\t       int reverse,\n+\t\t       int reverse, int raw_output,\n \t\t       const char **spec, int cnt);\n \n extern void diff_flush(void);\ndiff --git a/t/t0000-basic.sh b/t/t0000-basic.sh\n--- a/t/t0000-basic.sh\n+++ b/t/t0000-basic.sh\n@@ -166,7 +166,7 @@ cat >expected <<\\EOF\n EOF\n test_expect_success \\\n     'validate git-diff-files output for a know cache/work tree state.' \\\n-    'git-diff-files >current && cmp -s current expected'\n+    'git-diff-files >current && diff >/dev/null -b current expected'\n \n test_expect_success \\\n     'git-update-cache --refresh should succeed.' \\\ndiff --git a/t/t0110-environment-names-old.sh b/t/t0110-environment-names-old.sh\ndiff --git a/t/t1000-read-tree-m-3way.sh b/t/t1000-read-tree-m-3way.sh\ndiff --git a/t/t2002-checkout-cache-u.sh b/t/t2002-checkout-cache-u.sh\n--- a/t/t2002-checkout-cache-u.sh\n+++ b/t/t2002-checkout-cache-u.sh\n@@ -29,3 +29,5 @@ rm -f path0 &&\n git-read-tree $t &&\n git-checkout-cache -u -f -a &&\n git-diff-files | diff - /dev/null'\n+\n+test_done\ndiff --git a/t/t4000-diff-format.sh b/t/t4000-diff-format.sh\ndiff --git a/t/t4001-diff-rename.sh b/t/t4001-diff-rename.sh\n--- a/t/t4001-diff-rename.sh\n+++ b/t/t4001-diff-rename.sh\n@@ -58,3 +58,9 @@ rename new path1\n  Line 13\n  Line 14\n EOF\n+\n+test_expect_success \\\n+    'validate the output.' \\\n+    'diff -I \"rename similarity.*\" >/dev/null current expected'\n+\n+test_done\n------------------------------------------------\n\n"},{"id":"3597","messageId":"Pine.LNX.4.58.0505192229540.2206@ppc970.osdl.org","threadId":"667","inReplyTo":"7vll6awta3.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH] diff overhaul","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2005-05-20T05:30:34Z","receivedAt":"2005-05-20T05:30:34Z","isPatch":true,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\n\nOn Thu, 19 May 2005, Junio C Hamano wrote:\n> \n> This also allowed diff-cache and diff-files to acquire -R\n> (reverse) option to generate diff in reverse.  Users of\n> diff-tree can swap two trees easily so I did not add -R there.\n\nActually, diff-tree would want it too.\n\nYou can only swap the trees easily if you give two trees, not if you give \na commit.\n\n\t\tLinus\n"}]}