Skip to content

Commit 7178c9a

Browse files
committed
Merge branch 'sb/clone-origin' into next
"git clone" learned clone.defaultremotename configuration variable to customize what nickname to use to call the remote the repository was cloned from. * sb/clone-origin: clone: allow configurable default for `-o`/`--origin` clone: read new remote name from remote_name instead of option_origin clone: validate --origin option before use refs: consolidate remote name validation remote: add tests for add and rename with invalid names clone: use more conventional config/option layering clone: add tests for --template and some disallowed option pairs
2 parents c238dab + de9ed3e commit 7178c9a

File tree

9 files changed

+158
-23
lines changed

9 files changed

+158
-23
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: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +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 = NULL;
5657
static char *option_branch = NULL;
5758
static struct string_list option_not = STRING_LIST_INIT_NODUP;
5859
static const char *real_git_dir;
@@ -721,7 +722,7 @@ static void update_head(const struct ref *our, const struct ref *remote,
721722
if (!option_bare) {
722723
update_ref(msg, "HEAD", &our->old_oid, NULL, 0,
723724
UPDATE_REFS_DIE_ON_ERR);
724-
install_branch_config(0, head, option_origin, our->name);
725+
install_branch_config(0, head, remote_name, our->name);
725726
}
726727
} else if (our) {
727728
struct commit *c = lookup_commit_reference(the_repository,
@@ -851,8 +852,26 @@ static int checkout(int submodule_progress)
851852
return err;
852853
}
853854

855+
static int git_clone_config(const char *k, const char *v, void *cb)
856+
{
857+
if (!strcmp(k, "clone.defaultremotename")) {
858+
free(remote_name);
859+
remote_name = xstrdup(v);
860+
}
861+
return git_default_config(k, v, cb);
862+
}
863+
854864
static int write_one_config(const char *key, const char *value, void *data)
855865
{
866+
/*
867+
* give git_clone_config a chance to write config values back to the
868+
* environment, since git_config_set_multivar_gently only deals with
869+
* config-file writes
870+
*/
871+
int apply_failed = git_clone_config(key, value, data);
872+
if (apply_failed)
873+
return apply_failed;
874+
856875
return git_config_set_multivar_gently(key,
857876
value ? value : "true",
858877
CONFIG_REGEX_NONE, 0);
@@ -905,12 +924,12 @@ static void write_refspec_config(const char *src_ref_prefix,
905924
}
906925
/* Configure the remote */
907926
if (value.len) {
908-
strbuf_addf(&key, "remote.%s.fetch", option_origin);
927+
strbuf_addf(&key, "remote.%s.fetch", remote_name);
909928
git_config_set_multivar(key.buf, value.buf, "^$", 0);
910929
strbuf_reset(&key);
911930

912931
if (option_mirror) {
913-
strbuf_addf(&key, "remote.%s.mirror", option_origin);
932+
strbuf_addf(&key, "remote.%s.mirror", remote_name);
914933
git_config_set(key.buf, "true");
915934
strbuf_reset(&key);
916935
}
@@ -963,6 +982,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
963982
struct strvec ref_prefixes = STRVEC_INIT;
964983

965984
packet_trace_identity("clone");
985+
986+
git_config(git_clone_config, NULL);
987+
966988
argc = parse_options(argc, argv, prefix, builtin_clone_options,
967989
builtin_clone_usage, 0);
968990

@@ -991,9 +1013,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
9911013
option_no_checkout = 1;
9921014
}
9931015

994-
if (!option_origin)
995-
option_origin = "origin";
996-
9971016
repo_name = argv[0];
9981017

9991018
path = get_repo_path(repo_name, &is_bundle);
@@ -1124,9 +1143,30 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
11241143
if (real_git_dir)
11251144
git_dir = real_git_dir;
11261145

1146+
/*
1147+
* additional config can be injected with -c, make sure it's included
1148+
* after init_db, which clears the entire config environment.
1149+
*/
11271150
write_config(&option_config);
11281151

1129-
git_config(git_default_config, NULL);
1152+
/*
1153+
* re-read config after init_db and write_config to pick up any config
1154+
* injected by --template and --config, respectively.
1155+
*/
1156+
git_config(git_clone_config, NULL);
1157+
1158+
/*
1159+
* apply the remote name provided by --origin only after this second
1160+
* call to git_config, to ensure it overrides all config-based values.
1161+
*/
1162+
if (option_origin != NULL)
1163+
remote_name = xstrdup(option_origin);
1164+
1165+
if (remote_name == NULL)
1166+
remote_name = xstrdup("origin");
1167+
1168+
if (!valid_remote_name(remote_name))
1169+
die(_("'%s' is not a valid remote name"), remote_name);
11301170

11311171
if (option_bare) {
11321172
if (option_mirror)
@@ -1135,15 +1175,15 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
11351175

11361176
git_config_set("core.bare", "true");
11371177
} else {
1138-
strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
1178+
strbuf_addf(&branch_top, "refs/remotes/%s/", remote_name);
11391179
}
11401180

1141-
strbuf_addf(&key, "remote.%s.url", option_origin);
1181+
strbuf_addf(&key, "remote.%s.url", remote_name);
11421182
git_config_set(key.buf, repo);
11431183
strbuf_reset(&key);
11441184

11451185
if (option_no_tags) {
1146-
strbuf_addf(&key, "remote.%s.tagOpt", option_origin);
1186+
strbuf_addf(&key, "remote.%s.tagOpt", remote_name);
11471187
git_config_set(key.buf, "--no-tags");
11481188
strbuf_reset(&key);
11491189
}
@@ -1154,7 +1194,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
11541194
if (option_sparse_checkout && git_sparse_checkout_init(dir))
11551195
return 1;
11561196

1157-
remote = remote_get(option_origin);
1197+
remote = remote_get(remote_name);
11581198

11591199
refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix,
11601200
branch_top.buf);
@@ -1266,15 +1306,15 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12661306

12671307
if (!our_head_points_at)
12681308
die(_("Remote branch %s not found in upstream %s"),
1269-
option_branch, option_origin);
1309+
option_branch, remote_name);
12701310
}
12711311
else
12721312
our_head_points_at = remote_head_points_at;
12731313
}
12741314
else {
12751315
if (option_branch)
12761316
die(_("Remote branch %s not found in upstream %s"),
1277-
option_branch, option_origin);
1317+
option_branch, remote_name);
12781318

