git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 17, 2012, 04:48 UTC
Message-ID
<7vobl5xnjx.fsf@alter.siamese.dyndns.org>
In-Reply-To
<1347783184-20045-1-git-send-email-ralf.thielow@gmail.com>
Ralf Thielow <ralf.thielow@gmail.com> writes:
> - add tests for the refspec installed by the clone command
Thanks.
> +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?

> +	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.
> +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.

> +	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
Previous: Ralf ThielowNext: Nguyen Thai Ngoc Duy
Message 15 of 39 in “is this behaviour expected for "git clone --single-branch"?”
  1. Ralf ThielowSep 13, 2012
  2. Junio C HamanoSep 13, 2012
  3. Ralf ThielowSep 13, 2012
  4. clone: fix refspec on "--single-branch" optionRalf Thielow, Sep 14, 2012
  5. Junio C HamanoSep 14, 2012
  6. Junio C HamanoSep 14, 2012
  7. Nguyen Thai Ngoc DuySep 14, 2012
  8. Ralf ThielowSep 14, 2012
  9. Junio C HamanoSep 14, 2012
  10. [PATCHv2] clone: fix refspec on "--single-branch" optionRalf Thielow, Sep 14, 2012
  11. Junio C HamanoSep 14, 2012
  12. [PATCHv3] clone: fix refspec on "--single-branch" optionRalf Thielow, Sep 14, 2012
  13. Junio C HamanoSep 14, 2012
  14. [PATCHv4] clone --single: limit the fetch refspec to fetched branchRalf Thielow, Sep 16, 2012
  15. Junio C HamanoSep 17, 2012
  16. Nguyen Thai Ngoc DuySep 17, 2012
  17. Nguyen Thai Ngoc DuySep 17, 2012
  18. [PATCHv5] clone --single: limit the fetch refspec to fetched branchRalf Thielow, Sep 17, 2012
  19. Junio C HamanoSep 17, 2012
  20. Ralf ThielowSep 17, 2012
  21. Junio C HamanoSep 17, 2012
  22. Ralf ThielowSep 18, 2012
  23. Junio C HamanoSep 18, 2012
  24. [PATCHv6] clone --single: limit the fetch refspec to fetched branchRalf Thielow, Sep 18, 2012
  25. Junio C HamanoSep 18, 2012
  26. Junio C HamanoSep 18, 2012
  27. [PATCHv7] clone --single: limit the fetch refspec to fetched branchRalf Thielow, Sep 19, 2012
  28. Junio C HamanoSep 19, 2012
  29. [PATCHv8] clone --single: limit the fetch refspec to fetched branchRalf Thielow, Sep 20, 2012
  30. Junio C HamanoSep 20, 2012
  31. Nguyen Thai Ngoc DuySep 19, 2012
  32. Ralf ThielowSep 19, 2012
  33. Junio C HamanoSep 17, 2012
  34. Nguyen Thai Ngoc DuySep 18, 2012
  35. Junio C HamanoSep 18, 2012
  36. Ralf ThielowSep 17, 2012
  37. Junio C HamanoSep 17, 2012
  38. Nguyen Thai Ngoc DuySep 18, 2012
  39. Junio C HamanoSep 14, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.