repo: add git_repository_initialbranch Provide a helper function to get the initial branch for a repository, respecting the `init.defaultBranch` configuration option, if set, and returning the "default default" (currently `master`) otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
diff --git a/src/repository.c b/src/repository.c
index f778912..e4a374d 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -2354,6 +2354,40 @@ static int repo_contains_no_reference(git_repository *repo)
return error;
}
+int git_repository_initialbranch(git_buf *out, git_repository *repo)
+{
+ git_config *config;
+ git_config_entry *entry = NULL;
+ const char *branch;
+ int error;
+
+ if ((error = git_repository_config__weakptr(&config, repo)) < 0)
+ return error;
+
+ if ((error = git_config_get_entry(&entry, config, "init.defaultbranch")) == 0) {
+ branch = entry->value;
+ }
+ else if (error == GIT_ENOTFOUND) {
+ branch = GIT_BRANCH_DEFAULT;
+ }
+ else {
+ goto done;
+ }
+
+ if ((error = git_buf_puts(out, GIT_REFS_HEADS_DIR)) < 0 ||
+ (error = git_buf_puts(out, branch)) < 0)
+ goto done;
+
+ if (!git_reference_is_valid_name(out->ptr)) {
+ git_error_set(GIT_ERROR_INVALID, "the value of init.defaultBranch is not a valid reference name");
+ error = -1;
+ }
+
+done:
+ git_config_entry_free(entry);
+ return error;
+}
+
int git_repository_is_empty(git_repository *repo)
{
git_reference *head = NULL;
diff --git a/src/repository.h b/src/repository.h
index d73e77d..de009ba 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -232,4 +232,10 @@ extern size_t git_repository__reserved_names_posix_len;
bool git_repository__reserved_names(
git_buf **out, size_t *outlen, git_repository *repo, bool include_ntfs);
+/*
+ * The default branch for the repository; the `init.defaultBranch`
+ * configuration option, if set, or `master` if it is not.
+ */
+int git_repository_initialbranch(git_buf *out, git_repository *repo);
+
#endif