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

Re: Q about git rev-parse {--is-inside-work-tree, --show-cdup}

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 3, 2010, 21:25 UTC
Message-ID
<7v4oauo8fj.fsf@alter.siamese.dyndns.org>
In-Reply-To
<4CF95BDC.60506@dirk.my1.cc>
Dirk Süsserott <newsletter@dirk.my1.cc> writes:
Show 34 quoted lines
> Hi together,
>
> I wrote some shell scripts that do sth with my git repositories. I place
> my scripts in my ~/bin folder (not in the repo). So the first step in my
> scripts is always to check whether they get called from whithin a git
> repo and bail out if they don't.
>
>
> I do this like so:
>
> ---------------------
> if [ "$(git rev-parse --is-inside-work-tree)" = "true" ] # (1)
> then
>      here=$(pwd)
>      cdup=$(git rev-parse --show-cdup);    # (2a)
>      cdup=${cdup:-"."}                     # (2b)
>      cd $cdup                              # (2c)
>
>      [do sth useful from the topdir]
>
>      cd $here
>      exit 0;
> else
>      echo "Not inside a git working tree."
>      exit 1;
> fi
> ---------------------
>
> I have two questions:
>
> 1. Wouldn't it be useful, if "git rev-parse" (1) had an option "-q" that
> simply indicates whether "--is-inside-work-tree" is true by means of the
> return code? Actually it has an option "-q" but that doesn't work with
> "--is-inside-work-tree".

That would break existing scripts that expect "-q" to squelch only the error output, no? I think the risk of breaking existing scripts that other people wrote over time that you (and I) haven't seen outweighs any benefit (i.e. "if test $(rev-parse...) = true" vs "if rev-parse...") you are seeing here.

Another thing to consider is what the script should do when rev-parse detects an error. Do we know that all scripts want to behave exactly the same way in two cases: (1) when run outside the work tree; and (2) when they cannot determine if they were run from inside or outside? I don't think so.

In your example, you are only interested to work inside work tree, and you may find "if rev-parse... then do this interesting thing else fail fi" sufficient, but a script by somebody else may want to make sure that it is not run inside any git controlled working tree, and it cannot say "if rev-parse then punt else do this big thing fi" is not an appropriate way to write it.

So in that sense, "if rev-parse ..." is not a huge improvement to begin with, for people who want to write their script strictly. They would probably need to say something like:

	ans=$(git rev-parse ...) || {
		do the error thing
                exit 1
	}
       	case "$ans" in
        true)
		do the inside-work-tree thing ;;
	false)
		do the outside-work-tree thing ;;
	*)
        	do the oops thing;;
	esac
Previous: Jeff KingNext: Jeff King
Message 3 of 6 in “Q about git rev-parse {--is-inside-work-tree, --show-cdup}”
  1. Dirk SüsserottDec 3, 2010
  2. Jeff KingDec 3, 2010
  3. Junio C HamanoDec 3, 2010
  4. Jeff KingDec 3, 2010
  5. Dirk SüsserottDec 4, 2010
  6. Andreas SchwabDec 4, 2010

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.