All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [WIP PATCH 03/26] Save setup_git_dir* info globally for later use
Date: Tue, 16 Feb 2010 23:04:54 +0700	[thread overview]
Message-ID: <1266336317-607-4-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1266336317-607-1-git-send-email-pclouds@gmail.com>

For one thing, this could help catching setup_git_dir being run twice,
which is usually wrong.

This also helps moving up setup_git_dir* from inside builtin commands
to handle_internal_command(). Without this, run_builtin()
needs to find another way to pass "nongit_ok" to builtin commands.

The main program has to intialize startup_info pointer properly. For
builtin commands, git.c will take care of that. If startup_info is
NULL, the prior behavior should apply.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Could have save the command name in startup_info too, so error
 messages are clearer..

 cache.h       |    8 ++++++++
 environment.c |    1 +
 git.c         |   31 +++++++++++++++++++++++--------
 setup.c       |   14 +++++++++++++-
 4 files changed, 45 insertions(+), 9 deletions(-)

diff --git a/cache.h b/cache.h
index d478eff..fcbed37 100644
--- a/cache.h
+++ b/cache.h
@@ -1040,4 +1040,12 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix);
 char *alias_lookup(const char *alias);
 int split_cmdline(char *cmdline, const char ***argv);
 
+/* git.c */
+struct startup_info {
+	const char *prefix;
+	int have_set_gitdir;
+	int have_repository;
+};
+extern struct startup_info *startup_info;
+
 #endif /* CACHE_H */
diff --git a/environment.c b/environment.c
index 739ec27..1ab8815 100644
--- a/environment.c
+++ b/environment.c
@@ -52,6 +52,7 @@ enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
 char *notes_ref_name;
 int grafts_replace_parents = 1;
 int core_apply_sparse_checkout;
+struct startup_info *startup_info;
 
 /* Parallel index stat data preload? */
 int core_preload_index = 0;
diff --git a/git.c b/git.c
index b3e23f1..ea29cca 100644
--- a/git.c
+++ b/git.c
@@ -13,6 +13,8 @@ const char git_usage_string[] =
 const char git_more_info_string[] =
 	"See 'git help COMMAND' for more information on a specific command.";
 
+static struct startup_info opts;
+struct startup_info *startup_info;
 static int use_pager = -1;
 struct pager_config {
 	const char *cmd;
@@ -206,9 +208,6 @@ static int handle_alias(int *argcp, const char ***argv)
 		ret = 1;
 	}
 
-	if (subdir && chdir(subdir))
-		die_errno("Cannot change to '%s'", subdir);
-
 	errno = saved_errno;
 
 	return ret;
