Skip to contentSkip to conversation

is this behaviour expected for "git clone --single-branch"?

PATCH In review Ralf Thielow 3 participants 39 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

Hi,

I know people which have a separate directory for every branch. In this case it doesn't make sense to download the whole repo with all branches. So I guess the "--single-branch" option is the solution in that case!?! But I'm wondering about it's behaviour.

# first clone the branch of the repo
$git clone --single-branch --branch master myrepo ./master
$cd master
# now calling "git branch -a" to see what I have
$git branch -a
*master
remotes/origin/HEAD -> origin/master
remotes/origin/master

# fine, now pulling from origin
$git pull
From myrepo
* [new branch] foo -> origin/foo
* [new branch] bar -> origin/bar
...

Hm?

# looking again to my branches
$git branch -a
*master
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/bar
remotes/origin/foo
...

After cloning (or fetching), I now have all branches which is not what I want, because I'm only interested in the one I've cloned. I think it's not very useful for the use-case of having one directory for one branch.

$git version
git version 1.7.12.395.g6b149ce

Thanks

#1 lore raw
Quoted 1 line · Ralf Thielow
# looking again to my branches

Don't look at your branches, but look at how the refspecs are configured by "git clone" in .git/config; I suspect we just write the default 'refs/heads/*:refs/remotes/origin/*' pattern.

#2 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 1 line · Ralf Thielow
# looking again to my branches

Don't look at your branches, but look at how the refspecs are configured by "git clone" in .git/config; I suspect we just write the default 'refs/heads/*:refs/remotes/origin/*' pattern.

Indeed.

$ cat .git/config
[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
#3 lore raw

PATCH 1/1 clone: fix refspec on "--single-branch" option

After using "git clone" with the "--single-branch" option, the configured refspec for this repo was "+refs/heads/*:refs/remotes/origin/*". After fetching changes from this repo again, it'll receive all refs instead of the single ref which was used in "--single-branch". Fixing the refspec that it just contains the ref of the branch which was cloned.

---
 builtin/clone.c | 5 ++++-

1 Datei geändert, 4 Zeilen hinzugefügt(+), 1 Zeile entfernt(-)

builtin/clone.c +4 -1
index 5e8f3ba..3e74d55 100644
@@ -754,7 +754,10 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 		strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
 	}
 
-	strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
+	if (option_single_branch)
+		strbuf_addf(&value, "+%s%s:%s%s", src_ref_prefix, option_branch, branch_top.buf, option_branch);
+	else
+		strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
 
 	if (option_mirror || !option_bare) {
 		/* Configure the remote */
Signature · 1 line
1.7.12.395.g6b149ce.dirty
#4 lore raw

Re: [PATCH] clone: fix refspec on "--single-branch" option

Quoted 19 lines · Ralf Thielow

After using "git clone" with the "--single-branch" option, the configured refspec for this repo was "+refs/heads/*:refs/remotes/origin/*". After fetching changes from this repo again, it'll receive all refs instead of the single ref which was used in "--single-branch". Fixing the refspec that it just contains the ref of the branch which was cloned.

---
 builtin/clone.c | 5 ++++-

1 Datei geändert, 4 Zeilen hinzugefügt(+), 1 Zeile entfernt(-)

builtin/clone.c +0 -0
index 5e8f3ba..3e74d55 100644

builtin/clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 760

Quoted 8 lines
builtin/clone.c +4 -1
@@ -754,7 +754,10 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 		strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
 	}
 
-	strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
+	if (option_single_branch)
+		strbuf_addf(&value, "+%s%s:%s%s", src_ref_prefix, option_branch, branch_top.buf, option_branch);
+	else
+		strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);

Who guarantees at this point in the codepath that option_branch is set when option_single_branch is non-zero? Until we talk with the remote, "clone --single-branch" without an explicit "--branch" will not learn which branch at the remote we are going to fetch (it will be their HEAD).

I wonder if this should be more like this:

	if (option_single_branch) {
		if (option_branch)
			Your patch "+refs/heads/foo:refs/remotes/origin/foo";
		else
			"HEAD";
        } else {
        	Original "+refs/heads/*:refs/remotes/origin/*";
	}

That is, "clone --single-branch" will continue fetching from and integrating with their HEAD without storing any remote tracking branch.

#5 lore raw

Re: [PATCH] clone: fix refspec on "--single-branch" option

Quoted 17 lines · Junio C Hamano

Who guarantees at this point in the codepath that option_branch is set when option_single_branch is non-zero? Until we talk with the remote, "clone --single-branch" without an explicit "--branch" will not learn which branch at the remote we are going to fetch (it will be their HEAD).

I wonder if this should be more like this:

	if (option_single_branch) {
		if (option_branch)
			Your patch "+refs/heads/foo:refs/remotes/origin/foo";
		else
			"HEAD";
        } else {
        	Original "+refs/heads/*:refs/remotes/origin/*";
	}
Quoted 3 lines

That is, "clone --single-branch" will continue fetching from and integrating with their HEAD without storing any remote tracking branch.

Alternatively, if you can move the logic to set up this configuration further down so that it happens after we talked to the other side and figured out remote_head_points_at, you could instead set it up to keep a single remote tracking branch.

Even if you did so, guess_remote_head() may not find any branch when the other repository's HEAD is detached, so you would need to decide what to do in such a case, and "fetch and integrate their HEAD without using any remote tracking branch" may be a reasonable thing to do in such a case.

#6 lore raw

PATCH v2 1/1 clone: fix refspec on "--single-branch" option

After a repo was cloned with the "--single-branch" option, the configured refspec looks like "+refs/heads/*:refs/remotes/origin/*". After fetching from this repo again, it'll receive all refs instead of just the ref from the single branch. Fixing this by configure exactly the ref of the branch the user specified in the "git clone" command.

---

Quoted 10 lines

Alternatively, if you can move the logic to set up this configuration further down so that it happens after we talked to the other side and figured out remote_head_points_at, you could instead set it up to keep a single remote tracking branch.

Even if you did so, guess_remote_head() may not find any branch when the other repository's HEAD is detached, so you would need to decide what to do in such a case, and "fetch and integrate their HEAD without using any remote tracking branch" may be a reasonable thing to do in such a case.

This second version now covers also the "--single-branch"
option when it was called without "--branch". It also covers
the "detached HEAD" case.
I've tested all the use-cases that have been described above and it works as expected with this patch. But there's just one thing. It fetches also all the tags even if they're not on this branch. I'm still in the "learning process", perhaps someone else can fix this problem or point me to the reason.
I think it comes from "transport_fetch_refs(transport, mapped_refs);" on line 813 which is called with a full "+refs/heads/*:refs/remotes/origin/*" refspec. Thanks

 builtin/clone.c | 41 +++++++++++++++++++++++++++--------------

1 Datei geändert, 27 Zeilen hinzugefügt(+), 14 Zeilen entfernt(-)

