clone: accept config options on the command line

Clone does all of init, "remote add", fetch, and checkout without giving the user a chance to intervene and set any configuration. This patch allows you to set config options in the newly created repository after the clone, but before we do any other operations. In many cases, this is a minor convenience over something like: git clone git://... git config core.whatever true But in some cases, it can bring extra efficiency by changing how the fetch or checkout work. For example, setting line-ending config before the checkout avoids having to re-checkout all of the contents with the correct line endings. It also provides a mechanism for passing information to remote helpers during a clone; the helpers may read the git config to influence how they operate. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 9, 2011 at 16:56 UTC 84054f79de35015fc92f73ec4780102dd820e452
3 files changed +71 -1
Documentation/git-clone.txt
+11
@@ -159,6 +159,17 @@ objects from the source repository into a pack in the cloned repository.
159 Specify the directory from which templates will be used;
160 (See the "TEMPLATE DIRECTORY" section of linkgit:git-init[1].)
161
162 +--config <key>=<value>::
163 +-c <key>=<value>::
164 + Set a configuration variable in the newly-created repository;
165 + this takes effect immediately after the repository is
166 + initialized, but before the remote history is fetched or any
167 + files checked out. The key is in the same format as expected by
168 + linkgit:git-config[1] (e.g., `core.eol=true`). If multiple
169 + values are given for the same key, each value will be written to
170 + the config file. This makes it safe, for example, to add
171 + additional fetch refspecs to the origin remote.
172 +
173 --depth <depth>::
174 Create a 'shallow' clone with a history truncated to the
175 specified number of revisions. A shallow repository has a
builtin/clone.c
+20 -1
@@ -46,6 +46,7 @@ static const char *real_git_dir;
46 static char *option_upload_pack = "git-upload-pack";
47 static int option_verbosity;
48 static int option_progress;
49 +static struct string_list option_config;
50
51 static struct option builtin_clone_options[] = {
52 OPT__VERBOSITY(&option_verbosity),
@@ -83,7 +84,8 @@ static struct option builtin_clone_options[] = {
84 "create a shallow clone of that depth"),
85 OPT_STRING(0, "separate-git-dir", &real_git_dir, "gitdir",
86 "separate git dir from working tree"),
86 -
87 + OPT_STRING_LIST('c', "config", &option_config, "key=value",
88 + "set config inside the new repository"),
89 OPT_END()
90 };
91
@@ -364,6 +366,22 @@ static void write_remote_refs(const struct ref *local_refs)
366 clear_extra_refs();
367 }
368
369 +static int write_one_config(const char *key, const char *value, void *data)
370 +{
371 + return git_config_set_multivar(key, value ? value : "true", "^$", 0);
372 +}
373 +
374 +static void write_config(struct string_list *config)
375 +{
376 + int i;
377 +
378 + for (i = 0; i < config->nr; i++) {
379 + if (git_config_parse_parameter(config->items[i].string,
380 + write_one_config, NULL) < 0)
381 + die("unable to write parameters to config file");
382 + }
383 +}
384 +
385 int cmd_clone(int argc, const char **argv, const char *prefix)
386 {
387 int is_bundle = 0, is_local;
@@ -482,6 +500,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
500 printf(_("Cloning into %s...\n"), dir);
501 }
502 init_db(option_template, INIT_DB_QUIET);
503 + write_config(&option_config);
504
505 /*
506 * At this point, the config exists, so we do not need the
t/t5708-clone-config.sh new
+40
@@ -0,0 +1,40 @@
1 +#!/bin/sh
2 +
3 +test_description='tests for git clone -c key=value'
4 +. ./test-lib.sh
5 +
6 +test_expect_success 'clone -c sets config in cloned repo' '
7 + rm -rf child &&
8 + git clone -c core.foo=bar . child &&
9 + echo bar >expect &&
10 + git --git-dir=child/.git config core.foo >actual &&
11 + test_cmp expect actual
12 +'
13 +
14 +test_expect_success 'clone -c can set multi-keys' '
15 + rm -rf child &&
16 + git clone -c core.foo=bar -c core.foo=baz . child &&
17 + { echo bar; echo baz; } >expect &&
18 + git --git-dir=child/.git config --get-all core.foo >actual &&
19 + test_cmp expect actual
20 +'
21 +
22 +test_expect_success 'clone -c without a value is boolean true' '
23 + rm -rf child &&
24 + git clone -c core.foo . child &&
25 + echo true >expect &&
26 + git --git-dir=child/.git config --bool core.foo >actual &&
27 + test_cmp expect actual
28 +'
29 +
30 +test_expect_success 'clone -c config is available during clone' '
31 + echo content >file &&
32 + git add file &&
33 + git commit -m one &&
34 + rm -rf child &&
35 + git clone -c core.autocrlf . child &&
36 + printf "content\\r\\n" >expect &&
37 + test_cmp expect child/file
38 +'
39 +
40 +test_done