# [PATCH] Ensure proper setup of git_dir for git-hash-object

4 messages from 2009-02-28 to 2009-02-28. Participants: newren@gmail.com, Junio C Hamano, Elijah Newren.
Thread: https://gitlist.dev/t/18068

## newren@gmail.com, 2009-02-28 19:56

Subject: [PATCH] Ensure proper setup of git_dir for git-hash-object
Message-ID: <1235851009-16739-1-git-send-email-newren@gmail.com>
URL: https://gitlist.dev/e/1235851009-16739-1-git-send-email-newren%40gmail.com

```
From: Elijah Newren <newren@gmail.com>

Call setup_git_directory() before git_config() to make sure git_dir is set
to the proper value.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
Without this patch:
$ mkdir tmp
$ cd tmp/
$ git init --bare
Initialized empty Git repository in /home/newren/floss-development/git/tmp/
$ echo hi | git hash-object -w --stdin
error: unable to create temporary sha1 filename .git/objects/45: No such file or directory

fatal: Unable to add stdin to database
$ echo hi | git --git-dir=. hash-object -w --stdin
45b983be36b73c0788dc9cbcb76cbb80fc7bb057

 hash-object.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/hash-object.c b/hash-object.c
index 37e6677..ebb3bed 100644
--- a/hash-object.c
+++ b/hash-object.c
@@ -84,8 +84,6 @@ int main(int argc, const char **argv)
 
 	git_extract_argv0_path(argv[0]);
 
-	git_config(git_default_config, NULL);
-
 	argc = parse_options(argc, argv, hash_object_options, hash_object_usage, 0);
 
 	if (write_object) {
@@ -95,6 +93,8 @@ int main(int argc, const char **argv)
 			vpath = prefix_filename(prefix, prefix_length, vpath);
 	}
 
+	git_config(git_default_config, NULL);
+
 	if (stdin_paths) {
 		if (hashstdin)
 			errstr = "Can't use --stdin-paths with --stdin";
-- 
1.6.0.6

```

## Junio C Hamano, 2009-02-28 20:59

Subject: Re: [PATCH] Ensure proper setup of git_dir for git-hash-object
Message-ID: <7v3adyffax.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v3adyffax.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <1235851009-16739-1-git-send-email-newren@gmail.com>

```
newren@gmail.com writes:

> Without this patch:
> $ mkdir tmp
> $ cd tmp/
> $ git init --bare
> Initialized empty Git repository in /home/newren/floss-development/git/tmp/
> $ echo hi | git hash-object -w --stdin
> error: unable to create temporary sha1 filename .git/objects/45: No such file or directory
>
> fatal: Unable to add stdin to database
> $ echo hi | git --git-dir=. hash-object -w --stdin
> 45b983be36b73c0788dc9cbcb76cbb80fc7bb057

Does the patched version work without -w option?  Should it?

```

## Elijah Newren, 2009-02-28 21:20

Subject: Re: [PATCH] Ensure proper setup of git_dir for git-hash-object
Message-ID: <51419b2c0902281320neec7960vfbe7b806c8db56ca@mail.gmail.com>
URL: https://gitlist.dev/e/51419b2c0902281320neec7960vfbe7b806c8db56ca%40mail.gmail.com
In-Reply-To: <7v3adyffax.fsf@gitster.siamese.dyndns.org>

```
On Sat, Feb 28, 2009 at 1:59 PM, Junio C Hamano <gitster@pobox.com> wrote:
> newren@gmail.com writes:
>
>> Without this patch:
>> $ mkdir tmp
>> $ cd tmp/
>> $ git init --bare
>> Initialized empty Git repository in /home/newren/floss-development/git/tmp/
>> $ echo hi | git hash-object -w --stdin
>> error: unable to create temporary sha1 filename .git/objects/45: No such file or directory
>>
>> fatal: Unable to add stdin to database
>> $ echo hi | git --git-dir=. hash-object -w --stdin
>> 45b983be36b73c0788dc9cbcb76cbb80fc7bb057
>
> Does the patched version work without -w option?  Should it?

Yes, the patched version works with or without the -w option (at least
in my testing -- maybe you know of a case I'm missing?)  I would
certainly expect it to work in both cases.

I basically arrived at the patch by realizing that git_config was
setting git_dir incorrectly as a side-effect, causing
setup_git_directory to notice it was already set and not try any of
it's more detailed logic to figure out the correct value.  Then I did
some grepping and noticed that other source files (archive.c,
builtin-apply.c, builtin-diff, etc., etc.) call
setup_git_directory[_gently] before git_config, and that hash-object.c
seemed to be the only one that didn't follow that trend.

```

## Elijah Newren, 2009-02-28 21:39

Subject: Re: [PATCH] Ensure proper setup of git_dir for git-hash-object
Message-ID: <51419b2c0902281339k35f21b72v83310000b60a1416@mail.gmail.com>
URL: https://gitlist.dev/e/51419b2c0902281339k35f21b72v83310000b60a1416%40mail.gmail.com
In-Reply-To: <7v3adyffax.fsf@gitster.siamese.dyndns.org>

```
On Sat, Feb 28, 2009 at 1:59 PM, Junio C Hamano <gitster@pobox.com> wrote:
> newren@gmail.com writes:
>
>> Without this patch:
>> $ mkdir tmp
>> $ cd tmp/
>> $ git init --bare
>> Initialized empty Git repository in /home/newren/floss-development/git/tmp/
>> $ echo hi | git hash-object -w --stdin
>> error: unable to create temporary sha1 filename .git/objects/45: No such file or directory
>>
>> fatal: Unable to add stdin to database
>> $ echo hi | git --git-dir=. hash-object -w --stdin
>> 45b983be36b73c0788dc9cbcb76cbb80fc7bb057
>
> Does the patched version work without -w option?  Should it?

Sorry, I think I partially missed what you were asking earlier.  When
-w is not passed there is no dependence on git_dir, so it does not
matter if it is set up or not.  Some evidence that this is true (in
addition to my basic testing): The call to git_config was added to
hash-object.c in revision ff350ccf49a800c4c90f817d346fb1bcb96e02e7;
prior to that revision, when -w was not passed, there would be no
setup of git_dir by either git_config or git_setup_directory.

```
