Convert resolve_ref+xstrdup to new resolve_refdup function
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Dec 13, 2011 at 21:17 UTC
96ec7b1e708863d0cd6b8e72a986d6f0a1bb64db
12 files changed
+47
-50
builtin/branch.c
+5
-7
@@ -104,6 +104,7 @@ static int branch_merged(int kind, const char *name,
104
*/
105
struct commit *reference_rev = NULL;
106
const char *reference_name = NULL;
107
+ void *reference_name_to_free = NULL;
108
int merged;
109
110
if (kind == REF_LOCAL_BRANCH) {
@@ -114,11 +115,9 @@ static int branch_merged(int kind, const char *name,
115
branch->merge &&
116
branch->merge[0] &&
117
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)
120
reference_rev = lookup_commit_reference(sha1);
121
- }
121
}
122
if (!reference_rev)
123
reference_rev = head_rev;
@@ -143,7 +142,7 @@ static int branch_merged(int kind, const char *name,
142
" '%s', even though it is merged to HEAD."),
143
name, reference_name);
144
}
146
- free((char *)reference_name);
145
+ free(reference_name_to_free);
146
return merged;
147
}
148
@@ -731,10 +730,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
730
731
track = git_branch_track;
732
734
- head = resolve_ref("HEAD", head_sha1, 0, NULL);
733
+ head = resolve_refdup("HEAD", head_sha1, 0, NULL);
734
if (!head)
735
die(_("Failed to resolve HEAD as a valid ref."));
737
- head = xstrdup(head);
736
if (!strcmp(head, "HEAD")) {
737
detached = 1;
738
} else {
builtin/checkout.c
+7
-8
@@ -696,17 +696,14 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
696
{
697
int ret = 0;
698
struct branch_info old;
699
+ void *path_to_free;
700
unsigned char rev[20];
701
int flag;
702
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);
704
old.commit = lookup_commit_reference_gently(rev, 1);
706
- if (!(flag & REF_ISSYMREF)) {
707
- free((char *)old.path);
705
+ if (!(flag & REF_ISSYMREF))
706
old.path = NULL;
709
- }
707
708
if (old.path && !prefixcmp(old.path, "refs/heads/"))
709
old.name = old.path + strlen("refs/heads/");
@@ -720,8 +717,10 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
717
}
718
719
ret = merge_working_tree(opts, &old, new);
723
- if (ret)
720
+ if (ret) {
721
+ free(path_to_free);
722
return ret;
723
+ }
724
725
if (!opts->quiet && !old.path && old.commit && new->commit != old.commit)
726
orphaned_commit_warning(old.commit);
@@ -729,7 +728,7 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
728
update_refs_for_switch(opts, &old, new);
729
730
ret = post_checkout_hook(old.commit, new->commit, 1);
732
- free((char *)old.path);
731
+ free(path_to_free);
732
return ret || opts->writeout_error;
733
}
734
builtin/fmt-merge-msg.c
+4
-3
@@ -372,14 +372,15 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
372
int i = 0, pos = 0;
373
unsigned char head_sha1[20];
374
const char *current_branch;
375
+ void *current_branch_to_free;
376
377
/* 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);
380
if (!current_branch)
381
die("No current branch");
382
if (!prefixcmp(current_branch, "refs/heads/"))
383
current_branch += 11;
382
- current_branch = xstrdup(current_branch);
384
385
/* get a line */
386
while (pos < in->len) {
@@ -421,7 +422,7 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
422
}
423
424
strbuf_complete_line(out);
424
- free((char *)current_branch);
425
+ free(current_branch_to_free);
426
return 0;
427
}
428
builtin/for-each-ref.c
+2
-5
@@ -628,11 +628,8 @@ static void populate_value(struct refinfo *ref)
628
629
if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
630
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)
633
ref->symref = "";
634
}
635
builtin/merge.c
+5
-7
@@ -1096,6 +1096,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1096
struct commit_list *common = NULL;
1097
const char *best_strategy = NULL, *wt_strategy = NULL;
1098
struct commit_list **remotes = &remoteheads;
1099
+ void *branch_to_free;
1100
1101
if (argc == 2 && !strcmp(argv[1], "-h"))
1102
usage_with_options(builtin_merge_usage, builtin_merge_options);
@@ -1104,12 +1105,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1105
* Check if we are _not_ on a detached HEAD, i.e. if there is a
1106
* current branch.
1107
*/
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;
1111
if (!branch || is_null_sha1(head_sha1))
1112
head_commit = NULL;
1113
else
@@ -1520,6 +1518,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1518
ret = suggest_conflicts(option_renormalize);
1519
1520
done:
1523
- free((char *)branch);
1521
+ free(branch_to_free);
1522
return ret;
1523
}
builtin/notes.c
+4
-3
@@ -804,6 +804,7 @@ static int merge_commit(struct notes_merge_options *o)
804
struct notes_tree *t;
805
struct commit *partial;
806
struct pretty_print_context pretty_ctx;
807
+ void *local_ref_to_free;
808
int ret;
809
810
/*
@@ -826,10 +827,10 @@ static int merge_commit(struct notes_merge_options *o)
827
t = xcalloc(1, sizeof(struct notes_tree));
828
init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
829
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);
832
if (!o->local_ref)
833
die("Failed to resolve NOTES_MERGE_REF");
832
- o->local_ref = xstrdup(o->local_ref);
834
835
if (notes_merge_commit(o, t, partial, sha1))
836
die("Failed to finalize notes merge");
@@ -846,7 +847,7 @@ static int merge_commit(struct notes_merge_options *o)
847
free_notes(t);
848
strbuf_release(&msg);
849
ret = merge_abort(o);
849
- free((char *)o->local_ref);
850
+ free(local_ref_to_free);
851
return ret;
852
}
853
builtin/receive-pack.c
+3
-4
@@ -37,6 +37,7 @@ static int prefer_ofs_delta = 1;
37
static int auto_update_server_info;
38
static int auto_gc = 1;
39
static const char *head_name;
40
+static void *head_name_to_free;
41
static int sent_capabilities;
42
43
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
696
697
check_aliased_updates(commands);
698
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);
701
702
for (cmd = commands; cmd; cmd = cmd->next)
703
if (!cmd->skip_update)
builtin/show-branch.c
+1
-3
@@ -726,10 +726,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
726
727
if (ac == 0) {
728
static const char *fake_av[2];
729
- const char *refname;
729
731
- refname = resolve_ref("HEAD", sha1, 1, NULL);
732
- fake_av[0] = xstrdup(refname);
730
+ fake_av[0] = resolve_refdup("HEAD", sha1, 1, NULL);
731
fake_av[1] = NULL;
732
av = fake_av;
733
ac = 1;
cache.h
+1
@@ -866,6 +866,7 @@ extern int read_ref(const char *filename, unsigned char *sha1);
866
* errno is sometimes set on errors, but not always.
867
*/
868
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);
870
871
extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
872
extern int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);
reflog-walk.c
+8
-7
@@ -50,11 +50,12 @@ static struct complete_reflogs *read_complete_reflog(const char *ref)
50
for_each_reflog_ent(ref, read_one_reflog, reflogs);
51
if (reflogs->nr == 0) {
52
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);
56
if (name) {
55
- name = xstrdup(name);
57
for_each_reflog_ent(name, read_one_reflog, reflogs);
57
- free((char *)name);
58
+ free(name_to_free);
59
}
60
}
61
if (reflogs->nr == 0) {
@@ -171,11 +172,11 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
172
else {
173
if (*branch == '\0') {
174
unsigned char sha1[20];
174
- const char *head = resolve_ref("HEAD", sha1, 0, NULL);
175
- if (!head)
176
- die ("No current branch");
175
free(branch);
178
- branch = xstrdup(head);
176
+ branch = resolve_refdup("HEAD", sha1, 0, NULL);
177
+ if (!branch)
178
+ die ("No current branch");
179
+
180
}
181
reflogs = read_complete_reflog(branch);
182
if (!reflogs || reflogs->nr == 0) {
refs.c
+6
@@ -605,6 +605,12 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
605
return ref;
606
}
607
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
+
614
/* The argument to filter_refs */
615
struct ref_filter {
616
const char *pattern;
wt-status.c
+1
-3
@@ -111,7 +111,6 @@ void status_printf_more(struct wt_status *s, const char *color,
111
void wt_status_prepare(struct wt_status *s)
112
{
113
unsigned char sha1[20];
114
- const char *head;
114
115
memset(s, 0, sizeof(*s));
116
memcpy(s->color_palette, default_wt_status_colors,
@@ -119,8 +118,7 @@ void wt_status_prepare(struct wt_status *s)
118
s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
119
s->use_color = -1;
120
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);
122
s->reference = "HEAD";
123
s->fp = stdout;
124
s->index_file = get_index_file();