builtin/clone.c +27 -14
index 5e8f3ba..3ddf5ab 100644
@@ -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,33 @@ 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", our_head_points_at->name,
+									branch_top.buf, prettify_refname(remote_head_points_at->name));
+		} 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)
Signature · 1 line
1.7.12.395.g6b149ce.dirty
#7 lore raw

Re: [PATCHv2] clone: fix refspec on "--single-branch" option

builtin/clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 850

Quoted 3 lines · Ralf Thielow
builtin/clone.c +3 -0
+			else if (remote_head_points_at)
+					strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name,
+									branch_top.buf, prettify_refname(remote_head_points_at->name));

As refspec maps names that appear on the source side to names that appear on the destination side, and for fetch, the "soruce side" is the remote, using "our_head_points_at" on the source side makes it look very fishy (even though it may be a name derived from remote_head_points_at and has the correct and appropriate value).

"prettify" also is very questionable. It is meant to strip commonly known prefix to make it easier to read by humans, and we can change its result based solely on aesthetics in the future. It is not suitable for coming up with a value for configuration in the longer term.

Can we make the part you moved de-dented a bit, perhaps by making it into a small helper function or something? It is extremely hard to read with overly looooooong lines.

#8 lore raw

PATCH v3 1/1 clone: fix refspec on "--single-branch" option

After a repo was cloned with the "--single-branch" option, the configured refspec looks like "+refs/heads/*:refs/remotes/origin/*". After fetching from this repo again, it'll receive all refs instead of just the ref from the single branch. Fixing this by configure exactly the ref of the branch the user specified in the "git clone" command.

---

Quoted 6 lines

As refspec maps names that appear on the source side to names that appear on the destination side, and for fetch, the "soruce side" is the remote, using "our_head_points_at" on the source side makes it look very fishy (even though it may be a name derived from remote_head_points_at and has the correct and appropriate value).

Quoted 6 lines

"prettify" also is very questionable. It is meant to strip commonly known prefix to make it easier to read by humans, and we can change its result based solely on aesthetics in the future. It is not suitable for coming up with a value for configuration in the longer term.

Thanks. I've fixed.

Quoted 4 lines

Can we make the part you moved de-dented a bit, perhaps by making it into a small helper function or something? It is extremely hard to read with overly looooooong lines.

Would be nice but the list of arguments will become too long. I did a bit of reformating and think it now looks a bit nicer.

 builtin/clone.c | 43 +++++++++++++++++++++++++++++--------------

1 Datei geändert, 29 Zeilen hinzugefügt(+), 14 Zeilen entfernt(-)

builtin/clone.c +29 -14
index 5e8f3ba..06e3d3a 100644
@@ -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,35 @@ 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/"));
+		} 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)
Signature · 1 line
1.7.12.396.g3cff853.dirty
#9 lore raw

Re: [PATCHv3] clone: fix refspec on "--single-branch" option

builtin/clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 852

Quoted 11 lines · Ralf Thielow
builtin/clone.c +11 -0
+	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/"));

We have already set "remote.origin.url" to this repository, so the next "git fetch" would simply fetch from "HEAD" per default. Perhaps worth commenting that here?

Other than that, looks good. Perhaps we would want a test or two, too?

Thanks.

#10 lore raw

PATCH v4 1/1 clone --single: limit the fetch refspec to fetched branch

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.

---

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
builtin/clone.c +35 -14
index 5e8f3ba..be4c62b 100644
@@ -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)
t/t5709-clone-refspec.sh new file +59 -0

59 lines not on this page · raw message

Signature · 1 line
1.7.12.396.g6bea32d.dirty
#11 lore raw

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

Quoted 1 line · Ralf Thielow

- add tests for the refspec installed by the clone command

Thanks.

t/t5709-clone-refspec.sh line 22

Quoted 2 lines
t/t5709-clone-refspec.sh new file +2 -0
+test_expect_success 'refspec contains all branches by default' '
+	git clone "file://$PWD" dir_all &&

There have been numerous "on windows which should we use, $PWD or $(pwd)?" gotchas. In this particular case, wouldn't the tests work equally well with "." to avoid the uneasiness factor?

t/t5709-clone-refspec.sh line 25

Quoted 3 lines
t/t5709-clone-refspec.sh new file +3 -0
+	echo "+refs/heads/*:refs/remotes/origin/*" > expected &&
+	git --git-dir=dir_all/.git config --get remote.origin.fetch > actual &&
+	test_cmp expected actual

I am a bit torn with this one.

We'd want to make sure a single clone from a repository with two branches will not result in a repository where the next fetch will not pull in the other branch, and the value of the "remote.origin.fetch" is an implementation detail that happen to be what affects the outcome. The test is not checking the expected outcome in a direct way (imagine how this test will break when we do another change similar to the migration from .git/remotes/origin to the remote.origin.fetch variables).

I'll let it pass for now, though.

t/t5709-clone-refspec.sh line 54

Quoted 4 lines
t/t5709-clone-refspec.sh new file +4 -0
+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 &&

If earlier tests failed, there may not be any expected file and the "rm expected" will fail. You can just say

	>expected &&

instead. "touch" is a way to update the timestamp of the file; do not use it when you want to make sure an empty file exists.

t/t5709-clone-refspec.sh line 57

Quoted 3 lines
t/t5709-clone-refspec.sh new file +3 -0
+	git --git-dir=dir_detached/.git config --get remote.origin.fetch > actual
+	test_cmp expected actual
+'

I'd feel better if the test were like this instead:

	git checkout two^ &&
        git clone --single-branch . dir_detached &&
        (
		cd dir_detached &&
                git fetch &&
                git for-each-ref refs/remotes/origin >actual
	) &&
        >expect &&
        test_cmp expect actual

That is what I would call "testing the desired results in the most direct way".

Perhaps like this?

-- >8 -- t5709-clone-refspec.sh -- >8 --
#!/bin/sh

test_description='test refspec written by clone-command' . ./test-lib.sh

test_expect_success 'setup' '

	# Make two branches, "master" and "side"
	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 side &&
	echo four >file &&
	git commit -a -m four &&
	git checkout master &&

	# default clone
	git clone . dir_all &&

	# default --single that follows HEAD=master
	git clone --single-branch . dir_master &&

	# default --single that follows HEAD=side
	git checkout side &&
	git clone --single-branch . dir_side &&

	# explicit --single that follows side
	git checkout master &&
	git clone --single-branch --branch side . dir_side2 &&

	# --single that does not know what branch to follow
	git checkout two^ &&
	git clone --single-branch . dir_detached &&

	# advance both "master" and "side" branches
	git checkout side &&
	echo five >file &&
	git commit -a -m five &&
	git checkout master &&
	echo six >file &&
	git commit -a -m six

'

test_expect_success 'by default all branches will be kept updated' '

	(
		cd dir_all && git fetch &&
		git for-each-ref refs/remotes/origin |
		sed -e "/HEAD$/d" \
		    -e "s|/remotes/origin/|/heads/|" >../actual
	) &&
	# follow both master and side
	git for-each-ref refs/heads >expect &&
	test_cmp expect actual

