6
6
#include "cache.h"
7
7
#include "builtin.h"
8
8
#include "exec_cmd.h"
9
+ #include "parse-options.h"
9
10
10
11
#ifndef DEFAULT_GIT_TEMPLATE_DIR
11
12
#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
@@ -370,8 +371,16 @@ static int guess_repository_type(const char *git_dir)
370
371
return 1 ;
371
372
}
372
373
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
+ };
375
384
376
385
/*
377
386
* 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)
384
393
const char * git_dir ;
385
394
const char * template_dir = NULL ;
386
395
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 );
406
415
}
407
416
408
417
if (init_shared_repository != -1 )
0 commit comments