builtin/merge: convert to struct object_id
Additionally convert several uses of the constant 40 into GIT_SHA1_HEXSZ. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Feb 21, 2017 at 23:47 UTC
52684310baadac2de07548af67993309d00ce0cf
1 file changed
+66
-68
builtin/merge.c
+66
-68
@@ -244,7 +244,7 @@ static void drop_save(void)
244
unlink(git_path_merge_mode());
245
}
246
247
-static int save_state(unsigned char *stash)
247
+static int save_state(struct object_id *stash)
248
{
249
int len;
250
struct child_process cp = CHILD_PROCESS_INIT;
@@ -265,7 +265,7 @@ static int save_state(unsigned char *stash)
265
else if (!len) /* no changes */
266
return -1;
267
strbuf_setlen(&buffer, buffer.len-1);
268
- if (get_sha1(buffer.buf, stash))
268
+ if (get_oid(buffer.buf, stash))
269
die(_("not a valid object: %s"), buffer.buf);
270
return 0;
271
}
@@ -305,18 +305,18 @@ static void reset_hard(unsigned const char *sha1, int verbose)
305
die(_("read-tree failed"));
306
}
307
308
-static void restore_state(const unsigned char *head,
309
- const unsigned char *stash)
308
+static void restore_state(const struct object_id *head,
309
+ const struct object_id *stash)
310
{
311
struct strbuf sb = STRBUF_INIT;
312
const char *args[] = { "stash", "apply", NULL, NULL };
313
314
- if (is_null_sha1(stash))
314
+ if (is_null_oid(stash))
315
return;
316
317
- reset_hard(head, 1);
317
+ reset_hard(head->hash, 1);
318
319
- args[2] = sha1_to_hex(stash);
319
+ args[2] = oid_to_hex(stash);
320
321
/*
322
* It is OK to ignore error here, for example when there was
@@ -376,10 +376,10 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
376
377
static void finish(struct commit *head_commit,
378
struct commit_list *remoteheads,
379
- const unsigned char *new_head, const char *msg)
379
+ const struct object_id *new_head, const char *msg)
380
{
381
struct strbuf reflog_message = STRBUF_INIT;
382
- const unsigned char *head = head_commit->object.oid.hash;
382
+ const struct object_id *head = &head_commit->object.oid;
383
384
if (!msg)
385
strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION"));
@@ -397,7 +397,7 @@ static void finish(struct commit *head_commit,
397
else {
398
const char *argv_gc_auto[] = { "gc", "--auto", NULL };
399
update_ref(reflog_message.buf, "HEAD",
400
- new_head, head, 0,
400
+ new_head->hash, head->hash, 0,
401
UPDATE_REFS_DIE_ON_ERR);
402
/*
403
* We ignore errors in 'gc --auto', since the
@@ -416,7 +416,7 @@ static void finish(struct commit *head_commit,
416
DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
417
opts.detect_rename = DIFF_DETECT_RENAME;
418
diff_setup_done(&opts);
419
- diff_tree_sha1(head, new_head, "", &opts);
419
+ diff_tree_sha1(head->hash, new_head->hash, "", &opts);
420
diffcore_std(&opts);
421
diff_flush(&opts);
422
}
@@ -431,7 +431,7 @@ static void finish(struct commit *head_commit,
431
static void merge_name(const char *remote, struct strbuf *msg)
432
{
433
struct commit *remote_head;
434
- unsigned char branch_head[20];
434
+ struct object_id branch_head;
435
struct strbuf buf = STRBUF_INIT;
436
struct strbuf bname = STRBUF_INIT;
437
const char *ptr;
@@ -441,25 +441,25 @@ static void merge_name(const char *remote, struct strbuf *msg)
441
strbuf_branchname(&bname, remote);
442
remote = bname.buf;
443
444
- memset(branch_head, 0, sizeof(branch_head));
444
+ oidclr(&branch_head);
445
remote_head = get_merge_parent(remote);
446
if (!remote_head)
447
die(_("'%s' does not point to a commit"), remote);
448
449
- if (dwim_ref(remote, strlen(remote), branch_head, &found_ref) > 0) {
449
+ if (dwim_ref(remote, strlen(remote), branch_head.hash, &found_ref) > 0) {
450
if (starts_with(found_ref, "refs/heads/")) {
451
strbuf_addf(msg, "%s\t\tbranch '%s' of .\n",
452
- sha1_to_hex(branch_head), remote);
452
+ oid_to_hex(&branch_head), remote);
453
goto cleanup;
454
}
455
if (starts_with(found_ref, "refs/tags/")) {
456
strbuf_addf(msg, "%s\t\ttag '%s' of .\n",
457
- sha1_to_hex(branch_head), remote);
457
+ oid_to_hex(&branch_head), remote);
458
goto cleanup;
459
}
460
if (starts_with(found_ref, "refs/remotes/")) {
461
strbuf_addf(msg, "%s\t\tremote-tracking branch '%s' of .\n",
462
- sha1_to_hex(branch_head), remote);
462
+ oid_to_hex(&branch_head), remote);
463
goto cleanup;
464
}
465
}
@@ -590,8 +590,8 @@ static int git_merge_config(const char *k, const char *v, void *cb)
590
return git_diff_ui_config(k, v, cb);
591
}
592
593
-static int read_tree_trivial(unsigned char *common, unsigned char *head,
594
- unsigned char *one)
593
+static int read_tree_trivial(struct object_id *common, struct object_id *head,
594
+ struct object_id *one)
595
{
596
int i, nr_trees = 0;
597
struct tree *trees[MAX_UNPACK_TREES];
@@ -606,13 +606,13 @@ static int read_tree_trivial(unsigned char *common, unsigned char *head,
606
opts.verbose_update = 1;
607
opts.trivial_merges_only = 1;
608
opts.merge = 1;
609
- trees[nr_trees] = parse_tree_indirect(common);
609
+ trees[nr_trees] = parse_tree_indirect(common->hash);
610
if (!trees[nr_trees++])
611
return -1;
612
- trees[nr_trees] = parse_tree_indirect(head);
612
+ trees[nr_trees] = parse_tree_indirect(head->hash);
613
if (!trees[nr_trees++])
614
return -1;
615
- trees[nr_trees] = parse_tree_indirect(one);
615
+ trees[nr_trees] = parse_tree_indirect(one->hash);
616
if (!trees[nr_trees++])
617
return -1;
618
opts.fn = threeway_merge;
@@ -626,9 +626,9 @@ static int read_tree_trivial(unsigned char *common, unsigned char *head,
626
return 0;
627
}
628
629
-static void write_tree_trivial(unsigned char *sha1)
629
+static void write_tree_trivial(struct object_id *oid)
630
{
631
- if (write_cache_as_tree(sha1, 0, NULL))
631
+ if (write_cache_as_tree(oid->hash, 0, NULL))
632
die(_("git write-tree failed to write a tree"));
633
}
634
@@ -781,7 +781,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
781
782
static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
783
{
784
- unsigned char result_tree[20], result_commit[20];
784
+ struct object_id result_tree, result_commit;
785
struct commit_list *parents, **pptr = &parents;
786
static struct lock_file lock;
787
@@ -792,15 +792,15 @@ static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
792
return error(_("Unable to write index."));
793
rollback_lock_file(&lock);
794
795
- write_tree_trivial(result_tree);
795
+ write_tree_trivial(&result_tree);
796
printf(_("Wonderful.\n"));
797
pptr = commit_list_append(head, pptr);
798
pptr = commit_list_append(remoteheads->item, pptr);
799
prepare_to_commit(remoteheads);
800
- if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents,
801
- result_commit, NULL, sign_commit))
800
+ if (commit_tree(merge_msg.buf, merge_msg.len, result_tree.hash, parents,
801
+ result_commit.hash, NULL, sign_commit))
802
die(_("failed to write commit object"));
803
- finish(head, remoteheads, result_commit, "In-index merge");
803
+ finish(head, remoteheads, &result_commit, "In-index merge");
804
drop_save();
805
return 0;
806
}
@@ -809,12 +809,12 @@ static int finish_automerge(struct commit *head,
809
int head_subsumed,
810
struct commit_list *common,
811
struct commit_list *remoteheads,
812
- unsigned char *result_tree,
812
+ struct object_id *result_tree,
813
const char *wt_strategy)
814
{
815
struct commit_list *parents = NULL;
816
struct strbuf buf = STRBUF_INIT;
817
- unsigned char result_commit[20];
817
+ struct object_id result_commit;
818
819
free_commit_list(common);
820
parents = remoteheads;
@@ -822,11 +822,11 @@ static int finish_automerge(struct commit *head,
822
commit_list_insert(head, &parents);
823
strbuf_addch(&merge_msg, '\n');
824
prepare_to_commit(remoteheads);
825
- if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents,
826
- result_commit, NULL, sign_commit))
825
+ if (commit_tree(merge_msg.buf, merge_msg.len, result_tree->hash, parents,
826
+ result_commit.hash, NULL, sign_commit))
827
die(_("failed to write commit object"));
828
strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy);
829
- finish(head, remoteheads, result_commit, buf.buf);
829
+ finish(head, remoteheads, &result_commit, buf.buf);
830
strbuf_release(&buf);
831
drop_save();
832
return 0;
@@ -854,18 +854,18 @@ static int suggest_conflicts(void)
854
}
855
856
static struct commit *is_old_style_invocation(int argc, const char **argv,
857
- const unsigned char *head)
857
+ const struct object_id *head)
858
{
859
struct commit *second_token = NULL;
860
if (argc > 2) {
861
- unsigned char second_sha1[20];
861
+ struct object_id second_oid;
862
863
- if (get_sha1(argv[1], second_sha1))
863
+ if (get_oid(argv[1], &second_oid))
864
return NULL;
865
- second_token = lookup_commit_reference_gently(second_sha1, 0);
865
+ second_token = lookup_commit_reference_gently(second_oid.hash, 0);
866
if (!second_token)
867
die(_("'%s' is not a commit"), argv[1]);
868
- if (hashcmp(second_token->object.oid.hash, head))
868
+ if (oidcmp(&second_token->object.oid, head))
869
return NULL;
870
}
871
return second_token;
@@ -1038,7 +1038,7 @@ static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge
1038
die_errno(_("could not close '%s'"), filename);
1039
1040
for (pos = 0; pos < merge_names->len; pos = npos) {
1041
- unsigned char sha1[20];
1041
+ struct object_id oid;
1042
char *ptr;
1043
struct commit *commit;
1044
@@ -1048,16 +1048,16 @@ static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge
1048
else
1049
npos = merge_names->len;
1050
1051
- if (npos - pos < 40 + 2 ||
1052
- get_sha1_hex(merge_names->buf + pos, sha1))
1051
+ if (npos - pos < GIT_SHA1_HEXSZ + 2 ||
1052
+ get_oid_hex(merge_names->buf + pos, &oid))
1053
commit = NULL; /* bad */
1054
- else if (memcmp(merge_names->buf + pos + 40, "\t\t", 2))
1054
+ else if (memcmp(merge_names->buf + pos + GIT_SHA1_HEXSZ, "\t\t", 2))
1055
continue; /* not-for-merge */
1056
else {
1057
- char saved = merge_names->buf[pos + 40];
1058
- merge_names->buf[pos + 40] = '\0';
1057
+ char saved = merge_names->buf[pos + GIT_SHA1_HEXSZ];
1058
+ merge_names->buf[pos + GIT_SHA1_HEXSZ] = '\0';
1059
commit = get_merge_parent(merge_names->buf + pos);
1060
- merge_names->buf[pos + 40] = saved;
1060
+ merge_names->buf[pos + GIT_SHA1_HEXSZ] = saved;
1061
}
1062
if (!commit) {
1063
if (ptr)
@@ -1117,9 +1117,7 @@ static struct commit_list *collect_parents(struct commit *head_commit,
1117
1118
int cmd_merge(int argc, const char **argv, const char *prefix)
1119
{
1120
- unsigned char result_tree[20];
1121
- unsigned char stash[20];
1122
- unsigned char head_sha1[20];
1120
+ struct object_id result_tree, stash, head_oid;
1121
struct commit *head_commit;
1122
struct strbuf buf = STRBUF_INIT;
1123
const char *head_arg;
@@ -1138,13 +1136,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1136
* Check if we are _not_ on a detached HEAD, i.e. if there is a
1137
* current branch.
1138
*/
1141
- branch = branch_to_free = resolve_refdup("HEAD", 0, head_sha1, NULL);
1139
+ branch = branch_to_free = resolve_refdup("HEAD", 0, head_oid.hash, NULL);
1140
if (branch && starts_with(branch, "refs/heads/"))
1141
branch += 11;
1144
- if (!branch || is_null_sha1(head_sha1))
1142
+ if (!branch || is_null_oid(&head_oid))
1143
head_commit = NULL;
1144
else
1147
- head_commit = lookup_commit_or_die(head_sha1, "HEAD");
1145
+ head_commit = lookup_commit_or_die(head_oid.hash, "HEAD");
1146
1147
init_diff_ui_defaults();
1148
git_config(git_merge_config, NULL);
@@ -1242,7 +1240,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1240
* to forbid "git merge" into a branch yet to be born.
1241
* We do the same for "git pull".
1242
*/
1245
- unsigned char *remote_head_sha1;
1243
+ struct object_id *remote_head_oid;
1244
if (squash)
1245
die(_("Squash commit into empty head not supported yet"));
1246
if (fast_forward == FF_NO)
@@ -1254,9 +1252,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1252
die(_("%s - not something we can merge"), argv[0]);
1253
if (remoteheads->next)
1254
die(_("Can merge only exactly one commit into empty head"));
1257
- remote_head_sha1 = remoteheads->item->object.oid.hash;
1258
- read_empty(remote_head_sha1, 0);
1259
- update_ref("initial pull", "HEAD", remote_head_sha1,
1255
+ remote_head_oid = &remoteheads->item->object.oid;
1256
+ read_empty(remote_head_oid->hash, 0);
1257
+ update_ref("initial pull", "HEAD", remote_head_oid->hash,
1258
NULL, 0, UPDATE_REFS_DIE_ON_ERR);
1259
goto done;
1260
}
@@ -1270,7 +1268,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1268
* additional safety measure to check for it.
1269
*/
1270
if (!have_message &&
1273
- is_old_style_invocation(argc, argv, head_commit->object.oid.hash)) {
1271
+ is_old_style_invocation(argc, argv, &head_commit->object.oid)) {
1272
warning("old-style 'git merge <msg> HEAD <commit>' is deprecated.");
1273
strbuf_addstr(&merge_msg, argv[0]);
1274
head_arg = argv[1];
@@ -1422,7 +1420,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1420
goto done;
1421
}
1422
1425
- finish(head_commit, remoteheads, commit->object.oid.hash, msg.buf);
1423
+ finish(head_commit, remoteheads, &commit->object.oid, msg.buf);
1424
drop_save();
1425
goto done;
1426
} else if (!remoteheads->next && common->next)
@@ -1441,9 +1439,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1439
/* See if it is really trivial. */
1440
git_committer_info(IDENT_STRICT);
1441
printf(_("Trying really trivial in-index merge...\n"));
1444
- if (!read_tree_trivial(common->item->object.oid.hash,
1445
- head_commit->object.oid.hash,
1446
- remoteheads->item->object.oid.hash)) {
1442
+ if (!read_tree_trivial(&common->item->object.oid,
1443
+ &head_commit->object.oid,
1444
+ &remoteheads->item->object.oid)) {
1445
ret = merge_trivial(head_commit, remoteheads);
1446
goto done;
1447
}
@@ -1495,14 +1493,14 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1493
/*
1494
* Stash away the local changes so that we can try more than one.
1495
*/
1498
- save_state(stash))
1499
- hashclr(stash);
1496
+ save_state(&stash))
1497
+ oidclr(&stash);
1498
1499
for (i = 0; i < use_strategies_nr; i++) {
1500
int ret;
1501
if (i) {
1502
printf(_("Rewinding the tree to pristine...\n"));
1505
- restore_state(head_commit->object.oid.hash, stash);
1503
+ restore_state(&head_commit->object.oid, &stash);
1504
}
1505
if (use_strategies_nr != 1)
1506
printf(_("Trying merge strategy %s...\n"),
@@ -1547,7 +1545,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1545
}
1546
1547
/* Automerge succeeded. */
1550
- write_tree_trivial(result_tree);
1548
+ write_tree_trivial(&result_tree);
1549
automerge_was_ok = 1;
1550
break;
1551
}
@@ -1559,7 +1557,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1557
if (automerge_was_ok) {
1558
ret = finish_automerge(head_commit, head_subsumed,
1559
common, remoteheads,
1562
- result_tree, wt_strategy);
1560
+ &result_tree, wt_strategy);
1561
goto done;
1562
}
1563
@@ -1568,7 +1566,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1566
* it up.
1567
*/
1568
if (!best_strategy) {
1571
- restore_state(head_commit->object.oid.hash, stash);
1569
+ restore_state(&head_commit->object.oid, &stash);
1570
if (use_strategies_nr > 1)
1571
fprintf(stderr,
1572
_("No merge strategy handled the merge.\n"));
@@ -1581,7 +1579,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1579
; /* We already have its result in the working tree. */
1580
else {
1581
printf(_("Rewinding the tree to pristine...\n"));
1584
- restore_state(head_commit->object.oid.hash, stash);
1582
+ restore_state(&head_commit->object.oid, &stash);
1583
printf(_("Using the %s to prepare resolving by hand.\n"),
1584
best_strategy);
1585
try_merge_strategy(best_strategy, common, remoteheads,