Skip to content

Commit 00a09d5

Browse files
peffgitster
authored andcommitted
introduce "extensions" form of core.repositoryformatversion
Normally we try to avoid bumps of the whole-repository core.repositoryformatversion field. However, it is unavoidable if we want to safely change certain aspects of git in a backwards-incompatible way (e.g., modifying the set of ref tips that we must traverse to generate a list of unreachable, safe-to-prune objects). If we were to bump the repository version for every such change, then any implementation understanding version `X` would also have to understand `X-1`, `X-2`, and so forth, even though the incompatibilities may be in orthogonal parts of the system, and there is otherwise no reason we cannot implement one without the other (or more importantly, that the user cannot choose to use one feature without the other, weighing the tradeoff in compatibility only for that particular feature). This patch documents the existing repositoryformatversion strategy and introduces a new format, "1", which lets a repository specify that it must run with an arbitrary set of extensions. This can be used, for example: - to inform git that the objects should not be pruned based only on the reachability of the ref tips (e.g, because it has "clone --shared" children) - that the refs are stored in a format besides the usual "refs" and "packed-refs" directories Because we bump to format "1", and because format "1" requires that a running git knows about any extensions mentioned, we know that older versions of the code will not do something dangerous when confronted with these new formats. For example, if the user chooses to use database storage for refs, they may set the "extensions.refbackend" config to "db". Older versions of git will not understand format "1" and bail. Versions of git which understand "1" but do not know about "refbackend", or which know about "refbackend" but not about the "db" backend, will refuse to run. This is annoying, of course, but much better than the alternative of claiming that there are no refs in the repository, or writing to a location that other implementations will not read. Note that we are only defining the rules for format 1 here. We do not ever write format 1 ourselves; it is a tool that is meant to be used by users and future extensions to provide safety with older implementations. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent df97e5d commit 00a09d5

File tree

4 files changed

+159
-3
lines changed

4 files changed

+159
-3
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Git Repository Format Versions
2+
==============================
3+
4+
Every git repository is marked with a numeric version in the
5+
`core.repositoryformatversion` key of its `config` file. This version
6+
specifies the rules for operating on the on-disk repository data. An
7+
implementation of git which does not understand a particular version
8+
advertised by an on-disk repository MUST NOT operate on that repository;
9+
doing so risks not only producing wrong results, but actually losing
10+
data.
11+
12+
Because of this rule, version bumps should be kept to an absolute
13+
minimum. Instead, we generally prefer these strategies:
14+
15+
- bumping format version numbers of individual data files (e.g.,
16+
index, packfiles, etc). This restricts the incompatibilities only to
17+
those files.
18+
19+
- introducing new data that gracefully degrades when used by older
20+
clients (e.g., pack bitmap files are ignored by older clients, which
21+
simply do not take advantage of the optimization they provide).
22+
23+
A whole-repository format version bump should only be part of a change
24+
that cannot be independently versioned. For instance, if one were to
25+
change the reachability rules for objects, or the rules for locking
26+
refs, that would require a bump of the repository format version.
27+
28+
Note that this applies only to accessing the repository's disk contents
29+
directly. An older client which understands only format `0` may still
30+
connect via `git://` to a repository using format `1`, as long as the
31+
server process understands format `1`.
32+
33+
The preferred strategy for rolling out a version bump (whether whole
34+
repository or for a single file) is to teach git to read the new format,
35+
and allow writing the new format with a config switch or command line
36+
option (for experimentation or for those who do not care about backwards
37+
compatibility with older gits). Then after a long period to allow the
38+
reading capability to become common, we may switch to writing the new
39+
format by default.
40+
41+
The currently defined format versions are:
42+
43+
Version `0`
44+
-----------
45+
46+
This is the format defined by the initial version of git, including but
47+
not limited to the format of the repository directory, the repository
48+
configuration file, and the object and ref storage. Specifying the
49+
complete behavior of git is beyond the scope of this document.
50+
51+
Version `1`
52+
-----------
53+
54+
This format is identical to version `0`, with the following exceptions:
55+
56+
1. When reading the `core.repositoryformatversion` variable, a git
57+
implementation which supports version 1 MUST also read any
58+
configuration keys found in the `extensions` section of the
59+
configuration file.
60+
61+
2. If a version-1 repository specifies any `extensions.*` keys that
62+
the running git has not implemented, the operation MUST NOT
63+
proceed. Similarly, if the value of any known key is not understood
64+
by the implementation, the operation MUST NOT proceed.
65+
66+
Note that if no extensions are specified in the config file, then
67+
`core.repositoryformatversion` SHOULD be set to `0` (setting it to `1`
68+
provides no benefit, and makes the repository incompatible with older
69+
implementations of git).
70+
71+
This document will serve as the master list for extensions. Any
72+
implementation wishing to define a new extension should make a note of
73+
it here, in order to claim the name.
74+
75+
The defined extensions are:
76+
77+
`noop`
78+
~~~~~~
79+
80+
This extension does not change git's behavior at all. It is useful only
81+
for testing format-1 compatibility.

