Skip to content

Commit f937bc2

Browse files
kyleamgitster
authored andcommitted
add: error appropriately on repository with no commits
The previous commit made 'git add' abort when given a repository that doesn't have a commit checked out. However, the output upon failure isn't appropriate: % git add repo warning: adding embedded git repository: repo hint: You've added another git repository inside your current repository. hint: [...] error: unable to index file 'repo/' fatal: adding files failed The hint doesn't apply in this case, and the error message doesn't tell the user why 'repo' couldn't be added to the index. Provide better output by teaching add_to_index() to error when given a git directory where HEAD can't be resolved. To avoid the embedded repository warning and hint, call check_embedded_repo() only after add_file_to_index() succeeds because, in general, its output doesn't make sense if adding to the index fails. Signed-off-by: Kyle Meyer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b228270 commit f937bc2

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

builtin/add.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,12 @@ static int add_files(struct dir_struct *dir, int flags)
374374
}
375375

376376
for (i = 0; i < dir->nr; i++) {
377-
check_embedded_repo(dir->entries[i]->name);
378377
if (add_file_to_index(&the_index, dir->entries[i]->name, flags)) {
379378
if (!ignore_add_errors)
380379
die(_("adding files failed"));
381380
exit_status = 1;
381+
} else {
382+
check_embedded_repo(dir->entries[i]->name);
382383
}
383384
}
384385
return exit_status;

read-cache.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
702702
int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|
703703
(intent_only ? ADD_CACHE_NEW_ONLY : 0));
704704
int hash_flags = HASH_WRITE_OBJECT;
705+
struct object_id oid;
705706

706707
if (flags & ADD_CACHE_RENORMALIZE)
707708
hash_flags |= HASH_RENORMALIZE;
@@ -711,6 +712,8 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
711712

712713
namelen = strlen(path);
713714
if (S_ISDIR(st_mode)) {
715+
if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
716+
return error(_("'%s' does not have a commit checked out"), path);
714717
while (namelen && path[namelen-1] == '/')
715718
namelen--;
716719
}

t/t3700-add.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,17 @@ test_expect_success '"git add ." in empty repo' '
296296
)
297297
'
298298

299+
test_expect_success 'error on a repository with no commits' '
300+
rm -fr empty &&
301+
git init empty &&
302+
test_must_fail git add empty >actual 2>&1 &&
303+
cat >expect <<-EOF &&
304+
error: '"'empty/'"' does not have a commit checked out
305+
fatal: adding files failed
306+
EOF
307+
test_i18ncmp expect actual
308+
'
309+
299310
test_expect_success 'git add --dry-run of existing changed file' "
300311
echo new >>track-this &&
301312
git add --dry-run track-this >actual 2>&1 &&

0 commit comments

Comments
 (0)