Skip to content

Commit 8202d12

Browse files
committed
Merge branch 'sb/format-patch-base-patch-id-fix'
The "--base" option of "format-patch" computed the patch-ids for prerequisite patches in an unstable way, which has been updated to compute in a way that is compatible with "git patch-id --stable". * sb/format-patch-base-patch-id-fix: format-patch: make --base patch-id output stable format-patch: inform user that patch-id generation is unstable
2 parents cf3269f + a8f6855 commit 8202d12

File tree

7 files changed

+66
-32
lines changed

7 files changed

+66
-32
lines changed

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
@@ -5990,6 +5990,22 @@ static int remove_space(char *line, int len)
59905990
return dst - line;
59915991
}
59925992

5993+
void flush_one_hunk(struct object_id *result, git_SHA_CTX *ctx)
5994+
{
5995+
unsigned char hash[GIT_MAX_RAWSZ];
5996+
unsigned short carry = 0;
5997+
int i;
5998+
5999+
git_SHA1_Final(hash, ctx);
6000+
git_SHA1_Init(ctx);
6001+
/* 20-byte sum, with carry */
6002+
for (i = 0; i < GIT_SHA1_RAWSZ; ++i) {
6003+
carry += result->hash[i] + hash[i];
6004+
result->hash[i] = carry;
6005+
carry >>= 8;
6006+
}
6007+
}
6008+
59936009
static void patch_id_consume(void *priv, char *line, unsigned long len)
59946010
{
59956011
struct patch_id_t *data = priv;
@@ -6014,8 +6030,8 @@ static void patch_id_add_mode(git_SHA_CTX *ctx, unsigned mode)
60146030
git_SHA1_Update(ctx, buf, len);
60156031
}
60166032

6017-
/* returns 0 upon success, and writes result into sha1 */
6018-
static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
6033+
/* returns 0 upon success, and writes result into oid */
6034+
static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only, int stable)
60196035
{
60206036
struct diff_queue_struct *q = &diff_queued_diff;
60216037
int i;
@@ -6025,6 +6041,7 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
60256041
git_SHA1_Init(&ctx);
60266042
memset(&data, 0, sizeof(struct patch_id_t));
60276043
data.ctx = &ctx;
6044+
oidclr(oid);
60286045

60296046
for (i = 0; i < q->nr; i++) {
60306047
xpparam_t xpp;
@@ -6100,17 +6117,22 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
61006117
patch_id_consume, &data, &xpp, &xecfg))
61016118
return error("unable to generate patch-id diff for %s",
61026119
p->one->path);
6120+
6121+
if (stable)
6122+
flush_one_hunk(oid, &ctx);
61036123
}
61046124

6105-
git_SHA1_Final(oid->hash, &ctx);
6125+
if (!stable)
6126+
git_SHA1_Final(oid->hash, &ctx);
6127+
61066128
return 0;
61076129
}
61086130

6109-
int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
6131+
int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only, int stable)
61106132
{
61116133
struct diff_queue_struct *q = &diff_queued_diff;
61126134
int i;
6113-
int result = diff_get_patch_id(options, oid, diff_header_only);
6135+
int result = diff_get_patch_id(options, oid, diff_header_only, stable);
61146136

61156137
for (i = 0; i < q->nr; i++)
61166138
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: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,27 @@ test_expect_success setup '
3636
git checkout master &&
3737
git diff-tree -p C2 | git apply --index &&
3838
test_tick &&
39-
git commit -m "Master accepts moral equivalent of #2"
39+
git commit -m "Master accepts moral equivalent of #2" &&
4040
41+
git checkout side &&
42+
git checkout -b patchid &&
43+
for i in 5 6 1 2 3 A 4 B C 7 8 9 10 D E F; do echo "$i"; done >file2 &&
44+
for i in 1 2 3 A 4 B C 7 8 9 10 D E F 5 6; do echo "$i"; done >file3 &&
45+
for i in 8 9 10; do echo "$i"; done >file &&
46+
git add file file2 file3 &&
47+
test_tick &&
48+
git commit -m "patchid 1" &&
49+
for i in 4 A B 7 8 9 10; do echo "$i"; done >file2 &&
50+
for i in 8 9 10 5 6; do echo "$i"; done >file3 &&
51+
git add file2 file3 &&
52+
test_tick &&
53+
git commit -m "patchid 2" &&
54+
for i in 10 5 6; do echo "$i"; done >file &&
55+
git add file &&
56+
test_tick &&
57+
git commit -m "patchid 3" &&
58+
59+
git checkout master
4160
'
4261

4362
test_expect_success "format-patch --ignore-if-in-upstream" '
@@ -1559,7 +1578,7 @@ test_expect_success 'format-patch -o overrides format.outputDirectory' '
15591578
'
15601579

15611580
test_expect_success 'format-patch --base' '
1562-
git checkout side &&
1581+
git checkout patchid &&
15631582
git format-patch --stdout --base=HEAD~3 -1 | tail -n 7 >actual1 &&
15641583
git format-patch --stdout --base=HEAD~3 HEAD~.. | tail -n 7 >actual2 &&
15651584
echo >expected &&
@@ -1568,7 +1587,14 @@ test_expect_success 'format-patch --base' '
15681587
echo "prerequisite-patch-id: $(git show --patch HEAD~1 | git patch-id --stable | awk "{print \$1}")" >>expected &&
15691588
signature >> expected &&
15701589
test_cmp expected actual1 &&
1571-
test_cmp expected actual2
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 --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
15721598
'
15731599

15741600
test_expect_success 'format-patch --base errors out when base commit is in revision list' '

0 commit comments

Comments
 (0)