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
Jeff King <peff@peff.net>
Date
Dec 3, 2010, 21:20 UTC
Message-ID
<20101203212035.GB2632@sigill.intra.peff.net>
In-Reply-To
<4CF95BDC.60506@dirk.my1.cc>
On Fri, Dec 03, 2010 at 10:06:36PM +0100, Dirk Süsserott wrote:
Show 24 quoted lines
> 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
> ---------------------
Since you are writing shell scripts, you may be interested in doing:
  . git-sh-setup
  require_work_tree
  cd_to_toplevel
> 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".

Yeah, in general rev-parse's "-q" is annoying. It only works with checking revisions (like "git rev-parse -q --verify HEAD"), and it probably should apply to a lot more. Patches welcome.

> 2. Wouldn't it be useful, if "git rev-parse --show-cdup" (2a) would
> return a dot "." instead of nothing if we are already in the topdir?
> That would make the steps (2a), (2b), (2c) to a simple "cd $(git
> rev-parse --show-cdup)".

I'm hesitant to change it, as that is a public interface for shell scripts. However, there is also "--show-toplevel" which will do what you want in one line (or just cd_to_toplevel as I mentioned above).

> What do you think? If appreciated, I'd like to work on a patch for (1).
Please do.
-Peff
Previous: Dirk SüsserottNext: Junio C Hamano
Message 2 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.