]> git.ipfire.org Git - thirdparty/git.git/commitdiff
clone: accept config options on the command line
authorJeff King <peff@peff.net>
Thu, 9 Jun 2011 20:56:19 +0000 (16:56 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 22 Jun 2011 18:25:21 +0000 (11:25 -0700)
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>
Documentation/git-clone.txt
builtin/clone.c
t/t5708-clone-config.sh [new file with mode: 0755]

index b093e45497248076c335391f3e95b6384520ef6b..4b8b26b75e63cc56e679d2e2c6d8fd1240010419 100644 (file)
@@ -159,6 +159,17 @@ objects from the source repository into a pack in the cloned repository.
        Specify the directory from which templates will be used;
        (See the "TEMPLATE DIRECTORY" section of linkgit:git-init[1].)
 
+--config <key>=<value>::
+-c <key>=<value>::
+       Set a configuration variable in the newly-created repository;
+       this takes effect immediately after the repository is
+       initialized, but before the remote history is fetched or any
+       files checked out.  The key is in the same format as expected by
+       linkgit:git-config[1] (e.g., `core.eol=true`). If multiple
+       values are given for the same key, each value will be written to
+       the config file. This makes it safe, for example, to add
+       additional fetch refspecs to the origin remote.
+
 --depth <depth>::
        Create a 'shallow' clone with a history truncated to the
        specified number of revisions.  A shallow repository has a
index f579794d9a93a0e55289921f20b8f68b85211ca1..a15784a7b8406be3ead20839aab0f8de2cc52f3d 100644 (file)
@@ -46,6 +46,7 @@ static const char *real_git_dir;
 static char *option_upload_pack = "git-upload-pack";
 static int option_verbosity;
 static int option_progress;
+static struct string_list option_config;
 
 static struct option builtin_clone_options[] = {
        OPT__VERBOSITY(&option_verbosity),
@@ -83,7 +84,8 @@ static struct option builtin_clone_options[] = {
                    "create a shallow clone of that depth"),
        OPT_STRING(0, "separate-git-dir", &real_git_dir, "gitdir",
                   "separate git dir from working tree"),
-
+       OPT_STRING_LIST('c', "config", &option_config, "key=value",
+                       "set config inside the new repository"),
        OPT_END()
 };
 
@@ -364,6 +366,22 @@ static void write_remote_refs(const struct ref *local_refs)
        clear_extra_refs();
 }
 
+static int write_one_config(const char *key, const char *value, void *data)
+{
+       return git_config_set_multivar(key, value ? value : "true", "^$", 0);
+}
+
+static void write_config(struct string_list *config)
+{
+       int i;
+
+       for (i = 0; i < config->nr; i++) {
+               if (git_config_parse_parameter(config->items[i].string,
+                                              write_one_config, NULL) < 0)
+                       die("unable to write parameters to config file");
+       }
+}
+
 int cmd_clone(int argc, const char **argv, const char *prefix)
 {
        int is_bundle = 0, is_local;
@@ -482,6 +500,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
                        printf(_("Cloning into %s...\n"), dir);
        }
        init_db(option_template, INIT_DB_QUIET);
+       write_config(&option_config);
 
        /*
         * At this point, the config exists, so we do not need the
diff --git a/t/t5708-clone-config.sh b/t/t5708-clone-config.sh
new file mode 100755 (executable)
index 0000000..27d730c
--- /dev/null
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+test_description='tests for git clone -c key=value'
+. ./test-lib.sh
+
+test_expect_success 'clone -c sets config in cloned repo' '
+       rm -rf child &&
+       git clone -c core.foo=bar . child &&
+       echo bar >expect &&
+       git --git-dir=child/.git config core.foo >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'clone -c can set multi-keys' '
+       rm -rf child &&
+       git clone -c core.foo=bar -c core.foo=baz . child &&
+       { echo bar; echo baz; } >expect &&
+       git --git-dir=child/.git config --get-all core.foo >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'clone -c without a value is boolean true' '
+       rm -rf child &&
+       git clone -c core.foo . child &&
+       echo true >expect &&
+       git --git-dir=child/.git config --bool core.foo >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'clone -c config is available during clone' '
+       echo content >file &&
+       git add file &&
+       git commit -m one &&
+       rm -rf child &&
+       git clone -c core.autocrlf . child &&
+       printf "content\\r\\n" >expect &&
+       test_cmp expect child/file
+'
+
+test_done