'

test_expect_success '--single-branch while HEAD pointing at master' '

	(
		cd dir_master && git fetch &&
		git for-each-ref refs/remotes/origin |
		sed -e "/HEAD$/d" \
		    -e "s|/remotes/origin/|/heads/|" >../actual
	) &&
	# only follow master
	git for-each-ref refs/heads/master >expect &&
	test_cmp expect actual

'

test_expect_success '--single-branch while HEAD pointing at side' '

	(
		cd dir_side && git fetch &&
		git for-each-ref refs/remotes/origin |
		sed -e "/HEAD$/d" \
		    -e "s|/remotes/origin/|/heads/|" >../actual
	) &&
	# only follow side
	git for-each-ref refs/heads/side >expect &&
	test_cmp expect actual

'

test_expect_success '--single-branch with explicit --branch side' '

	(
		cd dir_side2 && git fetch &&
		git for-each-ref refs/remotes/origin |
		sed -e "/HEAD$/d" \
		    -e "s|/remotes/origin/|/heads/|" >../actual
	) &&
	# only follow side
	git for-each-ref refs/heads/side >expect &&
	test_cmp expect actual

'

test_expect_success '--single-branch with detached' '

	(
		cd dir_detached && git fetch &&
		git for-each-ref refs/remotes/origin |
		sed -e "/HEAD$/d" \
		    -e "s|/remotes/origin/|/heads/|" >../actual
	)
	# nothing
	>expect &&
	test_cmp expect actual

'

test_done

#12 lore raw

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

In reply to Ralf Thielow

builtin/clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 843

Quoted 2 lines · Ralf Thielow
builtin/clone.c +2 -0
+       if (option_mirror || !option_bare) {
+               strbuf_reset(&value);

I think we should use a new strbuf local variable here to avoid resetting this. At least reviewers don't have to check if this statememt causes any effect later on because "value"'s value is gone.

Quoted 5 lines
builtin/clone.c +5 -0
+               if (option_single_branch) {
+                       if (option_branch)
+                               strbuf_addf(&value, "+%s%s:%s%s",
+                                               src_ref_prefix, option_branch,
+                                               branch_top.buf, option_branch);

builtin/clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 858

Quoted 10 lines
builtin/clone.c +10 -0
+                       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.
+                        */

Maybe document updates too? Though if it's obvious that --single-branch should prepare refspec so that only one branch is fetched later on, then maybe not.

builtin/clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 861

Quoted 3 lines
builtin/clone.c +3 -0
+               } else {
+                       strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
+               }

--mirror --single-branch combination does not look right. The "heads/" part is missing..

$ git branch
  master * wildmatch
$ LANG=C ./git clone --mirror --single-branch .git abc
Cloning into bare repository 'abc'...
done.
$ grep fetch abc/config
        fetch = +refs/heads/wildmatch:refs/wildmatch
$ rm -rf abc
$ LANG=C ./git clone --mirror --single-branch --branch=master .git abc
Cloning into bare repository 'abc'...
done.
$ grep fetch abc/config
        fetch = +refs/master:refs/master
Signature · 1 line
Duy
#13 lore raw

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

Quoted 2 lines · Nguyen Thai Ngoc Duy

--mirror --single-branch combination does not look right. The "heads/" part is missing..

It also does not look right for cloning a tag:

$ LANG=C ./git clone --single-branch --branch=v1.7.0 .git abc
Cloning into 'abc'...
done.
Note: checking out 'e923eaeb901ff056421b9007adcbbce271caa7b6'.

You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:

git checkout -b new_branch_name

$ grep fetch abc/.git/config
        fetch = +refs/heads/v1.7.0:refs/remotes/origin/v1.7.0
Signature · 1 line
Duy
#14 lore raw

PATCH v5 1/1 clone --single: limit the fetch refspec to fetched branch

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.

---

changes in v5:
- extract a function to write refspec config
- handle --mirror option (test added)
- install correct refspec if the value of --branch is a tag (test added) Thanks to Junio for:
- refactor tests
- add tests for created refs

I'm not happy about using "our_head_points_to" for the remote part of the refspec. Junio already complaint about that. As far as I can see it's the only way of getting the information that it's a tag. Also the condition "is this a tag" might not be the best way.

 builtin/clone.c          |  66 +++++++++++++++-----
 t/t5709-clone-refspec.sh | 156 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 208 insertions(+), 14 deletions(-)
 create mode 100755 t/t5709-clone-refspec.sh
builtin/clone.c +52 -14
index 5e8f3ba..431635c 100644
@@ -610,6 +610,55 @@ static void write_config(struct string_list *config)
 	}
 }
 
+static void write_refspec_config(const char* src_ref_prefix,
+		const struct ref* our_head_points_at,
+		const struct ref* remote_head_points_at, struct strbuf* branch_top)
+{
+	struct strbuf key = STRBUF_INIT;
+	struct strbuf value = STRBUF_INIT;
+
+	if (option_mirror || !option_bare) {
+		if (option_single_branch && !option_mirror) {
+			if (option_branch) {
+				if (strstr(our_head_points_at->name, "refs/tags/"))
+					strbuf_addf(&value, "+%s:%s", our_head_points_at->name,
+						our_head_points_at->name);
+				else
+					strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name,
+						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);
+			}
+		}
+	}
+
+	strbuf_release(&key);
+	strbuf_release(&value);
+}
+
 int cmd_clone(int argc, const char **argv, const char *prefix)
 {
 	int is_bundle = 0, is_local;
@@ -755,20 +804,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 +888,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 					      "refs/heads/master");
 	}
 
+	write_refspec_config(src_ref_prefix, our_head_points_at,
+			remote_head_points_at, &branch_top);
+
 	if (is_local)
 		clone_local(path, git_dir);
 	else if (refs && complete_refs_before_fetch)
t/t5709-clone-refspec.sh new file +156 -0

156 lines not on this page · raw message

Signature · 1 line
1.7.12.397.ge29f79e.dirty
#15 lore raw

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

Quoted 1 line · Ralf Thielow

- handle --mirror option (test added)

Handle how? I personally think erroring out is the right way to handle it, but if we care about people who have been misusing the combination of single and mirror, the second best way would be to imply "mirror" and "single" combination as "bare" and "single" without "mirror".

Quoted 1 line

- install correct refspec if the value of --branch is a tag (test added)

What is the definition of "correct"? I see the documentation says "--branch can also take tags and treat them like detached HEAD", and even though I _think_ allowing tags was a huge mistake, if we claim we treat it like detached HEAD, we should do the same when coming up with the refspec used for the follow-up "fetch".

Whatever we decide to do, the semantics we decided to use at least need to be described in the commit log message, not just in the "changes from the previous iteration". Updating the "Documentation/git-clone.txt" would also be necessary.

