# Add "--git-dir" flag to git-rev-parse

7 messages from 2005-09-18 to 2005-09-19. Participants: Linus Torvalds, David Kågedal, A Large Angry SCM, Matthias Urlichs.
Thread: https://gitlist.dev/t/1850

## Linus Torvalds, 2005-09-18 18:18

Subject: Add "--git-dir" flag to git-rev-parse
Message-ID: <Pine.LNX.4.58.0509181114190.26803@g5.osdl.org>
URL: https://gitlist.dev/e/Pine.LNX.4.58.0509181114190.26803%40g5.osdl.org

```

Especially when you're deep inside the git repository, it's not all that
trivial for scripts to figure out where GIT_DIR is if it isn't set.

So add a flag to git-rev-parse to show where it is, since it will have 
figured it out anyway.

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---
diff --git a/rev-parse.c b/rev-parse.c
--- a/rev-parse.c
+++ b/rev-parse.c
@@ -191,6 +191,22 @@ int main(int argc, char **argv)
 					puts(prefix);
 				continue;
 			}
+			if (!strcmp(arg, "--git-dir")) {
+				const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
+				static char cwd[PATH_MAX];
+				if (gitdir) {
+					puts(gitdir);
+					continue;
+				}
+				if (!prefix) {
+					puts(".git");
+					continue;
+				}
+				if (!getcwd(cwd, PATH_MAX))
+					die("unable to get current working directory");
+				printf("%s/.git\n", cwd);
+				continue;
+			}
 			if (verify)
 				die("Needed a single revision");
 			show_flag(arg);

```

## David Kågedal, 2005-09-19 10:19

Subject: [PATCH] Added -d and -e options to the "git" script.
Message-ID: <u5tmzm9bbrb.fsf@lysator.liu.se>
URL: https://gitlist.dev/e/u5tmzm9bbrb.fsf%40lysator.liu.se
In-Reply-To: <Pine.LNX.4.58.0509181114190.26803@g5.osdl.org>

```
These flags override the GIT_DIR and EDITOR environment variables.

---
This is more generic way to specify GIT_DIR than Linus' suggestion,
although it is only targeted at more porcelainish usage of the git
driver script.

I needed this for the Emacs commands I've been hacking on, since it's
hard to set environment variables for single commands without
involving /bin/env or a shell.

 git.sh |   37 +++++++++++++++++++++++++------------
 1 files changed, 25 insertions(+), 12 deletions(-)

b9a38ed5a16289f86b353c0bb541e607d35180a9
diff --git a/git.sh b/git.sh
--- a/git.sh
+++ b/git.sh
@@ -2,19 +2,32 @@
 
 cmd=
 path=$(dirname $0)
-case "$#" in
-0)	;;
-*)	cmd="$1"
-	shift
-	case "$cmd" in
-	-v|--v|--ve|--ver|--vers|--versi|--versio|--version)
-		echo "git version @@GIT_VERSION@@"
-		exit 0 ;;
-	esac
-	test -x $path/git-$cmd && exec $path/git-$cmd "$@" ;;
-esac
 
-echo "Usage: git COMMAND [OPTIONS] [TARGET]"
+while test "$#" -gt 0
+do
+        case "$1" in
+        -d)	export GIT_DIR
+                GIT_DIR=$2
+                shift
+                ;;
+        -e)	export EDITOR
+                EDITOR=$2
+                shift
+                ;;
+        *)	cmd="$1"
+                shift
+                case "$cmd" in
+                -v|--v|--ve|--ver|--vers|--versi|--versio|--version)
+                        echo "git version @@GIT_VERSION@@"
+                        exit 0 ;;
+                esac
+                test -x $path/git-$cmd && exec $path/git-$cmd "$@"
+                ;;
+        esac
+        shift
+done
+
+echo "Usage: git [GLOBAL OPTIONS] COMMAND [COMMAND OPTIONS]"
 if [ -n "$cmd" ]; then
     echo " git command '$cmd' not found: commands are:"
 else

-- 
David Kågedal

```

## A Large Angry SCM, 2005-09-19 14:14

Subject: Re: [PATCH] Added -d and -e options to the "git" script.
Message-ID: <432EC7D6.6080105@gmail.com>
URL: https://gitlist.dev/e/432EC7D6.6080105%40gmail.com
In-Reply-To: <u5tmzm9bbrb.fsf@lysator.liu.se>

```
David Kågedal wrote:
> These flags override the GIT_DIR and EDITOR environment variables.
> 
[Patch snipped]

An option for GIT_DIR maybe since it's a Git environment variable but a 
command line option to change the EDITOR _environment_ variable is NOT 
appropriate for git.

```

## David Kågedal, 2005-09-19 14:24

Subject: Re: [PATCH] Added -d and -e options to the "git" script.
Message-ID: <u5tvf0xb0fp.fsf@lysator.liu.se>
URL: https://gitlist.dev/e/u5tvf0xb0fp.fsf%40lysator.liu.se
In-Reply-To: <432EC7D6.6080105@gmail.com>

