Provide API access to init_db()
The caller first calls set_git_dir() to specify the GIT_DIR, and then calls init_db() to initialize it. This also cleans up various parts of the code to account for the fact that everything is done with GIT_DIR set, so it's unnecessary to pass the specified directory around. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Daniel Barkalow committed
Apr 27, 2008 at 13:39 UTC
f225aeb278eb1c8c028cd2ff1fdfe7a45067b8e1
2 files changed
+108
-95
builtin-init-db.c
+104
-95
@@ -104,12 +104,14 @@ static void copy_templates_1(char *path, int baselen,
104
}
105
}
106
107
-static void copy_templates(const char *git_dir, int len, const char *template_dir)
107
+static void copy_templates(const char *template_dir)
108
{
109
char path[PATH_MAX];
110
char template_path[PATH_MAX];
111
int template_len;
112
DIR *dir;
113
+ const char *git_dir = get_git_dir();
114
+ int len = strlen(git_dir);
115
116
if (!template_dir)
117
template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
@@ -156,6 +158,8 @@ static void copy_templates(const char *git_dir, int len, const char *template_di
158
}
159
160
memcpy(path, git_dir, len);
161
+ if (len && path[len - 1] != '/')
162
+ path[len++] = '/';
163
path[len] = 0;
164
copy_templates_1(path, len,
165
template_path, template_len,
@@ -163,8 +167,9 @@ static void copy_templates(const char *git_dir, int len, const char *template_di
167
closedir(dir);
168
}
169
166
-static int create_default_files(const char *git_dir, const char *template_path)
170
+static int create_default_files(const char *template_path)
171
{
172
+ const char *git_dir = get_git_dir();
173
unsigned len = strlen(git_dir);
174
static char path[PATH_MAX];
175
struct stat st1;
@@ -183,19 +188,15 @@ static int create_default_files(const char *git_dir, const char *template_path)
188
/*
189
* Create .git/refs/{heads,tags}
190
*/
186
- strcpy(path + len, "refs");
187
- safe_create_dir(path, 1);
188
- strcpy(path + len, "refs/heads");
189
- safe_create_dir(path, 1);
190
- strcpy(path + len, "refs/tags");
191
- safe_create_dir(path, 1);
191
+ safe_create_dir(git_path("refs"), 1);
192
+ safe_create_dir(git_path("refs/heads"), 1);
193
+ safe_create_dir(git_path("refs/tags"), 1);
194
195
/* First copy the templates -- we might have the default
196
* config file there, in which case we would want to read
197
* from it after installing.
198
*/
197
- path[len] = 0;
198
- copy_templates(path, len, template_path);
199
+ copy_templates(template_path);
200
201
git_config(git_default_config);
202
@@ -204,14 +205,10 @@ static int create_default_files(const char *git_dir, const char *template_path)
205
* shared-repository settings, we would need to fix them up.
206
*/
207
if (shared_repository) {
207
- path[len] = 0;
208
- adjust_shared_perm(path);
209
- strcpy(path + len, "refs");
210
- adjust_shared_perm(path);
211
- strcpy(path + len, "refs/heads");
212
- adjust_shared_perm(path);
213
- strcpy(path + len, "refs/tags");
214
- adjust_shared_perm(path);
208
+ adjust_shared_perm(get_git_dir());
209
+ adjust_shared_perm(git_path("refs"));
210
+ adjust_shared_perm(git_path("refs/heads"));
211
+ adjust_shared_perm(git_path("refs/tags"));
212
}
213
214
/*
@@ -251,8 +248,10 @@ static int create_default_files(const char *git_dir, const char *template_path)
248
/* allow template config file to override the default */
249
if (log_all_ref_updates == -1)
250
git_config_set("core.logallrefupdates", "true");
254
- if (work_tree != git_work_tree_cfg)
251
+ if (prefixcmp(git_dir, work_tree) ||
252
+ strcmp(git_dir + strlen(work_tree), "/.git")) {
253
git_config_set("core.worktree", work_tree);
254
+ }
255
}
256
257
/* Check if symlink is supported in the work tree */
@@ -272,42 +271,90 @@ static int create_default_files(const char *git_dir, const char *template_path)
271
return reinit;
272
}
273
275
-static void guess_repository_type(const char *git_dir)
274
+int init_db(const char *template_dir, unsigned int flags)
275
+{
276
+ const char *sha1_dir;
277
+ char *path;
278
+ int len, reinit;
279
+
280
+ safe_create_dir(get_git_dir(), 0);
281
+
282
+ /* Check to see if the repository version is right.
283
+ * Note that a newly created repository does not have
284
+ * config file, so this will not fail. What we are catching
285
+ * is an attempt to reinitialize new repository with an old tool.
286
+ */
287
+ check_repository_format();
288
+
289
+ reinit = create_default_files(template_dir);
290
+
291
+ sha1_dir = get_object_directory();
292
+ len = strlen(sha1_dir);
293
+ path = xmalloc(len + 40);
294
+ memcpy(path, sha1_dir, len);
295
+
296
+ safe_create_dir(sha1_dir, 1);
297
+ strcpy(path+len, "/pack");
298
+ safe_create_dir(path, 1);
299
+ strcpy(path+len, "/info");
300
+ safe_create_dir(path, 1);
301
+
302
+ if (shared_repository) {
303
+ char buf[10];
304
+ /* We do not spell "group" and such, so that
305
+ * the configuration can be read by older version
306
+ * of git. Note, we use octal numbers for new share modes,
307
+ * and compatibility values for PERM_GROUP and
308
+ * PERM_EVERYBODY.
309
+ */
310
+ if (shared_repository == PERM_GROUP)
311
+ sprintf(buf, "%d", OLD_PERM_GROUP);
312
+ else if (shared_repository == PERM_EVERYBODY)
313
+ sprintf(buf, "%d", OLD_PERM_EVERYBODY);
314
+ else
315
+ sprintf(buf, "0%o", shared_repository);
316
+ git_config_set("core.sharedrepository", buf);
317
+ git_config_set("receive.denyNonFastforwards", "true");
318
+ }
319
+
320
+ if (!(flags & INIT_DB_QUIET))
321
+ printf("%s%s Git repository in %s/\n",
322
+ reinit ? "Reinitialized existing" : "Initialized empty",
323
+ shared_repository ? " shared" : "",
324
+ get_git_dir());
325
+
326
+ return 0;
327
+}
328
+
329
+static int guess_repository_type(const char *git_dir)
330
{
331
char cwd[PATH_MAX];
332
const char *slash;
333
280
- if (0 <= is_bare_repository_cfg)
281
- return;
282
- if (!git_dir)
283
- return;
284
-
334
/*
335
* "GIT_DIR=. git init" is always bare.
336
* "GIT_DIR=`pwd` git init" too.
337
*/
338
if (!strcmp(".", git_dir))
290
- goto force_bare;
339
+ return 1;
340
if (!getcwd(cwd, sizeof(cwd)))
341
die("cannot tell cwd");
342
if (!strcmp(git_dir, cwd))
294
- goto force_bare;
343
+ return 1;
344
/*
345
* "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
346
*/
347
if (!strcmp(git_dir, ".git"))
299
- return;
348
+ return 0;
349
slash = strrchr(git_dir, '/');
350
if (slash && !strcmp(slash, "/.git"))
302
- return;
351
+ return 0;
352
353
/*
354
* Otherwise it is often bare. At this point
355
* we are just guessing.
356
*/
308
- force_bare:
309
- is_bare_repository_cfg = 1;
310
- return;
357
+ return 1;
358
}
359
360
static const char init_db_usage[] =
@@ -322,11 +369,9 @@ static const char init_db_usage[] =
369
int cmd_init_db(int argc, const char **argv, const char *prefix)
370
{
371
const char *git_dir;
325
- const char *sha1_dir;
372
const char *template_dir = NULL;
327
- char *path;
328
- int len, i, reinit;
329
- int quiet = 0;
373
+ unsigned int flags = 0;
374
+ int i;
375
376
for (i = 1; i < argc; i++, argv++) {
377
const char *arg = argv[1];
@@ -337,7 +382,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
382
else if (!prefixcmp(arg, "--shared="))
383
shared_repository = git_config_perm("arg", arg+9);
384
else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
340
- quiet = 1;
385
+ flags |= INIT_DB_QUIET;
386
else
387
usage(init_db_usage);
388
}
@@ -354,71 +399,35 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
399
GIT_WORK_TREE_ENVIRONMENT,
400
GIT_DIR_ENVIRONMENT);
401
357
- guess_repository_type(git_dir);
358
-
359
- if (is_bare_repository_cfg <= 0) {
360
- git_work_tree_cfg = xcalloc(PATH_MAX, 1);
361
- if (!getcwd(git_work_tree_cfg, PATH_MAX))
362
- die ("Cannot access current working directory.");
363
- if (access(get_git_work_tree(), X_OK))
364
- die ("Cannot access work tree '%s'",
365
- get_git_work_tree());
366
- }
367
-
402
/*
403
* Set up the default .git directory contents
404
*/
371
- git_dir = getenv(GIT_DIR_ENVIRONMENT);
405
if (!git_dir)
406
git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
374
- safe_create_dir(git_dir, 0);
375
-
376
- /* Check to see if the repository version is right.
377
- * Note that a newly created repository does not have
378
- * config file, so this will not fail. What we are catching
379
- * is an attempt to reinitialize new repository with an old tool.
380
- */
381
- check_repository_format();
382
-
383
- reinit = create_default_files(git_dir, template_dir);
384
-
385
- /*
386
- * And set up the object store.
387
- */
388
- sha1_dir = get_object_directory();
389
- len = strlen(sha1_dir);
390
- path = xmalloc(len + 40);
391
- memcpy(path, sha1_dir, len);
392
-
393
- safe_create_dir(sha1_dir, 1);
394
- strcpy(path+len, "/pack");
395
- safe_create_dir(path, 1);
396
- strcpy(path+len, "/info");
397
- safe_create_dir(path, 1);
407
399
- if (shared_repository) {
400
- char buf[10];
401
- /* We do not spell "group" and such, so that
402
- * the configuration can be read by older version
403
- * of git. Note, we use octal numbers for new share modes,
404
- * and compatibility values for PERM_GROUP and
405
- * PERM_EVERYBODY.
406
- */
407
- if (shared_repository == PERM_GROUP)
408
- sprintf(buf, "%d", OLD_PERM_GROUP);
409
- else if (shared_repository == PERM_EVERYBODY)
410
- sprintf(buf, "%d", OLD_PERM_EVERYBODY);
411
- else
412
- sprintf(buf, "0%o", shared_repository);
413
- git_config_set("core.sharedrepository", buf);
414
- git_config_set("receive.denyNonFastforwards", "true");
408
+ if (is_bare_repository_cfg < 0)
409
+ is_bare_repository_cfg = guess_repository_type(git_dir);
410
+
411
+ if (!is_bare_repository_cfg) {
412
+ if (git_dir) {
413
+ const char *git_dir_parent = strrchr(git_dir, '/');
414
+ if (git_dir_parent) {
415
+ char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
416
+ git_work_tree_cfg = xstrdup(make_absolute_path(rel));
417
+ free(rel);
418
+ }
419
+ }
420
+ if (!git_work_tree_cfg) {
421
+ git_work_tree_cfg = xcalloc(PATH_MAX, 1);
422
+ if (!getcwd(git_work_tree_cfg, PATH_MAX))
423
+ die ("Cannot access current working directory.");
424
+ }
425
+ if (access(get_git_work_tree(), X_OK))
426
+ die ("Cannot access work tree '%s'",
427
+ get_git_work_tree());
428
}
429
417
- if (!quiet)
418
- printf("%s%s Git repository in %s/\n",
419
- reinit ? "Reinitialized existing" : "Initialized empty",
420
- shared_repository ? " shared" : "",
421
- git_dir);
430
+ set_git_dir(make_absolute_path(git_dir));
431
423
- return 0;
432
+ return init_db(template_dir, flags);
433
}
cache.h
+4
@@ -324,6 +324,10 @@ extern const char *prefix_filename(const char *prefix, int len, const char *path
324
extern void verify_filename(const char *prefix, const char *name);
325
extern void verify_non_filename(const char *prefix, const char *name);
326
327
+#define INIT_DB_QUIET 0x0001
328
+
329
+extern int init_db(const char *template_dir, unsigned int flags);
330
+
331
#define alloc_nr(x) (((x)+16)*3/2)
332
333
/*