@matheusbsbs / matheus-bagliano / commits / 5cc8f37250
Fork 0

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>

Johannes Schindelin committed Mar 24, 2008 at 16:14 UTC 5cc8f372509298d13632d8784bc851a587937550
2 files changed +20 -2
builtin-init-db.c
+3 -2
@@ -167,9 +167,9 @@ static int create_default_files(const char *git_dir, const char *template_path)
167 {
168 unsigned len = strlen(git_dir);
169 static char path[PATH_MAX];
170 - unsigned char sha1[20];
170 struct stat st1;
171 char repo_version_string[10];
172 + char junk[2];
173 int reinit;
174 int filemode;
175
@@ -219,7 +219,8 @@ static int create_default_files(const char *git_dir, const char *template_path)
219 * branch, if it does not exist yet.
220 */
221 strcpy(path + len, "HEAD");
222 - reinit = !read_ref("HEAD", sha1);
222 + reinit = (!access(path, R_OK)
223 + || readlink(path, junk, sizeof(junk)-1) != -1);
224 if (!reinit) {
225 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
226 exit(1);
t/t0001-init.sh
+17
@@ -113,4 +113,21 @@ test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
113 fi
114 '
115
116 +test_expect_success 'reinit' '
117 +
118 + (
119 + unset GIT_CONFIG GIT_WORK_TREE GIT_CONFIG
120 +
121 + mkdir again &&
122 + cd again &&
123 + git init >out1 2>err1 &&
124 + git init >out2 2>err2
125 + ) &&
126 + grep "Initialized empty" again/out1 &&
127 + grep "Reinitialized existing" again/out2 &&
128 + >again/empty &&
129 + test_cmp again/empty again/err1 &&
130 + test_cmp again/empty again/err2
131 +'
132 +
133 test_done