t/t5709-clone-refspec.sh line 58

Quoted 5 lines
t/t5709-clone-refspec.sh new file +5 -0
+test_expect_success 'refspec contains all branches by default' '
+	echo "+refs/heads/*:refs/remotes/origin/*" > expect &&
+	git --git-dir=dir_all/.git config --get remote.origin.fetch > actual &&
+	test_cmp expect actual
+'

I still think these checks that know the current implementation details (i.e. what exact configuration variables get what exact values) are wrong thing to have in the longer term. If the desired behaviour (i.e. "later fetch do not screw up") can be tested directly like the later parts of the test in this patch does, how that desired behaviour is implemented should not have to be cast in stone with these tests.

#16 lore raw

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

Quoted 10 lines · Junio C Hamano
Quoted 1 line · Ralf Thielow

- handle --mirror option (test added)

Handle how? I personally think erroring out is the right way to handle it, but if we care about people who have been misusing the combination of single and mirror, the second best way would be to imply "mirror" and "single" combination as "bare" and "single" without "mirror".

With this patch it would be "bare" and "single", and with the next fetch a "mirror". However, the combination of "single" and "mirror" doesn't really make sense so I also think we should error it out.

Quoted 8 lines
Quoted 1 line

- install correct refspec if the value of --branch is a tag (test added)

What is the definition of "correct"? I see the documentation says "--branch can also take tags and treat them like detached HEAD", and even though I _think_ allowing tags was a huge mistake, if we claim we treat it like detached HEAD, we should do the same when coming up with the refspec used for the follow-up "fetch".

This patch would install the refspec "+refs/tags/v1.7.10:refs/tags/v1.7.10", so we would do the same with the follow-up "fetch", not?
This can be seen as "it's not a bug, it's a feature". Why not cloning a tag of a repo if someone just want to build a tag without having anything else.

Quoted 5 lines

Whatever we decide to do, the semantics we decided to use at least need to be described in the commit log message, not just in the "changes from the previous iteration". Updating the "Documentation/git-clone.txt" would also be necessary.

Ok.

Quoted 6 lines

t/t5709-clone-refspec.sh line 58

Quoted 5 lines
t/t5709-clone-refspec.sh new file +5 -0
+test_expect_success 'refspec contains all branches by default' '
+     echo "+refs/heads/*:refs/remotes/origin/*" > expect &&
+     git --git-dir=dir_all/.git config --get remote.origin.fetch > actual &&
+     test_cmp expect actual
+'
Quoted 7 lines

I still think these checks that know the current implementation details (i.e. what exact configuration variables get what exact values) are wrong thing to have in the longer term. If the desired behaviour (i.e. "later fetch do not screw up") can be tested directly like the later parts of the test in this patch does, how that desired behaviour is implemented should not have to be cast in stone with these tests.

You wrote

Quoted 1 line

I'll let it pass for now, though.

so I haven't removed them yet. I'll delete them.

#17 lore raw

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

Quoted 9 lines · Ralf Thielow
Quoted 8 lines
Quoted 1 line

- install correct refspec if the value of --branch is a tag (test added)

What is the definition of "correct"? I see the documentation says "--branch can also take tags and treat them like detached HEAD", and even though I _think_ allowing tags was a huge mistake, if we claim we treat it like detached HEAD, we should do the same when coming up with the refspec used for the follow-up "fetch".

Quoted 4 lines

This patch would install the refspec "+refs/tags/v1.7.10:refs/tags/v1.7.10", so we would do the same with the follow-up "fetch", not?
This can be seen as "it's not a bug, it's a feature". Why not cloning a tag of a repo if someone just want to build a tag without having anything else.

Even though I did say I thought allowing tags was a huge mistake, I was not suggesting to break existing users by making such a clone into an error.

But the main point of the discussion is what should happen upon the next "git fetch" in the repository, no? Shouldn't the refspec be the same as the case where you "clone --single" a repository whose HEAD is detached at v1.7.10 in that example, instead of saying "fetch the same tag over and over"? After all that is the way I expect that most people would read "treat them line detached HEAD" in the documentation. Subsequent "git fetch" would fetch from the HEAD of the upstream just like a clone from a repository with a detached HEAD.

The above is *not* a rhetorical question; I just do not think of a sensible reason why we want a refspec that says "keep updating the v1.7.10 tag, as it might change on the other end, and do not bother what other things that happen in that upstream repository". What sensible workflow would benefit from such a refspec?

#18 lore raw

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

Quoted 20 lines · Junio C Hamano
Quoted 9 lines · Ralf Thielow
Quoted 8 lines
Quoted 1 line

- install correct refspec if the value of --branch is a tag (test added)

What is the definition of "correct"? I see the documentation says "--branch can also take tags and treat them like detached HEAD", and even though I _think_ allowing tags was a huge mistake, if we claim we treat it like detached HEAD, we should do the same when coming up with the refspec used for the follow-up "fetch".

Quoted 4 lines

This patch would install the refspec "+refs/tags/v1.7.10:refs/tags/v1.7.10", so we would do the same with the follow-up "fetch", not?
This can be seen as "it's not a bug, it's a feature". Why not cloning a tag of a repo if someone just want to build a tag without having anything else.

Even though I did say I thought allowing tags was a huge mistake, I was not suggesting to break existing users by making such a clone into an error.

Quoted 10 lines

But the main point of the discussion is what should happen upon the next "git fetch" in the repository, no? Shouldn't the refspec be the same as the case where you "clone --single" a repository whose HEAD is detached at v1.7.10 in that example, instead of saying "fetch the same tag over and over"? After all that is the way I expect that most people would read "treat them line detached HEAD" in the documentation. Subsequent "git fetch" would fetch from the HEAD of the upstream just like a clone from a repository with a detached HEAD.

IMO if a user uses "clone --single-branch --branch <tag>", then he/she wants to have this tag only. Why should the next "git fetch" fetching something different?
I read "treat them like detached HEAD" in the way that the resulting repo is in a "detached HEAD" state.

Quoted 7 lines

The above is *not* a rhetorical question; I just do not think of a sensible reason why we want a refspec that says "keep updating the v1.7.10 tag, as it might change on the other end, and do not bother what other things that happen in that upstream repository". What sensible workflow would benefit from such a refspec?

When using a tag in a different meaning than "tag this version and do never change it", for example if someone uses a tag to describe which revision of the project was lastly delivered to the customer, then they could use a tag "delivered". After a new version was delivered, someone changes this tag from "revx" to "revy". Having such a refspec, you can fetch this tag and always get the delivered version. This example is very theoretically.

#19 lore raw

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

Quoted 3 lines · Ralf Thielow

IMO if a user uses "clone --single-branch --branch <tag>", then he/she wants to have this tag only. Why should the next "git fetch" fetching something different?

OK, I can buy that.

#20 lore raw

PATCH v6 1/1 clone --single: limit the fetch refspec to fetched branch

In reply to Ralf Thielow

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.

