Skip to content

Commit 79543e7

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 564d025 commit 79543e7

File tree

1 file changed

+65
-38
lines changed

1 file changed

+65
-38
lines changed

setup.c

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,68 @@ void initialize_repository_version(int hash_algo, int reinit)
18851885
git_config_set_gently("extensions.objectformat", NULL);
18861886
}
18871887

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+
18881950
static int create_default_files(const char *template_path,
18891951
const char *original_git_dir,
18901952
const char *initial_branch,
@@ -1896,10 +1958,8 @@ static int create_default_files(const char *template_path,
18961958
struct stat st1;
18971959
struct strbuf buf = STRBUF_INIT;
18981960
char *path;
1899-
char junk[2];
19001961
int reinit;
19011962
int filemode;
1902-
struct strbuf err = STRBUF_INIT;
19031963
const char *init_template_dir = NULL;
19041964
const char *work_tree = get_git_work_tree();
19051965

@@ -1919,6 +1979,8 @@ static int create_default_files(const char *template_path,
19191979
reset_shared_repository();
19201980
git_config(git_default_config, NULL);
19211981

1982+
reinit = is_reinit();
1983+
19221984
/*
19231985
* We must make sure command-line options continue to override any
19241986
* values we might have just re-read from the config.
@@ -1962,39 +2024,7 @@ static int create_default_files(const char *template_path,
19622024
adjust_shared_perm(get_git_dir());
19632025
}
19642026

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);
19982028
initialize_repository_version(fmt->hash_algo, 0);
19992029

20002030
/* Check filemode trustability */
@@ -2158,9 +2188,6 @@ int init_db(const char *git_dir, const char *real_git_dir,
21582188
prev_bare_repository,
21592189
init_shared_repository,
21602190
flags & INIT_DB_QUIET);
2161-
if (reinit && initial_branch)
2162-
warning(_("re-init: ignored --initial-branch=%s"),
2163-
initial_branch);
21642191

21652192
create_object_directory();
21662193

0 commit comments

Comments
 (0)