Skip to content

Commit 0397ff2

Browse files
committed
Merge branch 'ns/init-mkdir'
* ns/init-mkdir: git init: optionally allow a directory argument Conflicts: builtin-init-db.c
2 parents 4d4097d + 53d4888 commit 0397ff2

File tree

3 files changed

+122
-7
lines changed

3 files changed

+122
-7
lines changed

Documentation/git-init.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-init - Create an empty git repository or reinitialize an existing one
88

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

1313

1414
OPTIONS
@@ -74,6 +74,9 @@ By default, the configuration flag receive.denyNonFastForwards is enabled
7474
in shared repositories, so that you cannot force a non fast-forwarding push
7575
into it.
7676

77+
If you name a (possibly non-existent) directory at the end of the command
78+
line, the command is run inside the directory (possibly after creating it).
79+
7780
--
7881

7982

builtin-init-db.c

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ static int shared_callback(const struct option *opt, const char *arg, int unset)
378378
}
379379

380380
static 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)

t/t0001-init.sh

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,81 @@ test_expect_success 'init rejects insanely long --template' '
208208
)
209209
'
210210

211+
test_expect_success 'init creates a new directory' '
212+
rm -fr newdir &&
213+
(
214+
git init newdir &&
215+
test -d newdir/.git/refs
216+
)
217+
'
218+
219+
test_expect_success 'init creates a new bare directory' '
220+
rm -fr newdir &&
221+
(
222+
git init --bare newdir &&
223+
test -d newdir/refs
224+
)
225+
'
226+
227+
test_expect_success 'init recreates a directory' '
228+
rm -fr newdir &&
229+
(
230+
mkdir newdir &&
231+
git init newdir &&
232+
test -d newdir/.git/refs
233+
)
234+
'
235+
236+
test_expect_success 'init recreates a new bare directory' '
237+
rm -fr newdir &&
238+
(
239+
mkdir newdir &&
240+
git init --bare newdir &&
241+
test -d newdir/refs
242+
)
243+
'
244+
245+
test_expect_success 'init creates a new deep directory' '
246+
rm -fr newdir &&
247+
(
248+
# Leading directories should honor umask while
249+
# the repository itself should follow "shared"
250+
umask 002 &&
251+
git init --bare --shared=0660 newdir/a/b/c &&
252+
test -d newdir/a/b/c/refs &&
253+
ls -ld newdir/a newdir/a/b > lsab.out &&
254+
! grep -v "^drwxrw[sx]r-x" ls.out &&
255+
ls -ld newdir/a/b/c > lsc.out &&
256+
! grep -v "^drwxrw[sx]---" lsc.out
257+
)
258+
'
259+
260+
test_expect_success 'init notices EEXIST (1)' '
261+
rm -fr newdir &&
262+
(
263+
>newdir &&
264+
test_must_fail git init newdir &&
265+
test -f newdir
266+
)
267+
'
268+
269+
test_expect_success 'init notices EEXIST (2)' '
270+
rm -fr newdir &&
271+
(
272+
mkdir newdir &&
273+
>newdir/a
274+
test_must_fail git init newdir/a/b &&
275+
test -f newdir/a
276+
)
277+
'
278+
279+
test_expect_success POSIXPERM 'init notices EPERM' '
280+
rm -fr newdir &&
281+
(
282+
mkdir newdir &&
283+
chmod -w newdir &&
284+
test_must_fail git init newdir/a/b
285+
)
286+
'
287+
211288
test_done

0 commit comments

Comments
 (0)