git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] init: show "Reinit" message even in an (existing) empty repository

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 25, 2008, 07:35 UTC
Message-ID
<7viqzbmf2h.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<alpine.LSU.1.00.0803241613480.4353@racer.site>
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 12 quoted lines
> diff --git a/builtin-init-db.c b/builtin-init-db.c
> index 1975910..ceb4727 100644
> --- a/builtin-init-db.c
> +++ b/builtin-init-db.c
> @@ -168,10 +168,9 @@ static int create_default_files(const char *git_dir, const char *template_path)
>  {
>  	unsigned len = strlen(git_dir);
>  	static char path[PATH_MAX];
> -	unsigned char sha1[20];
>  	struct stat st1;
>  	char repo_version_string[10];
> 	int reinit;
Here I saw a hand-edit, but that is less of a problem...
Show 9 quoted lines
>  	int filemode;
>  
>  	if (len > sizeof(path)-50)
> @@ -220,7 +219,7 @@ static int create_default_files(const char *git_dir, const char *template_path)
>  	 * branch, if it does not exist yet.
>  	 */
>  	strcpy(path + len, "HEAD");
> -	reinit = !read_ref("HEAD", sha1);
> +	reinit = !access(path, R_OK);

If this is a HEAD (or .git/HEAD) that is a dangling symlink (we do still support them, don't we?) wouldn't this access fail?

-- >8 --
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Date: Mon, 24 Mar 2008 16:14:52 +0100
Subject: [PATCH] init: show "Reinit" message even in an (existing) empty repository

Earlier, git-init tested for a valid HEAD ref, but if the repository was empty, there was none. Instead, test for the existence of the file $GIT_DIR/HEAD.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-init-db.c |    5 +++--
 t/t0001-init.sh   |   17 +++++++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/builtin-init-db.c b/builtin-init-db.c
index 79eaf8d..2854868 100644
--- a/builtin-init-db.c
+++ b/builtin-init-db.c
@@ -167,9 +167,9 @@ static int create_default_files(const char *git_dir, const char *template_path)
 {
 	unsigned len = strlen(git_dir);
 	static char path[PATH_MAX];
-	unsigned char sha1[20];
 	struct stat st1;
 	char repo_version_string[10];
+	char junk[2];
 	int reinit;
 	int filemode;
 
@@ -219,7 +219,8 @@ static int create_default_files(const char *git_dir, const char *template_path)
 	 * branch, if it does not exist yet.
 	 */
 	strcpy(path + len, "HEAD");
-	reinit = !read_ref("HEAD", sha1);
+	reinit = (!access(path, R_OK)
+		  || readlink(path, junk, sizeof(junk)-1) != -1);
 	if (!reinit) {
 		if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
 			exit(1);
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index c015405..b0289e3 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -113,4 +113,21 @@ test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
 	fi
 '
 
+test_expect_success 'reinit' '
+
+	(
+		unset GIT_CONFIG GIT_WORK_TREE GIT_CONFIG
+
+		mkdir again &&
+		cd again &&
+		git init >out1 2>err1 &&
+		git init >out2 2>err2
+	) &&
+	grep "Initialized empty" again/out1 &&
+	grep "Reinitialized existing" again/out2 &&
+	>again/empty &&
+	test_cmp again/empty again/err1 &&
+	test_cmp again/empty again/err2
+'
+
 test_done
-- 
1.5.5.rc1.121.g1594
Previous: Johannes SchindelinNext: Johannes Schindelin
Message 20 of 25 in “Store autocrlf during init-db (was [msysGit] autocrlf problems with Git-preview20080301.exe)”
  1. Steffen ProhaskaMar 23, 2008
  2. init-db: Store current value of autocrlfSteffen Prohaska, Mar 23, 2008
  3. Dmitry PotapovMar 24, 2008
  4. Johannes SchindelinMar 23, 2008
  5. Steffen ProhaskaMar 23, 2008
  6. Johannes SchindelinMar 23, 2008
  7. Steffen ProhaskaMar 23, 2008
  8. Johannes SchindelinMar 23, 2008
  9. Steffen ProhaskaMar 23, 2008
  10. Johannes SchindelinMar 23, 2008
  11. Steffen ProhaskaMar 23, 2008
  12. Johannes SchindelinMar 23, 2008
  13. Dmitry PotapovMar 23, 2008
  14. Johannes SchindelinMar 24, 2008
  15. Dmitry PotapovMar 24, 2008
  16. Johannes SchindelinMar 24, 2008
  17. Dmitry PotapovMar 24, 2008
  18. Introduce core.initHookJohannes Schindelin, Mar 24, 2008
  19. init: show "Reinit" message even in an (existing) empty repositoryJohannes Schindelin, Mar 24, 2008
  20. Junio C HamanoMar 25, 2008
  21. Johannes SchindelinMar 25, 2008
  22. Junio C HamanoMar 24, 2008
  23. Introduce core.initHookJohannes Schindelin, Mar 24, 2008
  24. Junio C HamanoMar 24, 2008
  25. Introduce core.initHookJohannes Schindelin, Mar 24, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.