format-patch: make --base patch-id output stable

We weren't flushing the context each time we processed a hunk in the patch-id generation code in diff.c, but we were doing that when we generated "stable" patch-ids with the 'patch-id' tool. Let's port that similar logic over from patch-id.c into diff.c so we can get the same hash when we're generating patch-ids for 'format-patch --base=' types of command invocations. Cc: Xiaolong Ye <xiaolong.ye@intel.com> Signed-off-by: Stephen Boyd <sboyd@kernel.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stephen Boyd committed Apr 26, 2019 at 16:51 UTC a8f6855f48fd474719e92eecc66c29a28fdc0f46
8 files changed +42 -34
Documentation/git-format-patch.txt
+1 -1
@@ -583,7 +583,7 @@ of 'base commit' in topological order before the patches can be applied.
583 The 'base commit' is shown as "base-commit: " followed by the 40-hex of
584 the commit object name. A 'prerequisite patch' is shown as
585 "prerequisite-patch-id: " followed by the 40-hex 'patch id', which can
586 -be obtained by passing the patch through the `git patch-id --unstable`
586 +be obtained by passing the patch through the `git patch-id --stable`
587 command.
588
589 Imagine that on top of the public commit P, you applied well-known
builtin/log.c
+1 -1
@@ -1435,7 +1435,7 @@ static void prepare_bases(struct base_tree_info *bases,
1435 struct object_id *patch_id;
1436 if (*commit_base_at(&commit_base, commit))
1437 continue;
1438 - if (commit_patch_id(commit, &diffopt, &oid, 0))
1438 + if (commit_patch_id(commit, &diffopt, &oid, 0, 1))
1439 die(_("cannot get patch id"));
1440 ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id);
1441 patch_id = bases->patch_id + bases->nr_patch_id;
builtin/patch-id.c
+1 -16
@@ -1,5 +1,6 @@
1 #include "builtin.h"
2 #include "config.h"
3 +#include "diff.h"
4
5 static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
6 {
@@ -54,22 +55,6 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
55 return 1;
56 }
57
57 -static void flush_one_hunk(struct object_id *result, git_SHA_CTX *ctx)
58 -{
59 - unsigned char hash[GIT_MAX_RAWSZ];
60 - unsigned short carry = 0;
61 - int i;
62 -
63 - git_SHA1_Final(hash, ctx);
64 - git_SHA1_Init(ctx);
65 - /* 20-byte sum, with carry */
66 - for (i = 0; i < GIT_SHA1_RAWSZ; ++i) {
67 - carry += result->hash[i] + hash[i];
68 - result->hash[i] = carry;
69 - carry >>= 8;
70 - }
71 -}
72 -
58 static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
59 struct strbuf *line_buf, int stable)
60 {
diff.c
+27 -5
@@ -5988,6 +5988,22 @@ static int remove_space(char *line, int len)
5988 return dst - line;
5989 }
5990
5991 +void flush_one_hunk(struct object_id *result, git_SHA_CTX *ctx)
5992 +{
5993 + unsigned char hash[GIT_MAX_RAWSZ];
5994 + unsigned short carry = 0;
5995 + int i;
5996 +
5997 + git_SHA1_Final(hash, ctx);
5998 + git_SHA1_Init(ctx);
5999 + /* 20-byte sum, with carry */
6000 + for (i = 0; i < GIT_SHA1_RAWSZ; ++i) {
6001 + carry += result->hash[i] + hash[i];
6002 + result->hash[i] = carry;
6003 + carry >>= 8;
6004 + }
6005 +}
6006 +
6007 static void patch_id_consume(void *priv, char *line, unsigned long len)
6008 {
6009 struct patch_id_t *data = priv;
@@ -6012,8 +6028,8 @@ static void patch_id_add_mode(git_SHA_CTX *ctx, unsigned mode)
6028 git_SHA1_Update(ctx, buf, len);
6029 }
6030
6015 -/* returns 0 upon success, and writes result into sha1 */
6016 -static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
6031 +/* returns 0 upon success, and writes result into oid */
6032 +static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only, int stable)
6033 {
6034 struct diff_queue_struct *q = &diff_queued_diff;
6035 int i;
@@ -6023,6 +6039,7 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
6039 git_SHA1_Init(&ctx);
6040 memset(&data, 0, sizeof(struct patch_id_t));
6041 data.ctx = &ctx;
6042 + oidclr(oid);
6043
6044 for (i = 0; i < q->nr; i++) {
6045 xpparam_t xpp;
@@ -6098,17 +6115,22 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
6115 patch_id_consume, &data, &xpp, &xecfg))
6116 return error("unable to generate patch-id diff for %s",
6117 p->one->path);
6118 +
6119 + if (stable)
6120 + flush_one_hunk(oid, &ctx);
6121 }
6122
6103 - git_SHA1_Final(oid->hash, &ctx);
6123 + if (!stable)
6124 + git_SHA1_Final(oid->hash, &ctx);
6125 +
6126 return 0;
6127 }
6128
6107 -int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
6129 +int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only, int stable)
6130 {
6131 struct diff_queue_struct *q = &diff_queued_diff;
6132 int i;
6111 - int result = diff_get_patch_id(options, oid, diff_header_only);
6133 + int result = diff_get_patch_id(options, oid, diff_header_only, stable);
6134
6135 for (i = 0; i < q->nr; i++)
6136 diff_free_filepair(q->queue[i]);
diff.h
+2 -1
@@ -436,7 +436,8 @@ int run_diff_files(struct rev_info *revs, unsigned int option);
436 int run_diff_index(struct rev_info *revs, int cached);
437
438 int do_diff_cache(const struct object_id *, struct diff_options *);
439 -int diff_flush_patch_id(struct diff_options *, struct object_id *, int);
439 +int diff_flush_patch_id(struct diff_options *, struct object_id *, int, int);
440 +void flush_one_hunk(struct object_id *, git_SHA_CTX *);
441
442 int diff_result_code(struct diff_options *, int);
443
patch-ids.c
+5 -5
@@ -11,7 +11,7 @@ static int patch_id_defined(struct commit *commit)
11 }
12
13 int commit_patch_id(struct commit *commit, struct diff_options *options,
14 - struct object_id *oid, int diff_header_only)
14 + struct object_id *oid, int diff_header_only, int stable)
15 {
16 if (!patch_id_defined(commit))
17 return -1;
@@ -22,7 +22,7 @@ int commit_patch_id(struct commit *commit, struct diff_options *options,
22 else
23 diff_root_tree_oid(&commit->object.oid, "", options);
24 diffcore_std(options);
25 - return diff_flush_patch_id(options, oid, diff_header_only);
25 + return diff_flush_patch_id(options, oid, diff_header_only, stable);
26 }
27
28 /*
@@ -46,11 +46,11 @@ static int patch_id_neq(const void *cmpfn_data,
46 struct patch_id *b = (void *)entry_or_key;
47
48 if (is_null_oid(&a->patch_id) &&
49 - commit_patch_id(a->commit, opt, &a->patch_id, 0))
49 + commit_patch_id(a->commit, opt, &a->patch_id, 0, 0))
50 return error("Could not get patch ID for %s",
51 oid_to_hex(&a->commit->object.oid));
52 if (is_null_oid(&b->patch_id) &&
53 - commit_patch_id(b->commit, opt, &b->patch_id, 0))
53 + commit_patch_id(b->commit, opt, &b->patch_id, 0, 0))
54 return error("Could not get patch ID for %s",
55 oid_to_hex(&b->commit->object.oid));
56 return !oideq(&a->patch_id, &b->patch_id);
@@ -80,7 +80,7 @@ static int init_patch_id_entry(struct patch_id *patch,
80 struct object_id header_only_patch_id;
81
82 patch->commit = commit;
83 - if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1))
83 + if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1, 0))
84 return -1;
85
86 hashmap_entry_init(patch, sha1hash(header_only_patch_id.hash));
patch-ids.h
+1 -1
@@ -20,7 +20,7 @@ struct patch_ids {
20 };
21
22 int commit_patch_id(struct commit *commit, struct diff_options *options,
23 - struct object_id *oid, int);
23 + struct object_id *oid, int, int);
24 int init_patch_ids(struct repository *, struct patch_ids *);
25 int free_patch_ids(struct patch_ids *);
26 struct patch_id *add_commit_patch_id(struct commit *, struct patch_ids *);
t/t4014-format-patch.sh
+4 -4
@@ -1583,15 +1583,15 @@ test_expect_success 'format-patch --base' '
1583 git format-patch --stdout --base=HEAD~3 HEAD~.. | tail -n 7 >actual2 &&
1584 echo >expected &&
1585 echo "base-commit: $(git rev-parse HEAD~3)" >>expected &&
1586 - echo "prerequisite-patch-id: $(git show --patch HEAD~2 | git patch-id --unstable | awk "{print \$1}")" >>expected &&
1587 - echo "prerequisite-patch-id: $(git show --patch HEAD~1 | git patch-id --unstable | awk "{print \$1}")" >>expected &&
1586 + echo "prerequisite-patch-id: $(git show --patch HEAD~2 | git patch-id --stable | awk "{print \$1}")" >>expected &&
1587 + echo "prerequisite-patch-id: $(git show --patch HEAD~1 | git patch-id --stable | awk "{print \$1}")" >>expected &&
1588 signature >> expected &&
1589 test_cmp expected actual1 &&
1590 test_cmp expected actual2 &&
1591 echo >fail &&
1592 echo "base-commit: $(git rev-parse HEAD~3)" >>fail &&
1593 - echo "prerequisite-patch-id: $(git show --patch HEAD~2 | git patch-id --stable | awk "{print \$1}")" >>fail &&
1594 - echo "prerequisite-patch-id: $(git show --patch HEAD~1 | git patch-id --stable | awk "{print \$1}")" >>fail &&
1593 + echo "prerequisite-patch-id: $(git show --patch HEAD~2 | git patch-id --unstable | awk "{print \$1}")" >>fail &&
1594 + echo "prerequisite-patch-id: $(git show --patch HEAD~1 | git patch-id --unstable | awk "{print \$1}")" >>fail &&
1595 signature >> fail &&
1596 ! test_cmp fail actual1 &&
1597 ! test_cmp fail actual2