Skip to content

Commit 48fa45f

Browse files
pks-tgitster
authored andcommitted
builtin/init: introduce --ref-format= value flag
Introduce a new `--ref-format` value flag for git-init(1) that allows the user to specify the ref format that is to be used for a newly initialized repository. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c4a531 commit 48fa45f

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

Documentation/git-init.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ SYNOPSIS
1111
[verse]
1212
'git init' [-q | --quiet] [--bare] [--template=<template-directory>]
1313
[--separate-git-dir <git-dir>] [--object-format=<format>]
14+
[--ref-format=<format>]
1415
[-b <branch-name> | --initial-branch=<branch-name>]
1516
[--shared[=<permissions>]] [<directory>]
1617

@@ -57,6 +58,12 @@ values are 'sha1' and (if enabled) 'sha256'. 'sha1' is the default.
5758
+
5859
include::object-format-disclaimer.txt[]
5960

61+
--ref-format=<format>::
62+
63+
Specify the given ref storage format for the repository. The valid values are:
64+
+
65+
include::ref-storage-format.txt[]
66+
6067
--template=<template-directory>::
6168

6269
Specify the directory from which templates will be used. (See the "TEMPLATE

builtin/init-db.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ static int shared_callback(const struct option *opt, const char *arg, int unset)
5858
static const char *const init_db_usage[] = {
5959
N_("git init [-q | --quiet] [--bare] [--template=<template-directory>]\n"
6060
" [--separate-git-dir <git-dir>] [--object-format=<format>]\n"
61+
" [--ref-format=<format>]\n"
6162
" [-b <branch-name> | --initial-branch=<branch-name>]\n"
6263
" [--shared[=<permissions>]] [<directory>]"),
6364
NULL
@@ -77,8 +78,10 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
7778
const char *template_dir = NULL;
7879
unsigned int flags = 0;
7980
const char *object_format = NULL;
81+
const char *ref_format = NULL;
8082
const char *initial_branch = NULL;
8183
int hash_algo = GIT_HASH_UNKNOWN;
84+
unsigned int ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN;
8285
int init_shared_repository = -1;
8386
const struct option init_db_options[] = {
8487
OPT_STRING(0, "template", &template_dir, N_("template-directory"),
@@ -96,6 +99,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
9699
N_("override the name of the initial branch")),
97100
OPT_STRING(0, "object-format", &object_format, N_("hash"),
98101
N_("specify the hash algorithm to use")),
102+
OPT_STRING(0, "ref-format", &ref_format, N_("format"),
103+
N_("specify the reference format to use")),
99104
OPT_END()
100105
};
101106

@@ -159,6 +164,12 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
159164
die(_("unknown hash algorithm '%s'"), object_format);
160165
}
161166

167+
if (ref_format) {
168+
ref_storage_format = ref_storage_format_by_name(ref_format);
169+
if (ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
170+
die(_("unknown ref storage format '%s'"), ref_format);
171+
}
172+
162173
if (init_shared_repository != -1)
163174
set_shared_repository(init_shared_repository);
164175

@@ -237,6 +248,6 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
237248

238249
flags |= INIT_DB_EXIST_OK;
239250
return init_db(git_dir, real_git_dir, template_dir, hash_algo,
240-
REF_STORAGE_FORMAT_UNKNOWN, initial_branch,
251+
ref_storage_format, initial_branch,
241252
init_shared_repository, flags);
242253
}

t/t0001-init.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,32 @@ test_expect_success 'init with GIT_DEFAULT_REF_FORMAT=garbage' '
576576
test_cmp expect err
577577
'
578578

579+
test_expect_success 'init with --ref-format=files' '
580+
test_when_finished "rm -rf refformat" &&
581+
git init --ref-format=files refformat &&
582+
echo files >expect &&
583+
git -C refformat rev-parse --show-ref-format >actual &&
584+
test_cmp expect actual
585+
'
586+
587+
test_expect_success 're-init with same format' '
588+
test_when_finished "rm -rf refformat" &&
589+
git init --ref-format=files refformat &&
590+
git init --ref-format=files refformat &&
591+
echo files >expect &&
592+
git -C refformat rev-parse --show-ref-format >actual &&
593+
test_cmp expect actual
594+
'
595+
596+
test_expect_success 'init with --ref-format=garbage' '
597+
test_when_finished "rm -rf refformat" &&
598+
cat >expect <<-EOF &&
599+
fatal: unknown ref storage format ${SQ}garbage${SQ}
600+
EOF
601+
test_must_fail git init --ref-format=garbage refformat 2>err &&
602+
test_cmp expect err
603+
'
604+
579605
test_expect_success MINGW 'core.hidedotfiles = false' '
580606
git config --global core.hidedotfiles false &&
581607
rm -rf newdir &&

0 commit comments

Comments
 (0)