Skip to content

Commit a8f6855

Browse files
bebarinogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Stephen Boyd <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6f93d26 commit a8f6855

File tree

8 files changed

+42
-34
lines changed

8 files changed

+42
-34
lines changed

Documentation/git-format-patch.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ of 'base commit' in topological order before the patches can be applied.
583583
The 'base commit' is shown as "base-commit: " followed by the 40-hex of
584584
the commit object name. A 'prerequisite patch' is shown as
585585
"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`
587587
command.
588588

589589
Imagine that on top of the public commit P, you applied well-known

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ static void prepare_bases(struct base_tree_info *bases,
14351435
struct object_id *patch_id;
14361436
if (*commit_base_at(&commit_base, commit))
14371437
continue;
1438-
if (commit_patch_id(commit, &diffopt, &oid, 0))
1438+
if (commit_patch_id(commit, &diffopt, &oid, 0, 1))
14391439
die(_("cannot get patch id"));
14401440
ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id);
14411441
patch_id = bases->patch_id + bases->nr_patch_id;

builtin/patch-id.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "builtin.h"
22
#include "config.h"
3+
#include "diff.h"
34

45
static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
56
{
@@ -54,22 +55,6 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
5455
return 1;
5556
}
5657

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-
7358
static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
7459
struct strbuf *line_buf, int stable)
7560
{

diff.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5988,6 +5988,22 @@ static int remove_space(char *line, int len)
59885988
return dst - line;
59895989
}
59905990

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+
59916007
static void patch_id_consume(void *priv, char *line, unsigned long len)
59926008
{
59936009
struct patch_id_t *data = priv;
@@ -6012,8 +6028,8 @@ static void patch_id_add_mode(git_SHA_CTX *ctx, unsigned mode)
60126028
git_SHA1_Update(ctx, buf, len);
60136029
}
60146030

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)
60176033
{
60186034
struct diff_queue_struct *q = &diff_queued_diff;
60196035
int i;
@@ -6023,6 +6039,7 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
60236039
git_SHA1_Init(&ctx);
60246040
memset(&data, 0, sizeof(struct patch_id_t));
60256041
data.ctx = &ctx;
6042+
oidclr(oid);
60266043

60276044
for (i = 0; i < q->nr; i++) {
60286045
xpparam_t xpp;
@@ -6098,17 +6115,22 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
60986115
patch_id_consume, &data, &xpp, &xecfg))
60996116
return error("unable to generate patch-id diff for %s",
61006117
p->one->path);
6118+
6119+
if (stable)
6120+
flush_one_hunk(oid, &ctx);
61016121
}
61026122

6103-
git_SHA1_Final(oid->hash, &ctx);
6123+
if (!stable)
6124+
git_SHA1_Final(oid->hash, &ctx);
6125+
61046126
return 0;
61056127
}
61066128

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)
61086130
{
61096131
struct diff_queue_struct *q = &diff_queued_diff;
61106132
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);
61126134

61136135
for (i = 0; i < q->nr; i++)
61146136
diff_free_filepair(q->queue[i]);

diff.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,8 @@ int run_diff_files(struct rev_info *revs, unsigned int option);
436436
int run_diff_index(struct rev_info *revs, int cached);
437437

438438
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 *);
440441

441442
int diff_result_code(struct diff_options *, int);
442443

patch-ids.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static int patch_id_defined(struct commit *commit)
1111
}
1212

1313
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)
1515
{
1616
if (!patch_id_defined(commit))
1717
return -1;
@@ -22,7 +22,7 @@ int commit_patch_id(struct commit *commit, struct diff_options *options,
2222
else
2323
diff_root_tree_oid(&commit->object.oid, "", options);
2424
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);
2626
}
2727

2828
/*
@@ -46,11 +46,11 @@ static int patch_id_neq(const void *cmpfn_data,
4646
struct patch_id *b = (void *)entry_or_key;
4747

4848
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))
5050
return error("Could not get patch ID for %s",
5151
oid_to_hex(&a->commit->object.oid));
5252
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))
5454
return error("Could not get patch ID for %s",
5555
oid_to_hex(&b->commit->object.oid));
5656
return !oideq(&a->patch_id, &b->patch_id);
@@ -80,7 +80,7 @@ static int init_patch_id_entry(struct patch_id *patch,
8080
struct object_id header_only_patch_id;
8181

8282
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))
8484
return -1;
8585

8686
hashmap_entry_init(patch, sha1hash(header_only_patch_id.hash));

patch-ids.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct patch_ids {
2020
};
2121

2222
int commit_patch_id(struct commit *commit, struct diff_options *options,
23-
struct object_id *oid, int);
23+
struct object_id *oid, int, int);
2424
int init_patch_ids(struct repository *, struct patch_ids *);
2525
int free_patch_ids(struct patch_ids *);
2626
struct patch_id *add_commit_patch_id(struct commit *, struct patch_ids *);

t/t4014-format-patch.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,15 +1583,15 @@ test_expect_success 'format-patch --base' '
15831583
git format-patch --stdout --base=HEAD~3 HEAD~.. | tail -n 7 >actual2 &&
15841584
echo >expected &&
15851585
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 &&
15881588
signature >> expected &&
15891589
test_cmp expected actual1 &&
15901590
test_cmp expected actual2 &&
15911591
echo >fail &&
15921592
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 &&
15951595
signature >> fail &&
15961596
! test_cmp fail actual1 &&
15971597
! test_cmp fail actual2

0 commit comments

Comments
 (0)