diff: round down similarity index
Rounding down the printed (dis)similarity index allows us to use "100%" as a special value that indicates complete rewrites and fully equal file contents, respectively. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Jun 25, 2007 at 00:23 UTC
125b7630521918b75c136bd95309054d3f6d9da5
2 files changed
+18
-11
Documentation/diff-format.txt
+7
@@ -126,6 +126,13 @@ the file that rename/copy produces, respectively.
126
If there is need for such substitution then the whole
127
pathname is put in double quotes.
128
129
+The similarity index is the percentage of unchanged lines, and
130
+the dissimilarity index is the percentage of changed lines. It
131
+is a rounded down integer, followed by a percent sign. The
132
+similarity index value of 100% is thus reserved for two equal
133
+files, while 100% dissimilarity means that no line from the old
134
+file made it into the new one.
135
+
136
137
combined diff format
138
--------------------
diff.c
+11
-11
@@ -1813,6 +1813,11 @@ static void diff_fill_sha1_info(struct diff_filespec *one)
1813
hashclr(one->sha1);
1814
}
1815
1816
+static int similarity_index(struct diff_filepair *p)
1817
+{
1818
+ return p->score * 100 / MAX_SCORE;
1819
+}
1820
+
1821
static void run_diff(struct diff_filepair *p, struct diff_options *o)
1822
{
1823
const char *pgm = external_diff();
@@ -1847,23 +1852,20 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
1852
"similarity index %d%%\n"
1853
"copy from %s\n"
1854
"copy to %s\n",
1850
- (int)(0.5 + p->score * 100.0/MAX_SCORE),
1851
- name_munged, other_munged);
1855
+ similarity_index(p), name_munged, other_munged);
1856
break;
1857
case DIFF_STATUS_RENAMED:
1858
len += snprintf(msg + len, sizeof(msg) - len,
1859
"similarity index %d%%\n"
1860
"rename from %s\n"
1861
"rename to %s\n",
1858
- (int)(0.5 + p->score * 100.0/MAX_SCORE),
1859
- name_munged, other_munged);
1862
+ similarity_index(p), name_munged, other_munged);
1863
break;
1864
case DIFF_STATUS_MODIFIED:
1865
if (p->score) {
1866
len += snprintf(msg + len, sizeof(msg) - len,
1867
"dissimilarity index %d%%\n",
1865
- (int)(0.5 + p->score *
1866
- 100.0/MAX_SCORE));
1868
+ similarity_index(p));
1869
complete_rewrite = 1;
1870
break;
1871
}
@@ -2387,8 +2389,7 @@ static void diff_flush_raw(struct diff_filepair *p,
2389
}
2390
2391
if (p->score)
2390
- sprintf(status, "%c%03d", p->status,
2391
- (int)(0.5 + p->score * 100.0/MAX_SCORE));
2392
+ sprintf(status, "%c%03d", p->status, similarity_index(p));
2393
else {
2394
status[0] = p->status;
2395
status[1] = 0;
@@ -2670,8 +2671,7 @@ static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2671
{
2672
char *names = pprint_rename(p->one->path, p->two->path);
2673
2673
- printf(" %s %s (%d%%)\n", renamecopy, names,
2674
- (int)(0.5 + p->score * 100.0/MAX_SCORE));
2674
+ printf(" %s %s (%d%%)\n", renamecopy, names, similarity_index(p));
2675
free(names);
2676
show_mode_change(p, 0);
2677
}
@@ -2695,7 +2695,7 @@ static void diff_summary(struct diff_filepair *p)
2695
if (p->score) {
2696
char *name = quote_one(p->two->path);
2697
printf(" rewrite %s (%d%%)\n", name,
2698
- (int)(0.5 + p->score * 100.0/MAX_SCORE));
2698
+ similarity_index(p));
2699
free(name);
2700
show_mode_change(p, 0);
2701
} else show_mode_change(p, 1);