Skip to content

Commit b7f7c07

Browse files
committed
Merge branch 'nd/resolve-ref'
* nd/resolve-ref: Copy resolve_ref() return value for longer use Convert many resolve_ref() calls to read_ref*() and ref_exists() Conflicts: builtin/fmt-merge-msg.c builtin/merge.c refs.c
2 parents eb8aa3d + d5a35c1 commit b7f7c07

17 files changed

+98
-59
lines changed

builtin/branch.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ static int branch_merged(int kind, const char *name,
115115
branch->merge[0] &&
116116
branch->merge[0]->dst &&
117117
(reference_name =
118-
resolve_ref(branch->merge[0]->dst, sha1, 1, NULL)) != NULL)
118+
resolve_ref(branch->merge[0]->dst, sha1, 1, NULL)) != NULL) {
119+
reference_name = xstrdup(reference_name);
119120
reference_rev = lookup_commit_reference(sha1);
121+
}
120122
}
121123
if (!reference_rev)
122124
reference_rev = head_rev;
@@ -141,6 +143,7 @@ static int branch_merged(int kind, const char *name,
141143
" '%s', even though it is merged to HEAD."),
142144
name, reference_name);
143145
}
146+
free((char *)reference_name);
144147
return merged;
145148
}
146149

@@ -186,7 +189,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
186189
free(name);
187190

