[PATCHv4] clone --single: limit the fetch refspec to fetched branch

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



After running "git clone --single", the resulting repository has the
usual default "+refs/heads/*:refs/remotes/origin/*" wildcard fetch
refspec installed, which means that a subsequent "git fetch" will
end up grabbing all the other branches.

Update the fetch refspec to cover only the singly cloned ref instead
to correct this.

Signed-off-by: Ralf Thielow <ralf.thielow@xxxxxxxxx>
---

Changes to v3:
- use commit message from Junio's topic branch
- add comment for the 'detached HEAD' case (also from Junio's topic branch)
(thanks for that)
- add tests for the refspec installed by the clone command

 builtin/clone.c          | 49 ++++++++++++++++++++++++++++------------
 t/t5709-clone-refspec.sh | 59 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 Dateien geändert, 94 Zeilen hinzugefügt(+), 14 Zeilen entfernt(-)
 create mode 100755 t/t5709-clone-refspec.sh

diff --git a/builtin/clone.c b/builtin/clone.c
index 5e8f3ba..be4c62b 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -755,20 +755,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 	}
 
 	strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
-
-	if (option_mirror || !option_bare) {
-		/* Configure the remote */
-		strbuf_addf(&key, "remote.%s.fetch", option_origin);
-		git_config_set_multivar(key.buf, value.buf, "^$", 0);
-		strbuf_reset(&key);
-
-		if (option_mirror) {
-			strbuf_addf(&key, "remote.%s.mirror", option_origin);
-			git_config_set(key.buf, "true");
-			strbuf_reset(&key);
-		}
-	}
-
 	strbuf_addf(&key, "remote.%s.url", option_origin);
 	git_config_set(key.buf, repo);
 	strbuf_reset(&key);
@@ -853,6 +839,41 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 					      "refs/heads/master");
 	}
 
+	if (option_mirror || !option_bare) {
+		strbuf_reset(&value);
+		if (option_single_branch) {
+			if (option_branch)
+				strbuf_addf(&value, "+%s%s:%s%s",
+						src_ref_prefix, option_branch,
+						branch_top.buf, option_branch);
+			else if (remote_head_points_at)
+				strbuf_addf(&value, "+%s:%s%s",
+						remote_head_points_at->name, branch_top.buf,
+						skip_prefix(remote_head_points_at->name, "refs/heads/"));
+			/*
+			 * otherwise, the next "git fetch" will
+			 * simply fetch from HEAD without updating
+			 * any remote tracking branch, which is what
+			 * we want.
+			 */
+		} else {
+			strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
+		}
+		/* Configure the remote */
+		if (value.len) {
+			strbuf_reset(&key);
+			strbuf_addf(&key, "remote.%s.fetch", option_origin);
+			git_config_set_multivar(key.buf, value.buf, "^$", 0);
+			strbuf_reset(&key);
+
+			if (option_mirror) {
+				strbuf_addf(&key, "remote.%s.mirror", option_origin);
+				git_config_set(key.buf, "true");
+				strbuf_reset(&key);
+			}
+		}
+	}
+
 	if (is_local)
 		clone_local(path, git_dir);
 	else if (refs && complete_refs_before_fetch)
diff --git a/t/t5709-clone-refspec.sh b/t/t5709-clone-refspec.sh
new file mode 100755
index 0000000..f4c8e31
--- /dev/null
+++ b/t/t5709-clone-refspec.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+test_description='test refspec written by clone-command'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	echo one >file &&
+	git add file &&
+	git commit -m one &&
+	echo two >file &&
+	git commit -a -m two &&
+	git tag two &&
+	echo three >file &&
+	git commit -a -m three &&
+	git checkout -b foo &&
+	echo four >file &&
+	git commit -a -m four &&
+	git checkout master
+'
+
+test_expect_success 'refspec contains all branches by default' '
+	git clone "file://$PWD" dir_all &&
+	echo "+refs/heads/*:refs/remotes/origin/*" > expected &&
+	git --git-dir=dir_all/.git config --get remote.origin.fetch > actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'refspec contains only master with option --single-branch and remotes HEAD point to master' '
+	git clone --single-branch "file://$PWD" dir_master &&
+	echo "+refs/heads/master:refs/remotes/origin/master" > expected &&
+	git --git-dir=dir_master/.git config --get remote.origin.fetch > actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'refspec contains only foo with option --single-branch and remotes HEAD point to foo' '
+	git checkout foo &&
+	git clone --single-branch "file://$PWD" dir_foo &&
+	echo "+refs/heads/foo:refs/remotes/origin/foo" > expected &&
+	git --git-dir=dir_foo/.git config --get remote.origin.fetch > actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'refspec contains one branch after using option --single-branch with --branch' '
+	git checkout master &&
+	git clone --single-branch --branch foo "file://$PWD" dir_foo2 &&
+	echo "+refs/heads/foo:refs/remotes/origin/foo" > expected &&
+	git --git-dir=dir_foo2/.git config --get remote.origin.fetch > actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'no refspec is written if remotes HEAD is detached' '
+	git checkout two^ &&
+	git clone --single-branch "file://$PWD" dir_detached &&
+	rm expected && touch expected &&
+	git --git-dir=dir_detached/.git config --get remote.origin.fetch > actual
+	test_cmp expected actual
+'
+
+test_done
-- 
1.7.12.396.g6bea32d.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]