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

Re: [PATCH v3] Teach git to change to a given directory using -C option

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 9, 2013, 16:32 UTC
Message-ID
<xmqqtxhu3qil.fsf@gitster.dls.corp.google.com>
In-Reply-To
<20130909134743.GA11335@gmail.com>
Nazri Ramliy <ayiehere@gmail.com> writes:
> Subject: git: run in a directory given with -C option
>
> This is similar in spirit to to "make -C dir ..." and "tar -C dir ...".

The doubled-"to to" which I locally fixed when I queued the last one (together with other rewording to make it more agreeable and easier to read) somehow came back ;-) Will fix locally again.

> +-C <path>::
> +	Run as if git was started in '<path>' instead of the current working
> +	directory.  When multiple '-C' options are given, each subsequent
I think this should be `-C` to typeset it as "typed literally".
Show 5 quoted lines
> +	non-absolute `-C <path>` is interpreted relative to the preceding `-C
> +	<path>`.
> ++
> +This option affects options that expect path name like '--git-dir' and
> +'--work-tree' in that their interpretations of the path names would be
Likewise for `--git-dir` and `--work-tree`.
> +made relative to the working directory caused by the '-C' option. For
and here.
Show 16 quoted lines
> diff --git a/t/t0056-git-C.sh b/t/t0056-git-C.sh
> new file mode 100755
> index 0000000..c0006da
> --- /dev/null
> +++ b/t/t0056-git-C.sh
> @@ -0,0 +1,82 @@
> +#!/bin/sh
> +
> +test_description='"-C <path>" option and its effects on other path-related options'
> +
> +. ./test-lib.sh
> +
> +test_expect_success '"git -C <path>" runs git from the directory <path>' '
> +	test_create_repo dir1 &&
> +	echo 1 >dir1/a.txt &&
> +	(cd dir1 && git add a.txt && git commit -m "initial in dir1") &&
Curious why this does not use -C here.
Show 9 quoted lines
> +	echo "initial in dir1" >expected &&
> +	git -C dir1 log --format=%s >actual &&
> +	test_cmp expected actual
> +'
> +
> +test_expect_success 'Multiple -C options: "-C dir1 -C dir2" is equivalent to "-C dir1/dir2"' '
> +	test_create_repo dir1/dir2 &&
> +	echo 1 >dir1/dir2/a.txt &&
> +	git -C dir1/dir2 add a.txt &&

Because "a.txt" exists in both dir1 and dir1/dir2, this has less chance of catching a bug (if somebody breaks the feature to run it in dir1 not dir1/dir2, "add" will happily say "Oh, I found something to add", instead of saying "Huh? there is no such path".

If you used b.txt instead, you would catch such a breakage.

Remember, tests are not about demonstrating how cool the new feature is and/or how well it works in an expected setting. Imagine ways other people can break your spiffy new feature in later patches, and design tests that are more likely to catch them.

The same comment applies throughout the remainder of this script.
> +	echo "initial in dir1/dir2" >expected &&
> +	git -C dir1/dir2 commit -m "initial in dir1/dir2" &&

to reduce possibilities of breaking this test in the future due to typos (e.g. somebody may want to say "initial commit in dir1/dir2"), doing this may be a better idea:

	msg="initial in dir1/dir2" &&
        echo "$msg" >expected &&
        git -C dir1/dir2 commit -m "$msg" &&
The same comment applies to the previous one.
Previous: Nazri RamliyNext: Eric Sunshine
Message 11 of 14 in “Teach git to change to a given directory using -C option”
  1. Teach git to change to a given directory using -C optionNazri Ramliy, Sep 3, 2013
  2. Junio C HamanoSep 3, 2013
  3. Eric SunshineSep 3, 2013
  4. Nazri RamliySep 4, 2013
  5. Eric SunshineSep 4, 2013
  6. Nazri RamliySep 4, 2013
  7. Eric SunshineSep 8, 2013
  8. Nazri RamliySep 9, 2013
  9. Eric SunshineSep 9, 2013
  10. Nazri RamliySep 9, 2013
  11. Junio C HamanoSep 9, 2013
  12. Eric SunshineSep 9, 2013
  13. Junio C HamanoSep 9, 2013
  14. Eric SunshineSep 9, 2013

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.