Skip to content

Commit 96ec7b1

Browse files
pcloudsgitster
authored andcommitted
Convert resolve_ref+xstrdup to new resolve_refdup function
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e4776bd commit 96ec7b1

File tree

12 files changed

+47
-50
lines changed

12 files changed

+47
-50
lines changed

builtin/branch.c

Lines changed: 5 additions & 7 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

@@ -731,10 +730,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
731730

732731
track = git_branch_track;
733732

734-
head = resolve_ref("HEAD", head_sha1, 0, NULL);
733+
head = resolve_refdup("HEAD", head_sha1, 0, NULL);
735734
if (!head)
736735
die(_("Failed to resolve HEAD as a valid ref."));
737-
head = xstrdup(head);
738736
if (!strcmp(head, "HEAD")) {
739737
detached = 1;
740738
} else {

builtin/checkout.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -696,17 +696,14 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
696696
{
697697
int ret = 0;
698698
struct branch_info old;
699+
void *path_to_free;
699700
unsigned char rev[20];
700701
int flag;
701702
memset(&old, 0, sizeof(old));
702-
old.path = resolve_ref("HEAD", rev, 0, &flag);
703-
if (old.path)
704-
old.path = xstrdup(old.path);
703+
old.path = path_to_free = resolve_refdup("HEAD", rev, 0, &flag);
705704
old.commit = lookup_commit_reference_gently(rev, 1);
706-
if (!(flag & REF_ISSYMREF)) {
707-
free((char *)old.path);
705+
if (!(flag & REF_ISSYMREF))
708706
old.path = NULL;
709-
}
710707

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

722719
ret = merge_working_tree(opts, &old, new);
723-
if (ret)
720+
if (ret) {
721+
free(path_to_free);
724722
return ret;
723+
}
725724

726725
if (!opts->quiet && !old.path && old.commit && new->commit != old.commit)
727726
orphaned_commit_warning(old.commit);
728727

729728
update_refs_for_switch(opts, &old, new);
730729

731730
ret = post_checkout_hook(old.commit, new->commit, 1);
732-
free((char *)old.path);
731+
free(path_to_free);
733732
return ret || opts->writeout_error;
734733
}
735734

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/merge.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
10961096
struct commit_list *common = NULL;
10971097
const char *best_strategy = NULL, *wt_strategy = NULL;
10981098
struct commit_list **remotes = &remoteheads;
1099+
void *branch_to_free;
10991100

11001101
if (argc == 2 && !strcmp(argv[1], "-h"))
11011102
usage_with_options(builtin_merge_usage, builtin_merge_options);
@@ -1104,12 +1105,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
11041105
* Check if we are _not_ on a detached HEAD, i.e. if there is a
11051106
* current branch.
11061107
*/
1107-
branch = resolve_ref("HEAD", head_sha1, 0, &flag);
1108-
if (branch) {
1109-
if (!prefixcmp(branch, "refs/heads/"))
1110-
branch += 11;
1111-
branch = xstrdup(branch);
1112-
}
1108+
branch = branch_to_free = resolve_refdup("HEAD", head_sha1, 0, &flag);
1109+
if (branch && !prefixcmp(branch, "refs/heads/"))
1110+
branch += 11;
11131111
if (!branch || is_null_sha1(head_sha1))
11141112
head_commit = NULL;
11151113
else
@@ -1520,6 +1518,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
15201518
ret = suggest_conflicts(option_renormalize);
15211519

15221520
done:
1523-
free((char *)branch);
1521+
free(branch_to_free);
15241522
return ret;
15251523
}

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

builtin/receive-pack.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static int prefer_ofs_delta = 1;
3737
static int auto_update_server_info;
3838
static int auto_gc = 1;
3939
static const char *head_name;
40+
static void *head_name_to_free;
4041
static int sent_capabilities;
4142

4243
static enum deny_action parse_deny_action(const char *var, const char *value)
@@ -695,10 +696,8 @@ static void execute_commands(struct command *commands, const char *unpacker_erro
695696

696697
check_aliased_updates(commands);
697698

698-
free((char *)head_name);
699-
head_name = resolve_ref("HEAD", sha1, 0, NULL);
700-
if (head_name)
701-
head_name = xstrdup(head_name);
699+
free(head_name_to_free);
700+
head_name = head_name_to_free = resolve_refdup("HEAD", sha1, 0, NULL);
702701

703702
for (cmd = commands; cmd; cmd = cmd->next)
704703
if (!cmd->skip_update)

builtin/show-branch.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -726,10 +726,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
726726

727727
if (ac == 0) {
728728
static const char *fake_av[2];
729-
const char *refname;
730729

731-
refname = resolve_ref("HEAD", sha1, 1, NULL);
732-
fake_av[0] = xstrdup(refname);
730+
fake_av[0] = resolve_refdup("HEAD", sha1, 1, NULL);
733731
fake_av[1] = NULL;
734732
av = fake_av;
735733
ac = 1;

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ extern int read_ref(const char *filename, unsigned char *sha1);
866866
* errno is sometimes set on errors, but not always.
867867
*/
868868
extern const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag);
869+
extern char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, int *flag);
869870

870871
extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
871872
extern int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);

reflog-walk.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ static struct complete_reflogs *read_complete_reflog(const char *ref)
5050
for_each_reflog_ent(ref, read_one_reflog, reflogs);
5151
if (reflogs->nr == 0) {
5252
unsigned char sha1[20];
53-
const char *name = resolve_ref(ref, sha1, 1, NULL);
53+
const char *name;
54+
void *name_to_free;
55+
name = name_to_free = resolve_refdup(ref, sha1, 1, NULL);
5456
if (name) {
55-
name = xstrdup(name);
5657
for_each_reflog_ent(name, read_one_reflog, reflogs);
57-
free((char *)name);
58+
free(name_to_free);
5859
}
5960
}
6061
if (reflogs->nr == 0) {
@@ -171,11 +172,11 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
171172
else {
172173
if (*branch == '\0') {
173174
unsigned char sha1[20];
174-
const char *head = resolve_ref("HEAD", sha1, 0, NULL);
175-
if (!head)
176-
die ("No current branch");
177175
free(branch);
178-
branch = xstrdup(head);
176+
branch = resolve_refdup("HEAD", sha1, 0, NULL);
177+
if (!branch)
178+
die ("No current branch");
179+
179180
}
180181
reflogs = read_complete_reflog(branch);
181182
if (!reflogs || reflogs->nr == 0) {

refs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,12 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
605605
return ref;
606606
}
607607

608+
char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, int *flag)
609+
{
610+
const char *ret = resolve_ref(ref, sha1, reading, flag);
611+
return ret ? xstrdup(ret) : NULL;
612+
}
613+
608614
/* The argument to filter_refs */
609615
struct ref_filter {
610616
const char *pattern;

wt-status.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,14 @@ void status_printf_more(struct wt_status *s, const char *color,
111111
void wt_status_prepare(struct wt_status *s)
112112
{
113113
unsigned char sha1[20];
114-
const char *head;
115114

116115
memset(s, 0, sizeof(*s));
117116
memcpy(s->color_palette, default_wt_status_colors,
118117
sizeof(default_wt_status_colors));
119118
s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
120119
s->use_color = -1;
121120
s->relative_paths = 1;
122-
head = resolve_ref("HEAD", sha1, 0, NULL);
123-
s->branch = head ? xstrdup(head) : NULL;
121+
s->branch = resolve_refdup("HEAD", sha1, 0, NULL);
124122
s->reference = "HEAD";
125123
s->fp = stdout;
126124
s->index_file = get_index_file();

0 commit comments

Comments
 (0)