# [PATCH] git config: error when editing a repo config and not being in one

8 messages from 2009-04-29 to 2009-04-30. Participants: Felipe Contreras, Johannes Schindelin, Junio C Hamano.
Thread: https://gitlist.dev/t/19133

## Felipe Contreras, 2009-04-29 22:25

Subject: [PATCH] git config: error when editing a repo config and not being in one
Message-ID: <1241043939-29013-1-git-send-email-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/1241043939-29013-1-git-send-email-felipe.contreras%40gmail.com

```
Let's throw an error on this specific case. If the user specifies the
config file, he must know what he is doing.

Teemu Likonen pointed this out.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 builtin-config.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/builtin-config.c b/builtin-config.c
index d8da72c..6e936e1 100644
--- a/builtin-config.c
+++ b/builtin-config.c
@@ -390,6 +390,8 @@ int cmd_config(int argc, const char **argv, const char *unused_prefix)
 	}
 	else if (actions == ACTION_EDIT) {
 		check_argc(argc, 0, 0);
+		if (!config_exclusive_filename && !is_inside_git_dir())
+			die("not in a git directory");
 		git_config(git_default_config, NULL);
 		launch_editor(config_exclusive_filename ?
 			      config_exclusive_filename : git_path("config"),
-- 
1.6.3.rc3.12.gb7937.dirty

```

## Johannes Schindelin, 2009-04-29 22:44

Subject: Re: [PATCH] git config: error when editing a repo config and not being in one
Message-ID: <alpine.DEB.1.00.0904300043030.10279@pacific.mpi-cbg.de>
URL: https://gitlist.dev/e/alpine.DEB.1.00.0904300043030.10279%40pacific.mpi-cbg.de
In-Reply-To: <1241043939-29013-1-git-send-email-felipe.contreras@gmail.com>

```
Hi,

On Thu, 30 Apr 2009, Felipe Contreras wrote:

> diff --git a/builtin-config.c b/builtin-config.c
> index d8da72c..6e936e1 100644
> --- a/builtin-config.c
> +++ b/builtin-config.c
> @@ -390,6 +390,8 @@ int cmd_config(int argc, const char **argv, const char *unused_prefix)
>  	}
>  	else if (actions == ACTION_EDIT) {
>  		check_argc(argc, 0, 0);
> +		if (!config_exclusive_filename && !is_inside_git_dir())
> +			die("not in a git directory");

Why not use the nongit variable?

Ciao,
Dscho

```

## Felipe Contreras, 2009-04-29 22:49

Subject: [PATCH] git config: error when editing a repo config and not being in one
Message-ID: <1241045387-30181-1-git-send-email-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/1241045387-30181-1-git-send-email-felipe.contreras%40gmail.com
In-Reply-To: <alpine.DEB.1.00.0904300043030.10279@pacific.mpi-cbg.de>

```
Let's throw an error on this specific case. If the user specifies the
config file, he must know what he is doing.

Teemu Likonen pointed this out.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 builtin-config.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/builtin-config.c b/builtin-config.c
index d8da72c..a81bc8b 100644
--- a/builtin-config.c
+++ b/builtin-config.c
@@ -390,6 +390,8 @@ int cmd_config(int argc, const char **argv, const char *unused_prefix)
 	}
 	else if (actions == ACTION_EDIT) {
 		check_argc(argc, 0, 0);
+		if (!config_exclusive_filename && nongit)
+			die("not in a git directory");
 		git_config(git_default_config, NULL);
 		launch_editor(config_exclusive_filename ?
 			      config_exclusive_filename : git_path("config"),
-- 
1.6.3.rc3.13.g195b.dirty

```

## Junio C Hamano, 2009-04-29 23:01

Subject: Re: [PATCH] git config: error when editing a repo config and not being in one
Message-ID: <7vws935ak8.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vws935ak8.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <1241043939-29013-1-git-send-email-felipe.contreras@gmail.com>

```
Felipe Contreras <felipe.contreras@gmail.com> writes:

> Let's throw an error on this specific case. If the user specifies the
> config file, he must know what he is doing.
>
> Teemu Likonen pointed this out.
>
> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> ---
>  builtin-config.c |    2 ++
>  1 files changed, 2 insertions(+), 0 deletions(-)
>
> diff --git a/builtin-config.c b/builtin-config.c
> index d8da72c..6e936e1 100644
> --- a/builtin-config.c
> +++ b/builtin-config.c
> @@ -390,6 +390,8 @@ int cmd_config(int argc, const char **argv, const char *unused_prefix)
>  	}
>  	else if (actions == ACTION_EDIT) {
>  		check_argc(argc, 0, 0);
> +		if (!config_exclusive_filename && !is_inside_git_dir())
> +			die("not in a git directory");
>  		git_config(git_default_config, NULL);
>  		launch_editor(config_exclusive_filename ?
>  			      config_exclusive_filename : git_path("config"),

How could this be correct?

When you are inside a work tree controlled by a git repository, and you
want to edit the configuration file that belongs to the repository, I do
not think is_inside_git_dir() is true.

What you are trying to catch is if $GIT_DIR exists (not in the sense that
"is it a non-empty string?", but in the sense that "does it point at an
existing directory?"), I think.

So perhaps you want to say something like this instead?

No, I did not test it.

 builtin-config.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/builtin-config.c b/builtin-config.c
index d8da72c..a4bd516 100644
--- a/builtin-config.c
+++ b/builtin-config.c
@@ -390,6 +390,12 @@ int cmd_config(int argc, const char **argv, const char *unused_prefix)
 	}
 	else if (actions == ACTION_EDIT) {
 		check_argc(argc, 0, 0);
+		if (!config_exclusive_filename) {
+			const char *git = get_git_dir();
+			struct stat sb;
+			if (!git || stat(git, &sb) || !S_ISDIR(sb.st_mode))
+				die("not in a git directory");
+		}
 		git_config(git_default_config, NULL);
 		launch_editor(config_exclusive_filename ?
 			      config_exclusive_filename : git_path("config"),

```

