Commit accd7848 by Gregory Herrero

diff_print: add a new 'print_index' flag when printing diff.

Add a new 'print_index' flag to let the caller decide whether or not
'index <oid>..<oid>' should be printed.
Since patch id needs not to have index when hashing a patch, it will be
useful soon.

Signed-off-by: Gregory Herrero <gregory.herrero@oracle.com>
parent 47dd665a
......@@ -57,7 +57,8 @@ extern int git_diff_delta__format_file_header(
const git_diff_delta *delta,
const char *oldpfx,
const char *newpfx,
int oid_strlen);
int oid_strlen,
bool print_index);
extern int git_diff_delta__cmp(const void *a, const void *b);
extern int git_diff_delta__casecmp(const void *a, const void *b);
......
......@@ -269,7 +269,8 @@ static int diff_print_modes(
}
static int diff_print_oid_range(
git_buf *out, const git_diff_delta *delta, int id_strlen)
git_buf *out, const git_diff_delta *delta, int id_strlen,
bool print_index)
{
char start_oid[GIT_OID_HEXSZ+1], end_oid[GIT_OID_HEXSZ+1];
......@@ -293,8 +294,9 @@ static int diff_print_oid_range(
git_oid_tostr(end_oid, id_strlen + 1, &delta->new_file.id);
if (delta->old_file.mode == delta->new_file.mode) {
git_buf_printf(out, "index %s..%s %o\n",
start_oid, end_oid, delta->old_file.mode);
if (print_index)
git_buf_printf(out, "index %s..%s %o\n",
start_oid, end_oid, delta->old_file.mode);
} else {
if (delta->old_file.mode == 0)
git_buf_printf(out, "new file mode %o\n", delta->new_file.mode);
......@@ -303,7 +305,8 @@ static int diff_print_oid_range(
else
diff_print_modes(out, delta);
git_buf_printf(out, "index %s..%s\n", start_oid, end_oid);
if (print_index)
git_buf_printf(out, "index %s..%s\n", start_oid, end_oid);
}
return git_buf_oom(out) ? -1 : 0;
......@@ -400,7 +403,8 @@ int git_diff_delta__format_file_header(
const git_diff_delta *delta,
const char *oldpfx,
const char *newpfx,
int id_strlen)
int id_strlen,
bool print_index)
{
git_buf old_path = GIT_BUF_INIT, new_path = GIT_BUF_INIT;
bool unchanged = delta_is_unchanged(delta);
......@@ -431,7 +435,8 @@ int git_diff_delta__format_file_header(
}
if (!unchanged) {
if ((error = diff_print_oid_range(out, delta, id_strlen)) < 0)
if ((error = diff_print_oid_range(out, delta,
id_strlen, print_index)) < 0)
goto done;
if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
......@@ -566,6 +571,7 @@ static int diff_print_patch_file(
(pi->flags & GIT_DIFF_FORCE_BINARY);
bool show_binary = !!(pi->flags & GIT_DIFF_SHOW_BINARY);
int id_strlen = pi->id_strlen;
bool print_index = true;
if (binary && show_binary)
id_strlen = delta->old_file.id_abbrev ? delta->old_file.id_abbrev :
......@@ -582,7 +588,8 @@ static int diff_print_patch_file(
return 0;
if ((error = git_diff_delta__format_file_header(
pi->buf, delta, oldpfx, newpfx, id_strlen)) < 0)
pi->buf, delta, oldpfx, newpfx,
id_strlen, print_index)) < 0)
return error;
pi->line.origin = GIT_DIFF_LINE_FILE_HDR;
......
......@@ -79,7 +79,7 @@ size_t git_patch_size(
git_buf file_header = GIT_BUF_INIT;
if (git_diff_delta__format_file_header(
&file_header, patch->delta, NULL, NULL, 0) < 0)
&file_header, patch->delta, NULL, NULL, 0, true) < 0)
git_error_clear();
else
out += git_buf_len(&file_header);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment