@@ -378,7 +378,7 @@ static int shared_callback(const struct option *opt, const char *arg, int unset)
378378}
379379
380380static const char * const init_db_usage [] = {
381- "git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]" ,
381+ "git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [directory] " ,
382382 NULL
383383};
384384
@@ -406,12 +406,47 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
406406 OPT_END ()
407407 };
408408
409- parse_options (argc , argv , prefix , init_db_options , init_db_usage , 0 );
410-
411- if (is_bare_repository_cfg == 1 ) {
409+ argc = parse_options (argc , argv , prefix , init_db_options , init_db_usage , 0 );
410+
411+ if (argc == 1 ) {
412+ int mkdir_tried = 0 ;
413+ retry :
414+ if (chdir (argv [0 ]) < 0 ) {
415+ if (!mkdir_tried ) {
416+ int saved ;
417+ /*
418+ * At this point we haven't read any configuration,
419+ * and we know shared_repository should always be 0;
420+ * but just in case we play safe.
421+ */
422+ saved = shared_repository ;
423+ shared_repository = 0 ;
424+ switch (safe_create_leading_directories_const (argv [0 ])) {
425+ case -3 :
426+ errno = EEXIST ;
427+ /* fallthru */
428+ case -1 :
429+ die_errno ("cannot mkdir %s" , argv [0 ]);
430+ break ;
431+ default :
432+ break ;
433+ }
434+ shared_repository = saved ;
435+ if (mkdir (argv [0 ], 0777 ) < 0 )
436+ die_errno ("cannot mkdir %s" , argv [0 ]);
437+ mkdir_tried = 1 ;
438+ goto retry ;
439+ }
440+ die_errno ("cannot chdir to %s" , argv [0 ]);
441+ }
442+ } else if (0 < argc ) {
443+ usage (init_db_usage [0 ]);
444+ }
445+ if (is_bare_repository_cfg == 1 ) {
412446 static char git_dir [PATH_MAX + 1 ];
413- setenv (GIT_DIR_ENVIRONMENT , getcwd (git_dir ,
414- sizeof (git_dir )), 0 );
447+
448+ setenv (GIT_DIR_ENVIRONMENT ,
449+ getcwd (git_dir , sizeof (git_dir )), 0 );
415450 }
416451
417452 if (init_shared_repository != -1 )
0 commit comments