That means:
If "--single" is used without "--branch" or "--mirror", the fetch refspec covers the branch on which remote's HEAD points to. If "--single" is used with "--branch", it'll cover only the branch specified in the "--branch" option.
If "--single" is combined with "--mirror", then it'll cover all refs of the cloned repository.
If "--single" is used with "--branch" that specifies a tag, then it'll cover only the ref for this tag.

---

changes in v6
- remove initial created tests (they tested in a too deep level)
- add tests for "--mirror" option
- add tests for the case of cloning a tag
- update commit message

I've tried to update "Documentation/git-clone.txt", but I don't know in which way this patch changes already described behaviour. The resulting refspec seems only be covered in the last part of the "--single-branch" section by describing "--no-single-branch", but this hasn't changed. Or did I miss something?

 builtin/clone.c          |  66 ++++++++++++++++-----
 t/t5709-clone-refspec.sh | 145 +++++++++++++++++++++++++++++++++++++++++++++++
 2 Dateien geändert, 197 Zeilen hinzugefügt(+), 14 Zeilen entfernt(-)
 create mode 100755 t/t5709-clone-refspec.sh
builtin/clone.c +52 -14
index 5e8f3ba..431635c 100644
@@ -610,6 +610,55 @@ static void write_config(struct string_list *config)
 	}
 }
 
+static void write_refspec_config(const char* src_ref_prefix,
+		const struct ref* our_head_points_at,
+		const struct ref* remote_head_points_at, struct strbuf* branch_top)
+{
+	struct strbuf key = STRBUF_INIT;
+	struct strbuf value = STRBUF_INIT;
+
+	if (option_mirror || !option_bare) {
+		if (option_single_branch && !option_mirror) {
+			if (option_branch) {
+				if (strstr(our_head_points_at->name, "refs/tags/"))
+					strbuf_addf(&value, "+%s:%s", our_head_points_at->name,
+						our_head_points_at->name);
+				else
+					strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name,
+						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);
+			}
+		}
+	}
+
+	strbuf_release(&key);
+	strbuf_release(&value);
+}
+
 int cmd_clone(int argc, const char **argv, const char *prefix)
 {
 	int is_bundle = 0, is_local;
@@ -755,20 +804,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 +888,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 					      "refs/heads/master");
 	}
 
+	write_refspec_config(src_ref_prefix, our_head_points_at,
+			remote_head_points_at, &branch_top);
+
 	if (is_local)
 		clone_local(path, git_dir);
 	else if (refs && complete_refs_before_fetch)
t/t5709-clone-refspec.sh new file +145 -0

145 lines not on this page · raw message

Signature · 1 line
1.7.12.396.g7954078
#21 lore raw

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

t/t5709-clone-refspec.sh line 54

Quoted 11 lines · Ralf Thielow

+ ...

t/t5709-clone-refspec.sh new file +10 -0
+	# explicit --single with tag
+	git clone --single-branch --branch two . dir_tag &&
+
+	# advance both "master" and "side" branches
+	git checkout side &&
+	echo five >file &&
+	git commit -a -m five &&
+	git checkout master &&
+	echo six >file &&
+	git commit -a -m six

Don't we also want to cover your "delivery" tag scenario by updating the tag "two" before letting the clones fetch in the following test?

Quoted 9 lines

+ ...

t/t5709-clone-refspec.sh new file +8 -0
+test_expect_success '--single-branch with explicit --branch tag' '
+	(
+		cd dir_tag && git fetch &&
+		git for-each-ref refs/tags >../actual
+	) &&
+	git for-each-ref refs/tags >expect &&
+	test_cmp expect actual
+'
#22 lore raw

PATCH v7 1/1 clone --single: limit the fetch refspec to fetched branch

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.

That means:
If "--single" is used without "--branch" or "--mirror", the fetch refspec covers the branch on which remote's HEAD points to. If "--single" is used with "--branch", it'll cover only the branch specified in the "--branch" option.
If "--single" is combined with "--mirror", then it'll cover all refs of the cloned repository.
If "--single" is used with "--branch" that specifies a tag, then it'll cover only the ref for this tag.

---

Changes in v7:
- remove a test which has shown that after the next "git fetch" the tags will be the same (was actually a bit senseless)
- add a test to show that the initial clone with "--single" fetches tags
- add a test to show when using "--single" with "--branch" which points to a tag, then the next "git fetch" will update this tag if it was updated in the cloned repository
- update documentation of "git clone" (Thanks to Junio)

 Documentation/git-clone.txt |  15 +++--
 builtin/clone.c             |  66 +++++++++++++++----
 t/t5709-clone-refspec.sh    | 155 ++++++++++++++++++++++++++++++++++++++++++++
 3 Dateien geändert, 218 Zeilen hinzugefügt(+), 18 Zeilen entfernt(-)
 create mode 100755 t/t5709-clone-refspec.sh
Documentation/git-clone.txt +11 -4
index c1ddd4c..6d98ef3 100644
@@ -29,7 +29,8 @@ currently active branch.
 After the clone, a plain `git fetch` without arguments will update
 all the remote-tracking branches, and a `git pull` without
 arguments will in addition merge the remote master branch into the
-current master branch, if any.
+current master branch, if any (this is untrue when "--single-branch"
+is given; see below).
 
 This default configuration is achieved by creating references to
 the remote branch heads under `refs/remotes/origin` and
@@ -152,9 +153,10 @@ objects from the source repository into a pack in the cloned repository.
 -b <name>::
 	Instead of pointing the newly created HEAD to the branch pointed
 	to by the cloned repository's HEAD, point to `<name>` branch
-	instead. `--branch` can also take tags and treat them like
-	detached HEAD. In a non-bare repository, this is the branch
-	that will be checked out.
+	instead. In a non-bare repository, this is the branch that will
+	be checked out.
+	`--branch` can also take tags and detaches the HEAD at that commit
+	in the resulting repository.
 
 --upload-pack <upload-pack>::
 -u <upload-pack>::
@@ -193,6 +195,11 @@ objects from the source repository into a pack in the cloned repository.
 	clone with the `--depth` option, this is the default, unless
 	`--no-single-branch` is given to fetch the histories near the
 	tips of all branches.
+	Further fetches into the resulting repository will only update the
+	remote tracking branch for the branch this option was used for the
+	initial cloning.  If the HEAD at the remote did not point at any
+	branch when `--single-branch` clone was made, no remote tracking
+	branch is created.
 
 --recursive::
 --recurse-submodules::
builtin/clone.c +52 -14

84 lines not on this page · raw message

t/t5709-clone-refspec.sh new file +155 -0

155 lines not on this page · raw message

Signature · 1 line
1.7.12.396.g7954078
#23 lore raw

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

