Skip to content

Commit

Permalink
diff: round down similarity index
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
René Scharfe authored and Junio C Hamano committed Jun 25, 2007
1 parent cfc0aef commit 125b763
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
7 changes: 7 additions & 0 deletions Documentation/diff-format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ the file that rename/copy produces, respectively.
If there is need for such substitution then the whole
pathname is put in double quotes.

The similarity index is the percentage of unchanged lines, and
the dissimilarity index is the percentage of changed lines. It
is a rounded down integer, followed by a percent sign. The
similarity index value of 100% is thus reserved for two equal
files, while 100% dissimilarity means that no line from the old
file made it into the new one.


combined diff format
--------------------
Expand Down
22 changes: 11 additions & 11 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,11 @@ static void diff_fill_sha1_info(struct diff_filespec *one)
hashclr(one->sha1);
}

static int similarity_index(struct diff_filepair *p)
{
return p->score * 100 / MAX_SCORE;
}

static void run_diff(struct diff_filepair *p, struct diff_options *o)
{
const char *pgm = external_diff();
Expand Down Expand Up @@ -1847,23 +1852,20 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
"similarity index %d%%\n"
"copy from %s\n"
"copy to %s\n",
(int)(0.5 + p->score * 100.0/MAX_SCORE),
name_munged, other_munged);
similarity_index(p), name_munged, other_munged);
break;
case DIFF_STATUS_RENAMED:
len += snprintf(msg + len, sizeof(msg) - len,
"similarity index %d%%\n"
"rename from %s\n"
"rename to %s\n",
(int)(0.5 + p->score * 100.0/MAX_SCORE),
name_munged, other_munged);
similarity_index(p), name_munged, other_munged);
break;
case DIFF_STATUS_MODIFIED:
if (p->score) {
len += snprintf(msg + len, sizeof(msg) - len,
"dissimilarity index %d%%\n",
(int)(0.5 + p->score *
100.0/MAX_SCORE));
similarity_index(p));
complete_rewrite = 1;
break;
}
Expand Down Expand Up @@ -2387,8 +2389,7 @@ static void diff_flush_raw(struct diff_filepair *p,
}

if (p->score)
sprintf(status, "%c%03d", p->status,
(int)(0.5 + p->score * 100.0/MAX_SCORE));
sprintf(status, "%c%03d", p->status, similarity_index(p));
else {
status[0] = p->status;
status[1] = 0;
Expand Down Expand Up @@ -2670,8 +2671,7 @@ static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
{
char *names = pprint_rename(p->one->path, p->two->path);

printf(" %s %s (%d%%)\n", renamecopy, names,
(int)(0.5 + p->score * 100.0/MAX_SCORE));
printf(" %s %s (%d%%)\n", renamecopy, names, similarity_index(p));
free(names);
show_mode_change(p, 0);
}
Expand All @@ -2695,7 +2695,7 @@ static void diff_summary(struct diff_filepair *p)
if (p->score) {
char *name = quote_one(p->two->path);
printf(" rewrite %s (%d%%)\n", name,
(int)(0.5 + p->score * 100.0/MAX_SCORE));
similarity_index(p));
free(name);
show_mode_change(p, 0);
} else show_mode_change(p, 1);
Expand Down

0 comments on commit 125b763

Please sign in to comment.