remote: add function to create detached remotes Right now it is only possible to create remotes from a repository. While this is probably the most common use-case, there are commands which make sense even without a repository, e.g. the equivalence of `git ls-remote`. Add a new function `git_remote_create_detached`, which simply accepts a URL.
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
diff --git a/AUTHORS b/AUTHORS
index 61e2113..458ff06 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -22,6 +22,7 @@ Dmitry Kakurin
Dmitry Kovega
Emeric Fermas
Emmanuel Rodriguez
+Eric Myhre
Florian Forster
Holger Weiss
Ingmar Vanhassel
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bcf8160..64fafc2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -138,6 +138,10 @@ v0.25
`git_merge_driver_source_file_options()` added as accessors to
`git_merge_driver_source`.
+* `git_remote_create_unattached()` creates a remote that is not associated
+ to any repository (and does not apply configuration like 'insteadof' rules).
+ This is mostly useful for e.g. emulating `git ls-remote` behavior.
+
### API removals
* `git_blob_create_fromchunks()` has been removed in favour of
diff --git a/include/git2/remote.h b/include/git2/remote.h
index e9e4e5b..b1e04d5 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -76,6 +76,24 @@ GIT_EXTERN(int) git_remote_create_anonymous(
const char *url);
/**
+ * Create a remote without a connected local repo
+ *
+ * Create a remote with the given url in-memory. You can use this when
+ * you have a URL instead of a remote's name.
+ *
+ * Contrasted with git_remote_create_anonymous, a detached remote
+ * will not consider any repo configuration values (such as insteadof url
+ * substitutions).
+ *
+ * @param out pointer to the new remote objects
+ * @param url the remote repository's URL
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_remote_create_detached(
+ git_remote **out,
+ const char *url);
+
+/**
* Get the information for a particular remote
*
* The name will be checked for validity.
diff --git a/src/remote.c b/src/remote.c
index 86fda5f..87fc17d 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -197,10 +197,10 @@ static int create_internal(git_remote **out, git_repository *repo, const char *n
git_buf var = GIT_BUF_INIT;
int error = -1;
- /* name is optional */
- assert(out && repo && url);
+ /* repo, name, and fetch are optional */
+ assert(out && url);
- if ((error = git_repository_config_snapshot(&config_ro, repo)) < 0)
+ if (repo && (error = git_repository_config_snapshot(&config_ro, repo)) < 0)
return error;
remote = git__calloc(1, sizeof(git_remote));
@@ -212,7 +212,11 @@ static int create_internal(git_remote **out, git_repository *repo, const char *n
(error = canonicalize_url(&canonical_url, url)) < 0)
goto on_error;
- remote->url = apply_insteadof(config_ro, canonical_url.ptr, GIT_DIRECTION_FETCH);
+ if (repo) {
+ remote->url = apply_insteadof(config_ro, canonical_url.ptr, GIT_DIRECTION_FETCH);
+ } else {
+ remote->url = git__strdup(canonical_url.ptr);
+ }
GITERR_CHECK_ALLOC(remote->url);
if (name != NULL) {
@@ -222,8 +226,9 @@ static int create_internal(git_remote **out, git_repository *repo, const char *n
if ((error = git_buf_printf(&var, CONFIG_URL_FMT, name)) < 0)
goto on_error;
- if ((error = git_repository_config__weakptr(&config_rw, repo)) < 0 ||
- (error = git_config_set_string(config_rw, var.ptr, canonical_url.ptr)) < 0)
+ if (repo &&
+ ((error = git_repository_config__weakptr(&config_rw, repo)) < 0 ||
+ (error = git_config_set_string(config_rw, var.ptr, canonical_url.ptr)) < 0))
goto on_error;
}
@@ -235,7 +240,7 @@ static int create_internal(git_remote **out, git_repository *repo, const char *n
if (name && (error = write_add_refspec(repo, name, fetch, true)) < 0)
goto on_error;
- if ((error = lookup_remote_prune_config(remote, config_ro, name)) < 0)
+ if (repo && (error = lookup_remote_prune_config(remote, config_ro, name)) < 0)
goto on_error;
/* Move the data over to where the matching functions can find them */
@@ -330,6 +335,11 @@ int git_remote_create_anonymous(git_remote **out, git_repository *repo, const ch
return create_internal(out, repo, NULL, url, NULL);
}
+int git_remote_create_detached(git_remote **out, const char *url)
+{
+ return create_internal(out, NULL, NULL, url, NULL);
+}
+
int git_remote_dup(git_remote **dest, git_remote *source)
{
size_t i;