Skip to content

Commit 6d9ba67

Browse files
dschoJunio C Hamano
authored andcommitted
Commands requiring a work tree must not run in GIT_DIR
This patch helps when you accidentally run something like git-clean in the git directory instead of the work tree. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 98d47d4 commit 6d9ba67

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

builtin-ls-files.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ static const char ls_files_usage[] =
323323
int cmd_ls_files(int argc, const char **argv, const char *prefix)
324324
{
325325
int i;
326-
int exc_given = 0;
326+
int exc_given = 0, require_work_tree = 0;
327327
struct dir_struct dir;
328328

329329
memset(&dir, 0, sizeof(dir));
@@ -363,14 +363,17 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
363363
}
364364
if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
365365
show_modified = 1;
366+
require_work_tree = 1;
366367
continue;
367368
}
368369
if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
369370
show_others = 1;
371+
require_work_tree = 1;
370372
continue;
371373
}
372374
if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
373375
dir.show_ignored = 1;
376+
require_work_tree = 1;
374377
continue;
375378
}
376379
if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
@@ -379,6 +382,7 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
379382
}
380383
if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
381384
show_killed = 1;
385+
require_work_tree = 1;
382386
continue;
383387
}
384388
if (!strcmp(arg, "--directory")) {
@@ -447,6 +451,10 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
447451
break;
448452
}
449453

454+
if (require_work_tree &&
455+
(is_bare_repository() || is_inside_git_dir()))
456+
die("This operation must be run in a work tree");
457+
450458
pathspec = get_pathspec(prefix, argv + i);
451459

452460
/* Verify that the pathspec matches the prefix */

builtin-rev-parse.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
347347
printf("%s/.git\n", cwd);
348348
continue;
349349
}
350+
if (!strcmp(arg, "--is-inside-git-dir")) {
351+
printf("%s\n", is_inside_git_dir() ? "true"
352+
: "false");
353+
continue;
354+
}
350355
if (!strncmp(arg, "--since=", 8)) {
351356
show_datestring("--max-age=", arg+8);
352357
continue;

git-sh-setup.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ cd_to_toplevel () {
4848
}
4949

5050
require_work_tree () {
51-
test $(is_bare_repository) = false ||
51+
test $(is_bare_repository) = false &&
52+
test $(git-rev-parse --is-inside-git-dir) = false ||
5253
die "fatal: $0 cannot be used without a working tree."
5354
}
5455

git.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,9 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
299299
prefix = setup_git_directory();
300300
if (p->option & USE_PAGER)
301301
setup_pager();
302-
if ((p->option & NOT_BARE) && is_bare_repository())
303-
die("%s cannot be used in a bare git directory", cmd);
302+
if ((p->option & NOT_BARE) &&
303+
(is_bare_repository() || is_inside_git_dir()))
304+
die("%s must be run in a work tree", cmd);
304305
trace_argv_printf(argv, argc, "trace: built-in: git");
305306

306307
exit(p->fn(argc, argv, prefix));

0 commit comments

Comments
 (0)