refspec: add and use refspec_appendf()
Add a function for building a refspec using printf-style formatting. It frees callers from managing their own buffer. Use it throughout the tree to shorten and simplify its callers. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Sep 5, 2020 at 16:49 UTC
1af8b8c0a570ee0b12a19fdd920a3ea09fb22a75
6 files changed
+35
-49
builtin/clone.c
+2
-5
@@ -953,7 +953,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
953
struct ref *mapped_refs;
954
const struct ref *ref;
955
struct strbuf key = STRBUF_INIT;
956
- struct strbuf default_refspec = STRBUF_INIT;
956
struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
957
struct transport *transport = NULL;
958
const char *src_ref_prefix = "refs/heads/";
@@ -1157,9 +1156,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1156
1157
remote = remote_get(option_origin);
1158
1160
- strbuf_addf(&default_refspec, "+%s*:%s*", src_ref_prefix,
1161
- branch_top.buf);
1162
- refspec_append(&remote->fetch, default_refspec.buf);
1159
+ refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix,
1160
+ branch_top.buf);
1161
1162
transport = transport_get(remote, remote->url[0]);
1163
transport_set_verbosity(transport, option_verbosity, option_progress);
@@ -1332,7 +1330,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1330
strbuf_release(&reflog_msg);
1331
strbuf_release(&branch_top);
1332
strbuf_release(&key);
1335
- strbuf_release(&default_refspec);
1333
junk_mode = JUNK_LEAVE_ALL;
1334
1335
strvec_clear(&ref_prefixes);
builtin/fetch.c
+2
-5
@@ -1738,15 +1738,12 @@ static int fetch_one(struct remote *remote, int argc, const char **argv,
1738
1739
for (i = 0; i < argc; i++) {
1740
if (!strcmp(argv[i], "tag")) {
1741
- char *tag;
1741
i++;
1742
if (i >= argc)
1743
die(_("You need to specify a tag name."));
1744
1746
- tag = xstrfmt("refs/tags/%s:refs/tags/%s",
1747
- argv[i], argv[i]);
1748
- refspec_append(&rs, tag);
1749
- free(tag);
1745
+ refspec_appendf(&rs, "refs/tags/%s:refs/tags/%s",
1746
+ argv[i], argv[i]);
1747
} else {
1748
refspec_append(&rs, argv[i]);
1749
}
builtin/push.c
+10
-30
@@ -78,12 +78,9 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
78
memset(&query, 0, sizeof(struct refspec_item));
79
query.src = matched->name;
80
if (!query_refspecs(&remote->push, &query) && query.dst) {
81
- struct strbuf buf = STRBUF_INIT;
82
- strbuf_addf(&buf, "%s%s:%s",
83
- query.force ? "+" : "",
84
- query.src, query.dst);
85
- refspec_append(refspec, buf.buf);
86
- strbuf_release(&buf);
81
+ refspec_appendf(refspec, "%s%s:%s",
82
+ query.force ? "+" : "",
83
+ query.src, query.dst);
84
return;
85
}
86
}
@@ -92,11 +89,8 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
89
skip_prefix(matched->name, "refs/heads/", &branch_name)) {
90
struct branch *branch = branch_get(branch_name);
91
if (branch->merge_nr == 1 && branch->merge[0]->src) {
95
- struct strbuf buf = STRBUF_INIT;
96
- strbuf_addf(&buf, "%s:%s",
97
- ref, branch->merge[0]->src);
98
- refspec_append(refspec, buf.buf);
99
- strbuf_release(&buf);
92
+ refspec_appendf(refspec, "%s:%s",
93
+ ref, branch->merge[0]->src);
94
return;
95
}
96
}
@@ -113,23 +107,17 @@ static void set_refspecs(const char **refs, int nr, const char *repo)
107
for (i = 0; i < nr; i++) {
108
const char *ref = refs[i];
109
if (!strcmp("tag", ref)) {
116
- struct strbuf tagref = STRBUF_INIT;
110
if (nr <= ++i)
111
die(_("tag shorthand without <tag>"));
112
ref = refs[i];
113
if (deleterefs)
121
- strbuf_addf(&tagref, ":refs/tags/%s", ref);
114
+ refspec_appendf(&rs, ":refs/tags/%s", ref);
115
else
123
- strbuf_addf(&tagref, "refs/tags/%s", ref);
124
- refspec_append(&rs, tagref.buf);
125
- strbuf_release(&tagref);
116
+ refspec_appendf(&rs, "refs/tags/%s", ref);
117
} else if (deleterefs) {
127
- struct strbuf delref = STRBUF_INIT;
118
if (strchr(ref, ':'))
119
die(_("--delete only accepts plain target ref names"));
130
- strbuf_addf(&delref, ":%s", ref);
131
- refspec_append(&rs, delref.buf);
132
- strbuf_release(&delref);
120
+ refspec_appendf(&rs, ":%s", ref);
121
} else if (!strchr(ref, ':')) {
122
if (!remote) {
123
/* lazily grab remote and local_refs */
@@ -200,8 +188,6 @@ static const char message_detached_head_die[] =
188
static void setup_push_upstream(struct remote *remote, struct branch *branch,
189
int triangular, int simple)
190
{
203
- struct strbuf refspec = STRBUF_INIT;
204
-
191
if (!branch)
192
die(_(message_detached_head_die), remote->name);
193
if (!branch->merge_nr || !branch->merge || !branch->remote_name)
@@ -227,20 +213,14 @@ static void setup_push_upstream(struct remote *remote, struct branch *branch,
213
die_push_simple(branch, remote);
214
}
215
230
- strbuf_addf(&refspec, "%s:%s", branch->refname, branch->merge[0]->src);
231
- refspec_append(&rs, refspec.buf);
232
- strbuf_release(&refspec);
216
+ refspec_appendf(&rs, "%s:%s", branch->refname, branch->merge[0]->src);
217
}
218
219
static void setup_push_current(struct remote *remote, struct branch *branch)
220
{
237
- struct strbuf refspec = STRBUF_INIT;
238
-
221
if (!branch)
222
die(_(message_detached_head_die), remote->name);
241
- strbuf_addf(&refspec, "%s:%s", branch->refname, branch->refname);
242
- refspec_append(&rs, refspec.buf);
243
- strbuf_release(&refspec);
223
+ refspec_appendf(&rs, "%s:%s", branch->refname, branch->refname);
224
}
225
226
static int is_workflow_triangular(struct remote *remote)
refspec.c
+16
-2
@@ -153,7 +153,7 @@ void refspec_init(struct refspec *rs, int fetch)
153
rs->fetch = fetch;
154
}
155
156
-void refspec_append(struct refspec *rs, const char *refspec)
156
+static void refspec_append_nodup(struct refspec *rs, char *refspec)
157
{
158
struct refspec_item item;
159
@@ -163,7 +163,21 @@ void refspec_append(struct refspec *rs, const char *refspec)
163
rs->items[rs->nr++] = item;
164
165
ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
166
- rs->raw[rs->raw_nr++] = xstrdup(refspec);
166
+ rs->raw[rs->raw_nr++] = refspec;
167
+}
168
+
169
+void refspec_append(struct refspec *rs, const char *refspec)
170
+{
171
+ refspec_append_nodup(rs, xstrdup(refspec));
172
+}
173
+
174
+void refspec_appendf(struct refspec *rs, const char *fmt, ...)
175
+{
176
+ va_list ap;
177
+
178
+ va_start(ap, fmt);
179
+ refspec_append_nodup(rs, xstrvfmt(fmt, ap));
180
+ va_end(ap);
181
}
182
183
void refspec_appendn(struct refspec *rs, const char **refspecs, int nr)
refspec.h
+2
@@ -56,6 +56,8 @@ void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
56
void refspec_item_clear(struct refspec_item *item);
57
void refspec_init(struct refspec *rs, int fetch);
58
void refspec_append(struct refspec *rs, const char *refspec);
59
+__attribute__((format (printf,2,3)))
60
+void refspec_appendf(struct refspec *rs, const char *fmt, ...);
61
void refspec_appendn(struct refspec *rs, const char **refspecs, int nr);
62
void refspec_clear(struct refspec *rs);
63
remote.c
+3
-7
@@ -287,19 +287,15 @@ static void read_branches_file(struct remote *remote)
287
frag = (char *)git_default_branch_name();
288
289
add_url_alias(remote, strbuf_detach(&buf, NULL));
290
- strbuf_addf(&buf, "refs/heads/%s:refs/heads/%s",
291
- frag, remote->name);
292
- refspec_append(&remote->fetch, buf.buf);
290
+ refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
291
+ frag, remote->name);
292
293
/*
294
* Cogito compatible push: push current HEAD to remote #branch
295
* (master if missing)
296
*/
298
- strbuf_reset(&buf);
299
- strbuf_addf(&buf, "HEAD:refs/heads/%s", frag);
300
- refspec_append(&remote->push, buf.buf);
297
+ refspec_appendf(&remote->push, "HEAD:refs/heads/%s", frag);
298
remote->fetch_tags = 1; /* always auto-follow */
302
- strbuf_release(&buf);
299
}
300
301
static int handle_config(const char *key, const char *value, void *cb)