Quoted 19 lines · Ralf Thielow
t/t5709-clone-refspec.sh new file +13 -0
new file mode 100755
index 0000000..69a0779
@@ -0,0 +1,155 @@
+#!/bin/sh
+
+test_description='test refspec written by clone-command'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	# Make two branches, "master" and "side"
+ ...
+	# explicit --single with tag
+	git clone --single-branch --branch two . dir_tag &&
+
+	# advance both "master" and "side" branches
+	git checkout side &&

t/t5709-clone-refspec.sh line 23

Quoted 10 lines
t/t5709-clone-refspec.sh new file +10 -0
+	echo five >file &&
+	git commit -a -m five &&
+	git checkout master &&
+	echo six >file &&
+	git commit -a -m six
+'
+
+ ...
+test_expect_success '--single-branch with explicit --branch with tag fetches updated tag' '
+	git tag -d two && git tag two &&

Curious. Was it insufficient to update this tag at the end of the "setup" process like everybody else where

	# advance both "master" and "side" branches

happens?

Not that it is wrong to do this, but it looked somewhat inconsistent.

#24 lore raw

PATCH v8 1/1 clone --single: limit the fetch refspec to fetched branch

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.

That means:
If "--single" is used without "--branch" or "--mirror", the fetch refspec covers the branch on which remote's HEAD points to. If "--single" is used with "--branch", it'll cover only the branch specified in the "--branch" option.
If "--single" is combined with "--mirror", then it'll cover all refs of the cloned repository.
If "--single" is used with "--branch" that specifies a tag, then it'll cover only the ref for this tag.

---

changes in v8:
- remove unnecessary strbuf_reset
- remove test from v7 which has shown that tags get fetched when cloning
- add test to show that tags will not being updated when using simple "git clone"
- change position of the tag in the setup, not directly in the test

 Documentation/git-clone.txt |  15 +++--
 builtin/clone.c             |  65 ++++++++++++++----
 t/t5709-clone-refspec.sh    | 156 ++++++++++++++++++++++++++++++++++++++++++++
 3 Dateien geändert, 218 Zeilen hinzugefügt(+), 18 Zeilen entfernt(-)
 create mode 100755 t/t5709-clone-refspec.sh
Documentation/git-clone.txt +11 -4
index c1ddd4c..6d98ef3 100644
@@ -29,7 +29,8 @@ currently active branch.
 After the clone, a plain `git fetch` without arguments will update
 all the remote-tracking branches, and a `git pull` without
 arguments will in addition merge the remote master branch into the
-current master branch, if any.
+current master branch, if any (this is untrue when "--single-branch"
+is given; see below).
 
 This default configuration is achieved by creating references to
 the remote branch heads under `refs/remotes/origin` and
@@ -152,9 +153,10 @@ objects from the source repository into a pack in the cloned repository.
 -b <name>::
 	Instead of pointing the newly created HEAD to the branch pointed
 	to by the cloned repository's HEAD, point to `<name>` branch
-	instead. `--branch` can also take tags and treat them like
-	detached HEAD. In a non-bare repository, this is the branch
-	that will be checked out.
+	instead. In a non-bare repository, this is the branch that will
+	be checked out.
+	`--branch` can also take tags and detaches the HEAD at that commit
+	in the resulting repository.
 
 --upload-pack <upload-pack>::
 -u <upload-pack>::
@@ -193,6 +195,11 @@ objects from the source repository into a pack in the cloned repository.
 	clone with the `--depth` option, this is the default, unless
 	`--no-single-branch` is given to fetch the histories near the
 	tips of all branches.
+	Further fetches into the resulting repository will only update the
+	remote tracking branch for the branch this option was used for the
+	initial cloning.  If the HEAD at the remote did not point at any
+	branch when `--single-branch` clone was made, no remote tracking
+	branch is created.
 
 --recursive::
 --recurse-submodules::
builtin/clone.c +51 -14

83 lines not on this page · raw message

t/t5709-clone-refspec.sh new file +156 -0

156 lines not on this page · raw message

Signature · 1 line
1.7.12.396.g7954078
#25 lore raw

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

Thanks.

#26 lore raw

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

In reply to Ralf Thielow
Quoted 27 lines · Ralf Thielow

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.

That means:
If "--single" is used without "--branch" or "--mirror", the fetch refspec covers the branch on which remote's HEAD points to. If "--single" is used with "--branch", it'll cover only the branch specified in the "--branch" option.
If "--single" is combined with "--mirror", then it'll cover all refs of the cloned repository.
If "--single" is used with "--branch" that specifies a tag, then it'll cover only the ref for this tag.

---

changes in v6
- remove initial created tests (they tested in a too deep level)
- add tests for "--mirror" option
- add tests for the case of cloning a tag
- update commit message

Quoted 5 lines

I've tried to update "Documentation/git-clone.txt", but I don't know in which way this patch changes already described behaviour. The resulting refspec seems only be covered in the last part of the "--single-branch" section by describing "--no-single-branch", but this hasn't changed. Or did I miss something?

I do not think we are changing anything. The whole "--single-branch" was an half-baked afterthought hack that nobody anticipated the user to later issue "git fetch" in the resulting repository, and everybody involved in the series (the maintainer included) were just happy to see the resulting code only transferred objects needed for the branch without wasting bandwidth for objects needed for other branches, and stopped thinking beyond that X-<.

If anything, we need to _add_ the description of what happens when further fetches are done. The second and the third paragraph in the DESCRIPTION section talks only about the normal case, so at least at the end of the second paragraph we should say "But if you use --single-branch, all of this is different". And what happens when you do use --single-branch should be described in the part that describes that option, e.g.

 Documentation/git-clone.txt | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
Documentation/git-clone.txt +12 -3
index c1ddd4c..f59bc49 100644
@@ -29,7 +29,8 @@ currently active branch.
 After the clone, a plain `git fetch` without arguments will update
 all the remote-tracking branches, and a `git pull` without
 arguments will in addition merge the remote master branch into the
-current master branch, if any.
+current master branch, if any (this is untrue when "--single-branch"
+is given; see below).
 
 This default configuration is achieved by creating references to
 the remote branch heads under `refs/remotes/origin` and
@@ -152,9 +153,11 @@ objects from the source repository into a pack in the cloned repository.
 -b <name>::
 	Instead of pointing the newly created HEAD to the branch pointed
 	to by the cloned repository's HEAD, point to `<name>` branch
-	instead. `--branch` can also take tags and treat them like
-	detached HEAD. In a non-bare repository, this is the branch
+	instead. In a non-bare repository, this is the branch
 	that will be checked out.
++
+`--branch` can also take tags and detaches the HEAD
+at that commit in the resulting repository. 
 
 --upload-pack <upload-pack>::
 -u <upload-pack>::
@@ -193,6 +196,12 @@ objects from the source repository into a pack in the cloned repository.
 	clone with the `--depth` option, this is the default, unless
 	`--no-single-branch` is given to fetch the histories near the
 	tips of all branches.
++
+Further fetches into the resulting repository will only update the
+remote tracking branch for the branch this option was used for the
+initial cloning.  If the HEAD at the remote did not point at any
+branch when `--single-branch` clone was made, no remote tracking
+branch is created.
 
 --recursive::
 --recurse-submodules::