```
A Large Angry SCM <gitzilla@gmail.com> writes:

> David Kågedal wrote:
>> These flags override the GIT_DIR and EDITOR environment variables.
>>
> [Patch snipped]
>
> An option for GIT_DIR maybe since it's a Git environment variable but a 
> command line option to change the EDITOR _environment_ variable is NOT 
> appropriate for git.

Yeah, I should have separated those two so that the GIT_DIR option
could be accepted on its own.  And it should maybe have a --git-dir=
log form as well.

The reason I did -e too was simply that I needed it for the same
reason I needed -d.  I wanted to run git with specific values for
GIT_DIR and EDITOR without having to setenv them in emacs.  But the
EDITOR trick I'm using is a hack and should be replaced anyway. (I use
EDITOR=cat to get access to the default commit message with comments
from 'git commit').

So please disregard the -e part of my patch for now.  You are free to
disregard the -d part as well, but I think that makes some kind of
sense.

And If we go down this path, maybe flags for other GIT_* environment
variables should be settable with git command line flags too?

-- 
David Kågedal

```

## A Large Angry SCM, 2005-09-19 14:42

Subject: Re: [PATCH] Added -d and -e options to the "git" script.
Message-ID: <432ECE5A.7040703@gmail.com>
URL: https://gitlist.dev/e/432ECE5A.7040703%40gmail.com
In-Reply-To: <u5tvf0xb0fp.fsf@lysator.liu.se>

```
David Kågedal wrote:
> A Large Angry SCM <gitzilla@gmail.com> writes:
> 
>>David Kågedal wrote:
>>>These flags override the GIT_DIR and EDITOR environment variables.
>>>
>>[Patch snipped]
>>
[...]
> 
> So please disregard the -e part of my patch for now.  You are free to
> disregard the -d part as well, but I think that makes some kind of
> sense.
> 
> And If we go down this path, maybe flags for other GIT_* environment
> variables should be settable with git command line flags too?
> 

Please no!

There are better methods of setting environment variables than an 
endless set of command options. Use the env command or you own 
(personal) command wrapper instead.

```

## Matthias Urlichs, 2005-09-19 14:42

Subject: Re: [PATCH] Added -d and -e options to the "git" script.
Message-ID: <pan.2005.09.19.14.42.44.111271@smurf.noris.de>
URL: https://gitlist.dev/e/pan.2005.09.19.14.42.44.111271%40smurf.noris.de
In-Reply-To: <u5tmzm9bbrb.fsf@lysator.liu.se>

```
Hi, David Kågedal wrote:
> +        case "$1" in
> +        -e)	[...]
> +        *)	cmd="$1"
> +                shift
> +                case "$cmd" in
> +                -v|--v|--ve|--ver|--vers|--versi|--versio|--version)

Umm, can we please not have two case statements nested like that?

Better written as:
> +        case "$1" in
> +        -e)	[...]
> +        -v|--v|--ve|--ver|--vers|--versi|--versio|--version)
> +             [print and die]
> +        *)	cmd="$1"
> +             shift
> +             test -x $path/git-$cmd && exec $path/git-$cmd "$@"

Thanks.

-- 
Matthias Urlichs   |   {M:U} IT Design @ m-u-it.de   |  smurf@smurf.noris.de
Disclaimer: The quote was selected randomly. Really. | http://smurf.noris.de
 - -
It is a sad commentary on today's society that this fortune has to be
classified as "offensive" simply because it contains the word "fuck".

```

## David Kågedal, 2005-09-19 15:08

Subject: Re: [PATCH] Added -d and -e options to the "git" script.
Message-ID: <u5tpsr5aye0.fsf@lysator.liu.se>
URL: https://gitlist.dev/e/u5tpsr5aye0.fsf%40lysator.liu.se
In-Reply-To: <432ECE5A.7040703@gmail.com>

```
A Large Angry SCM <gitzilla@gmail.com> writes:

> There are better methods of setting environment variables than an 
> endless set of command options. Use the env command or you own 
> (personal) command wrapper instead.

Remember that, to the user, this isn't about setting environment
variables, It's about providing git parameters.

Currently, the only of doing this is by setting environment variables;
what I'm suggesting is an *alternative* to that.

The fact that the implementation of these flags would actually use
environment variables to propagate these parameters to subprocesses
doesn't necessary mean that it is the only sane way to specify it
originally.

Anyway, there are other options, such as

   $ git GIT_DIR=/foo/bar status

and today we already have

   $ env GIT_DIR=/foo/bar status

The advantage of these forms is of course that they don't limit the
parameters that can be set, so I could keep setting EDITOR if I want
to.  But if setting GIT_DIR is common enough it might make sense to
provide a short flag for that.

The summary of my ramblings are probably that I think the -d flag is a
good idea, but no way of specifying other parameters is needed.

-- 
David Kågedal

```
