Skip to content

Commit de9ed3e

Browse files
sjbaraggitster
authored andcommitted
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 <[email protected]> Helped-by: Johannes Schindelin <[email protected]> Helped-by: Derrick Stolee <[email protected]> Helped-by: Andrei Rybak <[email protected]> Signed-off-by: Sean Barag <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 75ca390 commit de9ed3e

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

Documentation/config.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ include::config/checkout.txt[]
334334

335335
include::config/clean.txt[]
336336

337+
include::config/clone.txt[]
338+
337339
include::config/color.txt[]
338340

339341
include::config/column.txt[]

Documentation/config/clone.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
clone.defaultRemoteName::
2+
The name of the remote to create when cloning a repository. Defaults to
3+
`origin`, and can be overridden by passing the `--origin` command-line
4+
option to linkgit:git-clone[1].

Documentation/git-clone.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ objects from the source repository into a pack in the cloned repository.
183183

184184
-o <name>::
185185
--origin <name>::
186-
Instead of using the remote name `origin` to keep track
187-
of the upstream repository, use `<name>`.
186+
Instead of using the remote name `origin` to keep track of the upstream
187+
repository, use `<name>`. Overrides `clone.defaultRemoteName` from the
188+
config.
188189

189190
-b <name>::
190191
--branch <name>::

builtin/clone.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static int option_shallow_submodules;
5353
static int deepen;
5454
static char *option_template, *option_depth, *option_since;
5555
static char *option_origin = NULL;
56-
static char *remote_name = "origin";
56+
static char *remote_name = NULL;
5757
static char *option_branch = NULL;
5858
static struct string_list option_not = STRING_LIST_INIT_NODUP;
5959
static const char *real_git_dir;
@@ -854,6 +854,10 @@ static int checkout(int submodule_progress)
854854

855855
static int git_clone_config(const char *k, const char *v, void *cb)
856856
{
857+
if (!strcmp(k, "clone.defaultremotename")) {
858+
free(remote_name);
859+
remote_name = xstrdup(v);
860+
}
857861
return git_default_config(k, v, cb);
858862
}
859863

@@ -1010,12 +1014,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
10101014
option_no_checkout = 1;
10111015
}
10121016

1013-
if (option_origin)
1014-
remote_name = option_origin;
1015-
1016-
if (!valid_remote_name(remote_name))
1017-
die(_("'%s' is not a valid remote name"), remote_name);
1018-
10191017
repo_name = argv[0];
10201018

10211019
path = get_repo_path(repo_name, &is_bundle);
@@ -1158,6 +1156,19 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
11581156
*/
11591157
git_config(git_clone_config, NULL);
11601158

1159+
/*
1160+
* apply the remote name provided by --origin only after this second
1161+
* call to git_config, to ensure it overrides all config-based values.
1162+
*/
1163+
if (option_origin != NULL)
1164+
remote_name = xstrdup(option_origin);
1165+
1166+
if (remote_name == NULL)
1167+
remote_name = xstrdup("origin");
1168+
1169+
if (!valid_remote_name(remote_name))
1170+
die(_("'%s' is not a valid remote name"), remote_name);
1171+
11611172
if (option_bare) {
11621173
if (option_mirror)
11631174
src_ref_prefix = "refs/";
@@ -1358,6 +1369,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
13581369
junk_mode = JUNK_LEAVE_REPO;
13591370
err = checkout(submodule_progress);
13601371

1372+
free(remote_name);
13611373
strbuf_release(&reflog_msg);
13621374
strbuf_release(&branch_top);
13631375
strbuf_release(&key);

t/t5606-clone-options.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ test_expect_success 'clone -o' '
1919
2020
'
2121

22+
test_expect_success 'rejects invalid -o/--origin' '
23+
24+
test_must_fail git clone -o "bad...name" parent clone-bad-name 2>err &&
25+
test_i18ngrep "'\''bad...name'\'' is not a valid remote name" err
26+
27+
'
28+
2229
test_expect_success 'disallows --bare with --origin' '
2330
2431
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' '
6370
6471
'
6572

73+
test_expect_success 'prefers config "clone.defaultRemoteName" over default' '
74+
75+
test_config_global clone.defaultRemoteName from_config &&
76+
git clone parent clone-config-origin &&
77+
git -C clone-config-origin rev-parse --verify refs/remotes/from_config/master
78+
79+
'
80+
81+
test_expect_success 'prefers --origin over -c config' '
82+
83+
git clone -c clone.defaultRemoteName=inline --origin from_option parent clone-o-and-inline-config &&
84+
git -C clone-o-and-inline-config rev-parse --verify refs/remotes/from_option/master
85+
86+
'
87+
6688
test_expect_success 'redirected clone does not show progress' '
6789
6890
git clone "file://$(pwd)/parent" clone-redirected >out 2>err &&

0 commit comments

Comments
 (0)