Skip to content

Commit 2e05710

Browse files
committed
Merge branch 'nd/resolve-ref'
* nd/resolve-ref: Rename resolve_ref() to resolve_ref_unsafe() Convert resolve_ref+xstrdup to new resolve_refdup function revert: convert resolve_ref() to read_ref_full()
2 parents b8fc5ab + 8cad474 commit 2e05710

21 files changed

+73
-76
lines changed

branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ int validate_new_branchname(const char *name, struct strbuf *ref,
181181
const char *head;
182182
unsigned char sha1[20];
183183

184-
head = resolve_ref("HEAD", sha1, 0, NULL);
184+
head = resolve_ref_unsafe("HEAD", sha1, 0, NULL);
185185
if (!is_bare_repository() && head && !strcmp(head, ref->buf))
186186
die("Cannot force update the current branch.");
187187
}

builtin/branch.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ static int branch_merged(int kind, const char *name,
104104
*/
105105
struct commit *reference_rev = NULL;
106106
const char *reference_name = NULL;
107+
void *reference_name_to_free = NULL;
107108
int merged;
108109

109110
if (kind == REF_LOCAL_BRANCH) {
@@ -114,11 +115,9 @@ static int branch_merged(int kind, const char *name,
114115
branch->merge &&
115116
branch->merge[0] &&
116117
branch->merge[0]->dst &&
117-
(reference_name =
118-
resolve_ref(branch->merge[0]->dst, sha1, 1, NULL)) != NULL) {
119-
reference_name = xstrdup(reference_name);
118+
(reference_name = reference_name_to_free =
119+
resolve_refdup(branch->merge[0]->dst, sha1, 1, NULL)) != NULL)
120120
reference_rev = lookup_commit_reference(sha1);
121-
}
122121
}
123122
if (!reference_rev)
124123
reference_rev = head_rev;
@@ -143,7 +142,7 @@ static int branch_merged(int kind, const char *name,
143142
" '%s', even though it is merged to HEAD."),
144143
name, reference_name);
145144
}
146-
free((char *)reference_name);
145+
free(reference_name_to_free);
147146
return merged;
148147
}
149148

@@ -253,7 +252,7 @@ static char *resolve_symref(const char *src, const char *prefix)
253252
int flag;
254253
const char *dst, *cp;
255254

256-
dst = resolve_ref(src, sha1, 0, &flag);
255+
dst = resolve_ref_unsafe(src, sha1, 0, &flag);
257256
if (!(dst && (flag & REF_ISSYMREF)))
258257
return NULL;
259258
if (prefix && (cp = skip_prefix(dst, prefix)))
@@ -738,10 +737,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
738737

739738
track = git_branch_track;
740739

741-
head = resolve_ref("HEAD", head_sha1, 0, NULL);
740+
head = resolve_refdup("HEAD", head_sha1, 0, NULL);
742741
if (!head)
743742
die(_("Failed to resolve HEAD as a valid ref."));
744-
head = xstrdup(head);
745743
if (!strcmp(head, "HEAD")) {
746744
detached = 1;
747745
} else {

builtin/checkout.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -705,17 +705,14 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
705705
{
706706
int ret = 0;
707707
struct branch_info old;
708+
void *path_to_free;
708709
unsigned char rev[20];
709710
int flag;
710711
memset(&old, 0, sizeof(old));
711-
old.path = resolve_ref("HEAD", rev, 0, &flag);
712-
if (old.path)
713-
old.path = xstrdup(old.path);
712+
old.path = path_to_free = resolve_refdup("HEAD", rev, 0, &flag);
714713
old.commit = lookup_commit_reference_gently(rev, 1);
715-
if (!(flag & REF_ISSYMREF)) {
716-
free((char *)old.path);
714+
if (!(flag & REF_ISSYMREF))
717715
old.path = NULL;
718-
}
719716

720717
if (old.path && !prefixcmp(old.path, "refs/heads/"))
721718
old.name = old.path + strlen("refs/heads/");
@@ -729,16 +726,18 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
729726
}
730727

731728
ret = merge_working_tree(opts, &old, new);
732-
if (ret)
729+
if (ret) {
730+
free(path_to_free);
733731
return ret;
732+
}
734733

735734
if (!opts->quiet && !old.path && old.commit && new->commit != old.commit)
736735
orphaned_commit_warning(old.commit);
737736

738737
update_refs_for_switch(opts, &old, new);
739738

740739
ret = post_checkout_hook(old.commit, new->commit, 1);
741-
free((char *)old.path);
740+
free(path_to_free);
742741
return ret || opts->writeout_error;
743742
}
744743

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +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);
1307+
head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL);
13081308
printf("[%s%s ",
13091309
!prefixcmp(head, "refs/heads/") ?
13101310
head + 11 :

builtin/fmt-merge-msg.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,14 +372,15 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
372372
int i = 0, pos = 0;
373373
unsigned char head_sha1[20];
374374
const char *current_branch;
375+
void *current_branch_to_free;
375376

376377
/* get current branch */
377-
current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
378+
current_branch = current_branch_to_free =
379+
resolve_refdup("HEAD", head_sha1, 1, NULL);
378380
if (!current_branch)
379381
die("No current branch");
380382
if (!prefixcmp(current_branch, "refs/heads/"))
381383
current_branch += 11;
382-
current_branch = xstrdup(current_branch);
383384

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

423424
strbuf_complete_line(out);
424-
free((char *)current_branch);
425+
free(current_branch_to_free);
425426
return 0;
426427
}
427428