## Junio C Hamano, 2009-04-29 23:22

Subject: Re: [PATCH] git config: error when editing a repo config and not being in one
Message-ID: <7v1vrb59ki.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v1vrb59ki.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <alpine.DEB.1.00.0904300043030.10279@pacific.mpi-cbg.de>

```
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> Hi,
>
> On Thu, 30 Apr 2009, Felipe Contreras wrote:
>
>> diff --git a/builtin-config.c b/builtin-config.c
>> index d8da72c..6e936e1 100644
>> --- a/builtin-config.c
>> +++ b/builtin-config.c
>> @@ -390,6 +390,8 @@ int cmd_config(int argc, const char **argv, const char *unused_prefix)
>>  	}
>>  	else if (actions == ACTION_EDIT) {
>>  		check_argc(argc, 0, 0);
>> +		if (!config_exclusive_filename && !is_inside_git_dir())
>> +			die("not in a git directory");
>
> Why not use the nongit variable?

Good; makes a lot more sense.

```

## Johannes Schindelin, 2009-04-30 08:37

Subject: Re: [PATCH] git config: error when editing a repo config and not being in one
Message-ID: <alpine.DEB.1.00.0904301036380.6621@intel-tinevez-2-302>
URL: https://gitlist.dev/e/alpine.DEB.1.00.0904301036380.6621%40intel-tinevez-2-302
In-Reply-To: <1241045387-30181-1-git-send-email-felipe.contreras@gmail.com>

```
Hi,

On Thu, 30 Apr 2009, Felipe Contreras wrote:

> Let's throw an error on this specific case. If the user specifies the
> config file, he must know what he is doing.
> 
> Teemu Likonen pointed this out.
> 
> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>

I would have Acked it, but Junio already applied it ;-)

Ciao,
Dscho

```

## Felipe Contreras, 2009-04-30 09:11

Subject: Re: [PATCH] git config: error when editing a repo config and not being in one
Message-ID: <94a0d4530904300211j57903a3ei782b4e3e2f967c8a@mail.gmail.com>
URL: https://gitlist.dev/e/94a0d4530904300211j57903a3ei782b4e3e2f967c8a%40mail.gmail.com
In-Reply-To: <alpine.DEB.1.00.0904301036380.6621@intel-tinevez-2-302>

```
On Thu, Apr 30, 2009 at 11:37 AM, Johannes Schindelin
<Johannes.Schindelin@gmx.de> wrote:
> Hi,
>
> On Thu, 30 Apr 2009, Felipe Contreras wrote:
>
>> Let's throw an error on this specific case. If the user specifies the
>> config file, he must know what he is doing.
>>
>> Teemu Likonen pointed this out.
>>
>> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
>
> I would have Acked it, but Junio already applied it ;-)

:)

Just for the record, where is people supposed to learn about 'nongit'?
Apparently it's not mentioned in the documentation.

-- 
Felipe Contreras

```

## Johannes Schindelin, 2009-04-30 09:21

Subject: Re: [PATCH] git config: error when editing a repo config and not being in one
Message-ID: <alpine.DEB.1.00.0904301117040.6621@intel-tinevez-2-302>
URL: https://gitlist.dev/e/alpine.DEB.1.00.0904301117040.6621%40intel-tinevez-2-302
In-Reply-To: <94a0d4530904300211j57903a3ei782b4e3e2f967c8a@mail.gmail.com>

```
Hi,

On Thu, 30 Apr 2009, Felipe Contreras wrote:

> On Thu, Apr 30, 2009 at 11:37 AM, Johannes Schindelin
> <Johannes.Schindelin@gmx.de> wrote:
>
> > On Thu, 30 Apr 2009, Felipe Contreras wrote:
> >
> >> Let's throw an error on this specific case. If the user specifies the 
> >> config file, he must know what he is doing.
> >>
> >> Teemu Likonen pointed this out.
> >>
> >> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> >
> > I would have Acked it, but Junio already applied it ;-)
> 
> :)
> 
> Just for the record, where is people supposed to learn about 'nongit'? 
> Apparently it's not mentioned in the documentation.

It's not about 'nongit'.  It is about setting up the git directory 
"gently", i.e you can ask for a setup _if possible_ using 
setup_git_directory_gently(), which will not fail if we're not in any 
repository.

To require a repository, call setup_git_directory().  This will die() if 
there is no repository.

As we already ask via _gently() in cmd_config(), it appeared natural to me 
to reuse that information rather than redoing the discovery.

Ciao,
Dscho

```
