Factor out code to convert local "url" into a path. Previously this code was shared between `local_push` and `local_connect`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
diff --git a/src/transports/local.c b/src/transports/local.c
index bd756c4..26ada48 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -156,6 +156,24 @@ on_error:
return -1;
}
+static int path_from_url_or_path(git_buf *local_path_out, const char *url_or_path)
+{
+ int error;
+
+ /* If url_or_path begins with file:// treat it as a URL */
+ if (!git__prefixcmp(url_or_path, "file://")) {
+ if ((error = git_path_fromurl(local_path_out, url_or_path)) < 0) {
+ return error;
+ }
+ } else { /* We assume url_or_path is already a path */
+ if ((error = git_buf_sets(local_path_out, url_or_path)) < 0) {
+ return error;
+ }
+ }
+
+ return 0;
+}
+
/*
* Try to open the url as a git directory. The direction doesn't
* matter in this case because we're calulating the heads ourselves.
@@ -181,17 +199,12 @@ static int local_connect(
t->direction = direction;
t->flags = flags;
- /* The repo layer doesn't want the prefix */
- if (!git__prefixcmp(t->url, "file://")) {
- if (git_path_fromurl(&buf, t->url) < 0) {
- git_buf_free(&buf);
- return -1;
- }
- path = git_buf_cstr(&buf);
-
- } else { /* We assume transport->url is already a path */
- path = t->url;
+ /* 'url' may be a url or path; convert to a path */
+ if ((error = path_from_url_or_path(&buf, url)) < 0) {
+ git_buf_free(&buf);
+ return error;
}
+ path = git_buf_cstr(&buf);
error = git_repository_open(&repo, path);
@@ -350,17 +363,12 @@ static int local_push(
unsigned int i;
size_t j;
- /* The repo layer doesn't want the prefix */
- if (!git__prefixcmp(push->remote->url, "file://")) {
- if (git_path_fromurl(&buf, push->remote->url) < 0) {
- git_buf_free(&buf);
- return -1;
- }
- path = git_buf_cstr(&buf);
-
- } else { /* We assume push->remote->url is already a path */
- path = push->remote->url;
+ /* 'push->remote->url' may be a url or path; convert to a path */
+ if ((error = path_from_url_or_path(&buf, push->remote->url)) < 0) {
+ git_buf_free(&buf);
+ return error;
}
+ path = git_buf_cstr(&buf);
error = git_repository_open(&remote_repo, path);