Skip to content

Commit 14de739

Browse files
committed
Merge branch 'mv/clone-strbuf'
* mv/clone-strbuf: builtin_clone: use strbuf in cmd_clone() builtin-clone: use strbuf in clone_local() and copy_or_link_directory() builtin-clone: use strbuf in guess_dir_name()
2 parents 2af9664 + b5ff37a commit 14de739

File tree

1 file changed

+59
-50
lines changed

1 file changed

+59
-50
lines changed

builtin-clone.c

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
134134
}
135135

136136
if (is_bare) {
137-
char *result = xmalloc(end - start + 5);
138-
sprintf(result, "%.*s.git", (int)(end - start), start);
139-
return result;
137+
struct strbuf result = STRBUF_INIT;
138+
strbuf_addf(&result, "%.*s.git", (int)(end - start), start);
139+
return strbuf_detach(&result, 0);
140140
}
141141

142142
return xstrndup(start, end - start);
@@ -183,36 +183,38 @@ static void setup_reference(const char *repo)
183183
free(ref_git_copy);
184184
}
185185

186-
static void copy_or_link_directory(char *src, char *dest)
186+
static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest)
187187
{
188188
struct dirent *de;
189189
struct stat buf;
190190
int src_len, dest_len;
191191
DIR *dir;
192192

193-
dir = opendir(src);
193+
dir = opendir(src->buf);
194194
if (!dir)
195-
die("failed to open %s\n", src);
195+
die("failed to open %s\n", src->buf);
196196

197-
if (mkdir(dest, 0777)) {
197+
if (mkdir(dest->buf, 0777)) {
198198
if (errno != EEXIST)
199-
die("failed to create directory %s\n", dest);
200-
else if (stat(dest, &buf))
201-
die("failed to stat %s\n", dest);
199+
die("failed to create directory %s\n", dest->buf);
200+
else if (stat(dest->buf, &buf))
201+
die("failed to stat %s\n", dest->buf);
202202
else if (!S_ISDIR(buf.st_mode))
203-
die("%s exists and is not a directory\n", dest);
203+
die("%s exists and is not a directory\n", dest->buf);
204204
}
205205

206-
src_len = strlen(src);
207-
src[src_len] = '/';
208-
dest_len = strlen(dest);
209-
dest[dest_len] = '/';
206+
strbuf_addch(src, '/');
207+
src_len = src->len;
208+
strbuf_addch(dest, '/');
209+
dest_len = dest->len;
210210

211211
while ((de = readdir(dir)) != NULL) {
212-
strcpy(src + src_len + 1, de->d_name);
213-
strcpy(dest + dest_len + 1, de->d_name);
214-
if (stat(src, &buf)) {
215-
warning ("failed to stat %s\n", src);
212+
strbuf_setlen(src, src_len);
213+
strbuf_addstr(src, de->d_name);
214+
strbuf_setlen(dest, dest_len);
215+
strbuf_addstr(dest, de->d_name);
216+
if (stat(src->buf, &buf)) {
217+
warning ("failed to stat %s\n", src->buf);
216218
continue;
217219
}
218220
if (S_ISDIR(buf.st_mode)) {
@@ -221,17 +223,17 @@ static void copy_or_link_directory(char *src, char *dest)
221223
continue;
222224
}
223225

224-
if (unlink(dest) && errno != ENOENT)
225-
die("failed to unlink %s\n", dest);
226+
if (unlink(dest->buf) && errno != ENOENT)
227+
die("failed to unlink %s\n", dest->buf);
226228
if (!option_no_hardlinks) {
227-
if (!link(src, dest))
229+
if (!link(src->buf, dest->buf))
228230
continue;
229231
if (option_local)
230-
die("failed to create link %s\n", dest);
232+
die("failed to create link %s\n", dest->buf);
231233
option_no_hardlinks = 1;
232234
}
233-
if (copy_file(dest, src, 0666))
234-
die("failed to copy file to %s\n", dest);
235+
if (copy_file(dest->buf, src->buf, 0666))
236+
die("failed to copy file to %s\n", dest->buf);
235237
}
236238
closedir(dir);
237239
}
@@ -240,17 +242,19 @@ static const struct ref *clone_local(const char *src_repo,
240242
const char *dest_repo)
241243
{
242244
const struct ref *ret;
243-
char src[PATH_MAX];
244-
char dest[PATH_MAX];
245+
struct strbuf src = STRBUF_INIT;
246+
struct strbuf dest = STRBUF_INIT;
245247
struct remote *remote;
246248
struct transport *transport;
247249

248250
if (option_shared)
249251
add_to_alternates_file(src_repo);
250252
else {
251-
snprintf(src, PATH_MAX, "%s/objects", src_repo);
252-
snprintf(dest, PATH_MAX, "%s/objects", dest_repo);
253-
copy_or_link_directory(src, dest);
253+
strbuf_addf(&src, "%s/objects", src_repo);
254+
strbuf_addf(&dest, "%s/objects", dest_repo);
255+
copy_or_link_directory(&src, &dest);
256+
strbuf_release(&src);
257+
strbuf_release(&dest);
254258
}
255259

256260
remote = remote_get(src_repo);
@@ -354,8 +358,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
354358
const char *repo_name, *repo, *work_tree, *git_dir;
355359
char *path, *dir;
356360
const struct ref *refs, *head_points_at, *remote_head, *mapped_refs;
357-
char branch_top[256], key[256], value[256];
358-
struct strbuf reflog_msg = STRBUF_INIT;
361+
struct strbuf key = STRBUF_INIT, value = STRBUF_INIT;
362+
struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
359363
struct transport *transport = NULL;
360364
char *src_ref_prefix = "refs/heads/";
361365

@@ -459,35 +463,36 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
459463
if (option_bare) {
460464
if (option_mirror)
461465
src_ref_prefix = "refs/";
462-
strcpy(branch_top, src_ref_prefix);
466+
strbuf_addstr(&branch_top, src_ref_prefix);
463467

464468
git_config_set("core.bare", "true");
465469
} else {
466-
snprintf(branch_top, sizeof(branch_top),
467-
"refs/remotes/%s/", option_origin);
470+
strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
468471
}
469472

470473
if (option_mirror || !option_bare) {
471474
/* Configure the remote */
472475
if (option_mirror) {
473-
snprintf(key, sizeof(key),
474-
"remote.%s.mirror", option_origin);
475-
git_config_set(key, "true");
476+
strbuf_addf(&key, "remote.%s.mirror", option_origin);
477+
git_config_set(key.buf, "true");
478+
strbuf_reset(&key);
476479
}
477480

478-
snprintf(key, sizeof(key), "remote.%s.url", option_origin);
479-
git_config_set(key, repo);
481+
strbuf_addf(&key, "remote.%s.url", option_origin);
482+
git_config_set(key.buf, repo);
483+
strbuf_reset(&key);
480484

481-
snprintf(key, sizeof(key), "remote.%s.fetch", option_origin);
482-
snprintf(value, sizeof(value),
483-
"+%s*:%s*", src_ref_prefix, branch_top);
484-
git_config_set_multivar(key, value, "^$", 0);
485+
strbuf_addf(&key, "remote.%s.fetch", option_origin);
486+
strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
487+
git_config_set_multivar(key.buf, value.buf, "^$", 0);
488+
strbuf_reset(&key);
489+
strbuf_reset(&value);
485490
}
486491

487492
refspec.force = 0;
488493
refspec.pattern = 1;
489494
refspec.src = src_ref_prefix;
490-
refspec.dst = branch_top;
495+
refspec.dst = branch_top.buf;
491496

492497
if (path && !is_bundle)
493498
refs = clone_local(path, git_dir);
@@ -541,18 +546,19 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
541546
head_points_at->old_sha1,
542547
NULL, 0, DIE_ON_ERR);
543548

544-
strbuf_addstr(&head_ref, branch_top);
549+
strbuf_addstr(&head_ref, branch_top.buf);
545550
strbuf_addstr(&head_ref, "HEAD");
546551

547552
/* Remote branch link */
548553
create_symref(head_ref.buf,
549554
head_points_at->peer_ref->name,
550555
reflog_msg.buf);
551556

552-
snprintf(key, sizeof(key), "branch.%s.remote", head);
553-
git_config_set(key, option_origin);
554-
snprintf(key, sizeof(key), "branch.%s.merge", head);
555-
git_config_set(key, head_points_at->name);
557+
strbuf_addf(&key, "branch.%s.remote", head);
558+
git_config_set(key.buf, option_origin);
559+
strbuf_reset(&key);
560+
strbuf_addf(&key, "branch.%s.merge", head);
561+
git_config_set(key.buf, head_points_at->name);
556562
}
557563
} else if (remote_head) {
558564
/* Source had detached HEAD pointing somewhere. */
@@ -602,6 +608,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
602608
}
603609

604610
strbuf_release(&reflog_msg);
611+
strbuf_release(&branch_top);
612+
strbuf_release(&key);
613+
strbuf_release(&value);
605614
junk_pid = 0;
606615
return 0;
607616
}

0 commit comments

Comments
 (0)