Skip to contentSkip to conversation

init: show absolute path of (re)initialized repository

PATCH Superseded Matt Hunter 4 participants 15 messages Oct 20 – Oct 28

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 init: show absolute path of (re)initialized repository

Since 20e38cba3926 (setup: split up concerns of init_db(), 2026-09-10), git-init produces the message:

Initialized empty Git repository in .git/

instead of the previous

Initialized empty Git repository in /full/path/to/repo/.git/

Prior to 20e38cba3926, the code producing this message was in function init_db (renamed at the time to create_repository). In that context, the git_dir variable was updated by 'git_dir = repo_get_git_dir(repo);' earlier in the function, to set the value used in printf.

None of the discussion [1] around the original patch indicates that this change was intentional, so address the regression by similarly using repo_get_git_dir in this code's new location.

In this case, git_dir is referencing owned memory that is later freed, so declare a new variable to hold the used string.

1: https://lore.kernel.org/git/20260825-pks-odb-write-alternates-at-creation-time-v1-0-911513ba95c3@pks.im/

---
 builtin/init-db.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
builtin/init-db.c +4 -3
index 374ed3a74706..a2628568ff6a 100644
@@ -248,18 +248,19 @@ int cmd_init_db(int argc,
 	create_object_database(the_repository, NULL);
 
 	if (!quiet) {
-		int len = strlen(git_dir);
+		const char *full_git_dir = repo_get_git_dir(the_repository);
+		int len = strlen(full_git_dir);
 
 		if (reinit)
 			printf(repo_settings_get_shared_repository(the_repository)
 			       ? _("Reinitialized existing shared Git repository in %s%s\n")
 			       : _("Reinitialized existing Git repository in %s%s\n"),
-			       git_dir, len && git_dir[len-1] != '/' ? "/" : "");
+			       full_git_dir, len && full_git_dir[len-1] != '/' ? "/" : "");
 		else
 			printf(repo_settings_get_shared_repository(the_repository)
 			       ? _("Initialized empty shared Git repository in %s%s\n")
 			       : _("Initialized empty Git repository in %s%s\n"),
-			       git_dir, len && git_dir[len-1] != '/' ? "/" : "");
+			       full_git_dir, len && full_git_dir[len-1] != '/' ? "/" : "");
 	}
 
 	free(template_dir_to_free);

base-commit: c51408889c3b372814bc20b9431c3b74d0d268eb

Signature · 1 line
2.56.0.257.gf7497c7d27
#1 lore raw
Quoted 9 lines · Matt Hunter

Since 20e38cba3926 (setup: split up concerns of init_db(), 2026-09-10), git-init produces the message:

Initialized empty Git repository in .git/

instead of the previous

Initialized empty Git repository in /full/path/to/repo/.git/

Quoted 8 lines

Prior to 20e38cba3926, the code producing this message was in function init_db (renamed at the time to create_repository). In that context, the git_dir variable was updated by 'git_dir = repo_get_git_dir(repo);' earlier in the function, to set the value used in printf.

None of the discussion [1] around the original patch indicates that this change was intentional, so address the regression by similarly using repo_get_git_dir in this code's new location.

This indeed wasn't an intentional change. I also confirmed that with v2.55 we print the full path.

Quoted 2 lines

In this case, git_dir is referencing owned memory that is later freed, so declare a new variable to hold the used string.

This sentence doesn't add any value as it simply explains parts that are obvious when reading the patch. I'd just drop it.

