clone: allow configurable default for `-o`/`--origin`
authorSean Barag <redacted>
Thu, 1 Oct 2020 03:46:16 +0000 (03:46 +0000)
committerJunio C Hamano <redacted>
Thu, 1 Oct 2020 05:09:13 +0000 (22:09 -0700)
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 <redacted>
Helped-by: Johannes Schindelin <redacted>
Helped-by: Derrick Stolee <redacted>
Helped-by: Andrei Rybak <redacted>
Signed-off-by: Sean Barag <redacted>
Signed-off-by: Junio C Hamano <redacted>
Documentation/config.txt
Documentation/config/clone.txt [new file with mode: 0644]
Documentation/git-clone.txt
builtin/clone.c
t/t5606-clone-options.sh

index 3042d80978d6158f827fa1ea272edb7d2b2d571b..354874facf9101b53f8d747edd2c628208a4d867 100644 (file)
@@ -334,6 +334,8 @@ include::config/checkout.txt[]
 
 include::config/clean.txt[]
 
+include::config/clone.txt[]
+
 include::config/color.txt[]
 
 include::config/column.txt[]
diff --git a/Documentation/config/clone.txt b/Documentation/config/clone.txt
new file mode 100644 (file)
index 0000000..47de36a
--- /dev/null
@@ -0,0 +1,4 @@
+clone.defaultRemoteName::
+       The name of the remote to create when cloning a repository.  Defaults to
+       `origin`, and can be overridden by passing the `--origin` command-line
+       option to linkgit:git-clone[1].
index c898310099895f92e884b7852af28786c6564c8b..f04bf6e6badbd7701e9bce89d4a2e15089d602d1 100644 (file)
@@ -183,8 +183,9 @@ objects from the source repository into a pack in the cloned repository.
 
 -o <name>::
 --origin <name>::
-       Instead of using the remote name `origin` to keep track
-       of the upstream repository, use `<name>`.
+       Instead of using the remote name `origin` to keep track of the upstream
+       repository, use `<name>`.  Overrides `clone.defaultRemoteName` from the
+       config.
 
 -b <name>::
 --branch <name>::
index c33fa127ce9c3b21726c529c9b4b867511e5a7c9..a144e7f35bf5dc2908d94766be413f41db273f1d 100644 (file)
@@ -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);
index 0422b242589a2f94ac324741e8c4aa28005488f2..5e201e7d8581efbbed0046a347d5ce5222c46f18 100755 (executable)
@@ -19,6 +19,13 @@ test_expect_success 'clone -o' '
 
 '
 
+test_expect_success 'rejects invalid -o/--origin' '
+
+       test_must_fail git clone -o "bad...name" parent clone-bad-name 2>err &&
+       test_i18ngrep "'\''bad...name'\'' is not a valid remote name" err
+
+'
+
 test_expect_success 'disallows --bare with --origin' '
 
        test_must_fail git clone -o foo --bare parent clone-bare-o 2>err &&
@@ -63,6 +70,21 @@ test_expect_success 'prefers -c config over --template config' '
 
 '
 
+test_expect_success 'prefers config "clone.defaultRemoteName" over default' '
+
+       test_config_global clone.defaultRemoteName from_config &&
+       git clone parent clone-config-origin &&
+       git -C clone-config-origin rev-parse --verify refs/remotes/from_config/master
+
+'
+
+test_expect_success 'prefers --origin over -c config' '
+
+       git clone -c clone.defaultRemoteName=inline --origin from_option parent clone-o-and-inline-config &&
+       git -C clone-o-and-inline-config rev-parse --verify refs/remotes/from_option/master
+
+'
+
 test_expect_success 'redirected clone does not show progress' '
 
        git clone "file://$(pwd)/parent" clone-redirected >out 2>err &&
git clone https://git.99rst.org/PROJECT