Skip to content

Commit 5cc8f37

Browse files
dschogitster
authored andcommitted
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>
1 parent 76ce946 commit 5cc8f37

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

builtin-init-db.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ static int create_default_files(const char *git_dir, const char *template_path)
167167
{
168168
unsigned len = strlen(git_dir);
169169
static char path[PATH_MAX];
170-
unsigned char sha1[20];
171170
struct stat st1;
172171
char repo_version_string[10];
172+
char junk[2];
173173
int reinit;
174174
int filemode;
175175

@@ -219,7 +219,8 @@ static int create_default_files(const char *git_dir, const char *template_path)
219219
* branch, if it does not exist yet.
220220
*/
221221
strcpy(path + len, "HEAD");
222-
reinit = !read_ref("HEAD", sha1);
222+
reinit = (!access(path, R_OK)
223+
|| readlink(path, junk, sizeof(junk)-1) != -1);
223224
if (!reinit) {
224225
if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
225226
exit(1);

t/t0001-init.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,21 @@ test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
113113
fi
114114
'
115115

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+
116133
test_done

0 commit comments

Comments
 (0)