git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Nguyen Thai Ngoc Duy <pclouds@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: Jonathan Nieder <jrnieder@gmail.com>, git@vger.kernel.org
Subject: Re: [PATCH 39/47] setup: limit get_git_work_tree()'s to explicit setup case only
Date: Wed, 19 Jan 2011 08:58:46 +0700	[thread overview]
Message-ID: <AANLkTi=bm6KO24XTMA+LM+xnszgFHJiV0inXZ-RJwwd-@mail.gmail.com> (raw)
In-Reply-To: <7v1v4aknij.fsf@alter.siamese.dyndns.org>

2011/1/19 Junio C Hamano <gitster@pobox.com>:
> This is for people who do "cd .git && GIT_WORK_TREE=.. git cmd". I have to
> wonder what happens to the pathspec given to the cmd---you are clearly
> outside of your working tree.

pathspecs are relative to worktree's root because prefix is NULL in
this case. Not very intuitive. And pathspecs that are supposed to take
files relative to user's cwd simply fail.

> Do we make sure that whatever GIT_WORK_TREE we end up with using is an
> ancestor directory of the $CWD when we require us to be inside the working
> tree?

We don't for most of commands (i.e. blindly chdir(worktree) in
setup_work_tree()). Only two commands (ls-files and rev-parse) seem to
do that.

> I think we should, as I don't think of a sane use case otherwise
> (unless you call "cd ../neigh; GIT_WORK_TREE=../work git diff ../work/foo"
> a sane way to futz with the file "foo" in the working tree "work" from a
> directory "neigh" that is unrelated to the repository).

But we don't strictly need that for whole tree operations. If
pathspecs are not given, wherever worktree is compared to cwd does not
matter much. The tricky thing is, when setup code does not know in
advance whether pathspecs are given. Perhaps something (untested) like
this for a start? Some commands like commit can also have
WHOLE_WORK_TREE, but it needs to check for is_inside_work_tree() by
itself it pathspec is given.

diff --git a/git.c b/git.c
index 68334f6..28abbdd 100644
--- a/git.c
+++ b/git.c
@@ -248,6 +248,10 @@ const char git_version_string[] = GIT_VERSION;
  * RUN_SETUP for reading from the configuration file.
  */
 #define NEED_WORK_TREE		(1<<3)
