Commit fdb116b364c59d1e3692441963ec47616c71b3db

Etienne Samson 2018-06-20T02:27:12

remote: add a creation flag for ignoring url.insteadOf

diff --git a/include/git2/remote.h b/include/git2/remote.h
index b39d081..7962f07 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -42,6 +42,14 @@ GIT_EXTERN(int) git_remote_create(
 		const char *url);
 
 /**
+ * Remote creation options flags
+ */
+typedef enum {
+	/** Ignore the repository apply.insteadOf configuration */
+	GIT_REMOTE_CREATE_SKIP_INSTEADOF = (1 << 0),
+} git_remote_create_flags;
+
+/**
  * Remote creation options structure
  *
  * Initialize with `GIT_REMOTE_CREATE_OPTIONS_INIT`. Alternatively, you can
@@ -65,6 +73,9 @@ typedef struct git_remote_create_options {
 
 	/** The fetchspec the remote should use. */
 	const char *fetchspec;
+
+	/** Additional flags for the remote. See git_remote_create_flags. */
+	unsigned int flags;
 } git_remote_create_options;
 
 #define GIT_REMOTE_CREATE_OPTIONS_VERSION 1
diff --git a/src/remote.c b/src/remote.c
index 1fa7b42..9dcd2fe 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -264,7 +264,7 @@ static int create_internal(git_remote **out, const char *url, const git_remote_c
 		(error = canonicalize_url(&canonical_url, url)) < 0)
 		goto on_error;
 
-	if (opts->repository) {
+	if (opts->repository && !(opts->flags & GIT_REMOTE_CREATE_SKIP_INSTEADOF)) {
 		remote->url = apply_insteadof(config_ro, canonical_url.ptr, GIT_DIRECTION_FETCH);
 	} else {
 		remote->url = git__strdup(canonical_url.ptr);
diff --git a/tests/remote/create.c b/tests/remote/create.c
index cbc6096..9c51ed6 100644
--- a/tests/remote/create.c
+++ b/tests/remote/create.c
@@ -311,6 +311,23 @@ void test_remote_create__with_opts_detached(void)
 	git_remote_free(remote);
 }
 
+
+void test_remote_create__with_opts_insteadof_disabled(void)
+{
+	git_remote *remote;
+	git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;
+
+	opts.repository = _repo;
+	opts.flags = GIT_REMOTE_CREATE_SKIP_INSTEADOF;
+
+	cl_git_pass(git_remote_create_with_opts(&remote, "http://example.com/libgit2/libgit2", &opts));
+
+	cl_assert_equal_s(git_remote_url(remote), "http://example.com/libgit2/libgit2");
+	cl_assert_equal_p(git_remote_pushurl(remote), NULL);
+
+	git_remote_free(remote);
+}
+
 static int create_with_name(git_remote **remote, git_repository *repo, const char *name, const char *url)
 {
 	git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;