Skip to content

Commit accd784

Browse files
committed
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>
1 parent 47dd665 commit accd784

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

src/diff.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ extern int git_diff_delta__format_file_header(
5757
const git_diff_delta *delta,
5858
const char *oldpfx,
5959
const char *newpfx,
60-
int oid_strlen);
60+
int oid_strlen,
61+
bool print_index);
6162

6263
extern int git_diff_delta__cmp(const void *a, const void *b);
6364
extern int git_diff_delta__casecmp(const void *a, const void *b);

src/diff_print.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ static int diff_print_modes(
269269
}
270270

271271
static int diff_print_oid_range(
272-
git_buf *out, const git_diff_delta *delta, int id_strlen)
272+
git_buf *out, const git_diff_delta *delta, int id_strlen,
273+
bool print_index)
273274
{
274275
char start_oid[GIT_OID_HEXSZ+1], end_oid[GIT_OID_HEXSZ+1];
275276

@@ -293,8 +294,9 @@ static int diff_print_oid_range(
293294
git_oid_tostr(end_oid, id_strlen + 1, &delta->new_file.id);
294295

295296
if (delta->old_file.mode == delta->new_file.mode) {
296-
git_buf_printf(out, "index %s..%s %o\n",
297-
start_oid, end_oid, delta->old_file.mode);
297+
if (print_index)
298+
git_buf_printf(out, "index %s..%s %o\n",
299+
start_oid, end_oid, delta->old_file.mode);
298300
} else {
299301
if (delta->old_file.mode == 0)
300302
git_buf_printf(out, "new file mode %o\n", delta->new_file.mode);
@@ -303,7 +305,8 @@ static int diff_print_oid_range(
303305
else
304306
diff_print_modes(out, delta);
305307

306-
git_buf_printf(out, "index %s..%s\n", start_oid, end_oid);
308+
if (print_index)
309+
git_buf_printf(out, "index %s..%s\n", start_oid, end_oid);
307310
}
308311

309312
return git_buf_oom(out) ? -1 : 0;
@@ -400,7 +403,8 @@ int git_diff_delta__format_file_header(
400403
const git_diff_delta *delta,
401404
const char *oldpfx,
402405
const char *newpfx,
403-
int id_strlen)
406+
int id_strlen,
407+
bool print_index)
404408
{
405409
git_buf old_path = GIT_BUF_INIT, new_path = GIT_BUF_INIT;
406410
bool unchanged = delta_is_unchanged(delta);
@@ -431,7 +435,8 @@ int git_diff_delta__format_file_header(
431435
}
432436

433437
if (!unchanged) {
434-
if ((error = diff_print_oid_range(out, delta, id_strlen)) < 0)
438+
if ((error = diff_print_oid_range(out, delta,
439+
id_strlen, print_index)) < 0)
435440
goto done;
436441

437442
if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
@@ -566,6 +571,7 @@ static int diff_print_patch_file(
566571
(pi->flags & GIT_DIFF_FORCE_BINARY);
567572
bool show_binary = !!(pi->flags & GIT_DIFF_SHOW_BINARY);
568573
int id_strlen = pi->id_strlen;
574+
bool print_index = true;
569575

570576
if (binary && show_binary)
571577
id_strlen = delta->old_file.id_abbrev ? delta->old_file.id_abbrev :
@@ -582,7 +588,8 @@ static int diff_print_patch_file(
582588
return 0;
583589

584590
if ((error = git_diff_delta__format_file_header(
585-
pi->buf, delta, oldpfx, newpfx, id_strlen)) < 0)
591+
pi->buf, delta, oldpfx, newpfx,
592+
id_strlen, print_index)) < 0)
586593
return error;
587594

588595
pi->line.origin = GIT_DIFF_LINE_FILE_HDR;

src/patch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ size_t git_patch_size(
7979
git_buf file_header = GIT_BUF_INIT;
8080

8181
if (git_diff_delta__format_file_header(
82-
&file_header, patch->delta, NULL, NULL, 0) < 0)
82+
&file_header, patch->delta, NULL, NULL, 0, true) < 0)
8383
git_error_clear();
8484
else
8585
out += git_buf_len(&file_header);

0 commit comments

Comments
 (0)