+/*
+ * This command only works on work tree as a whole
+ */
+#define WHOLE_WORK_TREE         (1<<4)

 struct cmd_struct {
 	const char *cmd;
@@ -283,7 +287,7 @@ static int run_builtin(struct cmd_struct *p, int
argc, const char **argv)
 	commit_pager_choice();

 	if (!help && p->option & NEED_WORK_TREE)
-		setup_work_tree();
+		setup_work_tree(p->option & WHOLE_WORK_TREE);

 	trace_argv_printf(argv, "trace: built-in: git");

@@ -328,7 +332,7 @@ static void handle_internal_command(int argc,
const char **argv)
 		{ "check-ref-format", cmd_check_ref_format },
 		{ "check-attr", cmd_check_attr, RUN_SETUP },
 		{ "cherry", cmd_cherry, RUN_SETUP },
-		{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
+		{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE |
WHOLE_WORK_TREE },
 		{ "clone", cmd_clone },
 		{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
 		{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
@@ -362,7 +366,7 @@ static void handle_internal_command(int argc,
const char **argv)
 		{ "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
 		{ "mailinfo", cmd_mailinfo },
 		{ "mailsplit", cmd_mailsplit },
-		{ "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
+		{ "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE | WHOLE_WORK_TREE },
 		{ "merge-base", cmd_merge_base, RUN_SETUP },
 		{ "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
 		{ "merge-index", cmd_merge_index, RUN_SETUP },
@@ -397,13 +401,13 @@ static void handle_internal_command(int argc,
const char **argv)
 		{ "reset", cmd_reset, RUN_SETUP },
 		{ "rev-list", cmd_rev_list, RUN_SETUP },
 		{ "rev-parse", cmd_rev_parse },
-		{ "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
+		{ "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE | WHOLE_WORK_TREE },
 		{ "rm", cmd_rm, RUN_SETUP },
 		{ "send-pack", cmd_send_pack, RUN_SETUP },
 		{ "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
 		{ "show-branch", cmd_show_branch, RUN_SETUP },
 		{ "show", cmd_show, RUN_SETUP },
-		{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
+		{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE | WHOLE_WORK_TREE },
 		{ "stripspace", cmd_stripspace },
 		{ "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
 		{ "tag", cmd_tag, RUN_SETUP },
diff --git a/setup.c b/setup.c
index 3d73269..c14e55c 100644
--- a/setup.c
+++ b/setup.c
@@ -208,7 +208,7 @@ int is_inside_work_tree(void)
 	return inside_work_tree;
 }

-void setup_work_tree(void)
+void setup_work_tree(int gently)
 {
 	const char *work_tree, *git_dir;
 	static int initialized = 0;
@@ -219,7 +219,11 @@ void setup_work_tree(void)
 	git_dir = get_git_dir();
 	if (!is_absolute_path(git_dir))
 		git_dir = make_absolute_path(git_dir);
-	if (!work_tree || chdir(work_tree))
+	if (work_tree && (gently || is_inside_work_tree()))
+		; 		/* Good */
+	else
+		die("This operation must be run in a work tree");
+	if (chdir(work_tree))
 		die("This operation must be run in a work tree");

 	/*

-- 
Duy

  reply	other threads:[~2011-01-19  1:59 UTC|newest]

Thread overview: 87+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-26 15:31 [PATCH 00/47] nd/setup updates on pu Nguyễn Thái Ngọc Duy
2010-11-26 15:31 ` [PATCH 01/47] builtins: print setup info if repo is found Nguyễn Thái Ngọc Duy
2011-01-26  0:49   ` [PATCH/RFC 0/3] trace: omit noisy repository discovery report Jonathan Nieder
2011-01-26  0:55     ` [PATCH 1/3] setup: do not expose tracing code Jonathan Nieder
2011-01-26  0:59     ` [PATCH 2/3] trace: omit repository discovery report Jonathan Nieder
2011-01-26  1:19       ` Sverre Rabbelier
2011-01-26  1:46         ` Jonathan Nieder
2011-01-26  1:53       ` Nguyen Thai Ngoc Duy
2011-01-26  1:01     ` [PATCH 3/3] tests: avoid unnecessary use of GIT_TRACE in repo-setup tests Jonathan Nieder
2011-01-26  1:45     ` [PATCH/RFC 0/3] trace: omit noisy repository discovery report Nguyen Thai Ngoc Duy
2011-01-26  5:07       ` Jeff King
2010-11-26 15:31 ` [PATCH 02/47] Add t1510 and basic rules that run repo setup Nguyễn Thái Ngọc Duy
2010-11-26 15:31 ` [PATCH 03/47] t1510: setup case #0 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 04/47] t1510: setup case #1 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 05/47] t1510: setup case #2 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 06/47] t1510: setup case #3 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 07/47] t1510: setup case #4 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 08/47] t1510: setup case #5 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 09/47] t1510: setup case #6 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 10/47] t1510: setup case #7 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 11/47] t1510: setup case #8 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 12/47] t1510: setup case #9 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 13/47] t1510: setup case #10 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 14/47] t1510: setup case #11 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 15/47] t1510: setup case #12 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 16/47] t1510: setup case #13 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 17/47] t1510: setup case #14 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 18/47] t1510: setup case #15 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 19/47] t1510: setup case #16 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 20/47] t1510: setup case #17 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 21/47] t1510: setup case #18 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 22/47] t1510: setup case #19 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 23/47] t1510: setup case #20 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 24/47] t1510: setup case #21 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 25/47] t1510: setup case #22 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 26/47] t1510: setup case #23 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 27/47] t1510: setup case #24 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 28/47] t1510: setup case #25 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 29/47] t1510: setup case #26 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 30/47] t1510: setup case #27 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 31/47] t1510: setup case #28 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 32/47] t1510: setup case #29 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 33/47] t1510: setup case #30 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 34/47] t1510: setup case #31 Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 35/47] git-rev-parse.txt: clarify --git-dir Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 36/47] rev-parse: prints --git-dir relative to user's cwd Nguyễn Thái Ngọc Duy
2010-12-22  1:56   ` Junio C Hamano
2010-12-22  7:05     ` Nguyen Thai Ngoc Duy
2010-11-26 15:32 ` [PATCH 37/47] Add git_config_early() Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 38/47] Use git_config_early() instead of git_config() during repo setup Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 39/47] setup: limit get_git_work_tree()'s to explicit setup case only Nguyễn Thái Ngọc Duy
2011-01-18  7:44   ` Jonathan Nieder
2011-01-18 13:59     ` Nguyen Thai Ngoc Duy
2011-01-18 19:41     ` Junio C Hamano
2011-01-19  1:58       ` Nguyen Thai Ngoc Duy [this message]
2011-01-19 12:37       ` [PATCH/RFC 0/3] setup: stop ignoring GIT_WORK_TREE (when GIT_DIR is unset) Jonathan Nieder
2011-01-19 12:38         ` [PATCH 1/3] tests: cosmetic improvements to the repo-setup test Jonathan Nieder
2011-01-19 12:42         ` [PATCH 3/3] setup: always honor GIT_WORK_TREE and core.worktree Jonathan Nieder
2011-01-19 14:48           ` Nguyen Thai Ngoc Duy
2011-01-19 19:31             ` Jonathan Nieder
2011-01-19 20:17               ` Junio C Hamano
2011-01-21 20:58                 ` Junio C Hamano
2011-01-21 22:02                   ` Junio C Hamano
2011-01-21 22:05                     ` Jonathan Nieder
2011-01-21 23:01                       ` Junio C Hamano
2011-01-22 10:30                   ` Nguyen Thai Ngoc Duy
2011-01-23 23:49                     ` Junio C Hamano
2011-01-24  8:45                       ` Jonathan Nieder
2011-01-19 18:51           ` Maaartin
2011-01-19 19:24             ` Junio C Hamano
2011-01-19 20:35               ` Maaartin
2011-01-19 20:52                 ` checkout to other directory (Re: [PATCH 3/3] setup: always honor GIT_WORK_TREE and core.worktree) Jonathan Nieder
2011-01-19 19:03           ` [PATCH 3/3] setup: always honor GIT_WORK_TREE and core.worktree Jonathan Nieder
2011-01-19 19:13           ` Junio C Hamano
2011-01-19 13:05         ` [PATCH/RFC 0/3] setup: stop ignoring GIT_WORK_TREE (when GIT_DIR is unset) Jonathan Nieder
2010-11-26 15:32 ` [PATCH 40/47] setup: clean up setup_bare_git_dir() Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 41/47] t1020-subdirectory: test alias expansion in a subdirectory Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 42/47] setup: clean up setup_discovered_git_dir() Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 43/47] setup: rework setup_explicit_git_dir() Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 44/47] Remove all logic from get_git_work_tree() Nguyễn Thái Ngọc Duy
2010-12-22  1:56   ` Junio C Hamano
2010-12-22  7:22     ` Nguyen Thai Ngoc Duy
2010-12-22 15:17       ` Junio C Hamano
2010-11-26 15:32 ` [PATCH 45/47] t0001: test git init when run via an alias Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 46/47] Revert "Documentation: always respect core.worktree if set" Nguyễn Thái Ngọc Duy
2010-11-26 15:32 ` [PATCH 47/47] git.txt: correct where --work-tree path is relative to Nguyễn Thái Ngọc Duy
2010-11-29 21:29 ` [PATCH 00/47] nd/setup updates on pu Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='AANLkTi=bm6KO24XTMA+LM+xnszgFHJiV0inXZ-RJwwd-@mail.gmail.com' \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jrnieder@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).