Skip to content

Commit

Permalink
Commands requiring a work tree must not run in GIT_DIR
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Johannes Schindelin authored and Junio C Hamano committed Feb 5, 2007
1 parent 98d47d4 commit 6d9ba67
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
10 changes: 9 additions & 1 deletion builtin-ls-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ static const char ls_files_usage[] =
int cmd_ls_files(int argc, const char **argv, const char *prefix)
{
int i;
int exc_given = 0;
int exc_given = 0, require_work_tree = 0;
struct dir_struct dir;

memset(&dir, 0, sizeof(dir));
Expand Down Expand Up @@ -363,14 +363,17 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
}
if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
show_modified = 1;
require_work_tree = 1;
continue;
}
if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
show_others = 1;
require_work_tree = 1;
continue;
}
if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
dir.show_ignored = 1;
require_work_tree = 1;
continue;
}
if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
Expand All @@ -379,6 +382,7 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
}
if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
show_killed = 1;
require_work_tree = 1;
continue;
}
if (!strcmp(arg, "--directory")) {
Expand Down Expand Up @@ -447,6 +451,10 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
break;
}

if (require_work_tree &&
(is_bare_repository() || is_inside_git_dir()))
die("This operation must be run in a work tree");

pathspec = get_pathspec(prefix, argv + i);

/* Verify that the pathspec matches the prefix */
Expand Down
5 changes: 5 additions & 0 deletions builtin-rev-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
printf("%s/.git\n", cwd);
continue;
}
if (!strcmp(arg, "--is-inside-git-dir")) {
printf("%s\n", is_inside_git_dir() ? "true"
: "false");
continue;
}
if (!strncmp(arg, "--since=", 8)) {
show_datestring("--max-age=", arg+8);
continue;
Expand Down
3 changes: 2 additions & 1 deletion git-sh-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ cd_to_toplevel () {
}

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

Expand Down
5 changes: 3 additions & 2 deletions git.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,9 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
prefix = setup_git_directory();
if (p->option & USE_PAGER)
setup_pager();
if ((p->option & NOT_BARE) && is_bare_repository())
die("%s cannot be used in a bare git directory", cmd);
if ((p->option & NOT_BARE) &&
(is_bare_repository() || is_inside_git_dir()))
die("%s must be run in a work tree", cmd);
trace_argv_printf(argv, argc, "trace: built-in: git");

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

0 comments on commit 6d9ba67

Please sign in to comment.