188191
name = xstrdup(mkpath(fmt, bname.buf));
189-
if (!resolve_ref(name, sha1, 1, NULL)) {
192+
if (read_ref(name, sha1)) {
190193
error(_("%sbranch '%s' not found."),
191194
remote, bname.buf);
192195
ret = 1;
@@ -565,7 +568,6 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
565568
static void rename_branch(const char *oldname, const char *newname, int force)
566569
{
567570
struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
568-
unsigned char sha1[20];
569571
struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
570572
int recovery = 0;
571573

@@ -577,7 +579,7 @@ static void rename_branch(const char *oldname, const char *newname, int force)
577579
* Bad name --- this could be an attempt to rename a
578580
* ref that we used to allow to be created by accident.
579581
*/
580-
if (resolve_ref(oldref.buf, sha1, 1, NULL))
582+
if (ref_exists(oldref.buf))
581583
recovery = 1;
582584
else
583585
die(_("Invalid branch name: '%s'"), oldname);

builtin/checkout.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
288288
commit_locked_index(lock_file))
289289
die(_("unable to write new index file"));
290290

291-
resolve_ref("HEAD", rev, 0, &flag);
291+
read_ref_full("HEAD", rev, 0, &flag);
292292
head = lookup_commit_reference_gently(rev, 1);
293293

294294
errs |= post_checkout_hook(head, head, 0);
@@ -699,7 +699,9 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
699699
unsigned char rev[20];
700700
int flag;
701701
memset(&old, 0, sizeof(old));
702-
old.path = xstrdup(resolve_ref("HEAD", rev, 0, &flag));
702+
old.path = resolve_ref("HEAD", rev, 0, &flag);
703+
if (old.path)
704+
old.path = xstrdup(old.path);
703705
old.commit = lookup_commit_reference_gently(rev, 1);
704706
if (!(flag & REF_ISSYMREF)) {
705707
free((char *)old.path);
@@ -866,7 +868,7 @@ static int parse_branchname_arg(int argc, const char **argv,
866868
setup_branch_path(new);
867869

868870
if (!check_refname_format(new->path, 0) &&
869-
resolve_ref(new->path, branch_rev, 1, NULL))
871+
!read_ref(new->path, branch_rev))
870872
hashcpy(rev, branch_rev);
871873
else
872874
new->path = NULL; /* not an existing branch */

builtin/commit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
12591259
struct commit *commit;
12601260
struct strbuf format = STRBUF_INIT;
12611261
unsigned char junk_sha1[20];
1262-
const char *head = resolve_ref("HEAD", junk_sha1, 0, NULL);
1262+
const char *head;
12631263
struct pretty_print_context pctx = {0};
12641264
struct strbuf author_ident = STRBUF_INIT;
12651265
struct strbuf committer_ident = STRBUF_INIT;
@@ -1304,6 +1304,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
13041304
rev.diffopt.break_opt = 0;
13051305
diff_setup_done(&rev.diffopt);
13061306

1307+
head = resolve_ref("HEAD", junk_sha1, 0, NULL);
13071308
printf("[%s%s ",
13081309
!prefixcmp(head, "refs/heads/") ?
13091310
head + 11 :

builtin/fmt-merge-msg.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
379379
die("No current branch");
380380
if (!prefixcmp(current_branch, "refs/heads/"))
381381
current_branch += 11;
382+
current_branch = xstrdup(current_branch);
382383

383384
/* get a line */
384385
while (pos < in->len) {
@@ -420,6 +421,7 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
420421
}
421422

422423
strbuf_complete_line(out);
424+
free((char *)current_branch);
423425
return 0;
424426
}
425427

builtin/merge.c

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ static void finish(struct commit *head_commit,
413413
static void merge_name(const char *remote, struct strbuf *msg)
414414
{
415415
struct commit *remote_head;
416-
unsigned char branch_head[20], buf_sha[20];
416+
unsigned char branch_head[20];
417417
struct strbuf buf = STRBUF_INIT;
418418
struct strbuf bname = STRBUF_INIT;
419419
const char *ptr;
@@ -477,7 +477,7 @@ static void merge_name(const char *remote, struct strbuf *msg)
477477
strbuf_addstr(&truname, "refs/heads/");
478478
strbuf_addstr(&truname, remote);
479479
strbuf_setlen(&truname, truname.len - len);
480-
if (resolve_ref(truname.buf, buf_sha, 1, NULL)) {
480+
if (ref_exists(truname.buf)) {
481481
strbuf_addf(msg,
482482
"%s\t\tbranch '%s'%s of .\n",
483483
sha1_to_hex(remote_head->object.sha1),
@@ -1091,7 +1091,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
10911091
struct commit *head_commit;
10921092
struct strbuf buf = STRBUF_INIT;
10931093
const char *head_arg;
1094-
int flag, i;
1094+
int flag, i, ret = 0;
10951095
int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
10961096
struct commit_list *common = NULL;
10971097
const char *best_strategy = NULL, *wt_strategy = NULL;
@@ -1105,8 +1105,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
11051105
* current branch.
11061106
*/
11071107
branch = resolve_ref("HEAD", head_sha1, 0, &flag);
1108-
if (branch && !prefixcmp(branch, "refs/heads/"))
1109-
branch += 11;
1108+
if (branch) {
1109+
if (!prefixcmp(branch, "refs/heads/"))
1110+
branch += 11;
1111+
branch = xstrdup(branch);
1112+
}
11101113
if (!branch || is_null_sha1(head_sha1))
11111114
head_commit = NULL;
11121115
else
@@ -1132,7 +1135,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
11321135
die(_("There is no merge to abort (MERGE_HEAD missing)."));
11331136

11341137
/* Invoke 'git reset --merge' */
1135-
return cmd_reset(nargc, nargv, prefix);
1138+
ret = cmd_reset(nargc, nargv, prefix);
1139+
goto done;
11361140
}
11371141

11381142
if (read_cache_unmerged())
@@ -1219,7 +1223,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12191223
read_empty(remote_head->object.sha1, 0);
12201224
update_ref("initial pull", "HEAD", remote_head->object.sha1,
12211225
NULL, 0, DIE_ON_ERR);
1222-
return 0;
1226+
goto done;
12231227
} else {
12241228
struct strbuf merge_names = STRBUF_INIT;
12251229

@@ -1308,7 +1312,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
13081312
* but first the most common case of merging one remote.
13091313
*/
13101314
finish_up_to_date("Already up-to-date.");
1311-
return 0;
1315+
goto done;
13121316
} else if (allow_fast_forward && !remoteheads->next &&
13131317
!common->next &&
13141318
!hashcmp(common->item->object.sha1, head_commit->object.sha1)) {
@@ -1329,16 +1333,20 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
13291333
strbuf_addstr(&msg,
13301334
" (no commit created; -m option ignored)");
13311335
commit = remoteheads->item;
1332-
if (!commit)
1333-
return 1;
1336+
if (!commit) {
1337+
ret = 1;
1338+
goto done;
1339+
}
13341340

13351341
if (checkout_fast_forward(head_commit->object.sha1,
1336-
commit->object.sha1))
1337-
return 1;
1342+
commit->object.sha1)) {
1343+
ret = 1;
1344+
goto done;
1345+
}
13381346

13391347
finish(head_commit, commit->object.sha1, msg.buf);
13401348
drop_save();
1341-
return 0;
1349+
goto done;
13421350
} else if (!remoteheads->next && common->next)
13431351
;
13441352
/*
@@ -1356,8 +1364,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
13561364
git_committer_info(IDENT_ERROR_ON_NO_NAME);
13571365
printf(_("Trying really trivial in-index merge...\n"));
13581366
if (!read_tree_trivial(common->item->object.sha1,
1359-
head_commit->object.sha1, remoteheads->item->object.sha1))
1360-
return merge_trivial(head_commit);
1367+
head_commit->object.sha1,
1368+
remoteheads->item->object.sha1)) {
1369+
ret = merge_trivial(head_commit);
1370+
goto done;
1371+
}
13611372
printf(_("Nope.\n"));
13621373
}
13631374
} else {
@@ -1385,7 +1396,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
13851396
}
13861397
if (up_to_date) {
13871398
finish_up_to_date("Already up-to-date. Yeeah!");
1388-
return 0;
1399+
goto done;
13891400
}
13901401
}
13911402

@@ -1467,9 +1478,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
14671478
* If we have a resulting tree, that means the strategy module
14681479
* auto resolved the merge cleanly.
14691480
*/
1470-
if (automerge_was_ok)
1471-
return finish_automerge(head_commit, common, result_tree,
1472-
wt_strategy);
1481+
if (automerge_was_ok) {
1482+
ret = finish_automerge(head_commit, common, result_tree,
1483+
wt_strategy);
1484+
goto done;
1485+
}
14731486

14741487
/*
14751488
* Pick the result from the best strategy and have the user fix
@@ -1483,7 +1496,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
14831496
else
14841497
fprintf(stderr, _("Merge with strategy %s failed.\n"),
14851498
use_strategies[0]->name);
1486-
return 2;
1499+
ret = 2;
1500+
goto done;
14871501
} else if (best_strategy == wt_strategy)
14881502
; /* We already have its result in the working tree. */
14891503
else {
@@ -1499,10 +1513,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
14991513
else
15001514
write_merge_state();
15011515

1502-
if (merge_was_ok) {
1516+
if (merge_was_ok)
15031517
fprintf(stderr, _("Automatic merge went well; "
15041518
"stopped before committing as requested\n"));
1505-
return 0;
1506-
} else
1507-
return suggest_conflicts(option_renormalize);
1519+
else
1520+
ret = suggest_conflicts(option_renormalize);
1521+
1522+
done:
1523+
free((char *)branch);
1524+
return ret;
15081525
}

builtin/notes.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@ static int merge_commit(struct notes_merge_options *o)
804804
struct notes_tree *t;
805805
struct commit *partial;
806806
struct pretty_print_context pretty_ctx;
807+
int ret;
807808

808809
/*
809810
* Read partial merge result from .git/NOTES_MERGE_PARTIAL,
@@ -828,6 +829,7 @@ static int merge_commit(struct notes_merge_options *o)
828829
o->local_ref = resolve_ref("NOTES_MERGE_REF", sha1, 0, NULL);
829830
if (!o->local_ref)
830831
die("Failed to resolve NOTES_MERGE_REF");
832+
o->local_ref = xstrdup(o->local_ref);
831833

832834
if (notes_merge_commit(o, t, partial, sha1))
833835
die("Failed to finalize notes merge");
@@ -843,7 +845,9 @@ static int merge_commit(struct notes_merge_options *o)
843845

844846
free_notes(t);
845847
strbuf_release(&msg);
846-
return merge_abort(o);
848+
ret = merge_abort(o);
849+
free((char *)o->local_ref);
850+
return ret;
847851
}
848852

849853
static int merge(int argc, const char **argv, const char *prefix)

builtin/receive-pack.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,10 @@ static void execute_commands(struct command *commands, const char *unpacker_erro
695695

696696
check_aliased_updates(commands);
697697

698+
free((char *)head_name);
698699
head_name = resolve_ref("HEAD", sha1, 0, NULL);
700+
if (head_name)
701+
head_name = xstrdup(head_name);
699702

700703
for (cmd = commands; cmd; cmd = cmd->next)
701704
if (!cmd->skip_update)

builtin/remote.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
343343
states->tracked.strdup_strings = 1;
344344
states->stale.strdup_strings = 1;
345345
for (ref = fetch_map; ref; ref = ref->next) {
346-
unsigned char sha1[20];
347-
if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
346+
if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
348347
string_list_append(&states->new, abbrev_branch(ref->name));
349348
else
350349
string_list_append(&states->tracked, abbrev_branch(ref->name));
@@ -710,7 +709,7 @@ static int mv(int argc, const char **argv)
710709
int flag = 0;
711710
unsigned char sha1[20];
712711

713-
resolve_ref(item->string, sha1, 1, &flag);
712+
read_ref_full(item->string, sha1, 1, &flag);
714713
if (!(flag & REF_ISSYMREF))
715714
continue;
716715
if (delete_ref(item->string, NULL, REF_NODEREF))
@@ -1220,10 +1219,9 @@ static int set_head(int argc, const char **argv)
12201219
usage_with_options(builtin_remote_sethead_usage, options);
12211220

12221221
if (head_name) {
1223-
unsigned char sha1[20];
12241222
strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
12251223
/* make sure it's valid */
1226-
if (!resolve_ref(buf2.buf, sha1, 1, NULL))
1224+
if (!ref_exists(buf2.buf))
12271225
result |= error("Not a valid ref: %s", buf2.buf);
12281226
else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
12291227
result |= error("Could not setup %s", buf.buf);

builtin/replace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
5858
had_error = 1;
5959
continue;
6060
}
61-
if (!resolve_ref(ref, sha1, 1, NULL)) {
61+
if (read_ref(ref, sha1)) {
6262
error("replace ref '%s' not found.", *p);
6363
had_error = 1;
6464
continue;
@@ -97,7 +97,7 @@ static int replace_object(const char *object_ref, const char *replace_ref,
9797
if (check_refname_format(ref, 0))
9898
die("'%s' is not a valid ref name.", ref);
9999

100-
if (!resolve_ref(ref, prev, 1, NULL))
100+
if (read_ref(ref, prev))
101101
hashclr(prev);
102102
else if (!force)
103103
die("replace ref '%s' already exists", ref);

builtin/show-ref.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
225225
unsigned char sha1[20];
226226

227227
if (!prefixcmp(*pattern, "refs/") &&
228-
resolve_ref(*pattern, sha1, 1, NULL)) {
228+
!read_ref(*pattern, sha1)) {
229229
if (!quiet)
230230
show_one(*pattern, sha1);
231231
}

0 commit comments

Comments
 (0)