Skip to content

Commit 84d2a03

Browse files
committed
repo: teach isempty about default branch config
The git_repository_isempty function now respects the init.defaultbranch setting (instead of hardcoding "master") to understand if a repository is empty or not.
1 parent 4cc3b2c commit 84d2a03

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/repository.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,20 +2391,22 @@ int git_repository_initialbranch(git_buf *out, git_repository *repo)
23912391
int git_repository_is_empty(git_repository *repo)
23922392
{
23932393
git_reference *head = NULL;
2394-
int is_empty = 0;
2394+
git_buf initialbranch = GIT_BUF_INIT;
2395+
int result = 0;
23952396

2396-
if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0)
2397-
return -1;
2397+
if ((result = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0 ||
2398+
(result = git_repository_initialbranch(&initialbranch, repo)) < 0)
2399+
goto done;
23982400

2399-
if (git_reference_type(head) == GIT_REFERENCE_SYMBOLIC)
2400-
is_empty =
2401-
(strcmp(git_reference_symbolic_target(head),
2402-
GIT_REFS_HEADS_DIR "master") == 0) &&
2403-
repo_contains_no_reference(repo);
2401+
result = (git_reference_type(head) == GIT_REFERENCE_SYMBOLIC &&
2402+
strcmp(git_reference_symbolic_target(head), initialbranch.ptr) == 0 &&
2403+
repo_contains_no_reference(repo));
24042404

2405+
done:
24052406
git_reference_free(head);
2407+
git_buf_dispose(&initialbranch);
24062408

2407-
return is_empty;
2409+
return result;
24082410
}
24092411

24102412
static const char *resolved_parent_path(const git_repository *repo, git_repository_item_t item, git_repository_item_t fallback)

0 commit comments

Comments
 (0)