init: use setup.c's repo version verification
We check our templates to make sure they are from a version of git we understand (otherwise we would init a repository we cannot ourselves run in!). But our simple integer check has fallen behind the times. Let's use the helpers that setup.c provides to do it right. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Mar 11, 2016 at 17:37 UTC
94ce167249781d2c80ba28412d853c426d41a55a
1 file changed
+12
-9
builtin/init-db.c
+12
-9
@@ -95,6 +95,8 @@ static void copy_templates(const char *template_dir)
95
struct strbuf path = STRBUF_INIT;
96
struct strbuf template_path = STRBUF_INIT;
97
size_t template_len;
98
+ struct repository_format template_format;
99
+ struct strbuf err = STRBUF_INIT;
100
DIR *dir;
101
char *to_free = NULL;
102
@@ -121,17 +123,18 @@ static void copy_templates(const char *template_dir)
123
124
/* Make sure that template is from the correct vintage */
125
strbuf_addstr(&template_path, "config");
124
- repository_format_version = 0;
125
- git_config_from_file(check_repository_format_version,
126
- template_path.buf, NULL);
126
+ read_repository_format(&template_format, template_path.buf);
127
strbuf_setlen(&template_path, template_len);
128
129
- if (repository_format_version &&
130
- repository_format_version != GIT_REPO_VERSION) {
131
- warning(_("not copying templates of "
132
- "a wrong format version %d from '%s'"),
133
- repository_format_version,
134
- template_dir);
129
+ /*
130
+ * No mention of version at all is OK, but anything else should be
131
+ * verified.
132
+ */
133
+ if (template_format.version >= 0 &&
134
+ verify_repository_format(&template_format, &err) < 0) {
135
+ warning(_("not copying templates from '%s': %s"),
136
+ template_dir, err.buf);
137
+ strbuf_release(&err);
138
goto close_free_return;
139
}
140