12791319
warning(_("You appear to have cloned an empty repository."));
12801320
mapped_refs = NULL;
@@ -1286,7 +1326,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12861326
const char *branch = git_default_branch_name();
12871327
char *ref = xstrfmt("refs/heads/%s", branch);
12881328

1289-
install_branch_config(0, branch, option_origin, ref);
1329+
install_branch_config(0, branch, remote_name, ref);
12901330
free(ref);
12911331
}
12921332
}
@@ -1295,7 +1335,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12951335
remote_head_points_at, &branch_top);
12961336

12971337
if (filter_options.choice)
1298-
partial_clone_register(option_origin, &filter_options);
1338+
partial_clone_register(remote_name, &filter_options);
12991339

13001340
if (is_local)
13011341
clone_local(path, git_dir);
@@ -1327,6 +1367,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
13271367
junk_mode = JUNK_LEAVE_REPO;
13281368
err = checkout(submodule_progress);
13291369

1370+
free(remote_name);
13301371
strbuf_release(&reflog_msg);
13311372
strbuf_release(&branch_top);
13321373
strbuf_release(&key);

builtin/remote.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ static int add(int argc, const char **argv)
194194
if (remote_is_configured(remote, 1))
195195
die(_("remote %s already exists."), name);
196196

197-
strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
198-
if (!valid_fetch_refspec(buf2.buf))
197+
if (!valid_remote_name(name))
199198
die(_("'%s' is not a valid remote name"), name);
200199

201200
strbuf_addf(&buf, "remote.%s.url", name);
@@ -696,11 +695,9 @@ static int mv(int argc, const char **argv)
696695
if (remote_is_configured(newremote, 1))
697696
die(_("remote %s already exists."), rename.new_name);
698697

699-
strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new_name);
700-
if (!valid_fetch_refspec(buf.buf))
698+
if (!valid_remote_name(rename.new_name))
701699
die(_("'%s' is not a valid remote name"), rename.new_name);
702700

703-
strbuf_reset(&buf);
704701
strbuf_addf(&buf, "remote.%s", rename.old_name);
705702
strbuf_addf(&buf2, "remote.%s", rename.new_name);
706703
if (git_config_rename_section(buf.buf, buf2.buf) < 1)

