Skip to content

Commit 0c22e09

Browse files
pks-tgitster
authored andcommitted
setup: make object format configurable via config
The object format for repositories can either be configured explicitly by passing the `--object-format=` option to git-init(1) or git-clone(1), or globally by setting the `GIT_DEFAULT_HASH` environment variable. While the former makes sense, setting random environment variables is not really a good user experience in case someone decides to only use SHA256 repositories. It is only natural to expect for a user that things like this can also be configured via their config. As such, introduce a new config "init.defaultObjectFormat", similar to "init.defaultBranch", that allows the user to configure the default object format when creating new repos. The precedence order now is the following, where the first one wins: 1. The `--object-format=` switch. 2. The `GIT_DEFAULT_HASH` environment variable. 3. The `init.defaultObjectFormat` config variable. This matches the typical precedence order we use in Git. We typically let the environment override the config such that the latter can easily be overridden on an ephemeral basis, for example by scripts. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 39e15b7 commit 0c22e09

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

Documentation/config/init.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ endif::[]
88
`init.defaultBranch`::
99
Allows overriding the default branch name e.g. when initializing
1010
a new repository.
11+
`init.defaultObjectFormat`::
12+
Allows overriding the default object format for new repositories. See
13+
`--object-format=` in linkgit:git-init[1]. Both the command line option
14+
and the `GIT_DEFAULT_HASH` environment variable take precedence over
15+
this config.

setup.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,11 +2284,49 @@ static void separate_git_dir(const char *git_dir, const char *git_link)
22842284
write_file(git_link, "gitdir: %s", git_dir);
22852285
}
22862286

2287+
struct default_format_config {
2288+
int hash;
2289+
};
2290+
2291+
static int read_default_format_config(const char *key, const char *value,
2292+
const struct config_context *ctx UNUSED,
2293+
void *payload)
2294+
{
2295+
struct default_format_config *cfg = payload;
2296+
char *str = NULL;
2297+
int ret;
2298+
2299+
if (!strcmp(key, "init.defaultobjectformat")) {
2300+
ret = git_config_string(&str, key, value);
2301+
if (ret)
2302+
goto out;
2303+
cfg->hash = hash_algo_by_name(str);
2304+
if (cfg->hash == GIT_HASH_UNKNOWN)
2305+
warning(_("unknown hash algorithm '%s'"), str);
2306+
goto out;
2307+
}
2308+
2309+
ret = 0;
2310+
out:
2311+
free(str);
2312+
return ret;
2313+
}
2314+
22872315
static void repository_format_configure(struct repository_format *repo_fmt,
22882316
int hash, enum ref_storage_format ref_format)
22892317
{
2318+
struct default_format_config cfg = {
2319+
.hash = GIT_HASH_UNKNOWN,
2320+
};
2321+
struct config_options opts = {
2322+
.respect_includes = 1,
2323+
.ignore_repo = 1,
2324+
.ignore_worktree = 1,
2325+
};
22902326
const char *env;
22912327

2328+
config_with_options(read_default_format_config, &cfg, NULL, NULL, &opts);
2329+
22922330
/*
22932331
* If we already have an initialized repo, don't allow the user to
22942332
* specify a different algorithm, as that could cause corruption.
@@ -2304,6 +2342,8 @@ static void repository_format_configure(struct repository_format *repo_fmt,
23042342
if (env_algo == GIT_HASH_UNKNOWN)
23052343
die(_("unknown hash algorithm '%s'"), env);
23062344
repo_fmt->hash_algo = env_algo;
2345+
} else if (cfg.hash != GIT_HASH_UNKNOWN) {
2346+
repo_fmt->hash_algo = cfg.hash;
23072347
}
23082348
repo_set_hash_algo(the_repository, repo_fmt->hash_algo);
23092349

t/t0001-init.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,56 @@ test_expect_success 'init honors --object-format' '
523523
test_cmp expected actual
524524
'
525525

526+
test_expect_success 'init honors init.defaultObjectFormat' '
527+
test_when_finished "rm -rf sha1 sha256" &&
528+
529+
test_config_global init.defaultObjectFormat sha1 &&
530+
(
531+
sane_unset GIT_DEFAULT_HASH &&
532+
git init sha1 &&
533+
git -C sha1 rev-parse --show-object-format >actual &&
534+
echo sha1 >expected &&
535+
test_cmp expected actual
536+
) &&
537+
538+
test_config_global init.defaultObjectFormat sha256 &&
539+
(
540+
sane_unset GIT_DEFAULT_HASH &&
541+
git init sha256 &&
542+
git -C sha256 rev-parse --show-object-format >actual &&
543+
echo sha256 >expected &&
544+
test_cmp expected actual
545+
)
546+
'
547+
548+
test_expect_success 'init warns about invalid init.defaultObjectFormat' '
549+
test_when_finished "rm -rf repo" &&
550+
test_config_global init.defaultObjectFormat garbage &&
551+
552+
echo "warning: unknown hash algorithm ${SQ}garbage${SQ}" >expect &&
553+
git init repo 2>err &&
554+
test_cmp expect err &&
555+
556+
git -C repo rev-parse --show-object-format >actual &&
557+
echo $GIT_DEFAULT_HASH >expected &&
558+
test_cmp expected actual
559+
'
560+
561+
test_expect_success '--object-format overrides GIT_DEFAULT_HASH' '
562+
test_when_finished "rm -rf repo" &&
563+
GIT_DEFAULT_HASH=sha1 git init --object-format=sha256 repo &&
564+
git -C repo rev-parse --show-object-format >actual &&
565+
echo sha256 >expected
566+
'
567+
568+
test_expect_success 'GIT_DEFAULT_HASH overrides init.defaultObjectFormat' '
569+
test_when_finished "rm -rf repo" &&
570+
test_config_global init.defaultObjectFormat sha1 &&
571+
GIT_DEFAULT_HASH=sha256 git init repo &&
572+
git -C repo rev-parse --show-object-format >actual &&
573+
echo sha256 >expected
574+
'
575+
526576
test_expect_success 'extensions.objectFormat is not allowed with repo version 0' '
527577
test_when_finished "rm -rf explicit-v0" &&
528578
git init --object-format=sha256 explicit-v0 &&

0 commit comments

Comments
 (0)