#27 lore raw

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

In reply to Ralf Thielow

t/t5709-clone-refspec.sh line 113

Quoted 8 lines · Ralf Thielow
t/t5709-clone-refspec.sh new file +8 -0
+test_expect_success '--single-branch with explicit --branch tag' '
+       (
+               cd dir_tag && git fetch &&
+               git for-each-ref refs/tags >../actual
+       ) &&
+       git for-each-ref refs/tags >expect &&
+       test_cmp expect actual
+'

We should have added the tag right after cloning, not until the first git-fetch. Not that I object how you do it in this patch. Just a note to myself that if I'm going to do that, I'll need to update this test to update the change tag before fetching and verify the tag is updated after git-fetch.

Signature · 1 line
Duy
#28 lore raw

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

Quoted 10 lines · Nguyen Thai Ngoc Duy

t/t5709-clone-refspec.sh line 113

Quoted 8 lines · Ralf Thielow
t/t5709-clone-refspec.sh new file +8 -0
+test_expect_success '--single-branch with explicit --branch tag' '
+       (
+               cd dir_tag && git fetch &&
+               git for-each-ref refs/tags >../actual
+       ) &&
+       git for-each-ref refs/tags >expect &&
+       test_cmp expect actual
+'
Quoted 7 lines

We should have added the tag right after cloning, not until the first git-fetch. Not that I object how you do it in this patch. Just a note to myself that if I'm going to do that, I'll need to update this test to update the change tag before fetching and verify the tag is updated after git-fetch.
--
Duy

Right. I'll create two tests to replace this one. The first test verifys that the tag
has been added after cloning. And in the second test, the tag gets updated in the cloned repository and we verify that the next fetch updates the tag, which catches my "delivery" tag example.

#29 lore raw

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

In reply to Nguyen Thai Ngoc Duy
Quoted 7 lines · Nguyen Thai Ngoc Duy
Quoted 2 lines · Nguyen Thai Ngoc Duy

--mirror --single-branch combination does not look right. The "heads/" part is missing..

It also does not look right for cloning a tag:

$ LANG=C ./git clone --single-branch --branch=v1.7.0 .git abc

What does it even _mean_ to clone a single branch and give a tag, not a branch, to the --branch option? Again, shouldn't it be diagnosed as an error in the first place?

#30 lore raw

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

Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Nguyen Thai Ngoc Duy
Quoted 2 lines · Nguyen Thai Ngoc Duy

--mirror --single-branch combination does not look right. The "heads/" part is missing..

Quoted 7 lines
Quoted 3 lines

It also does not look right for cloning a tag:

$ LANG=C ./git clone --single-branch --branch=v1.7.0 .git abc

What does it even _mean_ to clone a single branch and give a tag, not a branch, to the --branch option? Again, shouldn't it be diagnosed as an error in the first place?

That could be misleading. The original purpose is to clone just one ref, be it a branch or a tag. You might want to work on top of a specific version and may not want to clone everything. For cloning a tag, we're put in detached head state immediately.

Signature · 1 line
Duy
#31 lore raw

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

Quoted 11 lines · Nguyen Thai Ngoc Duy
Quoted 10 lines · Junio C Hamano
Quoted 7 lines · Nguyen Thai Ngoc Duy
Quoted 2 lines · Nguyen Thai Ngoc Duy

--mirror --single-branch combination does not look right. The "heads/" part is missing..

It also does not look right for cloning a tag:

$ LANG=C ./git clone --single-branch --branch=v1.7.0 .git abc
Quoted 7 lines
Quoted 3 lines

What does it even _mean_ to clone a single branch and give a tag, not a branch, to the --branch option? Again, shouldn't it be diagnosed as an error in the first place?

That could be misleading. The original purpose is to clone just one ref, be it a branch or a tag. You might want to work on top of a specific version and may not want to clone everything.

Yeah, see my other comments.

#32 lore raw

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

In reply to Nguyen Thai Ngoc Duy
Quoted 8 lines · Nguyen Thai Ngoc Duy

builtin/clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 843

Quoted 2 lines · Ralf Thielow
builtin/clone.c +2 -0
+       if (option_mirror || !option_bare) {
+               strbuf_reset(&value);

I think we should use a new strbuf local variable here to avoid resetting this. At least reviewers don't have to check if this statememt causes any effect later on because "value"'s value is gone.

It seems that we don't need this reset here because it's already done earlier in this function. The variable "key" is also used multiple times so I wouldn't use a new variable.

Quoted 16 lines
Quoted 5 lines
builtin/clone.c +5 -0
+               if (option_single_branch) {
+                       if (option_branch)
+                               strbuf_addf(&value, "+%s%s:%s%s",
+                                               src_ref_prefix, option_branch,
+                                               branch_top.buf, option_branch);

builtin/clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 858

Quoted 10 lines
builtin/clone.c +10 -0
+                       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.
+                        */
Quoted 4 lines

Maybe document updates too? Though if it's obvious that --single-branch should prepare refspec so that only one branch is fetched later on, then maybe not.

I think it's obvious.

Quoted 7 lines

builtin/clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 861

Quoted 3 lines
builtin/clone.c +3 -0
+               } else {
+                       strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top.buf);
+               }

--mirror --single-branch combination does not look right. The "heads/" part is missing..

Quoted 15 lines
$ git branch
  master * wildmatch
$ LANG=C ./git clone --mirror --single-branch .git abc
Cloning into bare repository 'abc'...
done.
$ grep fetch abc/config
        fetch = +refs/heads/wildmatch:refs/wildmatch
$ rm -rf abc
$ LANG=C ./git clone --mirror --single-branch --branch=master .git abc
Cloning into bare repository 'abc'...
done.
$ grep fetch abc/config
        fetch = +refs/master:refs/master
--
Duy

Thanks, I'll check this later and send a new version.

#33 lore raw

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

In reply to Nguyen Thai Ngoc Duy
Quoted 2 lines · Nguyen Thai Ngoc Duy

--mirror --single-branch combination does not look right. The "heads/" part is missing..

What does it supposed to do in the first place? "mirror" is primarily about grabbing everything without leaving stuff out, and "single" is about grabbing only one thing without other stuff. It smells like a nonsense case we didn't even bother to catch and diagnose as an error to me.

Besides, mirror implies bare, so we would expect to see

refs/heads/$single:refs/heads/$single

and that matches what the "single" gives us without "mirror", no?

How about explicitly forbidding that combination, or ignoring mirror when single is given?

#34 lore raw

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

Quoted 11 lines · Junio C Hamano
Quoted 2 lines · Nguyen Thai Ngoc Duy

--mirror --single-branch combination does not look right. The "heads/" part is missing..

What does it supposed to do in the first place? "mirror" is primarily about grabbing everything without leaving stuff out, and "single" is about grabbing only one thing without other stuff. It smells like a nonsense case we didn't even bother to catch and diagnose as an error to me.

