Merge branch 'ns/init-mkdir'
authorJunio C Hamano <gitster@pobox.com>
Wed, 5 Aug 2009 19:39:33 +0000 (12:39 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 5 Aug 2009 19:39:33 +0000 (12:39 -0700)
* ns/init-mkdir:
  git init: optionally allow a directory argument

Conflicts:
builtin-init-db.c

1  2 
builtin-init-db.c

index d68f61b9c8f6c7f0b2f76457ff8f257f31aaf30c,b7f708de1f164de6fbb7b6ad9a3a95d34a426fff..dd84caecbc2a07bca90c8524157d50a8fd5ae316
@@@ -371,16 -370,8 +371,16 @@@ static int guess_repository_type(const 
        return 1;
  }
  
 -static const char init_db_usage[] =
 -"git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [directory]";
 +static int shared_callback(const struct option *opt, const char *arg, int unset)
 +{
 +      *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
 +      return 0;
 +}
 +
 +static const char *const init_db_usage[] = {
-       "git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]",
++      "git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [directory]",
 +      NULL
 +};
  
  /*
   * If you want to, you can share the DB area with any number of branches.
@@@ -393,27 -384,67 +393,62 @@@ int cmd_init_db(int argc, const char **
        const char *git_dir;
        const char *template_dir = NULL;
        unsigned int flags = 0;
 -      int bare_given = 0;
 -      int i;
 -
 -      for (i = 1; i < argc; i++, argv++) {
 -              const char *arg = argv[1];
 -              if (!prefixcmp(arg, "--template="))
 -                      template_dir = arg+11;
 -              else if (!strcmp(arg, "--bare"))
 -                      bare_given = is_bare_repository_cfg = 1;
 -              else if (!strcmp(arg, "--shared"))
 -                      init_shared_repository = PERM_GROUP;
 -              else if (!prefixcmp(arg, "--shared="))
 -                      init_shared_repository = git_config_perm("arg", arg+9);
 -              else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
 -                      flags |= INIT_DB_QUIET;
 -              else if (arg[0] == '-')
 -                      usage(init_db_usage);
 -              else
 -                      break;
 -      }
 -
 -      if (i == argc - 1) {
 +      const struct option init_db_options[] = {
 +              OPT_STRING(0, "template", &template_dir, "template-directory",
 +                              "provide the directory from which templates will be used"),
 +              OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
 +                              "create a bare repository", 1),
 +              { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
 +                      "permissions",
 +                      "specify that the git repository is to be shared amongst several users",
 +                      PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
 +              OPT_BIT('q', "quiet", &flags, "be quiet", INIT_DB_QUIET),
 +              OPT_END()
 +      };
 +
-       parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
-       if(is_bare_repository_cfg == 1) {
++      argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
++
++      if (argc == 1) {
+               int mkdir_tried = 0;
+       retry:
 -              if (chdir(argv[1]) < 0) {
++              if (chdir(argv[0]) < 0) {
+                       if (!mkdir_tried) {
+                               int saved;
+                               /*
+                                * At this point we haven't read any configuration,
+                                * and we know shared_repository should always be 0;
+                                * but just in case we play safe.
+                                */
+                               saved = shared_repository;
+                               shared_repository = 0;
 -                              switch (safe_create_leading_directories_const(argv[1])) {
++                              switch (safe_create_leading_directories_const(argv[0])) {
+                               case -3:
+                                       errno = EEXIST;
+                                       /* fallthru */
+                               case -1:
 -                                      die_errno("cannot mkdir %s", argv[1]);
++                                      die_errno("cannot mkdir %s", argv[0]);
+                                       break;
+                               default:
+                                       break;
+                               }
+                               shared_repository = saved;
 -                              if (mkdir(argv[1], 0777) < 0)
 -                                      die_errno("cannot mkdir %s", argv[1]);
++                              if (mkdir(argv[0], 0777) < 0)
++                                      die_errno("cannot mkdir %s", argv[0]);
+                               mkdir_tried = 1;
+                               goto retry;
+                       }
 -                      die_errno("cannot chdir to %s", argv[1]);
++                      die_errno("cannot chdir to %s", argv[0]);
+               }
 -      } else if (i < argc - 1) {
 -              usage(init_db_usage);
++      } else if (0 < argc) {
++              usage(init_db_usage[0]);
+       }
 -      if (bare_given == 1) {
++      if (is_bare_repository_cfg == 1) {
                static char git_dir[PATH_MAX+1];
-               setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir,
-                                       sizeof(git_dir)), 0);
+               setenv(GIT_DIR_ENVIRONMENT,
+                       getcwd(git_dir, sizeof(git_dir)), 0);
        }
 +
        if (init_shared_repository != -1)
                shared_repository = init_shared_repository;