Subject: [PATCH 05/10] clone: new option to set preferred pack version for transfer

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Thu, 26 Sep 2013 09:26:44 +0700

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 Documentation/git-clone.txt |  4 ++++
 builtin/clone.c             | 19 +++++++++++++++++++
 t/t5601-clone.sh            | 12 ++++++++++++
 3 files changed, 35 insertions(+)

diff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt
index 450f158..6299a70 100644
--- a/Documentation/git-clone.txt
+++ b/Documentation/git-clone.txt
@@ -217,6 +217,10 @@ objects from the source repository into a pack in the cloned repository.
 	The result is Git repository can be separated from working
 	tree.
 
+--pack-version=<n>::
+	Define the preferred pack format version for data transfer.
+	Valid values are 2 and 4. Default value is specified by
+	core.preferredPackVersion setting. See linkgit:git-config[1].
 
 <repository>::
 	The (possibly remote) repository to clone from.  See the
diff --git a/builtin/clone.c b/builtin/clone.c
index 430307b..9c0e296 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -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 = -1;
+static int pack_version;
 static struct string_list option_config;
 static struct string_list option_reference;
 
@@ -98,6 +99,8 @@ static struct option builtin_clone_options[] = {
 		   N_("separate git dir from working tree")),
 	OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
 			N_("set config inside the new repository")),
+	OPT_INTEGER(0, "pack-version", &pack_version,
+		    N_("preferred pack version for transfer")),
 	OPT_END()
 };
 
@@ -764,6 +767,11 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 		usage_msg_opt(_("You must specify a repository to clone."),
 			builtin_clone_usage, builtin_clone_options);
 
+	if (!pack_version)
+		pack_version = core_default_pack_version;
+	if (pack_version != 2 && pack_version != 4)
+		die(_("invalid pack version %d"), pack_version);
+
 	if (option_single_branch == -1)
 		option_single_branch = option_depth ? 1 : 0;
 
@@ -794,6 +802,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 	is_local = option_local != 0 && path && !is_bundle;
 	if (is_local && option_depth)
 		warning(_("--depth is ignored in local clones; use file:// instead."));
+	if (is_local && pack_version == 4)
+		warning(_("--pack-version is ignored in local clones; use file:// instead."));
 	if (option_local > 0 && !is_local)
 		warning(_("--local is ignored"));
 
@@ -868,6 +878,12 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 	} else {
 		strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
 	}
+	if (pack_version != core_default_pack_version) {
+		struct strbuf sb = STRBUF_INIT;
+		strbuf_addf(&sb, "%d", pack_version);
+		git_config_set("core.preferredPackVersion", sb.buf);
+		strbuf_release(&sb);
+	}
 
 	strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
 	strbuf_addf(&key, "remote.%s.url", option_origin);
@@ -903,6 +919,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 			transport_set_option(transport, TRANS_OPT_UPLOADPACK,
 					     option_upload_pack);
 
+		if (pack_version == 4)
+			transport_set_option(transport, TRANS_OPT_PACKV4, "yes");
+
 		if (transport->smart_options && !option_depth)
 			transport->smart_options->check_self_contained_and_connected = 1;
 	}
diff --git a/t/t5601-clone.sh b/t/t5601-clone.sh
index 0629149..8118524 100755
--- a/t/t5601-clone.sh
+++ b/t/t5601-clone.sh
@@ -285,4 +285,16 @@ test_expect_success NOT_MINGW,NOT_CYGWIN 'clone local path foo:bar' '
 	git clone "./foo:bar" foobar
 '
 
+test_expect_success 'clone --pack-version=4' '
+	git clone --pack-version=4 --no-local src pv4 &&
+	P=`ls pv4/.git/objects/pack/pack-*.pack` &&
+	# Offset 4 is pack version
+	test-dump ntohl "$P" 4 >ver.actual &&
+	echo 4 >ver.expected &&
+	test_cmp ver.expected ver.actual &&
+	git --git-dir=pv4/.git config --int core.preferredPackVersion >cfgver.actual &&
+	echo 4 >cfgver.expected &&
+	test_cmp cfgver.expected cfgver.actual
+'
+
 test_done
-- 
1.8.2.82.gc24b958