Quoted 5 lines

Besides, mirror implies bare, so we would expect to see

refs/heads/$single:refs/heads/$single

and that matches what the "single" gives us without "mirror", no?

Yes that's what I think it should do too.

Quoted 2 lines

How about explicitly forbidding that combination, or ignoring mirror when single is given?

I see nothing wrong with mirroring just one branch. But if handling it is too complex than forbidding it is fine.

Signature · 1 line
Duy
#35 lore raw

Re: [PATCH] clone: fix refspec on "--single-branch" option

In reply to Junio C Hamano
Quoted 10 lines · Junio C Hamano

Alternatively, if you can move the logic to set up this configuration further down so that it happens after we talked to the other side and figured out remote_head_points_at, you could instead set it up to keep a single remote tracking branch.

Even if you did so, guess_remote_head() may not find any branch when the other repository's HEAD is detached, so you would need to decide what to do in such a case, and "fetch and integrate their HEAD without using any remote tracking branch" may be a reasonable thing to do in such a case.

Along the lines of this, perhaps.

 builtin/clone.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
builtin/clone.c +16 -0
index 5e8f3ba..c9e099d 100644
@@ -853,6 +853,22 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 					      "refs/heads/master");
 	}
 
+	if (option_single_branch) {
+		/* Fix up the refspec for fetch */
+		strbuf_reset(&value);
+		if (!remote_head_points_at)
+			strbuf_addf(&value, "HEAD");
+		else
+			strbuf_addf(&value, "%s:%s%s",
+				    remote_head_points_at->name,
+				    branch_top.buf,
+				    skip_prefix(remote_head_points_at->name, "refs/heads/"));
+
+		strbuf_reset(&key);
+		strbuf_addf(&key, "remote.%s.fetch", option_origin);
+		git_config_set_multivar(key.buf, value.buf, NULL, 1);
+	}
+
 	if (is_local)
 		clone_local(path, git_dir);
 	else if (refs && complete_refs_before_fetch)
#36 lore raw

Re: [PATCH] clone: fix refspec on "--single-branch" option

In reply to Junio C Hamano
Quoted 19 lines · Junio C Hamano
Quoted 17 lines · Junio C Hamano

Who guarantees at this point in the codepath that option_branch is set when option_single_branch is non-zero? Until we talk with the remote, "clone --single-branch" without an explicit "--branch" will not learn which branch at the remote we are going to fetch (it will be their HEAD).

I wonder if this should be more like this:

      if (option_single_branch) {
              if (option_branch)
                      Your patch "+refs/heads/foo:refs/remotes/origin/foo";
              else
                      "HEAD";
        } else {
              Original "+refs/heads/*:refs/remotes/origin/*";
      }
Quoted 8 lines
Quoted 3 lines

That is, "clone --single-branch" will continue fetching from and integrating with their HEAD without storing any remote tracking branch.

Alternatively, if you can move the logic to set up this configuration further down so that it happens after we talked to the other side and figured out remote_head_points_at, you could instead set it up to keep a single remote tracking branch.

That sounds reasonable. I have a question though, what should a user do when he/she want to fetch all branches again? Messing up with refspec in config file is not something I would like to do.

Perhaps a heuristic in git-fetch to detect "single branch" situation and ignore refspec? We could hint people that refspecs are not followed when remote has more than one branch. They could either fetch the another branch explicitly, which turns off the heuristic, or turn off the advice.

Quoted 5 lines

Even if you did so, guess_remote_head() may not find any branch when the other repository's HEAD is detached, so you would need to decide what to do in such a case, and "fetch and integrate their HEAD without using any remote tracking branch" may be a reasonable thing to do in such a case.

Signature · 1 line
Duy
#37 lore raw

Re: [PATCH] clone: fix refspec on "--single-branch" option

Quoted 10 lines · Nguyen Thai Ngoc Duy
Quoted 4 lines · Junio C Hamano

Alternatively, if you can move the logic to set up this configuration further down so that it happens after we talked to the other side and figured out remote_head_points_at, you could instead set it up to keep a single remote tracking branch.

That sounds reasonable. I have a question though, what should a user do when he/she want to fetch all branches again? Messing up with refspec in config file is not something I would like to do.

You first have to think ;-).

I would say there are two kinds of users.

- To the simplistic ones who fear the power of configuration, we can simply tell "You don't. Use 'single' when you want to keep working with the single branch. If you want full, reclone, and migrate your work from the single one by fetching from it to the full clone before discarding the single one".

- To the ones who wants to take the full advantage of flexibility of configuration, we can tell "remotes.$name.fetch configuration is your friend. Do whatever you want to do with it, but here are two hints". The hints would cover the case to revert to the default refspec, and the case to add another specific branch.

These days, with "git config" and "git remote" wrappers, I do not particularly see a need to fear the power of configuration, though.

#38 lore raw

Re: [PATCH] clone: fix refspec on "--single-branch" option

In reply to Nguyen Thai Ngoc Duy
Quoted 24 lines · Nguyen Thai Ngoc Duy
Quoted 23 lines · Junio C Hamano
Quoted 20 lines · Junio C Hamano

Who guarantees at this point in the codepath that option_branch is set when option_single_branch is non-zero? Until we talk with the remote, "clone --single-branch" without an explicit "--branch" will not learn which branch at the remote we are going to fetch (it will be their HEAD).

I wonder if this should be more like this:

      if (option_single_branch) {
              if (option_branch)
                      Your patch "+refs/heads/foo:refs/remotes/origin/foo";
              else
                      "HEAD";
        } else {
              Original "+refs/heads/*:refs/remotes/origin/*";
      }

That is, "clone --single-branch" will continue fetching from and integrating with their HEAD without storing any remote tracking branch.

Quoted 9 lines
Quoted 4 lines

Alternatively, if you can move the logic to set up this configuration further down so that it happens after we talked to the other side and figured out remote_head_points_at, you could instead set it up to keep a single remote tracking branch.

That sounds reasonable. I have a question though, what should a user do when he/she want to fetch all branches again? Messing up with refspec in config file is not something I would like to do.

$ git remote set-branches <remote> "*"
Quoted 6 lines

Perhaps a heuristic in git-fetch to detect "single branch" situation and ignore refspec? We could hint people that refspecs are not followed when remote has more than one branch. They could either fetch the another branch explicitly, which turns off the heuristic, or turn off the advice.

Such an advice when using "--single-branch" is a good idea, i think. Something like "The remote <remote> is configured to fetch only branch <branch>. If you want to fetch all branches, use "git remote set-branches <remote> "*"" or something like that.

Quoted 7 lines
Quoted 5 lines

Even if you did so, guess_remote_head() may not find any branch when the other repository's HEAD is detached, so you would need to decide what to do in such a case, and "fetch and integrate their HEAD without using any remote tracking branch" may be a reasonable thing to do in such a case.

--
Duy