22
22
static int init_is_bare_repository = 0 ;
23
23
static int init_shared_repository = -1 ;
24
24
static const char * init_db_template_dir ;
25
- static const char * git_link ;
26
25
27
26
static void copy_templates_1 (struct strbuf * path , struct strbuf * template ,
28
27
DIR * dir )
@@ -138,7 +137,7 @@ static void copy_templates(const char *template_dir)
138
137
goto close_free_return ;
139
138
}
140
139
141
- strbuf_addstr (& path , get_git_dir ());
140
+ strbuf_addstr (& path , get_git_common_dir ());
142
141
strbuf_complete (& path , '/' );
143
142
copy_templates_1 (& path , & template_path , dir );
144
143
close_free_return :
@@ -171,7 +170,8 @@ static int needs_work_tree_config(const char *git_dir, const char *work_tree)
171
170
return 1 ;
172
171
}
173
172
174
- static int create_default_files (const char * template_path )
173
+ static int create_default_files (const char * template_path ,
174
+ const char * original_git_dir )
175
175
{
176
176
struct stat st1 ;
177
177
struct strbuf buf = STRBUF_INIT ;
@@ -264,7 +264,7 @@ static int create_default_files(const char *template_path)
264
264
/* allow template config file to override the default */
265
265
if (log_all_ref_updates == -1 )
266
266
git_config_set ("core.logallrefupdates" , "true" );
267
- if (needs_work_tree_config (get_git_dir () , work_tree ))
267
+ if (needs_work_tree_config (original_git_dir , work_tree ))
268
268
git_config_set ("core.worktree" , work_tree );
269
269
}
270
270
@@ -312,34 +312,7 @@ static void create_object_directory(void)
312
312
strbuf_release (& path );
313
313
}
314
314
315
- int set_git_dir_init (const char * git_dir , const char * real_git_dir ,
316
- int exist_ok )
317
- {
318
- if (real_git_dir ) {
319
- struct stat st ;
320
-
321
- if (!exist_ok && !stat (git_dir , & st ))
322
- die (_ ("%s already exists" ), git_dir );
323
-
324
- if (!exist_ok && !stat (real_git_dir , & st ))
325
- die (_ ("%s already exists" ), real_git_dir );
326
-
327
- /*
328
- * make sure symlinks are resolved because we'll be
329
- * moving the target repo later on in separate_git_dir()
330
- */
331
- git_link = xstrdup (real_path (git_dir ));
332
- set_git_dir (real_path (real_git_dir ));
333
- }
334
- else {
335
- set_git_dir (real_path (git_dir ));
336
- git_link = NULL ;
337
- }
338
- startup_info -> have_repository = 1 ;
339
- return 0 ;
340
- }
341
-
342
- static void separate_git_dir (const char * git_dir )
315
+ static void separate_git_dir (const char * git_dir , const char * git_link )
343
316
{
344
317
struct stat st ;
345
318
@@ -360,13 +333,31 @@ static void separate_git_dir(const char *git_dir)
360
333
write_file (git_link , "gitdir: %s" , git_dir );
361
334
}
362
335
363
- int init_db (const char * template_dir , unsigned int flags )
336
+ int init_db (const char * git_dir , const char * real_git_dir ,
337
+ const char * template_dir , unsigned int flags )
364
338
{
365
339
int reinit ;
366
- const char * git_dir = get_git_dir ();
340
+ int exist_ok = flags & INIT_DB_EXIST_OK ;
341
+ char * original_git_dir = xstrdup (real_path (git_dir ));
342
+
343
+ if (real_git_dir ) {
344
+ struct stat st ;
345
+
346
+ if (!exist_ok && !stat (git_dir , & st ))
347
+ die (_ ("%s already exists" ), git_dir );
348
+
349
+ if (!exist_ok && !stat (real_git_dir , & st ))
350
+ die (_ ("%s already exists" ), real_git_dir );
367
351
368
- if (git_link )
369
- separate_git_dir (git_dir );
352
+ set_git_dir (real_path (real_git_dir ));
353
+ git_dir = get_git_dir ();
354
+ separate_git_dir (git_dir , original_git_dir );
355
+ }
356
+ else {
357
+ set_git_dir (real_path (git_dir ));
358
+ git_dir = get_git_dir ();
359
+ }
360
+ startup_info -> have_repository = 1 ;
370
361
371
362
safe_create_dir (git_dir , 0 );
372
363
@@ -379,7 +370,7 @@ int init_db(const char *template_dir, unsigned int flags)
379
370
*/
380
371
check_repository_format ();
381
372
382
- reinit = create_default_files (template_dir );
373
+ reinit = create_default_files (template_dir , original_git_dir );
383
374
384
375
create_object_directory ();
385
376
@@ -419,6 +410,7 @@ int init_db(const char *template_dir, unsigned int flags)
419
410
git_dir , len && git_dir [len - 1 ] != '/' ? "/" : "" );
420
411
}
421
412
413
+ free (original_git_dir );
422
414
return 0 ;
423
415
}
424
416
@@ -586,7 +578,6 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
586
578
set_git_work_tree (work_tree );
587
579
}
588
580
589
- set_git_dir_init (git_dir , real_git_dir , 1 );
590
-
591
- return init_db (template_dir , flags );
581
+ flags |= INIT_DB_EXIST_OK ;
582
+ return init_db (git_dir , real_git_dir , template_dir , flags );
592
583
}
0 commit comments