cache.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,13 @@ extern char *notes_ref_name;
686686

687687
extern int grafts_replace_parents;
688688

689+
/*
690+
* GIT_REPO_VERSION is the version we write by default. The
691+
* _READ variant is the highest number we know how to
692+
* handle.
693+
*/
689694
#define GIT_REPO_VERSION 0
695+
#define GIT_REPO_VERSION_READ 1
690696
extern int repository_format_version;
691697
extern int check_repository_format(void);
692698

setup.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
static int inside_git_dir = -1;
66
static int inside_work_tree = -1;
77
static int work_tree_config_is_bogus;
8+
static struct string_list unknown_extensions = STRING_LIST_INIT_DUP;
89

910
/*
1011
* The input parameter must contain an absolute path, and it must already be
@@ -352,10 +353,23 @@ void setup_work_tree(void)
352353

353354
static int check_repo_format(const char *var, const char *value, void *cb)
354355
{
356+
const char *ext;
357+
355358
if (strcmp(var, "core.repositoryformatversion") == 0)
356359
repository_format_version = git_config_int(var, value);
357360
else if (strcmp(var, "core.sharedrepository") == 0)
358361
shared_repository = git_config_perm(var, value);
362+
else if (skip_prefix(var, "extensions.", &ext)) {
363+
/*
364+
* record any known extensions here; otherwise,
365+
* we fall through to recording it as unknown, and
366+
* check_repository_format will complain
367+
*/
368+
if (!strcmp(ext, "noop"))
369+
;
370+
else
371+
string_list_append(&unknown_extensions, ext);
372+
}
359373
return 0;
360374
}
361375

@@ -366,6 +380,8 @@ static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
366380
config_fn_t fn;
367381
int ret = 0;
368382

383+
string_list_clear(&unknown_extensions, 0);
384+
369385
if (get_common_dir(&sb, gitdir))
370386
fn = check_repo_format;
371387
else
@@ -383,16 +399,31 @@ static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
383399
* is a good one.
384400
*/
385401
git_config_early(fn, NULL, repo_config);
386-
if (GIT_REPO_VERSION < repository_format_version) {
402+
if (GIT_REPO_VERSION_READ < repository_format_version) {
387403
if (!nongit_ok)
388404
die ("Expected git repo version <= %d, found %d",
389-
GIT_REPO_VERSION, repository_format_version);
405+
GIT_REPO_VERSION_READ, repository_format_version);
390406
warning("Expected git repo version <= %d, found %d",
391-
GIT_REPO_VERSION, repository_format_version);
407+
GIT_REPO_VERSION_READ, repository_format_version);
392408
warning("Please upgrade Git");
393409
*nongit_ok = -1;
394410
ret = -1;
395411
}
412+
413+
if (repository_format_version >= 1 && unknown_extensions.nr) {
414+
int i;
415+
416+
if (!nongit_ok)
417+
die("unknown repository extension: %s",
418+
unknown_extensions.items[0].string);
419+
420+
for (i = 0; i < unknown_extensions.nr; i++)
421+
warning("unknown repository extension: %s",
422+
unknown_extensions.items[i].string);
423+
*nongit_ok = -1;
424+
ret = -1;
425+
}
426+
396427
strbuf_release(&sb);
397428
return ret;
398429
}

t/t1302-repo-version.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,42 @@ test_expect_success 'gitdir required mode' '
6767
)
6868
'
6969

70+
check_allow () {
71+
git rev-parse --git-dir >actual &&
72+
echo .git >expect &&
73+
test_cmp expect actual
74+
}
75+
76+
check_abort () {
77+
test_must_fail git rev-parse --git-dir
78+
}
79+
80+
# avoid git-config, since it cannot be trusted to run
81+
# in a repository with a broken version
82+
mkconfig () {
83+
echo '[core]' &&
84+
echo "repositoryformatversion = $1" &&
85+
shift &&
86+
87+
if test $# -gt 0; then
88+
echo '[extensions]' &&
89+
for i in "$@"; do
90+
echo "$i"
91+
done
92+
fi
93+
}
94+
95+
while read outcome version extensions; do
96+
test_expect_success "$outcome version=$version $extensions" "
97+
mkconfig $version $extensions >.git/config &&
98+
check_${outcome}
99+
"
100+
done <<\EOF
101+
allow 0
102+
allow 1
103+
allow 1 noop
104+
abort 1 no-such-extension
105+
allow 0 no-such-extension
106+
EOF
107+
70108
test_done

0 commit comments

Comments
 (0)