refspec.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,16 @@ int valid_fetch_refspec(const char *fetch_refspec_str)
245245
return ret;
246246
}
247247

248+
int valid_remote_name(const char *name)
249+
{
250+
int result;
251+
struct strbuf refspec = STRBUF_INIT;
252+
strbuf_addf(&refspec, "refs/heads/test:refs/remotes/%s/test", name);
253+
result = valid_fetch_refspec(refspec.buf);
254+
strbuf_release(&refspec);
255+
return result;
256+
}
257+
248258
void refspec_ref_prefixes(const struct refspec *rs,
249259
struct strvec *ref_prefixes)
250260
{

refspec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void refspec_appendn(struct refspec *rs, const char **refspecs, int nr);
6464
void refspec_clear(struct refspec *rs);
6565

6666
int valid_fetch_refspec(const char *refspec);
67+
int valid_remote_name(const char *name);
6768

6869
struct strvec;
6970
/*

t/t5505-remote.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ test_expect_success 'rename errors out early when deleting non-existent branch'
179179
)
180180
'
181181

182+
test_expect_success 'rename errors out early when when new name is invalid' '
183+
test_config remote.foo.vcs bar &&
184+
echo "fatal: '\''invalid...name'\'' is not a valid remote name" >expect &&
185+
test_must_fail git remote rename foo invalid...name 2>actual &&
186+
test_i18ncmp expect actual
187+
'
188+
182189
test_expect_success 'add existing foreign_vcs remote' '
183190
test_config remote.foo.vcs bar &&
184191
echo "fatal: remote foo already exists." >expect &&
@@ -194,6 +201,12 @@ test_expect_success 'add existing foreign_vcs remote' '
194201
test_i18ncmp expect actual
195202
'
196203

204+
test_expect_success 'add invalid foreign_vcs remote' '
205+
echo "fatal: '\''invalid...name'\'' is not a valid remote name" >expect &&
206+
test_must_fail git remote add invalid...name bar 2>actual &&
207+
test_i18ncmp expect actual
208+
'
209+
197210
cat >test/expect <<EOF
198211
* remote origin
199212
Fetch URL: $(pwd)/one

t/t5606-clone-options.sh

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,73 @@ test_expect_success 'setup' '
1515
test_expect_success 'clone -o' '
1616
1717
git clone -o foo parent clone-o &&
18-
(cd clone-o && git rev-parse --verify refs/remotes/foo/master)
18+
git -C clone-o rev-parse --verify refs/remotes/foo/master
19+
20+
'
21+
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+
29+
test_expect_success 'disallows --bare with --origin' '
30+
31+
test_must_fail git clone -o foo --bare parent clone-bare-o 2>err &&
32+
test_debug "cat err" &&
33+
test_i18ngrep -e "--bare and --origin foo options are incompatible" err
34+
35+
'
36+
37+
test_expect_success 'disallows --bare with --separate-git-dir' '
38+
39+
test_must_fail git clone --bare --separate-git-dir dot-git-destiation parent clone-bare-sgd 2>err &&
40+
test_debug "cat err" &&
41+
test_i18ngrep -e "--bare and --separate-git-dir are incompatible" err
42+
43+
'
44+
45+
test_expect_success 'uses "origin" for default remote name' '
46+
47+
git clone parent clone-default-origin &&
48+
git -C clone-default-origin rev-parse --verify refs/remotes/origin/master
49+
50+
'
51+
52+
test_expect_success 'prefers --template config over normal config' '
53+
54+
template="$TRASH_DIRECTORY/template-with-config" &&
55+
mkdir "$template" &&
56+
git config --file "$template/config" foo.bar from_template &&
57+
test_config_global foo.bar from_global &&
58+
git clone "--template=$template" parent clone-template-config &&
59+
test "$(git -C clone-template-config config --local foo.bar)" = "from_template"
60+
61+
'
62+
63+
test_expect_success 'prefers -c config over --template config' '
64+
65+
template="$TRASH_DIRECTORY/template-with-ignored-config" &&
66+
mkdir "$template" &&
67+
git config --file "$template/config" foo.bar from_template &&
68+
git clone "--template=$template" -c foo.bar=inline parent clone-template-inline-config &&
69+
test "$(git -C clone-template-inline-config config --local foo.bar)" = "inline"
70+
71+
'
72+
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
1985
2086
'
2187

0 commit comments

Comments
 (0)