builtin/init-db.c int cmd_init_db(int argc, · line 251

Quoted 10 lines
builtin/init-db.c +1 -1
index 374ed3a74706..a2628568ff6a 100644
@@ -248,18 +248,19 @@ int cmd_init_db(int argc,
 	create_object_database(the_repository, NULL);
 
 	if (!quiet) {
-		int len = strlen(git_dir);
+		const char *full_git_dir = repo_get_git_dir(the_repository);

Previously, in init_db() we always assigned git_dir = repo_get_git_dir(repo), so it's clear that this change here does the equivalent.

builtin/init-db.c int cmd_init_db(int argc, · line 252

Quoted 1 line
builtin/init-db.c +1 -0
+		int len = strlen(full_git_dir);

I was wondering whether we want to fix the variable type to be a size_t while at it. But maybe we should just keep it as-is.

builtin/init-db.c int cmd_init_db(int argc, · line 264

Quoted 13 lines
builtin/init-db.c +2 -2
 		if (reinit)
 			printf(repo_settings_get_shared_repository(the_repository)
 			       ? _("Reinitialized existing shared Git repository in %s%s\n")
 			       : _("Reinitialized existing Git repository in %s%s\n"),
-			       git_dir, len && git_dir[len-1] != '/' ? "/" : "");
+			       full_git_dir, len && full_git_dir[len-1] != '/' ? "/" : "");
 		else
 			printf(repo_settings_get_shared_repository(the_repository)
 			       ? _("Initialized empty shared Git repository in %s%s\n")
 			       : _("Initialized empty Git repository in %s%s\n"),
-			       git_dir, len && git_dir[len-1] != '/' ? "/" : "");
+			       full_git_dir, len && full_git_dir[len-1] != '/' ? "/" : "");
 	}

The changes look obviously good to me.

I'm a bit on the fence about whether we want to have a regression test or not. It feels like a very specific thing in the user-facing output, so it's a bit dubious whether it's really worth it. But on the other hand, folks like you seem to care enough to make the test worthwhile, so maybe we should have one?

Thanks!

Patrick

#2 lore raw
Quoted 8 lines · Patrick Steinhardt
Quoted 4 lines · Matt Hunter

None of the discussion [1] around the original patch indicates that this change was intentional, so address the regression by similarly using repo_get_git_dir in this code's new location.

This indeed wasn't an intentional change. I also confirmed that with v2.55 we print the full path.

And with 2.56 :-) Output changed right at the beginning of this release cycle.

Quoted 6 lines
Quoted 3 lines

In this case, git_dir is referencing owned memory that is later freed, so declare a new variable to hold the used string.

This sentence doesn't add any value as it simply explains parts that are obvious when reading the patch. I'd just drop it.

Yeah.. Will do

Quoted 5 lines

builtin/init-db.c int cmd_init_db(int argc, · line 252

Quoted 2 lines
builtin/init-db.c +1 -0
+		int len = strlen(full_git_dir);

I was wondering whether we want to fix the variable type to be a size_t while at it. But maybe we should just keep it as-is.

It's not of consequence to this change, but I agree it would be more correct. I'll include it.

Quoted 4 lines

I'm a bit on the fence about whether we want to have a regression test or not. It feels like a very specific thing in the user-facing output, so it's a bit dubious whether it's really worth it.

This is my thought exactly.

Quoted 3 lines

But on the other
hand, folks like you seem to care enough to make the test worthwhile, so maybe we should have one?

I could be convinced to add one. I'll see if additional feedback comes in before sending a new revision, but won't plan for a test as of now.

Thanks for the review

Quoted 4 lines

Thanks!

Patrick

#3 lore raw
Quoted 7 lines · Matt Hunter
Quoted 6 lines · Patrick Steinhardt
Quoted 4 lines · Matt Hunter

None of the discussion [1] around the original patch indicates that this change was intentional, so address the regression by similarly using repo_get_git_dir in this code's new location.

Quoted 5 lines
Quoted 2 lines

This indeed wasn't an intentional change. I also confirmed that with v2.55 we print the full path.

And with 2.56 :-) Output changed right at the beginning of this release cycle.

Oh, that's something I missed. Even better then to fix this before it's part of any public release :)

Patrick

#4 lore raw
In reply to Patrick Steinhardt
Quoted 5 lines

I'm a bit on the fence about whether we want to have a regression test or not. It feels like a very specific thing in the user-facing output, so it's a bit dubious whether it's really worth it. But on the other hand, folks like you seem to care enough to make the test worthwhile, so maybe we should have one?

It seems self-evident that we need a regression test here. Because it's specifically the thing that would have prevented it, and the only thing that will prevent a new regression later on :)

Harald

#5 lore raw
Quoted 9 lines · Harald Nordgren
Quoted 5 lines

I'm a bit on the fence about whether we want to have a regression test or not. It feels like a very specific thing in the user-facing output, so it's a bit dubious whether it's really worth it. But on the other hand, folks like you seem to care enough to make the test worthwhile, so maybe we should have one?

It seems self-evident that we need a regression test here. Because it's specifically the thing that would have prevented it, and the only thing that will prevent a new regression later on :)

I wouldn't necessarily say self-evident as the result isn't wrong. It's still correct, even though it has changed compared to the previous status quo.

Patrick

#6 lore raw
Quoted 8 lines · Patrick Steinhardt
Quoted 6 lines · Harald Nordgren
Quoted 5 lines

I'm a bit on the fence about whether we want to have a regression test or not. It feels like a very specific thing in the user-facing output, so it's a bit dubious whether it's really worth it. But on the other hand, folks like you seem to care enough to make the test worthwhile, so maybe we should have one?

Quoted 7 lines
Quoted 3 lines

