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: [PATCH v2 17/19] Allow to undo setup_git_directory_gently() gracefully (and fix alias code)
Date: Sun, 21 Mar 2010 17:30:44 +0700 [thread overview]
Message-ID: <1269167446-7799-18-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1269167446-7799-1-git-send-email-pclouds@gmail.com>
unset_git_directory() can only clean up things as long as
set_git_dir() has not been called because set_git_dir() sets internal
state itself. Even worse, set_git_dir() may override $GIT_DIR env
variable.
Add unset_git_env() to undo set_git_dir(), allow unset_git_directory()
to undo a succesful setup_git_directory_gently().
While at there, fix alias handling code regarding git_dir {,un-}setup,
use unset_git_directory() instead of just chdir() back.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
cache.h | 1 +
environment.c | 20 ++++++++++++++++++++
git.c | 11 +++++------
setup.c | 2 ++
4 files changed, 28 insertions(+), 6 deletions(-)
diff --git a/cache.h b/cache.h
index e397e1d..b1ed150 100644
--- a/cache.h
+++ b/cache.h
@@ -419,6 +419,7 @@ extern void setup_work_tree(void);
extern const char *setup_git_directory_gently(int *);
extern const char *setup_git_directory(void);
extern void unset_git_directory(const char *prefix);
+extern void unset_git_env();
extern const char *prefix_path(const char *prefix, int len, const char *path);
extern const char *prefix_filename(const char *prefix, int len, const char *path);
extern int check_filename(const char *prefix, const char *name);
diff --git a/environment.c b/environment.c
index c36c902..6127025 100644
--- a/environment.c
+++ b/environment.c
@@ -62,6 +62,7 @@ char *git_work_tree_cfg;
static char *work_tree;
static const char *git_dir;
+static const char *original_git_dir;
static char *git_object_dir, *git_index_file, *git_refs_dir, *git_graft_file;
/*
@@ -81,6 +82,20 @@ const char * const local_repo_env[LOCAL_REPO_ENV_SIZE + 1] = {
NULL
};
+void unset_git_env(void)
+{
+ git_dir = NULL;
+ if (original_git_dir)
+ setenv(GIT_DIR_ENVIRONMENT, original_git_dir, 1);
+ else
+ unsetenv(GIT_DIR_ENVIRONMENT);
+ git_object_dir = NULL;
+ git_refs_dir = NULL;
+ git_index_file = NULL;
+ git_graft_file = NULL;
+ read_replace_refs = 1;
+}
+
static void setup_git_env(void)
{
git_dir = getenv(GIT_DIR_ENVIRONMENT);
@@ -184,6 +199,11 @@ char *get_graft_file(void)
int set_git_dir(const char *path)
{
+ static int original_git_dir_set = 0;
+ if (!original_git_dir_set) {
+ original_git_dir = getenv(GIT_DIR_ENVIRONMENT);
+ original_git_dir_set = 1;
+ }
if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
return error("Could not set GIT_DIR to '%s'", path);
setup_git_env();
diff --git a/git.c b/git.c
index 7e30498..db5bd95 100644
--- a/git.c
+++ b/git.c
@@ -146,14 +146,13 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
static int handle_alias(int *argcp, const char ***argv)
{
int envchanged = 0, ret = 0, saved_errno = errno;
- const char *subdir;
int count, option_count;
const char **new_argv;
const char *alias_command;
char *alias_string;
int unused_nongit;
- subdir = setup_git_directory_gently(&unused_nongit);
+ setup_git_directory_gently(&unused_nongit);
alias_command = (*argv)[0];
alias_string = alias_lookup(alias_command);
@@ -210,8 +209,7 @@ static int handle_alias(int *argcp, const char ***argv)
ret = 1;
}
- if (subdir && chdir(subdir))
- die_errno("Cannot change to '%s'", subdir);
+ unset_git_directory(startup_info->prefix);
errno = saved_errno;
@@ -240,8 +238,6 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
int status;
struct stat st;
- memset(&git_startup_info, 0, sizeof(git_startup_info));
- startup_info = &git_startup_info;
startup_info->help = argc == 2 && !strcmp(argv[1], "-h");
if (!startup_info->help) {
if (p->option & RUN_SETUP)
@@ -486,6 +482,9 @@ int main(int argc, const char **argv)
{
const char *cmd;
+ memset(&git_startup_info, 0, sizeof(git_startup_info));
+ startup_info = &git_startup_info;
+
cmd = git_extract_argv0_path(argv[0]);
if (!cmd)
cmd = "git-help";
diff --git a/setup.c b/setup.c
index 4de7bf0..1808ebe 100644
--- a/setup.c
+++ b/setup.c
@@ -336,6 +336,8 @@ void unset_git_directory(const char *prefix)
die("Cannot change to '%s'", prefix);
if (startup_info) {
+ if (startup_info->have_repository)
+ unset_git_env();
startup_info->prefix = NULL;
startup_info->have_repository = 0;
}
--
1.7.0.2.425.gb99f1
next prev parent reply other threads:[~2010-03-21 10:36 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-03-21 10:30 [PATCH v2 00/19] nd/setup part two, second round Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 01/19] Move enter_repo() to setup.c Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 02/19] enter_repo(): initialize other variables as setup_git_directory_gently() does Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 03/19] rev-parse --git-dir: print relative gitdir correctly Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 04/19] worktree setup: call set_git_dir explicitly Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 05/19] Add git_config_early() Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 06/19] Use git_config_early() instead of git_config() during repo setup Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 07/19] worktree setup: restore original state when things go wrong Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 08/19] init/clone: turn on startup->have_repository properly Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 09/19] git_config(): do not read .git/config if there is no repository Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 10/19] Do not read .git/info/exclude " Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 11/19] Do not read .git/info/attributes " Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 12/19] apply: do not check sha1 when repository has not been found Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 13/19] config: do not read .git/config if there is no repository Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 14/19] run_builtin(): save "-h" detection result for later use Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 15/19] builtins: utilize startup_info->help where possible Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 16/19] builtins: check for startup_info->help, print and exit early Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` Nguyễn Thái Ngọc Duy [this message]
2010-03-21 10:30 ` [PATCH v2 18/19] alias: keep repository found while collecting aliases as long as possible Nguyễn Thái Ngọc Duy
2010-03-21 10:30 ` [PATCH v2 19/19] Guard unallowed access to repository when it's not set up 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=1269167446-7799-18-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 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 NNTP newsgroup(s).