@matheusbsbs / matheus-bagliano / commits / 6d9ba67b0f
Fork 0

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>

Johannes Schindelin committed Jan 23, 2007 at 13:30 UTC 6d9ba67b0fc2f18c1d7a65514edbdcdf86429c1c
4 files changed +19 -4
builtin-ls-files.c
+9 -1
@@ -323,7 +323,7 @@ static const char ls_files_usage[] =
323 int cmd_ls_files(int argc, const char **argv, const char *prefix)
324 {
325 int i;
326 - int exc_given = 0;
326 + int exc_given = 0, require_work_tree = 0;
327 struct dir_struct dir;
328
329 memset(&dir, 0, sizeof(dir));
@@ -363,14 +363,17 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
363 }
364 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
365 show_modified = 1;
366 + require_work_tree = 1;
367 continue;
368 }
369 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
370 show_others = 1;
371 + require_work_tree = 1;
372 continue;
373 }
374 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
375 dir.show_ignored = 1;
376 + require_work_tree = 1;
377 continue;
378 }
379 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
@@ -379,6 +382,7 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
382 }
383 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
384 show_killed = 1;
385 + require_work_tree = 1;
386 continue;
387 }
388 if (!strcmp(arg, "--directory")) {
@@ -447,6 +451,10 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
451 break;
452 }
453
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 +
458 pathspec = get_pathspec(prefix, argv + i);
459
460 /* Verify that the pathspec matches the prefix */
builtin-rev-parse.c
+5
@@ -347,6 +347,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
347 printf("%s/.git\n", cwd);
348 continue;
349 }
350 + if (!strcmp(arg, "--is-inside-git-dir")) {
351 + printf("%s\n", is_inside_git_dir() ? "true"
352 + : "false");
353 + continue;
354 + }
355 if (!strncmp(arg, "--since=", 8)) {
356 show_datestring("--max-age=", arg+8);
357 continue;
git-sh-setup.sh
+2 -1
@@ -48,7 +48,8 @@ cd_to_toplevel () {
48 }
49
50 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 ||
53 die "fatal: $0 cannot be used without a working tree."
54 }
55
git.c
+3 -2
@@ -299,8 +299,9 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
299 prefix = setup_git_directory();
300 if (p->option & USE_PAGER)
301 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);
305 trace_argv_printf(argv, argc, "trace: built-in: git");
306
307 exit(p->fn(argc, argv, prefix));