It seems self-evident that we need a regression test here. Because it's specifically the thing that would have prevented it, and the only thing that will prevent a new regression later on :)

I wouldn't necessarily say self-evident as the result isn't wrong. It's still correct, even though it has changed compared to the previous status quo.

I guess it's semantics. The regression test locks down the behavior, so it won't be accidentally changed later on.

Harald

#7 lore raw
In reply to Patrick Steinhardt
Quoted 7 lines · Patrick Steinhardt
Quoted 6 lines · Harald Nordgren
Quoted 5 lines

I'm a bit on the fence about whether we want to have a regression test or not. It feels like a very specific thing in the user-facing output, so it's a bit dubious whether it's really worth it. But on the other hand, folks like you seem to care enough to make the test worthwhile, so maybe we should have one?

Quoted 7 lines
Quoted 3 lines

It seems self-evident that we need a regression test here. Because it's specifically the thing that would have prevented it, and the only thing that will prevent a new regression later on :)

I wouldn't necessarily say self-evident as the result isn't wrong. It's still correct, even though it has changed compared to the previous status quo.

It is correct in the sense that it is not wrong, but we cannot deny that it is much less useful when trying to determine where the new repository was created while reading a transcript that contains an invocation of the 'git init' command. One would need to go back and see where the user in the transcript 'cd'ed to before issuing the command.

This episode makes me wonder what other details, not captured in our existing tests, users find useful and have grown accustomed to.

Thanks.

#8 lore raw

PATCH v2 1/1 init: show absolute path of (re)initialized repository

In reply to Matt Hunter

Since 20e38cba3926 (setup: split up concerns of init_db(), 2026-09-10), git-init produces the message:

Initialized empty Git repository in .git/

instead of the previous

Initialized empty Git repository in /full/path/to/repo/.git/

Prior to 20e38cba3926, the code producing this message was in function init_db (renamed at the time to create_repository). In that context, the git_dir variable was updated by 'git_dir = repo_get_git_dir(repo);' earlier in the function, to set the value used in printf.

None of the discussion [1] around the original patch indicates that this change was intentional, so address the regression by similarly using repo_get_git_dir in this code's new location.

1: https://lore.kernel.org/git/20260825-pks-odb-write-alternates-at-creation-time-v1-0-911513ba95c3@pks.im/

---

Changes in v2:
- len type is updated to size_t
- added a regression test (changed my mind on this)
- some text is stripped from the patch message
- Link to v1: https://patch.msgid.link/[email protected]

 builtin/init-db.c | 7 ++++---
 t/t0001-init.sh   | 6 ++++++
 2 files changed, 10 insertions(+), 3 deletions(-)
builtin/init-db.c +4 -3
index 374ed3a74706..9612d2b8bfca 100644
@@ -248,18 +248,19 @@ int cmd_init_db(int argc,
 	create_object_database(the_repository, NULL);
 
 	if (!quiet) {
-		int len = strlen(git_dir);
+		const char *full_git_dir = repo_get_git_dir(the_repository);
+		size_t len = strlen(full_git_dir);
 
 		if (reinit)
 			printf(repo_settings_get_shared_repository(the_repository)
 			       ? _("Reinitialized existing shared Git repository in %s%s\n")
 			       : _("Reinitialized existing Git repository in %s%s\n"),
-			       git_dir, len && git_dir[len-1] != '/' ? "/" : "");
+			       full_git_dir, len && full_git_dir[len-1] != '/' ? "/" : "");
 		else
 			printf(repo_settings_get_shared_repository(the_repository)
 			       ? _("Initialized empty shared Git repository in %s%s\n")
 			       : _("Initialized empty Git repository in %s%s\n"),
-			       git_dir, len && git_dir[len-1] != '/' ? "/" : "");
+			       full_git_dir, len && full_git_dir[len-1] != '/' ? "/" : "");
 	}
 
 	free(template_dir_to_free);
t/t0001-init.sh +6 -0
index f1d227f27167..fbf8a0e41dad 100755
@@ -1000,4 +1000,10 @@ test_expect_success 'init honors GIT_OBJECT_DIRECTORY' '
 	test_path_is_dir custom-odb/info
 '
 
+test_expect_success 'init shows absolute path to gitdir' '
+	expect="$(pwd)/init-path" &&
+	git init init-path >actual &&
+	test_grep "$expect" actual
+'
+
 test_done

Range-diff against v1:
1: c0869122d3fb ! 1: 49d220d97aa8 init: show absolute path of (re)initialized repository
@@ Commit message
change was intentional, so address the regression by similarly using repo_get_git_dir in this code's new location.