builtin/for-each-ref.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -628,11 +628,8 @@ static void populate_value(struct refinfo *ref)
628628

629629
if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
630630
unsigned char unused1[20];
631-
const char *symref;
632-
symref = resolve_ref(ref->refname, unused1, 1, NULL);
633-
if (symref)
634-
ref->symref = xstrdup(symref);
635-
else
631+
ref->symref = resolve_refdup(ref->refname, unused1, 1, NULL);
632+
if (!ref->symref)
636633
ref->symref = "";
637634
}
638635

builtin/fsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ static int fsck_head_link(void)
563563
if (verbose)
564564
fprintf(stderr, "Checking HEAD link\n");
565565

566-
head_points_at = resolve_ref("HEAD", head_sha1, 0, &flag);
566+
head_points_at = resolve_ref_unsafe("HEAD", head_sha1, 0, &flag);
567567
if (!head_points_at)
568568
return error("Invalid HEAD");
569569
if (!strcmp(head_points_at, "HEAD"))

builtin/log.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ static char *find_branch_name(struct rev_info *rev)
10401040
if (positive < 0)
10411041
return NULL;
10421042
strbuf_addf(&buf, "refs/heads/%s", rev->cmdline.rev[positive].name);
1043-
branch = resolve_ref(buf.buf, branch_sha1, 1, NULL);
1043+
branch = resolve_ref_unsafe(buf.buf, branch_sha1, 1, NULL);
10441044
if (!branch ||
10451045
prefixcmp(branch, "refs/heads/") ||
10461046
hashcmp(rev->cmdline.rev[positive].item->sha1, branch_sha1))
@@ -1268,7 +1268,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
12681268

12691269
rev.pending.objects[0].item->flags |= UNINTERESTING;
12701270
add_head_to_pending(&rev);
1271-
ref = resolve_ref("HEAD", sha1, 1, NULL);
1271+
ref = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
12721272
if (ref && !prefixcmp(ref, "refs/heads/"))
12731273
branch_name = xstrdup(ref + strlen("refs/heads/"));
12741274
else

builtin/merge.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
11001100
struct commit_list *common = NULL;
11011101
const char *best_strategy = NULL, *wt_strategy = NULL;
11021102
struct commit_list **remotes = &remoteheads;
1103+
void *branch_to_free;
11031104

11041105
if (argc == 2 && !strcmp(argv[1], "-h"))
11051106
usage_with_options(builtin_merge_usage, builtin_merge_options);
@@ -1108,12 +1109,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
11081109
* Check if we are _not_ on a detached HEAD, i.e. if there is a
11091110
* current branch.
11101111
*/
1111-
branch = resolve_ref("HEAD", head_sha1, 0, &flag);
1112-
if (branch) {
1113-
if (!prefixcmp(branch, "refs/heads/"))
1114-
branch += 11;
1115-
branch = xstrdup(branch);
1116-
}
1112+
branch = branch_to_free = resolve_refdup("HEAD", head_sha1, 0, &flag);
1113+
if (branch && !prefixcmp(branch, "refs/heads/"))
1114+
branch += 11;
11171115
if (!branch || is_null_sha1(head_sha1))
11181116
head_commit = NULL;
11191117
else
@@ -1524,6 +1522,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
15241522
ret = suggest_conflicts(option_renormalize);
15251523

15261524
done:
1527-
free((char *)branch);
1525+
free(branch_to_free);
15281526
return ret;
15291527
}

builtin/notes.c

Lines changed: 4 additions & 3 deletions
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+
void *local_ref_to_free;
807808
int ret;
808809

809810
/*
@@ -826,10 +827,10 @@ static int merge_commit(struct notes_merge_options *o)
826827
t = xcalloc(1, sizeof(struct notes_tree));
827828
init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
828829

829-
o->local_ref = resolve_ref("NOTES_MERGE_REF", sha1, 0, NULL);
830+
o->local_ref = local_ref_to_free =
831+
resolve_refdup("NOTES_MERGE_REF", sha1, 0, NULL);
830832
if (!o->local_ref)
831833
die("Failed to resolve NOTES_MERGE_REF");
832-
o->local_ref = xstrdup(o->local_ref);
833834

834835
if (notes_merge_commit(o, t, partial, sha1))
835836
die("Failed to finalize notes merge");
@@ -846,7 +847,7 @@ static int merge_commit(struct notes_merge_options *o)
846847
free_notes(t);
847848
strbuf_release(&msg);
848849
ret = merge_abort(o);
849-
free((char *)o->local_ref);
850+
free(local_ref_to_free);
850851
return ret;
851852
}
852853

0 commit comments

Comments
 (0)