threads / discuss / 1850

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

Subject: Add "--git-dir" flag to git-rev-parse

## tl;dr

7 messages between Sep 18, 2005 and Sep 19, 2005.

replies: 6people: 4as markdown or json

Linus Torvalds· Sep 18, 2005, 18:18 UTC · lore

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· Sep 19, 2005, 10:19 UTC · re: Linus Torvalds · lore

[PATCH] Added -d and -e options to the "git" script.

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· Sep 19, 2005, 14:14 UTC · re: David Kågedal · lore

Re: [PATCH] Added -d and -e options to the "git" script.

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· Sep 19, 2005, 14:24 UTC · re: A Large Angry SCM · lore

Re: [PATCH] Added -d and -e options to the "git" script.

A Large Angry SCM <gitzilla@gmail.com> writes:
Show 8 quoted lines
> 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· Sep 19, 2005, 14:42 UTC · re: David Kågedal · lore

Re: [PATCH] Added -d and -e options to the "git" script.

David Kågedal wrote:
Show 7 quoted lines
> A Large Angry SCM <gitzilla@gmail.com> writes:
> 
>>David Kågedal wrote:
>>>These flags override the GIT_DIR and EDITOR environment variables.
>>>
>>[Patch snipped]
>>
[...]
Show 8 quoted lines
> 
> 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.

David Kågedal· Sep 19, 2005, 15:08 UTC · re: A Large Angry SCM · lore

Re: [PATCH] Added -d and -e options to the "git" script.

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
Matthias Urlichs· Sep 19, 2005, 14:42 UTC · re: David Kågedal · lore

Re: [PATCH] Added -d and -e options to the "git" script.

Hi, David Kågedal wrote:
Show 6 quoted lines
> +        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:
Show 7 quoted lines
> +        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".

← back to recent threads