- In this case, git_dir is referencing owned memory that is later freed,
- so declare a new variable to hold the used string.
-
1: https://lore.kernel.org/git/20260825-pks-odb-write-alternates-at-creation-time-v1-0-911513ba95c3@pks.im/

@@ builtin/init-db.c: int cmd_init_db(int argc,
if (!quiet) {
- int len = strlen(git_dir);
+ const char *full_git_dir = repo_get_git_dir(the_repository);
-+ int len = strlen(full_git_dir);
++ size_t len = strlen(full_git_dir);

if (reinit)
printf(repo_settings_get_shared_repository(the_repository)
@@ builtin/init-db.c: int cmd_init_db(int argc,
}

free(template_dir_to_free);
+
+ ## t/t0001-init.sh ##
+@@ t/t0001-init.sh: test_expect_success 'init honors GIT_OBJECT_DIRECTORY' '
+ test_path_is_dir custom-odb/info
+ '
+
++test_expect_success 'init shows absolute path to gitdir' '
++ expect="$(pwd)/init-path" &&
++ git init init-path >actual &&
++ test_grep "$expect" actual
++'
++
+ test_done

base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926

Signature · 1 line
2.56.0.257.gf7497c7d27
#9 lore raw

t/t0001-init.sh test_expect_success 'init honors GIT_OBJECT_DIRECTORY' ' · line 1004

Quoted 10 lines · Matt Hunter
t/t0001-init.sh +2 -0
index f1d227f27167..fbf8a0e41dad 100755
@@ -1000,4 +1000,10 @@ test_expect_success 'init honors GIT_OBJECT_DIRECTORY' '
 	test_path_is_dir custom-odb/info
 '
 
+test_expect_success 'init shows absolute path to gitdir' '
+	expect="$(pwd)/init-path" &&

I don't think this extra variable buys us anything, so I'd just drop it.

t/t0001-init.sh test_expect_success 'init honors GIT_OBJECT_DIRECTORY' ' · line 1007

Quoted 3 lines
t/t0001-init.sh +3 -0
+	git init init-path >actual &&
+	test_grep "$expect" actual
+'

I would have tested for the whole message, which is short enough to be reasonable, I'd say.

Other than those nits I'm happy, thanks!

Patrick

#10 lore raw
Quoted 7 lines · Patrick Steinhardt

t/t0001-init.sh test_expect_success 'init honors GIT_OBJECT_DIRECTORY' ' · line 1004

Quoted 3 lines · Matt Hunter
t/t0001-init.sh +2 -0
+test_expect_success 'init shows absolute path to gitdir' '
+	expect="$(pwd)/init-path" &&

I don't think this extra variable buys us anything, so I'd just drop it.

Quoted 6 lines

t/t0001-init.sh test_expect_success 'init honors GIT_OBJECT_DIRECTORY' ' · line 1007

Quoted 3 lines
t/t0001-init.sh +3 -0
+	git init init-path >actual &&
+	test_grep "$expect" actual
+'

I would have tested for the whole message, which is short enough to be reasonable, I'd say.

I considered this. I decided on this simpler version since there are 4 possible messages that could appear at the end of git init. Do we want separate tests/assertions to check all outputs

"Reinitialized existing shared Git repository in %s%s" "Reinitialized existing Git repository in %s%s" "Initialized empty shared Git repository in %s%s" "Initialized empty Git repository in %s%s"

are as expected?

Furthermore, the final path element is configurable as well, though should just be ".git" in the case of these tests. I figured, since the actual breakage was about the appearance of the path, then I would just test _that aspect_ of the output in this test.

How do we feel about this assertion

echo "Initialized empty Git repository in $(pwd)/init-path/.git/" >expect test_cmp expect actual

which tests the whole message of just the default case? The unnecessary variable you point out above is repurposed into a needed tmp file.

#11 lore raw
Quoted 19 lines · Matt Hunter
Quoted 7 lines · Patrick Steinhardt

t/t0001-init.sh test_expect_success 'init honors GIT_OBJECT_DIRECTORY' ' · line 1004

Quoted 3 lines · Matt Hunter
t/t0001-init.sh +2 -0
+test_expect_success 'init shows absolute path to gitdir' '
+	expect="$(pwd)/init-path" &&

I don't think this extra variable buys us anything, so I'd just drop it.

Quoted 6 lines

t/t0001-init.sh test_expect_success 'init honors GIT_OBJECT_DIRECTORY' ' · line 1007

Quoted 3 lines
t/t0001-init.sh +3 -0
+	git init init-path >actual &&
+	test_grep "$expect" actual
+'

I would have tested for the whole message, which is short enough to be reasonable, I'd say.

I considered this. I decided on this simpler version since there are 4 possible messages that could appear at the end of git init. Do we want separate tests/assertions to check all outputs

Quoted 6 lines

"Reinitialized existing shared Git repository in %s%s" "Reinitialized existing Git repository in %s%s" "Initialized empty shared Git repository in %s%s" "Initialized empty Git repository in %s%s"

are as expected?

I feel like that'd probably be a bit excessive.

Quoted 5 lines

Furthermore, the final path element is configurable as well, though should just be ".git" in the case of these tests. I figured, since the actual breakage was about the appearance of the path, then I would just test _that aspect_ of the output in this test.

Quoted 7 lines

How do we feel about this assertion

echo "Initialized empty Git repository in $(pwd)/init-path/.git/" >expect test_cmp expect actual

which tests the whole message of just the default case? The unnecessary variable you point out above is repurposed into a needed tmp file.

I'd say that's good enough for now. Thanks!

Patrick

#12 lore raw

PATCH v3 1/1 init: show absolute path of (re)initialized repository

In reply to Matt Hunter

Since 20e38cba3926 (setup: split up concerns of init_db(), 2026-09-10), git-init produces the message:

Initialized empty Git repository in .git/

instead of the previous

Initialized empty Git repository in /full/path/to/repo/.git/

Prior to 20e38cba3926, the code producing this message was in function init_db (renamed at the time to create_repository). In that context, the git_dir variable was updated by 'git_dir = repo_get_git_dir(repo);' earlier in the function, to set the value used in printf.

None of the discussion [1] around the original patch indicates that this change was intentional, so address the regression by similarly using repo_get_git_dir in this code's new location.

1: https://lore.kernel.org/git/20260825-pks-odb-write-alternates-at-creation-time-v1-0-911513ba95c3@pks.im/

---

Changes in v3:
- expand test to cover the full output of the init command
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- len type is updated to size_t
- added a regression test (changed my mind on this)
- some text is stripped from the patch message
- Link to v1: https://patch.msgid.link/[email protected]

 builtin/init-db.c | 7 ++++---
 t/t0001-init.sh   | 6 ++++++
 2 files changed, 10 insertions(+), 3 deletions(-)
builtin/init-db.c +4 -3
index 374ed3a74706..9612d2b8bfca 100644
@@ -248,18 +248,19 @@ int cmd_init_db(int argc,
 	create_object_database(the_repository, NULL);
 
 	if (!quiet) {
-		int len = strlen(git_dir);
+		const char *full_git_dir = repo_get_git_dir(the_repository);
+		size_t len = strlen(full_git_dir);
 
 		if (reinit)
 			printf(repo_settings_get_shared_repository(the_repository)
 			       ? _("Reinitialized existing shared Git repository in %s%s\n")
 			       : _("Reinitialized existing Git repository in %s%s\n"),
-			       git_dir, len && git_dir[len-1] != '/' ? "/" : "");
+			       full_git_dir, len && full_git_dir[len-1] != '/' ? "/" : "");
 		else
 			printf(repo_settings_get_shared_repository(the_repository)
 			       ? _("Initialized empty shared Git repository in %s%s\n")
 			       : _("Initialized empty Git repository in %s%s\n"),
-			       git_dir, len && git_dir[len-1] != '/' ? "/" : "");
+			       full_git_dir, len && full_git_dir[len-1] != '/' ? "/" : "");
 	}
 
 	free(template_dir_to_free);
t/t0001-init.sh +6 -0
index f1d227f27167..5d438dcc2804 100755
@@ -1000,4 +1000,10 @@ test_expect_success 'init honors GIT_OBJECT_DIRECTORY' '
 	test_path_is_dir custom-odb/info
 '
 
+test_expect_success 'init shows absolute path to gitdir' '
+	echo "Initialized empty Git repository in $(pwd)/init-path/.git/" >expect &&
+	git init init-path >actual &&
+	test_cmp expect actual
+'
+
 test_done

Range-diff against v2:
1: 49d220d97aa8 ! 1: 6c9adee7702b init: show absolute path of (re)initialized repository

    @@ t/t0001-init.sh: test_expect_success 'init honors GIT_OBJECT_DIRECTORY' '
      '
      
     +test_expect_success 'init shows absolute path to gitdir' '
    -+	expect="$(pwd)/init-path" &&
    ++	echo "Initialized empty Git repository in $(pwd)/init-path/.git/" >expect &&
     +	git init init-path >actual &&
    -+	test_grep "$expect" actual
    ++	test_cmp expect actual
     +'
     +
      test_done

base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926

Signature · 1 line
2.56.0.333.g9ad7b1359b