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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Dec 29, 2023 at 08:27 UTC
48fa45f5fb80de821a380a7ff81ab2c568d69e35
3 files changed
+45
-1
Documentation/git-init.txt
+7
@@ -11,6 +11,7 @@ SYNOPSIS
11
[verse]
12
'git init' [-q | --quiet] [--bare] [--template=<template-directory>]
13
[--separate-git-dir <git-dir>] [--object-format=<format>]
14
+ [--ref-format=<format>]
15
[-b <branch-name> | --initial-branch=<branch-name>]
16
[--shared[=<permissions>]] [<directory>]
17
@@ -57,6 +58,12 @@ values are 'sha1' and (if enabled) 'sha256'. 'sha1' is the default.
58
+
59
include::object-format-disclaimer.txt[]
60
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
+
67
--template=<template-directory>::
68
69
Specify the directory from which templates will be used. (See the "TEMPLATE
builtin/init-db.c
+12
-1
@@ -58,6 +58,7 @@ static int shared_callback(const struct option *opt, const char *arg, int unset)
58
static const char *const init_db_usage[] = {
59
N_("git init [-q | --quiet] [--bare] [--template=<template-directory>]\n"
60
" [--separate-git-dir <git-dir>] [--object-format=<format>]\n"
61
+ " [--ref-format=<format>]\n"
62
" [-b <branch-name> | --initial-branch=<branch-name>]\n"
63
" [--shared[=<permissions>]] [<directory>]"),
64
NULL
@@ -77,8 +78,10 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
78
const char *template_dir = NULL;
79
unsigned int flags = 0;
80
const char *object_format = NULL;
81
+ const char *ref_format = NULL;
82
const char *initial_branch = NULL;
83
int hash_algo = GIT_HASH_UNKNOWN;
84
+ unsigned int ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN;
85
int init_shared_repository = -1;
86
const struct option init_db_options[] = {
87
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)
99
N_("override the name of the initial branch")),
100
OPT_STRING(0, "object-format", &object_format, N_("hash"),
101
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")),
104
OPT_END()
105
};
106
@@ -159,6 +164,12 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
164
die(_("unknown hash algorithm '%s'"), object_format);
165
}
166
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
+
173
if (init_shared_repository != -1)
174
set_shared_repository(init_shared_repository);
175
@@ -237,6 +248,6 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
248
249
flags |= INIT_DB_EXIST_OK;
250
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,
252
init_shared_repository, flags);
253
}
t/t0001-init.sh
+26
@@ -576,6 +576,32 @@ test_expect_success 'init with GIT_DEFAULT_REF_FORMAT=garbage' '
576
test_cmp expect err
577
'
578
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
+
605
test_expect_success MINGW 'core.hidedotfiles = false' '
606
git config --global core.hidedotfiles false &&
607
rm -rf newdir &&