@@ -234,13 +233,26 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 {
 	int status, help;
 	struct stat st;
-	const char *prefix;
 
-	prefix = NULL;
 	help = argc == 2 && !strcmp(argv[1], "-h");
 	if (!help) {
-		if (p->option & RUN_SETUP)
-			prefix = setup_git_directory();
+		/* handle_alias() may have set up gitdir for alias lookup */
+		if (startup_info->have_set_gitdir) {
+			if (p->option & RUN_SETUP) {
+				if (!startup_info->have_repository)
+					die("No repository found");
+			}
+			else {
+				/* This is WRONG, but we can't get rid of it now */
+				startup_info->have_set_gitdir = 0;
+				if (startup_info->prefix && chdir(startup_info->prefix))
+					die("Cannot change to '%s'", startup_info->prefix);
+			}
+		}
+		else {
+			if (p->option & RUN_SETUP)
+				setup_git_directory();
+		}
 
 		if (use_pager == -1 && p->option & RUN_SETUP)
 			use_pager = check_pager_config(p->cmd);
@@ -254,7 +266,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 
 	trace_argv_printf(argv, "trace: built-in: git");
 
-	status = p->fn(argc, argv, prefix);
+	status = p->fn(argc, argv, startup_info->prefix);
 	if (status)
 		return status;
 
@@ -473,6 +485,9 @@ int main(int argc, const char **argv)
 {
 	const char *cmd;
 
+	startup_info = &opts;
+	memset(startup_info, 0, sizeof(*startup_info));
+
 	cmd = git_extract_argv0_path(argv[0]);
 	if (!cmd)
 		cmd = "git-help";
diff --git a/setup.c b/setup.c
index 183bcf6..0c05d36 100644
--- a/setup.c
+++ b/setup.c
@@ -236,7 +236,13 @@ void setup_work_tree(void)
 		git_dir = make_absolute_path(git_dir);
 	if (!work_tree || chdir(work_tree))
 		die("This operation must be run in a work tree");
+
+	/* we know gitdir is already set, we only adjust it to relative path */
+	if (startup_info)
+		startup_info->have_set_gitdir = 0;
 	set_git_dir(make_relative_path(git_dir, work_tree));
+	if (startup_info)
+		startup_info->have_set_gitdir = 1;
 	initialized = 1;
 }
 
@@ -447,8 +453,14 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	const char *prefix;
 
 	prefix = setup_git_directory_gently_1(nongit_ok);
-	if (!nongit_ok || (!*nongit_ok && check_repository_format_gently(nongit_ok)))
+	if (startup_info)
+		startup_info->have_set_gitdir = 1;
+	if ((!nongit_ok || !*nongit_ok) && check_repository_format_gently(nongit_ok))
 		prefix = NULL;
+	if (startup_info) {
+		startup_info->prefix = prefix;
+		startup_info->have_repository = !nongit_ok || !*nongit_ok;
+	}
 	return prefix;
 }
 
-- 
1.7.0.195.g637a2

  parent reply	other threads:[~2010-02-16 16:08 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-16 16:04 [WIP PATCH 00/26] Git setup cleanup series Nguyễn Thái Ngọc Duy
2010-02-16 16:04 ` [WIP PATCH 01/26] rev-parse --git-dir: print relative gitdir correctly Nguyễn Thái Ngọc Duy
2010-02-16 16:04 ` [WIP PATCH 02/26] setup_git_directory*: Explicitly set git dir Nguyễn Thái Ngọc Duy
2010-02-16 16:04 ` Nguyễn Thái Ngọc Duy [this message]
2010-02-16 16:04 ` [WIP PATCH 04/26] Add GIT_HARDENED_SETUP to detect gitdir/worktree related mis-setup errors Nguyễn Thái Ngọc Duy
2010-02-16 16:04 ` [WIP PATCH 05/26] enter_repo(): use setup_git_directory_gently internally Nguyễn Thái Ngọc Duy
2010-02-16 16:04 ` [WIP PATCH 06/26] Tweak init/clone to work properly with GIT_HARDENED_SETUP=1 Nguyễn Thái Ngọc Duy
2010-02-16 16:04 ` [WIP PATCH 07/26] Support running setup_git_dir_gently() from the beginning for builtin commands Nguyễn Thái Ngọc Duy
2010-02-16 16:04 ` [WIP PATCH 08/26] config: move up gitdir setup to run_builtin() Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 09/26] hash-object: move " Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 10/26] shortlog: move up " Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 11/26] Do not look for .git/info/exclude when gitdir is not set up Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 12/26] grep: move up gitdir setup to run_builtin() Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 13/26] USE_PAGER should not be used without RUN_SETUP* Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 14/26] Do not try to read $GIT_DIR/info/attributes if there is no repository Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 15/26] archive: move up gitdir setup to run_builtin() Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 16/26] mailinfo: " Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 17/26] check-ref-format: setup gitdir gently Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 18/26] verify-pack: set up " Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 19/26] apply: move up gitdir setup to run_builtin() Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 20/26] bundle: " Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 21/26] diff: " Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 22/26] help: " Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 23/26] ls-remote: " Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 24/26] var: " Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 25/26] merge-file: " Nguyễn Thái Ngọc Duy
2010-02-16 16:05 ` [WIP PATCH 26/26] Turn on GIT_HARDENED_SETUP for the whole test suite Nguyễn Thái Ngọc Duy

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

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

  git send-email \
    --in-reply-to=1266336317-607-4-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    /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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.