Do check_repository_format() early (re-fix)
This pushes check_repository_format() (actually _gently() version) to setup_git_directory_gently() in order to prevent from using unsupported repositories. New setup_git_directory_gently()'s behaviour is stop searching for a valid gitdir and return as if there is no gitdir if a unsupported repository is found. Warning will be thrown in these cases. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Dec 5, 2007 at 20:33 UTC
9459aa77a032621a29d53605542844641cca843a
2 files changed
+72
-12
setup.c
+26
-12
@@ -206,6 +206,22 @@ static const char *set_work_tree(const char *dir)
206
return NULL;
207
}
208
209
+static int check_repository_format_gently(int *nongit_ok)
210
+{
211
+ git_config(check_repository_format_version);
212
+ if (GIT_REPO_VERSION < repository_format_version) {
213
+ if (!nongit_ok)
214
+ die ("Expected git repo version <= %d, found %d",
215
+ GIT_REPO_VERSION, repository_format_version);
216
+ warning("Expected git repo version <= %d, found %d",
217
+ GIT_REPO_VERSION, repository_format_version);
218
+ warning("Please upgrade Git");
219
+ *nongit_ok = -1;
220
+ return -1;
221
+ }
222
+ return 0;
223
+}
224
+
225
/*
226
* We cannot decide in this function whether we are in the work tree or
227
* not, since the config can only be read _after_ this function was called.
@@ -232,12 +248,13 @@ const char *setup_git_directory_gently(int *nongit_ok)
248
249
if (!work_tree_env) {
250
retval = set_work_tree(gitdirenv);
235
- /* config may override worktree
236
- * see set_work_tree comment */
237
- check_repository_format();
251
+ /* config may override worktree */
252
+ if (check_repository_format_gently(nongit_ok))
253
+ return NULL;
254
return retval;
255
}
240
- check_repository_format();
256
+ if (check_repository_format_gently(nongit_ok))
257
+ return NULL;
258
retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
259
get_git_work_tree());
260
if (!retval || !*retval)
@@ -246,7 +263,6 @@ const char *setup_git_directory_gently(int *nongit_ok)
263
if (chdir(work_tree_env) < 0)
264
die ("Could not chdir to %s", work_tree_env);
265
strcat(buffer, "/");
249
- inside_work_tree = 1;
266
return retval;
267
}
268
if (nongit_ok) {
@@ -277,7 +293,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
293
if (!work_tree_env)
294
inside_work_tree = 0;
295
setenv(GIT_DIR_ENVIRONMENT, ".", 1);
280
- check_repository_format();
296
+ check_repository_format_gently(nongit_ok);
297
return NULL;
298
}
299
chdir("..");
@@ -298,7 +314,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
314
if (!work_tree_env)
315
inside_work_tree = 1;
316
git_work_tree_cfg = xstrndup(cwd, offset);
301
- check_repository_format();
317
+ if (check_repository_format_gently(nongit_ok))
318
+ return NULL;
319
if (offset == len)
320
return NULL;
321
@@ -349,16 +366,13 @@ int check_repository_format_version(const char *var, const char *value)
366
367
int check_repository_format(void)
368
{
352
- git_config(check_repository_format_version);
353
- if (GIT_REPO_VERSION < repository_format_version)
354
- die ("Expected git repo version <= %d, found %d",
355
- GIT_REPO_VERSION, repository_format_version);
356
- return 0;
369
+ return check_repository_format_gently(NULL);
370
}
371
372
const char *setup_git_directory(void)
373
{
374
const char *retval = setup_git_directory_gently(NULL);
375
+ check_repository_format();
376
377
/* If the work tree is not the default one, recompute prefix */
378
if (inside_work_tree < 0) {
t/t1302-repo-version.sh
new
+46
@@ -0,0 +1,46 @@
1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2007 Nguyễn Thái Ngọc Duy
4
+#
5
+
6
+test_description='Test repository version check'
7
+
8
+. ./test-lib.sh
9
+
10
+cat >test.patch <<EOF
11
+diff --git a/test.txt b/test.txt
12
+new file mode 100644
13
+--- /dev/null
14
++++ b/test.txt
15
+@@ -0,0 +1 @@
16
++123
17
+EOF
18
+
19
+test_create_repo "test"
20
+test_create_repo "test2"
21
+
22
+GIT_CONFIG=test2/.git/config git config core.repositoryformatversion 99 || exit 1
23
+
24
+test_expect_success 'gitdir selection on normal repos' '
25
+ (test "$(git config core.repositoryformatversion)" = 0 &&
26
+ cd test &&
27
+ test "$(git config core.repositoryformatversion)" = 0)'
28
+
29
+# Make sure it would stop at test2, not trash
30
+test_expect_success 'gitdir selection on unsupported repo' '
31
+ (cd test2 &&
32
+ test "$(git config core.repositoryformatversion)" = 99)'
33
+
34
+test_expect_success 'gitdir not required mode' '
35
+ (git apply --stat test.patch &&
36
+ cd test && git apply --stat ../test.patch &&
37
+ cd ../test2 && git apply --stat ../test.patch)'
38
+
39
+test_expect_success 'gitdir required mode on normal repos' '
40
+ (git apply --check --index test.patch &&
41
+ cd test && git apply --check --index ../test.patch)'
42
+
43
+test_expect_failure 'gitdir required mode on unsupported repo' '
44
+ (cd test2 && git apply --check --index ../test.patch)'
45
+
46
+test_done