summaryrefslogtreecommitdiff
path: root/builtin/clone.c
diff options
context:
space:
mode:
authorSean Barag <sean@barag.org>2020-10-01 03:46:16 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-10-01 05:09:13 (GMT)
commitde9ed3ef3740f8227cc924e845032954d1f1b1b7 (patch)
tree2854c591199100050994c1bf4876b6e4e8ba4d30 /builtin/clone.c
parent75ca3906b1ea6a00a20ef16c889e9d6a15a8defc (diff)
downloadgit-de9ed3ef3740f8227cc924e845032954d1f1b1b7.zip
git-de9ed3ef3740f8227cc924e845032954d1f1b1b7.tar.gz
git-de9ed3ef3740f8227cc924e845032954d1f1b1b7.tar.bz2
clone: allow configurable default for `-o`/`--origin`
While the default remote name of "origin" can be changed at clone-time with `git clone`'s `--origin` option, it was previously not possible to specify a default value for the name of that remote. Add support for a new `clone.defaultRemoteName` config, with the newly-created remote name resolved in priority order: 1. (Highest priority) A remote name passed directly to `git clone -o` 2. A `clone.defaultRemoteName=new_name` in config `git clone -c` 3. A `clone.defaultRemoteName` value set in `/path/to/template/config`, where `--template=/path/to/template` is provided 4. A `clone.defaultRemoteName` value set in a non-template config file 5. The default value of `origin` Helped-by: Junio C Hamano <gitster@pobox.com> Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de> Helped-by: Derrick Stolee <stolee@gmail.com> Helped-by: Andrei Rybak <rybak.a.v@gmail.com> Signed-off-by: Sean Barag <sean@barag.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/clone.c')
-rw-r--r--builtin/clone.c26
1 files changed, 19 insertions, 7 deletions
diff --git a/builtin/clone.c b/builtin/clone.c
index c33fa12..a144e7f 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -53,7 +53,7 @@ static int option_shallow_submodules;
static int deepen;
static char *option_template, *option_depth, *option_since;
static char *option_origin = NULL;
-static char *remote_name = "origin";
+static char *remote_name = NULL;
static char *option_branch = NULL;
static struct string_list option_not = STRING_LIST_INIT_NODUP;
static const char *real_git_dir;
@@ -854,6 +854,10 @@ static int checkout(int submodule_progress)
static int git_clone_config(const char *k, const char *v, void *cb)
{
+ if (!strcmp(k, "clone.defaultremotename")) {
+ free(remote_name);
+ remote_name = xstrdup(v);
+ }
return git_default_config(k, v, cb);
}
@@ -1010,12 +1014,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
option_no_checkout = 1;
}
- if (option_origin)
- remote_name = option_origin;
-
- if (!valid_remote_name(remote_name))
- die(_("'%s' is not a valid remote name"), remote_name);
-
repo_name = argv[0];
path = get_repo_path(repo_name, &is_bundle);
@@ -1158,6 +1156,19 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
*/
git_config(git_clone_config, NULL);
+ /*
+ * apply the remote name provided by --origin only after this second
+ * call to git_config, to ensure it overrides all config-based values.
+ */
+ if (option_origin != NULL)
+ remote_name = xstrdup(option_origin);
+
+ if (remote_name == NULL)
+ remote_name = xstrdup("origin");
+
+ if (!valid_remote_name(remote_name))
+ die(_("'%s' is not a valid remote name"), remote_name);
+
if (option_bare) {
if (option_mirror)
src_ref_prefix = "refs/";
@@ -1358,6 +1369,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
junk_mode = JUNK_LEAVE_REPO;
err = checkout(submodule_progress);
+ free(remote_name);
strbuf_release(&reflog_msg);
strbuf_release(&branch_top);
strbuf_release(&key);