Skip to content

Commit 4d4097d

Browse files
committed
Merge branch 'mk/init-db-parse-options'
* mk/init-db-parse-options: init-db: migrate to parse-options
2 parents d0410af + 596f91e commit 4d4097d

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

Documentation/git-init-db.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-init-db - Creates an empty git repository
88

99
SYNOPSIS
1010
--------
11-
'git init-db' [-q | --quiet] [--template=<template_directory>] [--shared[=<permissions>]]
11+
'git init-db' [-q | --quiet] [--bare] [--template=<template_directory>] [--shared[=<permissions>]]
1212

1313

1414
DESCRIPTION

builtin-init-db.c

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "cache.h"
77
#include "builtin.h"
88
#include "exec_cmd.h"
9+
#include "parse-options.h"
910

1011
#ifndef DEFAULT_GIT_TEMPLATE_DIR
1112
#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
@@ -370,8 +371,16 @@ static int guess_repository_type(const char *git_dir)
370371
return 1;
371372
}
372373

373-
static const char init_db_usage[] =
374-
"git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]";
374+
static int shared_callback(const struct option *opt, const char *arg, int unset)
375+
{
376+
*((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
377+
return 0;
378+
}
379+
380+
static const char *const init_db_usage[] = {
381+
"git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]",
382+
NULL
383+
};
375384

376385
/*
377386
* If you want to, you can share the DB area with any number of branches.
@@ -384,25 +393,25 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
384393
const char *git_dir;
385394
const char *template_dir = NULL;
386395
unsigned int flags = 0;
387-
int i;
388-
389-
for (i = 1; i < argc; i++, argv++) {
390-
const char *arg = argv[1];
391-
if (!prefixcmp(arg, "--template="))
392-
template_dir = arg+11;
393-
else if (!strcmp(arg, "--bare")) {
394-
static char git_dir[PATH_MAX+1];
395-
is_bare_repository_cfg = 1;
396-
setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir,
397-
sizeof(git_dir)), 0);
398-
} else if (!strcmp(arg, "--shared"))
399-
init_shared_repository = PERM_GROUP;
400-
else if (!prefixcmp(arg, "--shared="))
401-
init_shared_repository = git_config_perm("arg", arg+9);
402-
else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
403-
flags |= INIT_DB_QUIET;
404-
else
405-
usage(init_db_usage);
396+
const struct option init_db_options[] = {
397+
OPT_STRING(0, "template", &template_dir, "template-directory",
398+
"provide the directory from which templates will be used"),
399+
OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
400+
"create a bare repository", 1),
401+
{ OPTION_CALLBACK, 0, "shared", &init_shared_repository,
402+
"permissions",
403+
"specify that the git repository is to be shared amongst several users",
404+
PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
405+
OPT_BIT('q', "quiet", &flags, "be quiet", INIT_DB_QUIET),
406+
OPT_END()
407+
};
408+
409+
parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
410+
411+
if(is_bare_repository_cfg == 1) {
412+
static char git_dir[PATH_MAX+1];
413+
setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir,
414+
sizeof(git_dir)), 0);
406415
}
407416

408417
if (init_shared_repository != -1)

0 commit comments

Comments
 (0)