Skip to content

Commit ece5bb5

Browse files
committed
diff: make patchid computation work with all types of commits.
Current implementation of patchid is not computing a correct patchid when given a patch where, for example, a new file is added or removed. Some more corner cases need to be handled to have same behavior as git patch-id command. Add some more tests to cover those corner cases. Signed-off-by: Gregory Herrero <gregory.herrero@oracle.com>
1 parent 048e94a commit ece5bb5

File tree

3 files changed

+86
-61
lines changed

3 files changed

+86
-61
lines changed

src/diff.c

Lines changed: 21 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -426,81 +426,38 @@ static void strip_spaces(git_buf *buf)
426426
git_buf_truncate(buf, len);
427427
}
428428

429-
static int file_cb(
429+
int git_diff_patchid_print_callback__to_buf(
430430
const git_diff_delta *delta,
431-
float progress,
431+
const git_diff_hunk *hunk,
432+
const git_diff_line *line,
432433
void *payload)
433434
{
434435
struct patch_id_args *args = (struct patch_id_args *) payload;
435436
git_buf buf = GIT_BUF_INIT;
436-
int error;
437-
438-
GIT_UNUSED(progress);
437+
int error = 0;
439438

440-
if (!args->first_file &&
441-
(error = flush_hunk(&args->result, &args->ctx)) < 0)
442-
goto out;
443-
args->first_file = 0;
444-
445-
if ((error = git_buf_printf(&buf,
446-
"diff--gita/%sb/%s---a/%s+++b/%s",
447-
delta->old_file.path,
448-
delta->new_file.path,
449-
delta->old_file.path,
450-
delta->new_file.path)) < 0)
439+
if (line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL ||
440+
line->origin == GIT_DIFF_LINE_ADD_EOFNL ||
441+
line->origin == GIT_DIFF_LINE_DEL_EOFNL)
451442
goto out;
452443

453-
strip_spaces(&buf);
454-
455-
if ((error = git_hash_update(&args->ctx, buf.ptr, buf.size)) < 0)
444+
if ((error = git_diff_print_callback__to_buf(delta, hunk,
445+
line, &buf)) < 0)
456446
goto out;
457447

458-
out:
459-
git_buf_dispose(&buf);
460-
return error;
461-
}
462-
463-
static int patchid_line_cb(
464-
const git_diff_delta *delta,
465-
const git_diff_hunk *hunk,
466-
const git_diff_line *line,
467-
void *payload)
468-
{
469-
struct patch_id_args *args = (struct patch_id_args *) payload;
470-
git_buf buf = GIT_BUF_INIT;
471-
int error;
472-
473-
GIT_UNUSED(delta);
474-
GIT_UNUSED(hunk);
475-
476-
switch (line->origin) {
477-
case GIT_DIFF_LINE_ADDITION:
478-
git_buf_putc(&buf, '+');
479-
break;
480-
case GIT_DIFF_LINE_DELETION:
481-
git_buf_putc(&buf, '-');
482-
break;
483-
case GIT_DIFF_LINE_CONTEXT:
484-
break;
485-
case GIT_DIFF_LINE_CONTEXT_EOFNL:
486-
case GIT_DIFF_LINE_ADD_EOFNL:
487-
case GIT_DIFF_LINE_DEL_EOFNL:
488-
/*
489-
* Ignore EOF without newlines for patch IDs as whitespace is
490-
* not supposed to be significant.
491-
*/
492-
return 0;
493-
default:
494-
git_error_set(GIT_ERROR_PATCH, "invalid line origin for patch");
495-
return -1;
496-
}
497-
498-
git_buf_put(&buf, line->content, line->content_len);
499448
strip_spaces(&buf);
500449

450+
if (line->origin == GIT_DIFF_LINE_FILE_HDR &&
451+
!args->first_file &&
452+
(error = flush_hunk(&args->result, &args->ctx) < 0))
453+
goto out;
454+
501455
if ((error = git_hash_update(&args->ctx, buf.ptr, buf.size)) < 0)
502456
goto out;
503457

458+
if (line->origin == GIT_DIFF_LINE_FILE_HDR && args->first_file)
459+
args->first_file = 0;
460+
504461
out:
505462
git_buf_dispose(&buf);
506463
return error;
@@ -526,7 +483,10 @@ int git_diff_patchid(git_oid *out, git_diff *diff, git_diff_patchid_options *opt
526483
if ((error = git_hash_ctx_init(&args.ctx)) < 0)
527484
goto out;
528485

529-
if ((error = git_diff_foreach(diff, file_cb, NULL, NULL, patchid_line_cb, &args)) < 0)
486+
if ((error = git_diff_print(diff,
487+
GIT_DIFF_FORMAT_PATCH_ID,
488+
git_diff_patchid_print_callback__to_buf,
489+
&args)) < 0)
530490
goto out;
531491

532492
if ((error = (flush_hunk(&args.result, &args.ctx))) < 0)

tests/diff/patchid.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,39 @@ void test_diff_patchid__simple_commit(void)
2020
verify_patch_id(PATCH_SIMPLE_COMMIT, "06094b1948b878b7d9ff7560b4eae672a014b0ec");
2121
}
2222

23+
void test_diff_patchid__deleted_file(void)
24+
{
25+
verify_patch_id(PATCH_DELETE_ORIGINAL, "d18507fe189f49c028b32c8c34e1ad98dd6a1aad");
26+
verify_patch_id(PATCH_DELETED_FILE_2_HUNKS, "f31412498a17e6c3fbc635f2c5f9aa3ef4c1a9b7");
27+
}
28+
29+
void test_diff_patchid__created_file(void)
30+
{
31+
verify_patch_id(PATCH_ADD_ORIGINAL, "a7d39379308021465ae2ce65e338c048a3110db6");
32+
}
33+
34+
void test_diff_patchid__binary_file(void)
35+
{
36+
verify_patch_id(PATCH_ADD_BINARY_NOT_PRINTED, "2b31236b485faa30cf4dd33e4d6539829996739f");
37+
}
38+
39+
void test_diff_patchid__renamed_file(void)
40+
{
41+
verify_patch_id(PATCH_RENAME_EXACT, "4666d50cea4976f6f727448046d43461912058fd");
42+
verify_patch_id(PATCH_RENAME_SIMILAR, "a795087575fcb940227be524488bedd6b3d3f438");
43+
}
44+
45+
void test_diff_patchid__modechange(void)
46+
{
47+
verify_patch_id(PATCH_MODECHANGE_UNCHANGED, "dbf3423ee98375ef1c72a79fbd29a049a2bae771");
48+
verify_patch_id(PATCH_MODECHANGE_MODIFIED, "93aba696e1bbd2bbb73e3e3e62ed71f232137657");
49+
}
50+
51+
void test_diff_patchid__shuffle_hunks(void)
52+
{
53+
verify_patch_id(PATCH_DELETED_FILE_2_HUNKS_SHUFFLED, "f31412498a17e6c3fbc635f2c5f9aa3ef4c1a9b7");
54+
}
55+
2356
void test_diff_patchid__filename_with_spaces(void)
2457
{
2558
verify_patch_id(PATCH_APPEND_NO_NL, "f0ba05413beaef743b630e796153839462ee477a");

tests/patch/patch_common.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,38 @@
385385
"@@ -9,0 +10 @@ below it!\n" \
386386
"+insert at end\n"
387387

388+
#define PATCH_DELETED_FILE_2_HUNKS \
389+
"diff --git a/a b/a\n" \
390+
"index 7f129fd..af431f2 100644\n" \
391+
"--- a/a\n" \
392+
"+++ b/a\n" \
393+
"@@ -1 +1 @@\n" \
394+
"-a contents 2\n" \
395+
"+a contents\n" \
396+
"diff --git a/c/d b/c/d\n" \
397+
"deleted file mode 100644\n" \
398+
"index 297efb8..0000000\n" \
399+
"--- a/c/d\n" \
400+
"+++ /dev/null\n" \
401+
"@@ -1 +0,0 @@\n" \
402+
"-c/d contents\n"
403+
404+
#define PATCH_DELETED_FILE_2_HUNKS_SHUFFLED \
405+
"diff --git a/c/d b/c/d\n" \
406+
"deleted file mode 100644\n" \
407+
"index 297efb8..0000000\n" \
408+
"--- a/c/d\n" \
409+
"+++ /dev/null\n" \
410+
"@@ -1 +0,0 @@\n" \
411+
"-c/d contents\n" \
412+
"diff --git a/a b/a\n" \
413+
"index 7f129fd..af431f2 100644\n" \
414+
"--- a/a\n" \
415+
"+++ b/a\n" \
416+
"@@ -1 +1 @@\n" \
417+
"-a contents 2\n" \
418+
"+a contents\n"
419+
388420
#define PATCH_SIMPLE_COMMIT \
389421
"commit 15e119375018fba121cf58e02a9f17fe22df0df8\n" \
390422
"Author: Edward Thomson <ethomson@edwardthomson.com>\n" \

0 commit comments

Comments
 (0)