refs: convert update_ref and refs_update_ref to use struct object_id
Convert update_ref, refs_update_ref, and write_pseudoref to use struct object_id. Update the existing callers as well. Remove update_ref_oid, as it is no longer needed. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Oct 15, 2017 at 22:06 UTC
ae077771b09fac4d663e3f8c039318a97eb3a15b
17 files changed
+65
-74
bisect.c
+3
-2
@@ -685,11 +685,12 @@ static int bisect_checkout(const struct object_id *bisect_rev, int no_checkout)
685
char bisect_rev_hex[GIT_MAX_HEXSZ + 1];
686
687
memcpy(bisect_rev_hex, oid_to_hex(bisect_rev), GIT_SHA1_HEXSZ + 1);
688
- update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev->hash, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
688
+ update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
689
690
argv_checkout[2] = bisect_rev_hex;
691
if (no_checkout) {
692
- update_ref(NULL, "BISECT_HEAD", bisect_rev->hash, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
692
+ update_ref(NULL, "BISECT_HEAD", bisect_rev, NULL, 0,
693
+ UPDATE_REFS_DIE_ON_ERR);
694
} else {
695
int res;
696
res = run_command_v_opt(argv_checkout, RUN_GIT_CMD);
builtin/am.c
+7
-7
@@ -1068,8 +1068,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
1068
if (!get_oid("HEAD", &curr_head)) {
1069
write_state_text(state, "abort-safety", oid_to_hex(&curr_head));
1070
if (!state->rebasing)
1071
- update_ref_oid("am", "ORIG_HEAD", &curr_head, NULL, 0,
1072
- UPDATE_REFS_DIE_ON_ERR);
1071
+ update_ref("am", "ORIG_HEAD", &curr_head, NULL, 0,
1072
+ UPDATE_REFS_DIE_ON_ERR);
1073
} else {
1074
write_state_text(state, "abort-safety", "");
1075
if (!state->rebasing)
@@ -1686,8 +1686,8 @@ static void do_commit(const struct am_state *state)
1686
strbuf_addf(&sb, "%s: %.*s", reflog_msg, linelen(state->msg),
1687
state->msg);
1688
1689
- update_ref_oid(sb.buf, "HEAD", &commit, old_oid, 0,
1690
- UPDATE_REFS_DIE_ON_ERR);
1689
+ update_ref(sb.buf, "HEAD", &commit, old_oid, 0,
1690
+ UPDATE_REFS_DIE_ON_ERR);
1691
1692
if (state->rebasing) {
1693
FILE *fp = xfopen(am_path(state, "rewritten"), "a");
@@ -2147,9 +2147,9 @@ static void am_abort(struct am_state *state)
2147
clean_index(&curr_head, &orig_head);
2148
2149
if (has_orig_head)
2150
- update_ref_oid("am --abort", "HEAD", &orig_head,
2151
- has_curr_head ? &curr_head : NULL, 0,
2152
- UPDATE_REFS_DIE_ON_ERR);
2150
+ update_ref("am --abort", "HEAD", &orig_head,
2151
+ has_curr_head ? &curr_head : NULL, 0,
2152
+ UPDATE_REFS_DIE_ON_ERR);
2153
else if (curr_branch)
2154
delete_ref(NULL, curr_branch, NULL, REF_NODEREF);
2155
builtin/checkout.c
+1
-1
@@ -664,7 +664,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
664
if (!strcmp(new->name, "HEAD") && !new->path && !opts->force_detach) {
665
/* Nothing to do. */
666
} else if (opts->force_detach || !new->path) { /* No longer on any branch. */
667
- update_ref(msg.buf, "HEAD", new->commit->object.oid.hash, NULL,
667
+ update_ref(msg.buf, "HEAD", &new->commit->object.oid, NULL,
668
REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
669
if (!opts->quiet) {
670
if (old->path &&
builtin/clone.c
+7
-7
@@ -610,8 +610,8 @@ static void write_followtags(const struct ref *refs, const char *msg)
610
continue;
611
if (!has_object_file(&ref->old_oid))
612
continue;
613
- update_ref(msg, ref->name, ref->old_oid.hash,
614
- NULL, 0, UPDATE_REFS_DIE_ON_ERR);
613
+ update_ref(msg, ref->name, &ref->old_oid, NULL, 0,
614
+ UPDATE_REFS_DIE_ON_ERR);
615
}
616
}
617
@@ -682,23 +682,23 @@ static void update_head(const struct ref *our, const struct ref *remote,
682
if (create_symref("HEAD", our->name, NULL) < 0)
683
die(_("unable to update HEAD"));
684
if (!option_bare) {
685
- update_ref(msg, "HEAD", our->old_oid.hash, NULL, 0,
685
+ update_ref(msg, "HEAD", &our->old_oid, NULL, 0,
686
UPDATE_REFS_DIE_ON_ERR);
687
install_branch_config(0, head, option_origin, our->name);
688
}
689
} else if (our) {
690
struct commit *c = lookup_commit_reference(&our->old_oid);
691
/* --branch specifies a non-branch (i.e. tags), detach HEAD */
692
- update_ref(msg, "HEAD", c->object.oid.hash,
693
- NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
692
+ update_ref(msg, "HEAD", &c->object.oid, NULL, REF_NODEREF,
693
+ UPDATE_REFS_DIE_ON_ERR);
694
} else if (remote) {
695
/*
696
* We know remote HEAD points to a non-branch, or
697
* HEAD points to a branch but we don't know which one.
698
* Detach HEAD in all these cases.
699
*/
700
- update_ref(msg, "HEAD", remote->old_oid.hash,
701
- NULL, REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
700
+ update_ref(msg, "HEAD", &remote->old_oid, NULL, REF_NODEREF,
701
+ UPDATE_REFS_DIE_ON_ERR);
702
}
703
}
704
builtin/merge.c
+6
-7
@@ -405,9 +405,8 @@ static void finish(struct commit *head_commit,
405
printf(_("No merge message -- not updating HEAD\n"));
406
else {
407
const char *argv_gc_auto[] = { "gc", "--auto", NULL };
408
- update_ref(reflog_message.buf, "HEAD",
409
- new_head->hash, head->hash, 0,
410
- UPDATE_REFS_DIE_ON_ERR);
408
+ update_ref(reflog_message.buf, "HEAD", new_head, head,
409
+ 0, UPDATE_REFS_DIE_ON_ERR);
410
/*
411
* We ignore errors in 'gc --auto', since the
412
* user should see them.
@@ -1261,8 +1260,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1260
die(_("Can merge only exactly one commit into empty head"));
1261
remote_head_oid = &remoteheads->item->object.oid;
1262
read_empty(remote_head_oid->hash, 0);
1264
- update_ref("initial pull", "HEAD", remote_head_oid->hash,
1265
- NULL, 0, UPDATE_REFS_DIE_ON_ERR);
1263
+ update_ref("initial pull", "HEAD", remote_head_oid, NULL, 0,
1264
+ UPDATE_REFS_DIE_ON_ERR);
1265
goto done;
1266
}
1267
@@ -1357,8 +1356,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1356
free(list);
1357
}
1358
1360
- update_ref("updating ORIG_HEAD", "ORIG_HEAD", head_commit->object.oid.hash,
1361
- NULL, 0, UPDATE_REFS_DIE_ON_ERR);
1359
+ update_ref("updating ORIG_HEAD", "ORIG_HEAD",
1360
+ &head_commit->object.oid, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
1361
1362
if (remoteheads && !common) {
1363
/* No common ancestors found. */
builtin/notes.c
+5
-5
@@ -736,8 +736,8 @@ static int merge_commit(struct notes_merge_options *o)
736
format_commit_message(partial, "%s", &msg, &pretty_ctx);
737
strbuf_trim(&msg);
738
strbuf_insert(&msg, 0, "notes: ", 7);
739
- update_ref(msg.buf, o->local_ref, oid.hash,
740
- is_null_oid(&parent_oid) ? NULL : parent_oid.hash,
739
+ update_ref(msg.buf, o->local_ref, &oid,
740
+ is_null_oid(&parent_oid) ? NULL : &parent_oid,
741
0, UPDATE_REFS_DIE_ON_ERR);
742
743
free_notes(t);
@@ -850,12 +850,12 @@ static int merge(int argc, const char **argv, const char *prefix)
850
851
if (result >= 0) /* Merge resulted (trivially) in result_oid */
852
/* Update default notes ref with new commit */
853
- update_ref(msg.buf, default_notes_ref(), result_oid.hash, NULL,
854
- 0, UPDATE_REFS_DIE_ON_ERR);
853
+ update_ref(msg.buf, default_notes_ref(), &result_oid, NULL, 0,
854
+ UPDATE_REFS_DIE_ON_ERR);
855
else { /* Merge has unresolved conflicts */
856
const struct worktree *wt;
857
/* Update .git/NOTES_MERGE_PARTIAL with partial merge result */
858
- update_ref(msg.buf, "NOTES_MERGE_PARTIAL", result_oid.hash, NULL,
858
+ update_ref(msg.buf, "NOTES_MERGE_PARTIAL", &result_oid, NULL,
859
0, UPDATE_REFS_DIE_ON_ERR);
860
/* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
861
wt = find_shared_symref("NOTES_MERGE_REF", default_notes_ref());
builtin/pull.c
+1
-1
@@ -544,7 +544,7 @@ static int pull_into_void(const struct object_id *merge_head,
544
if (checkout_fast_forward(&empty_tree_oid, merge_head, 0))
545
return 1;
546
547
- if (update_ref("initial pull", "HEAD", merge_head->hash, curr_head->hash, 0, UPDATE_REFS_DIE_ON_ERR))
547
+ if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR))
548
return 1;
549
550
return 0;
builtin/reset.c
+2
-2
@@ -266,12 +266,12 @@ static int reset_refs(const char *rev, const struct object_id *oid)
266
if (!get_oid("HEAD", &oid_orig)) {
267
orig = &oid_orig;
268
set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
269
- update_ref_oid(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
269
+ update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
270
UPDATE_REFS_MSG_ON_ERR);
271
} else if (old_orig)
272
delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
273
set_reflog_message(&msg, "updating HEAD", rev);
274
- update_ref_status = update_ref_oid(msg.buf, "HEAD", oid, orig, 0,
274
+ update_ref_status = update_ref(msg.buf, "HEAD", oid, orig, 0,
275
UPDATE_REFS_MSG_ON_ERR);
276
strbuf_release(&msg);
277
return update_ref_status;
builtin/update-ref.c
+1
-1
@@ -437,7 +437,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
437
(oldval && !is_null_oid(&oldoid)) ? &oldoid : NULL,
438
flags);
439
else
440
- return update_ref(msg, refname, oid.hash, oldval ? oldoid.hash : NULL,
440
+ return update_ref(msg, refname, &oid, oldval ? &oldoid : NULL,
441
flags | create_reflog_flag,
442
UPDATE_REFS_DIE_ON_ERR);
443
}
notes-cache.c
+1
-1
@@ -59,7 +59,7 @@ int notes_cache_write(struct notes_cache *c)
59
if (commit_tree(c->validity, strlen(c->validity), tree_oid.hash, NULL,
60
commit_oid.hash, NULL, NULL) < 0)
61
return -1;
62
- if (update_ref("update notes cache", c->tree.update_ref, commit_oid.hash,
62
+ if (update_ref("update notes cache", c->tree.update_ref, &commit_oid,
63
NULL, 0, UPDATE_REFS_QUIET_ON_ERR) < 0)
64
return -1;
65
notes-utils.c
+1
-1
@@ -49,7 +49,7 @@ void commit_notes(struct notes_tree *t, const char *msg)
49
50
create_notes_commit(t, NULL, buf.buf, buf.len, commit_oid.hash);
51
strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
52
- update_ref(buf.buf, t->update_ref, commit_oid.hash, NULL, 0,
52
+ update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0,
53
UPDATE_REFS_DIE_ON_ERR);
54
55
strbuf_release(&buf);
refs.c
+16
-23
@@ -574,8 +574,8 @@ long get_files_ref_lock_timeout_ms(void)
574
return timeout_ms;
575
}
576
577
-static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
578
- const unsigned char *old_sha1, struct strbuf *err)
577
+static int write_pseudoref(const char *pseudoref, const struct object_id *oid,
578
+ const struct object_id *old_oid, struct strbuf *err)
579
{
580
const char *filename;
581
int fd;
@@ -583,7 +583,7 @@ static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
583
struct strbuf buf = STRBUF_INIT;
584
int ret = -1;
585
586
- strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1));
586
+ strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
587
588
filename = git_path("%s", pseudoref);
589
fd = hold_lock_file_for_update_timeout(&lock, filename,
@@ -595,12 +595,12 @@ static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
595
goto done;
596
}
597
598
- if (old_sha1) {
599
- unsigned char actual_old_sha1[20];
598
+ if (old_oid) {
599
+ struct object_id actual_old_oid;
600
601
- if (read_ref(pseudoref, actual_old_sha1))
601
+ if (read_ref(pseudoref, actual_old_oid.hash))
602
die("could not read ref '%s'", pseudoref);
603
- if (hashcmp(actual_old_sha1, old_sha1)) {
603
+ if (oidcmp(&actual_old_oid, old_oid)) {
604
strbuf_addf(err, "unexpected sha1 when writing '%s'", pseudoref);
605
rollback_lock_file(&lock);
606
goto done;
@@ -985,17 +985,9 @@ int ref_transaction_verify(struct ref_transaction *transaction,
985
flags, NULL, err);
986
}
987
988
-int update_ref_oid(const char *msg, const char *refname,
989
- const struct object_id *new_oid, const struct object_id *old_oid,
990
- unsigned int flags, enum action_on_err onerr)
991
-{
992
- return update_ref(msg, refname, new_oid ? new_oid->hash : NULL,
993
- old_oid ? old_oid->hash : NULL, flags, onerr);
994
-}
995
-
988
int refs_update_ref(struct ref_store *refs, const char *msg,
997
- const char *refname, const unsigned char *new_sha1,
998
- const unsigned char *old_sha1, unsigned int flags,
989
+ const char *refname, const struct object_id *new_oid,
990
+ const struct object_id *old_oid, unsigned int flags,
991
enum action_on_err onerr)
992
{
993
struct ref_transaction *t = NULL;
@@ -1004,11 +996,12 @@ int refs_update_ref(struct ref_store *refs, const char *msg,
996
997
if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
998
assert(refs == get_main_ref_store());
1007
- ret = write_pseudoref(refname, new_sha1, old_sha1, &err);
999
+ ret = write_pseudoref(refname, new_oid, old_oid, &err);
1000
} else {
1001
t = ref_store_transaction_begin(refs, &err);
1002
if (!t ||
1011
- ref_transaction_update(t, refname, new_sha1, old_sha1,
1003
+ ref_transaction_update(t, refname, new_oid ? new_oid->hash : NULL,
1004
+ old_oid ? old_oid->hash : NULL,
1005
flags, msg, &err) ||
1006
ref_transaction_commit(t, &err)) {
1007
ret = 1;
@@ -1038,12 +1031,12 @@ int refs_update_ref(struct ref_store *refs, const char *msg,
1031
}
1032
1033
int update_ref(const char *msg, const char *refname,
1041
- const unsigned char *new_sha1,
1042
- const unsigned char *old_sha1,
1034
+ const struct object_id *new_oid,
1035
+ const struct object_id *old_oid,
1036
unsigned int flags, enum action_on_err onerr)
1037
{
1045
- return refs_update_ref(get_main_ref_store(), msg, refname, new_sha1,
1046
- old_sha1, flags, onerr);
1038
+ return refs_update_ref(get_main_ref_store(), msg, refname, new_oid,
1039
+ old_oid, flags, onerr);
1040
}
1041
1042
char *shorten_unambiguous_ref(const char *refname, int strict)
refs.h
+1
-4
@@ -643,12 +643,9 @@ void ref_transaction_free(struct ref_transaction *transaction);
643
* argument.
644
*/
645
int refs_update_ref(struct ref_store *refs, const char *msg, const char *refname,
646
- const unsigned char *new_sha1, const unsigned char *old_sha1,
646
+ const struct object_id *new_oid, const struct object_id *old_oid,
647
unsigned int flags, enum action_on_err onerr);
648
int update_ref(const char *msg, const char *refname,
649
- const unsigned char *new_sha1, const unsigned char *old_sha1,
650
- unsigned int flags, enum action_on_err onerr);
651
-int update_ref_oid(const char *msg, const char *refname,
649
const struct object_id *new_oid, const struct object_id *old_oid,
650
unsigned int flags, enum action_on_err onerr);
651
sequencer.c
+4
-4
@@ -1115,11 +1115,11 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
1115
* write it at all.
1116
*/
1117
if (command == TODO_PICK && !opts->no_commit && (res == 0 || res == 1) &&
1118
- update_ref(NULL, "CHERRY_PICK_HEAD", commit->object.oid.hash, NULL,
1118
+ update_ref(NULL, "CHERRY_PICK_HEAD", &commit->object.oid, NULL,
1119
REF_NODEREF, UPDATE_REFS_MSG_ON_ERR))
1120
res = -1;
1121
if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) &&
1122
- update_ref(NULL, "REVERT_HEAD", commit->object.oid.hash, NULL,
1122
+ update_ref(NULL, "REVERT_HEAD", &commit->object.oid, NULL,
1123
REF_NODEREF, UPDATE_REFS_MSG_ON_ERR))
1124
res = -1;
1125
@@ -2124,8 +2124,8 @@ cleanup_head_ref:
2124
}
2125
msg = reflog_message(opts, "finish", "%s onto %s",
2126
head_ref.buf, buf.buf);
2127
- if (update_ref(msg, head_ref.buf, head.hash, orig.hash,
2128
- REF_NODEREF, UPDATE_REFS_MSG_ON_ERR)) {
2127
+ if (update_ref(msg, head_ref.buf, &head, &orig,
2128
+ REF_NODEREF, UPDATE_REFS_MSG_ON_ERR)) {
2129
res = error(_("could not update %s"),
2130
head_ref.buf);
2131
goto cleanup_head_ref;
t/helper/test-ref-store.c
+5
-5
@@ -233,15 +233,15 @@ static int cmd_update_ref(struct ref_store *refs, const char **argv)
233
const char *new_sha1_buf = notnull(*argv++, "old-sha1");
234
const char *old_sha1_buf = notnull(*argv++, "old-sha1");
235
unsigned int flags = arg_flags(*argv++, "flags");
236
- unsigned char old_sha1[20];
237
- unsigned char new_sha1[20];
236
+ struct object_id old_oid;
237
+ struct object_id new_oid;
238
239
- if (get_sha1_hex(old_sha1_buf, old_sha1) ||
240
- get_sha1_hex(new_sha1_buf, new_sha1))
239
+ if (get_oid_hex(old_sha1_buf, &old_oid) ||
240
+ get_oid_hex(new_sha1_buf, &new_oid))
241
die("not sha-1");
242
243
return refs_update_ref(refs, msg, refname,
244
- new_sha1, old_sha1,
244
+ &new_oid, &old_oid,
245
flags, UPDATE_REFS_DIE_ON_ERR);
246
}
247
transport-helper.c
+2
-1
@@ -795,7 +795,8 @@ static int push_update_refs_status(struct helper_data *data,
795
private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name);
796
if (!private)
797
continue;
798
- update_ref("update by helper", private, ref->new_oid.hash, NULL, 0, 0);
798
+ update_ref("update by helper", private, &ref->new_oid, NULL,
799
+ 0, 0);
800
free(private);
801
}
802
strbuf_release(&buf);
transport.c
+2
-2
@@ -305,8 +305,8 @@ void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int v
305
if (ref->deletion) {
306
delete_ref(NULL, rs.dst, NULL, 0);
307
} else
308
- update_ref("update by push", rs.dst,
309
- ref->new_oid.hash, NULL, 0, 0);
308
+ update_ref("update by push", rs.dst, &ref->new_oid,
309
+ NULL, 0, 0);
310
free(rs.dst);
311
}
312
}