setup: extract function to create the refdb
We're about to let callers skip creation of the reference database when calling `init_db()`. Extract the logic into a standalone function so that it becomes easier to do this refactoring. While at it, expand the comment that explains why we always create the "refs/" directory. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Dec 12, 2023 at 08:00 UTC
79543e760d0219063ab21f687e7538c3e89b2e99
1 file changed
+65
-38
setup.c
+65
-38
@@ -1885,6 +1885,68 @@ void initialize_repository_version(int hash_algo, int reinit)
1885
git_config_set_gently("extensions.objectformat", NULL);
1886
}
1887
1888
+static int is_reinit(void)
1889
+{
1890
+ struct strbuf buf = STRBUF_INIT;
1891
+ char junk[2];
1892
+ int ret;
1893
+
1894
+ git_path_buf(&buf, "HEAD");
1895
+ ret = !access(buf.buf, R_OK) || readlink(buf.buf, junk, sizeof(junk) - 1) != -1;
1896
+ strbuf_release(&buf);
1897
+ return ret;
1898
+}
1899
+
1900
+static void create_reference_database(const char *initial_branch, int quiet)
1901
+{
1902
+ struct strbuf err = STRBUF_INIT;
1903
+ int reinit = is_reinit();
1904
+
1905
+ /*
1906
+ * We need to create a "refs" dir in any case so that older versions of
1907
+ * Git can tell that this is a repository. This serves two main purposes:
1908
+ *
1909
+ * - Clients will know to stop walking the parent-directory chain when
1910
+ * detecting the Git repository. Otherwise they may end up detecting
1911
+ * a Git repository in a parent directory instead.
1912
+ *
1913
+ * - Instead of failing to detect a repository with unknown reference
1914
+ * format altogether, old clients will print an error saying that
1915
+ * they do not understand the reference format extension.
1916
+ */
1917
+ safe_create_dir(git_path("refs"), 1);
1918
+ adjust_shared_perm(git_path("refs"));
1919
+
1920
+ if (refs_init_db(&err))
1921
+ die("failed to set up refs db: %s", err.buf);
1922
+
1923
+ /*
1924
+ * Point the HEAD symref to the initial branch with if HEAD does
1925
+ * not yet exist.
1926
+ */
1927
+ if (!reinit) {
1928
+ char *ref;
1929
+
1930
+ if (!initial_branch)
1931
+ initial_branch = git_default_branch_name(quiet);
1932
+
1933
+ ref = xstrfmt("refs/heads/%s", initial_branch);
1934
+ if (check_refname_format(ref, 0) < 0)
1935
+ die(_("invalid initial branch name: '%s'"),
1936
+ initial_branch);
1937
+
1938
+ if (create_symref("HEAD", ref, NULL) < 0)
1939
+ exit(1);
1940
+ free(ref);
1941
+ }
1942
+
1943
+ if (reinit && initial_branch)
1944
+ warning(_("re-init: ignored --initial-branch=%s"),
1945
+ initial_branch);
1946
+
1947
+ strbuf_release(&err);
1948
+}
1949
+
1950
static int create_default_files(const char *template_path,
1951
const char *original_git_dir,
1952
const char *initial_branch,
@@ -1896,10 +1958,8 @@ static int create_default_files(const char *template_path,
1958
struct stat st1;
1959
struct strbuf buf = STRBUF_INIT;
1960
char *path;
1899
- char junk[2];
1961
int reinit;
1962
int filemode;
1902
- struct strbuf err = STRBUF_INIT;
1963
const char *init_template_dir = NULL;
1964
const char *work_tree = get_git_work_tree();
1965
@@ -1919,6 +1979,8 @@ static int create_default_files(const char *template_path,
1979
reset_shared_repository();
1980
git_config(git_default_config, NULL);
1981
1982
+ reinit = is_reinit();
1983
+
1984
/*
1985
* We must make sure command-line options continue to override any
1986
* values we might have just re-read from the config.
@@ -1962,39 +2024,7 @@ static int create_default_files(const char *template_path,
2024
adjust_shared_perm(get_git_dir());
2025
}
2026
1965
- /*
1966
- * We need to create a "refs" dir in any case so that older
1967
- * versions of git can tell that this is a repository.
1968
- */
1969
- safe_create_dir(git_path("refs"), 1);
1970
- adjust_shared_perm(git_path("refs"));
1971
-
1972
- if (refs_init_db(&err))
1973
- die("failed to set up refs db: %s", err.buf);
1974
-
1975
- /*
1976
- * Point the HEAD symref to the initial branch with if HEAD does
1977
- * not yet exist.
1978
- */
1979
- path = git_path_buf(&buf, "HEAD");
1980
- reinit = (!access(path, R_OK)
1981
- || readlink(path, junk, sizeof(junk)-1) != -1);
1982
- if (!reinit) {
1983
- char *ref;
1984
-
1985
- if (!initial_branch)
1986
- initial_branch = git_default_branch_name(quiet);
1987
-
1988
- ref = xstrfmt("refs/heads/%s", initial_branch);
1989
- if (check_refname_format(ref, 0) < 0)
1990
- die(_("invalid initial branch name: '%s'"),
1991
- initial_branch);
1992
-
1993
- if (create_symref("HEAD", ref, NULL) < 0)
1994
- exit(1);
1995
- free(ref);
1996
- }
1997
-
2027
+ create_reference_database(initial_branch, quiet);
2028
initialize_repository_version(fmt->hash_algo, 0);
2029
2030
/* Check filemode trustability */
@@ -2158,9 +2188,6 @@ int init_db(const char *git_dir, const char *real_git_dir,
2188
prev_bare_repository,
2189
init_shared_repository,
2190
flags & INIT_DB_QUIET);
2161
- if (reinit && initial_branch)
2162
- warning(_("re-init: ignored --initial-branch=%s"),
2163
- initial_branch);
2191
2192
create_object_directory();
2193