Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 18d7896

Browse files
committed
clone: re-use the local transport's path resolution
Whe already worked out the kinks with the function used in the local transport. Expose it and make use of it in the local clone method instead of trying to work it out again.
1 parent bccb36e commit 18d7896

File tree

4 files changed

+26
-35
lines changed

4 files changed

+26
-35
lines changed

src/clone.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -472,11 +472,10 @@ static bool can_link(const char *src, const char *dst, int link)
472472

473473
int git_clone_local_into(git_repository *repo, git_remote *remote, const git_checkout_options *co_opts, const char *branch, int link, const git_signature *signature)
474474
{
475-
int error, root, flags;
475+
int error, flags;
476476
git_repository *src;
477477
git_buf src_odb = GIT_BUF_INIT, dst_odb = GIT_BUF_INIT, src_path = GIT_BUF_INIT;
478478
git_buf reflog_message = GIT_BUF_INIT;
479-
const char *url;
480479

481480
assert(repo && remote);
482481

@@ -490,19 +489,8 @@ int git_clone_local_into(git_repository *repo, git_remote *remote, const git_che
490489
* repo, if it's not rooted, the path should be relative to
491490
* the repository's worktree/gitdir.
492491
*/
493-
url = git_remote_url(remote);
494-
if (!git__prefixcmp(url, "file://"))
495-
root = strlen("file://");
496-
else
497-
root = git_path_root(url);
498-
499-
if (root >= 0)
500-
git_buf_puts(&src_path, url + root);
501-
else
502-
git_buf_joinpath(&src_path, repository_base(repo), url);
503-
504-
if (git_buf_oom(&src_path))
505-
return -1;
492+
if ((error = git_path_from_url_or_path(&src_path, git_remote_url(remote))) < 0)
493+
return error;
506494

507495
/* Copy .git/objects/ from the source to the target */
508496
if ((error = git_repository_open(&src, git_buf_cstr(&src_path))) < 0) {

src/path.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,3 +1127,21 @@ int git_path_dirload_with_stat(
11271127

11281128
return error;
11291129
}
1130+
1131+
int git_path_from_url_or_path(git_buf *local_path_out, const char *url_or_path)
1132+
{
1133+
int error;
1134+
1135+
/* If url_or_path begins with file:// treat it as a URL */
1136+
if (!git__prefixcmp(url_or_path, "file://")) {
1137+
if ((error = git_path_fromurl(local_path_out, url_or_path)) < 0) {
1138+
return error;
1139+
}
1140+
} else { /* We assume url_or_path is already a path */
1141+
if ((error = git_buf_sets(local_path_out, url_or_path)) < 0) {
1142+
return error;
1143+
}
1144+
}
1145+
1146+
return 0;
1147+
}

src/path.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,4 +438,7 @@ extern int git_path_iconv(git_path_iconv_t *ic, char **in, size_t *inlen);
438438

439439
extern bool git_path_does_fs_decompose_unicode(const char *root);
440440

441+
/* Used for paths to repositories on the filesystem */
442+
extern int git_path_from_url_or_path(git_buf *local_path_out, const char *url_or_path);
443+
441444
#endif

src/transports/local.c

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -175,24 +175,6 @@ static int store_refs(transport_local *t)
175175
return -1;
176176
}
177177

178-
static int path_from_url_or_path(git_buf *local_path_out, const char *url_or_path)
179-
{
180-
int error;
181-
182-
/* If url_or_path begins with file:// treat it as a URL */
183-
if (!git__prefixcmp(url_or_path, "file://")) {
184-
if ((error = git_path_fromurl(local_path_out, url_or_path)) < 0) {
185-
return error;
186-
}
187-
} else { /* We assume url_or_path is already a path */
188-
if ((error = git_buf_sets(local_path_out, url_or_path)) < 0) {
189-
return error;
190-
}
191-
}
192-
193-
return 0;
194-
}
195-
196178
/*
197179
* Try to open the url as a git directory. The direction doesn't
198180
* matter in this case because we're calculating the heads ourselves.
@@ -222,7 +204,7 @@ static int local_connect(
222204
t->flags = flags;
223205

224206
/* 'url' may be a url or path; convert to a path */
225-
if ((error = path_from_url_or_path(&buf, url)) < 0) {
207+
if ((error = git_path_from_url_or_path(&buf, url)) < 0) {
226208
git_buf_free(&buf);
227209
return error;
228210
}
@@ -386,7 +368,7 @@ static int local_push(
386368
size_t j;
387369

388370
/* 'push->remote->url' may be a url or path; convert to a path */
389-
if ((error = path_from_url_or_path(&buf, push->remote->url)) < 0) {
371+
if ((error = git_path_from_url_or_path(&buf, push->remote->url)) < 0) {
390372
git_buf_free(&buf);
391373
return error;
392374
}

0 commit comments

Comments
 (0)