patch printing: include rename information
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
diff --git a/src/diff_print.c b/src/diff_print.c
index 29ffcdd..59f751c 100644
--- a/src/diff_print.c
+++ b/src/diff_print.c
@@ -322,6 +322,26 @@ static int diff_delta_format_with_paths(
return git_buf_printf(out, template, oldpfx, oldpath, newpfx, newpath);
}
+int diff_delta_format_rename_header(
+ git_buf *out,
+ const git_diff_delta *delta)
+{
+ if (delta->similarity > 100) {
+ giterr_set(GITERR_PATCH, "invalid similarity %d", delta->similarity);
+ return -1;
+ }
+
+ git_buf_printf(out,
+ "similarity index %d%%\n"
+ "rename from %s\n"
+ "rename to %s\n",
+ delta->similarity,
+ delta->old_file.path,
+ delta->new_file.path);
+
+ return git_buf_oom(out) ? -1 : 0;
+}
+
int git_diff_delta__format_file_header(
git_buf *out,
const git_diff_delta *delta,
@@ -341,6 +361,9 @@ int git_diff_delta__format_file_header(
git_buf_printf(out, "diff --git %s%s %s%s\n",
oldpfx, delta->old_file.path, newpfx, delta->new_file.path);
+ if (delta->status == GIT_DELTA_RENAMED)
+ GITERR_CHECK_ERROR(diff_delta_format_rename_header(out, delta));
+
GITERR_CHECK_ERROR(diff_print_oid_range(out, delta, oid_strlen));
if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
diff --git a/src/patch_parse.c b/src/patch_parse.c
index df2492e..aa767f3 100644
--- a/src/patch_parse.c
+++ b/src/patch_parse.c
@@ -306,7 +306,7 @@ static int parse_header_rename(
static int parse_header_renamefrom(
git_patch_parsed *patch, patch_parse_ctx *ctx)
{
- patch->base.delta->status |= GIT_DELTA_RENAMED;
+ patch->base.delta->status = GIT_DELTA_RENAMED;
return parse_header_rename(
(char **)&patch->base.delta->old_file.path,
@@ -317,7 +317,7 @@ static int parse_header_renamefrom(
static int parse_header_renameto(
git_patch_parsed *patch, patch_parse_ctx *ctx)
{
- patch->base.delta->status |= GIT_DELTA_RENAMED;
+ patch->base.delta->status = GIT_DELTA_RENAMED;
return parse_header_rename(
(char **)&patch->base.delta->new_file.path,