{"thread":{"id":"22985","subject":"[PATCH 00/16] nd/setup part two","startedAt":"2010-03-11T13:22:18Z","lastAt":"2010-03-20T08:10:46Z","messageCount":20,"participants":["Nguyễn Thái Ngọc Duy","Junio C Hamano","Nguyen Thai Ngoc Duy"],"isPatch":true,"patchVersion":1,"patchTotal":16},"messages":[{"id":"136584","messageId":"1268313754-28179-1-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":null,"subject":"[PATCH 00/16] nd/setup part two","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:18Z","receivedAt":"2010-03-11T13:22:18Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"While the first part is more of preventing faults. This part fixes the\nremaining faults (or introduces more faults, who knows). I think I\nhave got it to a readable/testable state.\n\nOn top of nd/setup, obviously.\n\nNguyễn Thái Ngọc Duy (16):\n  Move enter_repo() to setup.c\n  enter_repo(): initialize other variables as setup_git_directory_gently() does\n  rev-parse --git-dir: print relative gitdir correctly\n  worktree setup: call set_git_dir explicitly\n  Add git_config_early()\n\n    Preparation\n\n\n  Use git_config_early() instead of git_config() during repo setup\n  worktree setup: restore original state when things go wrong\n  init/clone: turn on startup->have_repository properly\n\n    Improve setup stuff\n\n\n  git_config(): do not read .git/config if there is no repository\n  Do not read .git/info/exclude if there is no repository\n  Do not read .git/info/attributes if there is no repository\n  apply: do not check sha1 when repository has not been found\n  config: do not read .git/config if there is no repository\n\n    Stop improper access to repo (and incorrectly set git_dir\n    along the way)\n\n\n  Allow to undo setup_git_directory_gently() gracefully (and fix alias code)\n  alias: keep repository found while collecting aliases as long as possible\n\n    Alias fix/improvement. I think I can add some tests for this.\n\n\n  Guard unallowed access to repository when it's not set up\n\n    The original patch [1] that has grown up to a 37-patch, 2-part series\n\n[1] http://mid.gmane.org/1265370468-6147-1-git-send-email-pclouds@gmail.com\n\n attr.c                  |    5 +-\n builtin/apply.c         |    2 +-\n builtin/clone.c         |    3 +-\n builtin/config.c        |    9 ++-\n builtin/init-db.c       |   10 ++-\n builtin/rev-parse.c     |    8 ++\n cache.h                 |    6 ++-\n config.c                |   22 +++++--\n dir.c                   |    8 ++-\n environment.c           |   33 ++++++++-\n git.c                   |   22 ++++---\n path.c                  |   91 -----------------------\n setup.c                 |  184 ++++++++++++++++++++++++++++++++++++++++++++---\n t/t1300-repo-config.sh  |   14 ++++\n t/t1302-repo-version.sh |    2 +-\n t/t7002-grep.sh         |   24 ++++++\n 16 files changed, 309 insertions(+), 134 deletions(-)\n"},{"id":"136590","messageId":"1268313754-28179-2-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 01/16] Move enter_repo() to setup.c","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:19Z","receivedAt":"2010-03-11T13:22:19Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n path.c  |   91 ---------------------------------------------------------------\n setup.c |   91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 91 insertions(+), 91 deletions(-)\n\ndiff --git a/path.c b/path.c\nindex b4c8d91..f42eb1b 100644\n--- a/path.c\n+++ b/path.c\n@@ -332,97 +332,6 @@ return_null:\n \treturn NULL;\n }\n \n-/*\n- * First, one directory to try is determined by the following algorithm.\n- *\n- * (0) If \"strict\" is given, the path is used as given and no DWIM is\n- *     done. Otherwise:\n- * (1) \"~/path\" to mean path under the running user's home directory;\n- * (2) \"~user/path\" to mean path under named user's home directory;\n- * (3) \"relative/path\" to mean cwd relative directory; or\n- * (4) \"/absolute/path\" to mean absolute directory.\n- *\n- * Unless \"strict\" is given, we try access() for existence of \"%s.git/.git\",\n- * \"%s/.git\", \"%s.git\", \"%s\" in this order.  The first one that exists is\n- * what we try.\n- *\n- * Second, we try chdir() to that.  Upon failure, we return NULL.\n- *\n- * Then, we try if the current directory is a valid git repository.\n- * Upon failure, we return NULL.\n- *\n- * If all goes well, we return the directory we used to chdir() (but\n- * before ~user is expanded), avoiding getcwd() resolving symbolic\n- * links.  User relative paths are also returned as they are given,\n- * except DWIM suffixing.\n- */\n-char *enter_repo(char *path, int strict)\n-{\n-\tstatic char used_path[PATH_MAX];\n-\tstatic char validated_path[PATH_MAX];\n-\n-\tif (!path)\n-\t\treturn NULL;\n-\n-\tif (!strict) {\n-\t\tstatic const char *suffix[] = {\n-\t\t\t\".git/.git\", \"/.git\", \".git\", \"\", NULL,\n-\t\t};\n-\t\tint len = strlen(path);\n-\t\tint i;\n-\t\twhile ((1 < len) && (path[len-1] == '/')) {\n-\t\t\tpath[len-1] = 0;\n-\t\t\tlen--;\n-\t\t}\n-\t\tif (PATH_MAX <= len)\n-\t\t\treturn NULL;\n-\t\tif (path[0] == '~') {\n-\t\t\tchar *newpath = expand_user_path(path);\n-\t\t\tif (!newpath || (PATH_MAX - 10 < strlen(newpath))) {\n-\t\t\t\tfree(newpath);\n-\t\t\t\treturn NULL;\n-\t\t\t}\n-\t\t\t/*\n-\t\t\t * Copy back into the static buffer. A pity\n-\t\t\t * since newpath was not bounded, but other\n-\t\t\t * branches of the if are limited by PATH_MAX\n-\t\t\t * anyway.\n-\t\t\t */\n-\t\t\tstrcpy(used_path, newpath); free(newpath);\n-\t\t\tstrcpy(validated_path, path);\n-\t\t\tpath = used_path;\n-\t\t}\n-\t\telse if (PATH_MAX - 10 < len)\n-\t\t\treturn NULL;\n-\t\telse {\n-\t\t\tpath = strcpy(used_path, path);\n-\t\t\tstrcpy(validated_path, path);\n-\t\t}\n-\t\tlen = strlen(path);\n-\t\tfor (i = 0; suffix[i]; i++) {\n-\t\t\tstrcpy(path + len, suffix[i]);\n-\t\t\tif (!access(path, F_OK)) {\n-\t\t\t\tstrcat(validated_path, suffix[i]);\n-\t\t\t\tbreak;\n-\t\t\t}\n-\t\t}\n-\t\tif (!suffix[i] || chdir(path))\n-\t\t\treturn NULL;\n-\t\tpath = validated_path;\n-\t}\n-\telse if (chdir(path))\n-\t\treturn NULL;\n-\n-\tif (access(\"objects\", X_OK) == 0 && access(\"refs\", X_OK) == 0 &&\n-\t    validate_headref(\"HEAD\") == 0) {\n-\t\tset_git_dir(\".\");\n-\t\tcheck_repository_format();\n-\t\treturn path;\n-\t}\n-\n-\treturn NULL;\n-}\n-\n int set_shared_perm(const char *path, int mode)\n {\n \tstruct stat st;\ndiff --git a/setup.c b/setup.c\nindex 8796c6f..3019da2 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -452,6 +452,97 @@ const char *setup_git_directory_gently(int *nongit_ok)\n \treturn prefix;\n }\n \n+/*\n+ * First, one directory to try is determined by the following algorithm.\n+ *\n+ * (0) If \"strict\" is given, the path is used as given and no DWIM is\n+ *     done. Otherwise:\n+ * (1) \"~/path\" to mean path under the running user's home directory;\n+ * (2) \"~user/path\" to mean path under named user's home directory;\n+ * (3) \"relative/path\" to mean cwd relative directory; or\n+ * (4) \"/absolute/path\" to mean absolute directory.\n+ *\n+ * Unless \"strict\" is given, we try access() for existence of \"%s.git/.git\",\n+ * \"%s/.git\", \"%s.git\", \"%s\" in this order.  The first one that exists is\n+ * what we try.\n+ *\n+ * Second, we try chdir() to that.  Upon failure, we return NULL.\n+ *\n+ * Then, we try if the current directory is a valid git repository.\n+ * Upon failure, we return NULL.\n+ *\n+ * If all goes well, we return the directory we used to chdir() (but\n+ * before ~user is expanded), avoiding getcwd() resolving symbolic\n+ * links.  User relative paths are also returned as they are given,\n+ * except DWIM suffixing.\n+ */\n+char *enter_repo(char *path, int strict)\n+{\n+\tstatic char used_path[PATH_MAX];\n+\tstatic char validated_path[PATH_MAX];\n+\n+\tif (!path)\n+\t\treturn NULL;\n+\n+\tif (!strict) {\n+\t\tstatic const char *suffix[] = {\n+\t\t\t\".git/.git\", \"/.git\", \".git\", \"\", NULL,\n+\t\t};\n+\t\tint len = strlen(path);\n+\t\tint i;\n+\t\twhile ((1 < len) && (path[len-1] == '/')) {\n+\t\t\tpath[len-1] = 0;\n+\t\t\tlen--;\n+\t\t}\n+\t\tif (PATH_MAX <= len)\n+\t\t\treturn NULL;\n+\t\tif (path[0] == '~') {\n+\t\t\tchar *newpath = expand_user_path(path);\n+\t\t\tif (!newpath || (PATH_MAX - 10 < strlen(newpath))) {\n+\t\t\t\tfree(newpath);\n+\t\t\t\treturn NULL;\n+\t\t\t}\n+\t\t\t/*\n+\t\t\t * Copy back into the static buffer. A pity\n+\t\t\t * since newpath was not bounded, but other\n+\t\t\t * branches of the if are limited by PATH_MAX\n+\t\t\t * anyway.\n+\t\t\t */\n+\t\t\tstrcpy(used_path, newpath); free(newpath);\n+\t\t\tstrcpy(validated_path, path);\n+\t\t\tpath = used_path;\n+\t\t}\n+\t\telse if (PATH_MAX - 10 < len)\n+\t\t\treturn NULL;\n+\t\telse {\n+\t\t\tpath = strcpy(used_path, path);\n+\t\t\tstrcpy(validated_path, path);\n+\t\t}\n+\t\tlen = strlen(path);\n+\t\tfor (i = 0; suffix[i]; i++) {\n+\t\t\tstrcpy(path + len, suffix[i]);\n+\t\t\tif (!access(path, F_OK)) {\n+\t\t\t\tstrcat(validated_path, suffix[i]);\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t\tif (!suffix[i] || chdir(path))\n+\t\t\treturn NULL;\n+\t\tpath = validated_path;\n+\t}\n+\telse if (chdir(path))\n+\t\treturn NULL;\n+\n+\tif (access(\"objects\", X_OK) == 0 && access(\"refs\", X_OK) == 0 &&\n+\t    validate_headref(\"HEAD\") == 0) {\n+\t\tset_git_dir(\".\");\n+\t\tcheck_repository_format();\n+\t\treturn path;\n+\t}\n+\n+\treturn NULL;\n+}\n+\n int git_config_perm(const char *var, const char *value)\n {\n \tint i;\n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136585","messageId":"1268313754-28179-3-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 02/16] enter_repo(): initialize other variables as setup_git_directory_gently() does","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:20Z","receivedAt":"2010-03-11T13:22:20Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n setup.c |    8 +++++++-\n 1 files changed, 7 insertions(+), 1 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 3019da2..e067292 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -535,8 +535,14 @@ char *enter_repo(char *path, int strict)\n \n \tif (access(\"objects\", X_OK) == 0 && access(\"refs\", X_OK) == 0 &&\n \t    validate_headref(\"HEAD\") == 0) {\n-\t\tset_git_dir(\".\");\n+\t\tinside_work_tree = 0;\n+\t\tinside_git_dir = 1;\n \t\tcheck_repository_format();\n+\t\tset_git_dir(\".\");\n+\t\tif (startup_info) {\n+\t\t\tstartup_info->prefix = NULL;\n+\t\t\tstartup_info->have_repository = 1;\n+\t\t}\n \t\treturn path;\n \t}\n \n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136587","messageId":"1268313754-28179-4-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 03/16] rev-parse --git-dir: print relative gitdir correctly","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:21Z","receivedAt":"2010-03-11T13:22:21Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"When git_dir is relative, it is relative to Git's current working\ndirectory, which is worktree top directory. \"git rev-parse --git-dir\"\nis expected to output relative to user's current working directory.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/rev-parse.c |    8 ++++++++\n 1 files changed, 8 insertions(+), 0 deletions(-)\n\ndiff --git a/builtin/rev-parse.c b/builtin/rev-parse.c\nindex 8fbf9d0..8819e8a 100644\n--- a/builtin/rev-parse.c\n+++ b/builtin/rev-parse.c\n@@ -646,6 +646,14 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n \t\t\t\tstatic char cwd[PATH_MAX];\n \t\t\t\tint len;\n \t\t\t\tif (gitdir) {\n+\t\t\t\t\tif (prefix && !is_absolute_path(gitdir)) {\n+\t\t\t\t\t\tint len;\n+\t\t\t\t\t\tif (!getcwd(cwd, PATH_MAX))\n+\t\t\t\t\t\t\tdie_errno(\"unable to get current working directory\");\n+\t\t\t\t\t\tlen = strlen(cwd);\n+\t\t\t\t\t\tprintf(\"%s%s%s\\n\", cwd, len && cwd[len-1] != '/' ? \"/\" : \"\", gitdir);\n+\t\t\t\t\t\tcontinue;\n+\t\t\t\t\t}\n \t\t\t\t\tputs(gitdir);\n \t\t\t\t\tcontinue;\n \t\t\t\t}\n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136586","messageId":"1268313754-28179-5-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 04/16] worktree setup: call set_git_dir explicitly","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:22Z","receivedAt":"2010-03-11T13:22:22Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n setup.c |   11 ++++++++---\n 1 files changed, 8 insertions(+), 3 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex e067292..43a8609 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -350,14 +350,17 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n \t\t\t\t/* config may override worktree */\n \t\t\t\tif (check_repository_format_gently(nongit_ok))\n \t\t\t\t\treturn NULL;\n+\t\t\t\tset_git_dir(gitdirenv);\n \t\t\t\treturn retval;\n \t\t\t}\n \t\t\tif (check_repository_format_gently(nongit_ok))\n \t\t\t\treturn NULL;\n \t\t\tretval = get_relative_cwd(buffer, sizeof(buffer) - 1,\n \t\t\t\t\tget_git_work_tree());\n-\t\t\tif (!retval || !*retval)\n+\t\t\tif (!retval || !*retval) {\n+\t\t\t\tset_git_dir(gitdirenv);\n \t\t\t\treturn NULL;\n+\t\t\t}\n \t\t\tset_git_dir(make_absolute_path(gitdirenv));\n \t\t\tif (chdir(work_tree_env) < 0)\n \t\t\t\tdie_errno (\"Could not chdir to '%s'\", work_tree_env);\n@@ -392,8 +395,10 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n \toffset = len = strlen(cwd);\n \tfor (;;) {\n \t\tgitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);\n-\t\tif (gitfile_dir || is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) {\n-\t\t\tif (gitfile_dir && set_git_dir(gitfile_dir))\n+\t\tif (!gitfile_dir && is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))\n+\t\t\tgitfile_dir = DEFAULT_GIT_DIR_ENVIRONMENT;\n+\t\tif (gitfile_dir) {\n+\t\t\tif (set_git_dir(gitfile_dir))\n \t\t\t\tdie(\"Repository setup failed\");\n \t\t\tinside_git_dir = 0;\n \t\t\tif (!work_tree_env)\n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136588","messageId":"1268313754-28179-6-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 05/16] Add git_config_early()","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:23Z","receivedAt":"2010-03-11T13:22:23Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"This version of git_config() will be used during repository setup.\nAs a repository is being set up, $GIT_DIR is not nailed down yet,\ngit_pathdup() should not be used to get $GIT_DIR/config.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n cache.h  |    1 +\n config.c |   19 ++++++++++++++-----\n 2 files changed, 15 insertions(+), 5 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex 68412c0..3bd219c 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -933,6 +933,7 @@ typedef int (*config_fn_t)(const char *, const char *, void *);\n extern int git_default_config(const char *, const char *, void *);\n extern int git_config_from_file(config_fn_t fn, const char *, void *);\n extern int git_config(config_fn_t fn, void *);\n+extern int git_config_early(config_fn_t fn, void *, const char *repo_config);\n extern int git_parse_ulong(const char *, unsigned long *);\n extern int git_config_int(const char *, const char *);\n extern unsigned long git_config_ulong(const char *, const char *);\ndiff --git a/config.c b/config.c\nindex 6963fbe..2d38096 100644\n--- a/config.c\n+++ b/config.c\n@@ -699,10 +699,9 @@ int git_config_global(void)\n \treturn !git_env_bool(\"GIT_CONFIG_NOGLOBAL\", 0);\n }\n \n-int git_config(config_fn_t fn, void *data)\n+int git_config_early(config_fn_t fn, void *data, const char *repo_config)\n {\n \tint ret = 0, found = 0;\n-\tchar *repo_config = NULL;\n \tconst char *home = NULL;\n \n \t/* Setting $GIT_CONFIG makes git read _only_ the given config file. */\n@@ -724,17 +723,27 @@ int git_config(config_fn_t fn, void *data)\n \t\tfree(user_config);\n \t}\n \n-\trepo_config = git_pathdup(\"config\");\n-\tif (!access(repo_config, R_OK)) {\n+\tif (repo_config && !access(repo_config, R_OK)) {\n \t\tret += git_config_from_file(fn, repo_config, data);\n \t\tfound += 1;\n \t}\n-\tfree(repo_config);\n \tif (found == 0)\n \t\treturn -1;\n \treturn ret;\n }\n \n+int git_config(config_fn_t fn, void *data)\n+{\n+\tchar *repo_config = NULL;\n+\tint ret;\n+\n+\trepo_config = git_pathdup(\"config\");\n+\tret = git_config_early(fn, data, repo_config);\n+\tif (repo_config)\n+\t\tfree(repo_config);\n+\treturn ret;\n+}\n+\n /*\n  * Find all the stuff for git_config_set() below.\n  */\n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136589","messageId":"1268313754-28179-7-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 06/16] Use git_config_early() instead of git_config() during repo setup","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:24Z","receivedAt":"2010-03-11T13:22:24Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n setup.c |   30 ++++++++++++++++++++++--------\n 1 files changed, 22 insertions(+), 8 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 43a8609..b8f88db 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -241,9 +241,21 @@ void setup_work_tree(void)\n \tinitialized = 1;\n }\n \n-static int check_repository_format_gently(int *nongit_ok)\n+static int check_repository_format_gently(const char *gitdir, int *nongit_ok)\n {\n-\tgit_config(check_repository_format_version, NULL);\n+\tchar repo_config[PATH_MAX+1];\n+\n+\t/*\n+\t * git_config() can't be used here because it calls git_pathdup()\n+\t * to get $GIT_CONFIG/config. That call will make setup_git_env()\n+\t * set git_dir to \".git\".\n+\t *\n+\t * We are in gitdir setup, no git dir has been found useable yet.\n+\t * Use a gentler version of git_config() to check if this repo\n+\t * is a good one.\n+\t */\n+\tsnprintf(repo_config, PATH_MAX, \"%s/config\", gitdir);\n+\tgit_config_early(check_repository_format_version, NULL, repo_config);\n \tif (GIT_REPO_VERSION < repository_format_version) {\n \t\tif (!nongit_ok)\n \t\t\tdie (\"Expected git repo version <= %d, found %d\",\n@@ -348,12 +360,12 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n \t\t\tif (!work_tree_env) {\n \t\t\t\tretval = set_work_tree(gitdirenv);\n \t\t\t\t/* config may override worktree */\n-\t\t\t\tif (check_repository_format_gently(nongit_ok))\n+\t\t\t\tif (check_repository_format_gently(gitdirenv, nongit_ok))\n \t\t\t\t\treturn NULL;\n \t\t\t\tset_git_dir(gitdirenv);\n \t\t\t\treturn retval;\n \t\t\t}\n-\t\t\tif (check_repository_format_gently(nongit_ok))\n+\t\t\tif (check_repository_format_gently(gitdirenv, nongit_ok))\n \t\t\t\treturn NULL;\n \t\t\tretval = get_relative_cwd(buffer, sizeof(buffer) - 1,\n \t\t\t\t\tget_git_work_tree());\n@@ -403,6 +415,8 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n \t\t\tinside_git_dir = 0;\n \t\t\tif (!work_tree_env)\n \t\t\t\tinside_work_tree = 1;\n+\t\t\tif (check_repository_format_gently(gitfile_dir, nongit_ok))\n+\t\t\t\treturn NULL;\n \t\t\troot_len = offset_1st_component(cwd);\n \t\t\tgit_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);\n \t\t\tbreak;\n@@ -411,6 +425,8 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n \t\t\tinside_git_dir = 1;\n \t\t\tif (!work_tree_env)\n \t\t\t\tinside_work_tree = 0;\n+\t\t\tif (check_repository_format_gently(gitfile_dir, nongit_ok))\n+\t\t\t\treturn NULL;\n \t\t\tif (offset != len) {\n \t\t\t\troot_len = offset_1st_component(cwd);\n \t\t\t\tcwd[offset > root_len ? offset : root_len] = '\\0';\n@@ -433,8 +449,6 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n \t\t\tdie_errno(\"Cannot change to '%s/..'\", cwd);\n \t}\n \n-\tif (check_repository_format_gently(nongit_ok))\n-\t\treturn NULL;\n \tif (offset == len)\n \t\treturn NULL;\n \n@@ -542,7 +556,7 @@ char *enter_repo(char *path, int strict)\n \t    validate_headref(\"HEAD\") == 0) {\n \t\tinside_work_tree = 0;\n \t\tinside_git_dir = 1;\n-\t\tcheck_repository_format();\n+\t\tcheck_repository_format_gently(\".\", NULL);\n \t\tset_git_dir(\".\");\n \t\tif (startup_info) {\n \t\t\tstartup_info->prefix = NULL;\n@@ -627,7 +641,7 @@ int check_repository_format_version(const char *var, const char *value, void *cb\n \n int check_repository_format(void)\n {\n-\treturn check_repository_format_gently(NULL);\n+\treturn check_repository_format_gently(get_git_dir(), NULL);\n }\n \n const char *setup_git_directory(void)\n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136591","messageId":"1268313754-28179-8-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 07/16] worktree setup: restore original state when things go wrong","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:25Z","receivedAt":"2010-03-11T13:22:25Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n cache.h |    1 +\n setup.c |   39 +++++++++++++++++++++++++++++++++++----\n 2 files changed, 36 insertions(+), 4 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex 3bd219c..8e9d818 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -418,6 +418,7 @@ extern const char **get_pathspec(const char *prefix, const char **pathspec);\n extern void setup_work_tree(void);\n extern const char *setup_git_directory_gently(int *);\n extern const char *setup_git_directory(void);\n+extern void unset_git_directory(const char *prefix);\n extern const char *prefix_path(const char *prefix, int len, const char *path);\n extern const char *prefix_filename(const char *prefix, int len, const char *path);\n extern int check_filename(const char *prefix, const char *name);\ndiff --git a/setup.c b/setup.c\nindex b8f88db..2f850ab 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -323,6 +323,26 @@ const char *read_gitfile_gently(const char *path)\n \treturn path;\n }\n \n+void unset_git_directory(const char *prefix)\n+{\n+\tif (prefix && chdir(prefix))\n+\t\tdie(\"Cannot change to '%s'\", prefix);\n+\n+\tif (startup_info) {\n+\t\tstartup_info->prefix = NULL;\n+\t\tstartup_info->have_repository = 0;\n+\t}\n+\n+\t/* Initialized in setup_git_directory_gently_1() */\n+\tinside_work_tree = -1;\n+\tinside_git_dir = -1;\n+\n+\t/* Initialized in check_repository_format_version() */\n+\trepository_format_version = 0xFF;\n+\tshared_repository = PERM_UMASK;\n+\tis_bare_repository_cfg = -1;\n+\tgit_work_tree_cfg = NULL;\n+}\n /*\n  * We cannot decide in this function whether we are in the work tree or\n  * not, since the config can only be read _after_ this function was called.\n@@ -403,6 +423,13 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n \t * - ../ (bare)\n \t * - ../../.git/\n \t *   etc.\n+\t *\n+\t * When a repository is found:\n+\t * - inside_git_dir/inside_work_tree are set\n+\t * - check_repository_format_gently() is called\n+\t *   if repo version is not supported, restore cwd\n+\t * - set_git_dir\n+\t * - calculate and return prefix\n \t */\n \toffset = len = strlen(cwd);\n \tfor (;;) {\n@@ -410,13 +437,15 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n \t\tif (!gitfile_dir && is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))\n \t\t\tgitfile_dir = DEFAULT_GIT_DIR_ENVIRONMENT;\n \t\tif (gitfile_dir) {\n-\t\t\tif (set_git_dir(gitfile_dir))\n-\t\t\t\tdie(\"Repository setup failed\");\n \t\t\tinside_git_dir = 0;\n \t\t\tif (!work_tree_env)\n \t\t\t\tinside_work_tree = 1;\n-\t\t\tif (check_repository_format_gently(gitfile_dir, nongit_ok))\n+\t\t\tif (check_repository_format_gently(gitfile_dir, nongit_ok)) {\n+\t\t\t\tunset_git_directory(offset != len ? cwd + offset + 1: NULL);\n \t\t\t\treturn NULL;\n+\t\t\t}\n+\t\t\tif (set_git_dir(gitfile_dir))\n+\t\t\t\tdie(\"Repository setup failed\");\n \t\t\troot_len = offset_1st_component(cwd);\n \t\t\tgit_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);\n \t\t\tbreak;\n@@ -425,8 +454,10 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n \t\t\tinside_git_dir = 1;\n \t\t\tif (!work_tree_env)\n \t\t\t\tinside_work_tree = 0;\n-\t\t\tif (check_repository_format_gently(gitfile_dir, nongit_ok))\n+\t\t\tif (check_repository_format_gently(gitfile_dir, nongit_ok)) {\n+\t\t\t\tunset_git_directory(offset != len ? cwd + offset + 1: NULL);\n \t\t\t\treturn NULL;\n+\t\t\t}\n \t\t\tif (offset != len) {\n \t\t\t\troot_len = offset_1st_component(cwd);\n \t\t\t\tcwd[offset > root_len ? offset : root_len] = '\\0';\n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136593","messageId":"1268313754-28179-9-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 08/16] init/clone: turn on startup->have_repository properly","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:26Z","receivedAt":"2010-03-11T13:22:26Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"With startup_info != NULL, many code path may be disabled, depending\non repo setup. Also move set_git_dir() closer to have_repository\nassignment to make it clear about repo setup.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/clone.c   |    3 +--\n builtin/init-db.c |    9 +++++----\n cache.h           |    2 +-\n 3 files changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/clone.c b/builtin/clone.c\nindex 58bacbd..ab7a3c4 100644\n--- a/builtin/clone.c\n+++ b/builtin/clone.c\n@@ -460,9 +460,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)\n \n \tif (safe_create_leading_directories_const(git_dir) < 0)\n \t\tdie(\"could not create leading directories of '%s'\", git_dir);\n-\tset_git_dir(make_absolute_path(git_dir));\n \n-\tinit_db(option_template, option_quiet ? INIT_DB_QUIET : 0);\n+\tinit_db(git_dir, option_template, option_quiet ? INIT_DB_QUIET : 0);\n \n \t/*\n \t * At this point, the config exists, so we do not need the\ndiff --git a/builtin/init-db.c b/builtin/init-db.c\nindex edc40ff..064b919 100644\n--- a/builtin/init-db.c\n+++ b/builtin/init-db.c\n@@ -294,12 +294,15 @@ static int create_default_files(const char *template_path)\n \treturn reinit;\n }\n \n-int init_db(const char *template_dir, unsigned int flags)\n+int init_db(const char *git_dir, const char *template_dir, unsigned int flags)\n {\n \tconst char *sha1_dir;\n \tchar *path;\n \tint len, reinit;\n \n+\tset_git_dir(make_absolute_path(git_dir));\n+\tstartup_info->have_repository = 1;\n+\n \tsafe_create_dir(get_git_dir(), 0);\n \n \tinit_is_bare_repository = is_bare_repository();\n@@ -509,7 +512,5 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)\n \t\t\t\t   get_git_work_tree());\n \t}\n \n-\tset_git_dir(make_absolute_path(git_dir));\n-\n-\treturn init_db(template_dir, flags);\n+\treturn init_db(git_dir, template_dir, flags);\n }\ndiff --git a/cache.h b/cache.h\nindex 8e9d818..dd27e11 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -427,7 +427,7 @@ extern void verify_non_filename(const char *prefix, const char *name);\n \n #define INIT_DB_QUIET 0x0001\n \n-extern int init_db(const char *template_dir, unsigned int flags);\n+extern int init_db(const char *git_dir, const char *template_dir, unsigned int flags);\n \n #define alloc_nr(x) (((x)+16)*3/2)\n \n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136592","messageId":"1268313754-28179-10-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 09/16] git_config(): do not read .git/config if there is no repository","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:27Z","receivedAt":"2010-03-11T13:22:27Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"If no repository is found, do not bother calling git_pathdup(). If a\ncommand forgets to call setup_git_directory*() or enter_repo(),\n$GIT_DIR/config will be missed, though.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n config.c               |    3 ++-\n t/t1300-repo-config.sh |   13 +++++++++++++\n 2 files changed, 15 insertions(+), 1 deletions(-)\n\ndiff --git a/config.c b/config.c\nindex 2d38096..07d854a 100644\n--- a/config.c\n+++ b/config.c\n@@ -737,7 +737,8 @@ int git_config(config_fn_t fn, void *data)\n \tchar *repo_config = NULL;\n \tint ret;\n \n-\trepo_config = git_pathdup(\"config\");\n+\tif (!startup_info || startup_info->have_repository)\n+\t\trepo_config = git_pathdup(\"config\");\n \tret = git_config_early(fn, data, repo_config);\n \tif (repo_config)\n \t\tfree(repo_config);\ndiff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh\nindex f11f98c..cfb70e2 100755\n--- a/t/t1300-repo-config.sh\n+++ b/t/t1300-repo-config.sh\n@@ -824,4 +824,17 @@ test_expect_success 'check split_cmdline return' \"\n \ttest_must_fail git merge master\n \t\"\n \n+test_expect_success 'skip .git/config if there is no repository' '\n+\t(\n+\t\tmkdir -p a/b/.git &&\n+\t\tcd a &&\n+\t\tGIT_CEILING_DIRECTORIES=\"`pwd`\" &&\n+\t\texport GIT_CEILING_DIRECTORIES &&\n+\t\tcd b &&\n+\t\techo \"[core]\" > .git/config &&\n+\t\techo \"wrong = true\" >> .git/config &&\n+\t\ttest -z \"$(git var -l | grep core.wrong)\"\n+\t)\n+'\n+\n test_done\n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136594","messageId":"1268313754-28179-11-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 10/16] Do not read .git/info/exclude if there is no repository","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:28Z","receivedAt":"2010-03-11T13:22:28Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n dir.c           |    8 +++++---\n t/t7002-grep.sh |   24 ++++++++++++++++++++++++\n 2 files changed, 29 insertions(+), 3 deletions(-)\n\ndiff --git a/dir.c b/dir.c\nindex 133c333..fee19fe 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -1024,9 +1024,11 @@ void setup_standard_excludes(struct dir_struct *dir)\n \tconst char *path;\n \n \tdir->exclude_per_dir = \".gitignore\";\n-\tpath = git_path(\"info/exclude\");\n-\tif (!access(path, R_OK))\n-\t\tadd_excludes_from_file(dir, path);\n+\tif (!startup_info || startup_info->have_repository) {\n+\t\tpath = git_path(\"info/exclude\");\n+\t\tif (!access(path, R_OK))\n+\t\t\tadd_excludes_from_file(dir, path);\n+\t}\n \tif (excludes_file && !access(excludes_file, R_OK))\n \t\tadd_excludes_from_file(dir, excludes_file);\n }\ndiff --git a/t/t7002-grep.sh b/t/t7002-grep.sh\nindex e249c3e..06ec4cb 100755\n--- a/t/t7002-grep.sh\n+++ b/t/t7002-grep.sh\n@@ -527,4 +527,28 @@ test_expect_success 'grep -e -- -- path' '\n \ttest_cmp expected actual\n '\n \n+test_expect_success 'Setup fake .git' '\n+\tcd t &&\n+\tGIT_CEILING_DIRECTORIES=\"`pwd`\" &&\n+\texport GIT_CEILING_DIRECTORIES &&\n+\tcd a &&\n+\tmkdir -p .git/info &&\n+\tcd ../..\n+\n+'\n+\n+test_expect_success 'Ignore fake .git/info/exclude' '\n+\t(\n+\t\tcd t/a &&\n+\t\techo v > .git/info/exclude &&\n+\t\tgit grep --no-index vvv . &&\n+\t\trm .git/info/exclude\n+\t)\n+'\n+\n+test_expect_success 'Unsetup fake .git' '\n+\trm -rf t/a &&\n+\tunset GIT_CEILING_DIRECTORIES\n+'\n+\n test_done\n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136595","messageId":"1268313754-28179-12-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 11/16] Do not read .git/info/attributes if there is no repository","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:29Z","receivedAt":"2010-03-11T13:22:29Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"This one is difficult to demonstrate by tests. So I'll describe the\ncall flow instead.\n\nSay we do \"git grep --no-index --show-function foo -- bar\" in no\nrepository. The call flow would be:\n - ...\n - cmd_grep\n - grep_directory\n - grep_file\n - grep_buffer\n - grep_buffer_1\n - userdiff_find_by_path\n - git_checkattr(\"diff\")\n - bootstrap_attr_stack\n - git_path(\"info/attributes\")\n\nBecause no repository is found, git_dir in environment.c would be\nNULL. When it reaches git_path(\"info/attributes\"), it will\nautomatically set git_dir to \".git\". If we have $(cwd)/.git/info/attributes\nthen that file will be read, no matter $(cwd)/.git is a valid\nrepository.\n\nThis bug is hard to be exposed, because after git_checkattr(\"diff\")\nfinds something (wrong). It will look up for diff drivers from\n.git/config. If we do thing correctly, .git/config will not be read,\nthus no diff drivers, no visible impact. If .git/config is read,\ngit_dir must be set already, all this would not happen.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n attr.c |    5 ++++-\n 1 files changed, 4 insertions(+), 1 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex f5346ed..f1362de 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -480,7 +480,10 @@ static void bootstrap_attr_stack(void)\n \t\t\tdebug_push(elem);\n \t\t}\n \n-\t\telem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);\n+\t\tif (!startup_info || startup_info->have_repository)\n+\t\t\telem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);\n+\t\telse\n+\t\t\telem = NULL;\n \t\tif (!elem)\n \t\t\telem = xcalloc(1, sizeof(*elem));\n \t\telem->origin = NULL;\n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136596","messageId":"1268313754-28179-13-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 12/16] apply: do not check sha1 when repository has not been found","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:30Z","receivedAt":"2010-03-11T13:22:30Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/apply.c |    2 +-\n 1 files changed, 1 insertions(+), 1 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex d27aac6..ea7bf57 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -2441,7 +2441,7 @@ static int apply_binary(struct image *img, struct patch *patch)\n \t\treturn 0; /* deletion patch */\n \t}\n \n-\tif (has_sha1_file(sha1)) {\n+\tif (startup_info->have_repository && has_sha1_file(sha1)) {\n \t\t/* We already have the postimage */\n \t\tenum object_type type;\n \t\tunsigned long size;\n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136597","messageId":"1268313754-28179-14-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 13/16] config: do not read .git/config if there is no repository","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:31Z","receivedAt":"2010-03-11T13:22:31Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/config.c        |    9 ++++++---\n t/t1300-repo-config.sh  |    3 ++-\n t/t1302-repo-version.sh |    2 +-\n 3 files changed, 9 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/config.c b/builtin/config.c\nindex ecc8f87..3fca3b4 100644\n--- a/builtin/config.c\n+++ b/builtin/config.c\n@@ -159,7 +159,8 @@ static int get_value(const char *key_, const char *regex_)\n \tlocal = config_exclusive_filename;\n \tif (!local) {\n \t\tconst char *home = getenv(\"HOME\");\n-\t\tlocal = repo_config = git_pathdup(\"config\");\n+\t\tif (startup_info->have_repository)\n+\t\t\tlocal = repo_config = git_pathdup(\"config\");\n \t\tif (git_config_global() && home)\n \t\t\tglobal = xstrdup(mkpath(\"%s/.gitconfig\", home));\n \t\tif (git_config_system())\n@@ -197,7 +198,8 @@ static int get_value(const char *key_, const char *regex_)\n \t\tgit_config_from_file(show_config, system_wide, NULL);\n \tif (do_all && global)\n \t\tgit_config_from_file(show_config, global, NULL);\n-\tgit_config_from_file(show_config, local, NULL);\n+\tif (local)\n+\t\tgit_config_from_file(show_config, local, NULL);\n \tif (!do_all && !seen && global)\n \t\tgit_config_from_file(show_config, global, NULL);\n \tif (!do_all && !seen && system_wide)\n@@ -215,7 +217,8 @@ static int get_value(const char *key_, const char *regex_)\n \t\tret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;\n \n free_strings:\n-\tfree(repo_config);\n+\tif (repo_config)\n+\t\tfree(repo_config);\n \tfree(global);\n \treturn ret;\n }\ndiff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh\nindex cfb70e2..b040f72 100755\n--- a/t/t1300-repo-config.sh\n+++ b/t/t1300-repo-config.sh\n@@ -833,7 +833,8 @@ test_expect_success 'skip .git/config if there is no repository' '\n \t\tcd b &&\n \t\techo \"[core]\" > .git/config &&\n \t\techo \"wrong = true\" >> .git/config &&\n-\t\ttest -z \"$(git var -l | grep core.wrong)\"\n+\t\ttest -z \"$(git var -l | grep core.wrong)\" &&\n+\t\ttest -z \"$(git config --bool core.wrong)\"\n \t)\n '\n \ndiff --git a/t/t1302-repo-version.sh b/t/t1302-repo-version.sh\nindex 8d305b4..74bf51f 100755\n--- a/t/t1302-repo-version.sh\n+++ b/t/t1302-repo-version.sh\n@@ -29,7 +29,7 @@ test_expect_success 'gitdir selection on normal repos' '\n # Make sure it would stop at test2, not trash\n test_expect_success 'gitdir selection on unsupported repo' '\n \t(cd test2 &&\n-\ttest \"$(git config core.repositoryformatversion)\" = 99)'\n+\ttest \"$(git config --file=.git/config core.repositoryformatversion)\" = 99)'\n \n test_expect_success 'gitdir not required mode' '\n \t(git apply --stat test.patch &&\n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136598","messageId":"1268313754-28179-15-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 14/16] Allow to undo setup_git_directory_gently() gracefully (and fix alias code)","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:32Z","receivedAt":"2010-03-11T13:22:32Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"unset_git_directory() can only clean up things as long as\nset_git_dir() has not been called because set_git_dir() keeps internal\nstate itself. Even worse, set_git_dir() may override $GIT_DIR env\nvariable. All those are now handled by unset_git_env().\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n cache.h       |    1 +\n environment.c |   20 ++++++++++++++++++++\n git.c         |   11 +++++------\n setup.c       |    2 ++\n 4 files changed, 28 insertions(+), 6 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex dd27e11..bd9df24 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -419,6 +419,7 @@ extern void setup_work_tree(void);\n extern const char *setup_git_directory_gently(int *);\n extern const char *setup_git_directory(void);\n extern void unset_git_directory(const char *prefix);\n+extern void unset_git_env();\n extern const char *prefix_path(const char *prefix, int len, const char *path);\n extern const char *prefix_filename(const char *prefix, int len, const char *path);\n extern int check_filename(const char *prefix, const char *name);\ndiff --git a/environment.c b/environment.c\nindex c36c902..6127025 100644\n--- a/environment.c\n+++ b/environment.c\n@@ -62,6 +62,7 @@ char *git_work_tree_cfg;\n static char *work_tree;\n \n static const char *git_dir;\n+static const char *original_git_dir;\n static char *git_object_dir, *git_index_file, *git_refs_dir, *git_graft_file;\n \n /*\n@@ -81,6 +82,20 @@ const char * const local_repo_env[LOCAL_REPO_ENV_SIZE + 1] = {\n \tNULL\n };\n \n+void unset_git_env(void)\n+{\n+\tgit_dir = NULL;\n+\tif (original_git_dir)\n+\t\tsetenv(GIT_DIR_ENVIRONMENT, original_git_dir, 1);\n+\telse\n+\t\tunsetenv(GIT_DIR_ENVIRONMENT);\n+\tgit_object_dir = NULL;\n+\tgit_refs_dir = NULL;\n+\tgit_index_file = NULL;\n+\tgit_graft_file = NULL;\n+\tread_replace_refs = 1;\n+}\n+\n static void setup_git_env(void)\n {\n \tgit_dir = getenv(GIT_DIR_ENVIRONMENT);\n@@ -184,6 +199,11 @@ char *get_graft_file(void)\n \n int set_git_dir(const char *path)\n {\n+\tstatic int original_git_dir_set = 0;\n+\tif (!original_git_dir_set) {\n+\t\toriginal_git_dir = getenv(GIT_DIR_ENVIRONMENT);\n+\t\toriginal_git_dir_set = 1;\n+\t}\n \tif (setenv(GIT_DIR_ENVIRONMENT, path, 1))\n \t\treturn error(\"Could not set GIT_DIR to '%s'\", path);\n \tsetup_git_env();\ndiff --git a/git.c b/git.c\nindex 746470f..6a40304 100644\n--- a/git.c\n+++ b/git.c\n@@ -146,14 +146,13 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)\n static int handle_alias(int *argcp, const char ***argv)\n {\n \tint envchanged = 0, ret = 0, saved_errno = errno;\n-\tconst char *subdir;\n \tint count, option_count;\n \tconst char **new_argv;\n \tconst char *alias_command;\n \tchar *alias_string;\n \tint unused_nongit;\n \n-\tsubdir = setup_git_directory_gently(&unused_nongit);\n+\tsetup_git_directory_gently(&unused_nongit);\n \n \talias_command = (*argv)[0];\n \talias_string = alias_lookup(alias_command);\n@@ -210,8 +209,7 @@ static int handle_alias(int *argcp, const char ***argv)\n \t\tret = 1;\n \t}\n \n-\tif (subdir && chdir(subdir))\n-\t\tdie_errno(\"Cannot change to '%s'\", subdir);\n+\tunset_git_directory(startup_info->prefix);\n \n \terrno = saved_errno;\n \n@@ -240,8 +238,6 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)\n \tint status, help;\n \tstruct stat st;\n \n-\tmemset(&git_startup_info, 0, sizeof(git_startup_info));\n-\tstartup_info = &git_startup_info;\n \thelp = argc == 2 && !strcmp(argv[1], \"-h\");\n \tif (!help) {\n \t\tif (p->option & RUN_SETUP)\n@@ -486,6 +482,9 @@ int main(int argc, const char **argv)\n {\n \tconst char *cmd;\n \n+\tmemset(&git_startup_info, 0, sizeof(git_startup_info));\n+\tstartup_info = &git_startup_info;\n+\n \tcmd = git_extract_argv0_path(argv[0]);\n \tif (!cmd)\n \t\tcmd = \"git-help\";\ndiff --git a/setup.c b/setup.c\nindex 2f850ab..3264187 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -329,6 +329,8 @@ void unset_git_directory(const char *prefix)\n \t\tdie(\"Cannot change to '%s'\", prefix);\n \n \tif (startup_info) {\n+\t\tif (startup_info->have_repository)\n+\t\t\tunset_git_env();\n \t\tstartup_info->prefix = NULL;\n \t\tstartup_info->have_repository = 0;\n \t}\n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136599","messageId":"1268313754-28179-16-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 15/16] alias: keep repository found while collecting aliases as long as possible","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:33Z","receivedAt":"2010-03-11T13:22:33Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"In order to read aliases, $GIT_CONFIG/config must be read (if found).\nCurrently, after alias handling is done, we chdir() back to original\ncwd, pretending no repository setup is done.\n\nGiven plain chdir() is not a proper way to undo\nsetup_git_directory_gently(), and 80% builtin commands will need to\nsearch for a repository, we could keep the repository found in alias\nhandling code. Until we are clear, there's no need for a repository,\nthen we can undo the setup with unset_git_directory().\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n git.c |   15 ++++++++++-----\n 1 files changed, 10 insertions(+), 5 deletions(-)\n\ndiff --git a/git.c b/git.c\nindex 6a40304..aa6d029 100644\n--- a/git.c\n+++ b/git.c\n@@ -152,7 +152,8 @@ static int handle_alias(int *argcp, const char ***argv)\n \tchar *alias_string;\n \tint unused_nongit;\n \n-\tsetup_git_directory_gently(&unused_nongit);\n+\tif (!startup_info->have_repository)\n+\t\tsetup_git_directory_gently(&unused_nongit);\n \n \talias_command = (*argv)[0];\n \talias_string = alias_lookup(alias_command);\n@@ -209,8 +210,6 @@ static int handle_alias(int *argcp, const char ***argv)\n \t\tret = 1;\n \t}\n \n-\tunset_git_directory(startup_info->prefix);\n-\n \terrno = saved_errno;\n \n \treturn ret;\n@@ -240,12 +239,18 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)\n \n \thelp = argc == 2 && !strcmp(argv[1], \"-h\");\n \tif (!help) {\n-\t\tif (p->option & RUN_SETUP)\n+\t\tif ((p->option & RUN_SETUP) && !startup_info->have_repository)\n \t\t\tsetup_git_directory();\n-\t\tif (p->option & RUN_SETUP_GENTLY) {\n+\t\telse if ((p->option & RUN_SETUP_GENTLY) && !startup_info->have_repository) {\n \t\t\tint nongit_ok;\n \t\t\tsetup_git_directory_gently(&nongit_ok);\n \t\t}\n+\t\telse if (startup_info->have_repository) {\n+\t\t\tif (p->option & (RUN_SETUP_GENTLY | RUN_SETUP))\n+\t\t\t\t; /* done already */\n+\t\t\telse\n+\t\t\t\tunset_git_directory(startup_info->prefix);\n+\t\t}\n \n \t\tif (use_pager == -1 && p->option & RUN_SETUP)\n \t\t\tuse_pager = check_pager_config(p->cmd);\n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136600","messageId":"1268313754-28179-17-git-send-email-pclouds@gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 16/16] Guard unallowed access to repository when it's not set up","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-11T13:22:34Z","receivedAt":"2010-03-11T13:22:34Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Many code path will skip repo access if startup_info->have_repository\nis false. This may be a fault if startup_info->have_repository has not\nbeen properly initialized.\n\nSo the rule is one of the following commands must be run before any\nrepo access. And none of them can be called twice.\n\n - setup_git_directory*\n - enter_repo\n - init_db\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/init-db.c |    1 +\n cache.h           |    1 +\n config.c          |    2 ++\n environment.c     |   13 +++++++++++--\n setup.c           |   13 +++++++++++++\n 5 files changed, 28 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/init-db.c b/builtin/init-db.c\nindex 064b919..d4c415c 100644\n--- a/builtin/init-db.c\n+++ b/builtin/init-db.c\n@@ -302,6 +302,7 @@ int init_db(const char *git_dir, const char *template_dir, unsigned int flags)\n \n \tset_git_dir(make_absolute_path(git_dir));\n \tstartup_info->have_repository = 1;\n+\tstartup_info->have_run_setup_gitdir = 1;\n \n \tsafe_create_dir(get_git_dir(), 0);\n \ndiff --git a/cache.h b/cache.h\nindex bd9df24..1a6ae8c 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1060,6 +1060,7 @@ int split_cmdline(char *cmdline, const char ***argv);\n /* git.c */\n struct startup_info {\n \tconst char *prefix;\n+\tint have_run_setup_gitdir;\n \tint have_repository;\n };\n extern struct startup_info *startup_info;\ndiff --git a/config.c b/config.c\nindex 07d854a..9981b09 100644\n--- a/config.c\n+++ b/config.c\n@@ -737,6 +737,8 @@ int git_config(config_fn_t fn, void *data)\n \tchar *repo_config = NULL;\n \tint ret;\n \n+\tif (startup_info && !startup_info->have_run_setup_gitdir)\n+\t\tdie(\"internal error: access to .git/config without repo setup\");\n \tif (!startup_info || startup_info->have_repository)\n \t\trepo_config = git_pathdup(\"config\");\n \tret = git_config_early(fn, data, repo_config);\ndiff --git a/environment.c b/environment.c\nindex 6127025..17f0cbe 100644\n--- a/environment.c\n+++ b/environment.c\n@@ -98,9 +98,18 @@ void unset_git_env(void)\n \n static void setup_git_env(void)\n {\n+\tif (startup_info && startup_info->have_run_setup_gitdir)\n+\t\tdie(\"internal error: setup_git_env can't be called twice\");\n \tgit_dir = getenv(GIT_DIR_ENVIRONMENT);\n-\tif (!git_dir)\n-\t\tgit_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);\n+\tif (!git_dir) {\n+\t\t/*\n+\t\t * Repo detection should be done by setup_git_directory*\n+\t\t * or enter_repo, not by this function\n+\t\t */\n+\t\t if (startup_info)\n+\t\t\t die(\"internal error: $GIT_DIR is empty\");\n+\t\t git_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);\n+\t}\n \tif (!git_dir)\n \t\tgit_dir = DEFAULT_GIT_DIR_ENVIRONMENT;\n \tgit_object_dir = getenv(DB_ENVIRONMENT);\ndiff --git a/setup.c b/setup.c\nindex 3264187..96af5e3 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -237,7 +237,17 @@ void setup_work_tree(void)\n \t\tgit_dir = make_absolute_path(git_dir);\n \tif (!work_tree || chdir(work_tree))\n \t\tdie(\"This operation must be run in a work tree\");\n+\n+\t/*\n+\t * have_run_setup_gitdir is unset in order to avoid die()ing\n+\t * inside set_git_env(). We don't actually initialize\n+\t * repo twice, we're just relative-izing gitdir\n+\t */\n+\tif (startup_info)\n+\t\tstartup_info->have_run_setup_gitdir = 0;\n \tset_git_dir(make_relative_path(git_dir, work_tree));\n+\tif (startup_info)\n+\t\tstartup_info->have_run_setup_gitdir = 1;\n \tinitialized = 1;\n }\n \n@@ -333,6 +343,7 @@ void unset_git_directory(const char *prefix)\n \t\t\tunset_git_env();\n \t\tstartup_info->prefix = NULL;\n \t\tstartup_info->have_repository = 0;\n+\t\tstartup_info->have_run_setup_gitdir = 0;\n \t}\n \n \t/* Initialized in setup_git_directory_gently_1() */\n@@ -499,6 +510,7 @@ const char *setup_git_directory_gently(int *nongit_ok)\n \tprefix = setup_git_directory_gently_1(nongit_ok);\n \tif (startup_info) {\n \t\tstartup_info->prefix = prefix;\n+\t\tstartup_info->have_run_setup_gitdir = 1;\n \t\tstartup_info->have_repository = !nongit_ok || !*nongit_ok;\n \t}\n \treturn prefix;\n@@ -593,6 +605,7 @@ char *enter_repo(char *path, int strict)\n \t\tset_git_dir(\".\");\n \t\tif (startup_info) {\n \t\t\tstartup_info->prefix = NULL;\n+\t\t\tstartup_info->have_run_setup_gitdir = 1;\n \t\t\tstartup_info->have_repository = 1;\n \t\t}\n \t\treturn path;\n-- \n1.7.0.1.384.g6abcaa\n"},{"id":"136623","messageId":"7vljdyy2kh.fsf@alter.siamese.dyndns.org","threadId":"22985","inReplyTo":"1268313754-28179-5-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH 04/16] worktree setup: call set_git_dir explicitly","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-03-11T21:24:14Z","receivedAt":"2010-03-11T21:24:14Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:\n\n> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n> ---\n>  setup.c |   11 ++++++++---\n>  1 files changed, 8 insertions(+), 3 deletions(-)\n>\n> diff --git a/setup.c b/setup.c\n> index e067292..43a8609 100644\n> --- a/setup.c\n> +++ b/setup.c\n> @@ -350,14 +350,17 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n>  \t\t\t\t/* config may override worktree */\n>  \t\t\t\tif (check_repository_format_gently(nongit_ok))\n>  \t\t\t\t\treturn NULL;\n> +\t\t\t\tset_git_dir(gitdirenv);\n>  \t\t\t\treturn retval;\n>  \t\t\t}\n>  \t\t\tif (check_repository_format_gently(nongit_ok))\n>  \t\t\t\treturn NULL;\n>  \t\t\tretval = get_relative_cwd(buffer, sizeof(buffer) - 1,\n>  \t\t\t\t\tget_git_work_tree());\n> -\t\t\tif (!retval || !*retval)\n> +\t\t\tif (!retval || !*retval) {\n> +\t\t\t\tset_git_dir(gitdirenv);\n>  \t\t\t\treturn NULL;\n> +\t\t\t}\n>  \t\t\tset_git_dir(make_absolute_path(gitdirenv));\n>  \t\t\tif (chdir(work_tree_env) < 0)\n>  \t\t\t\tdie_errno (\"Could not chdir to '%s'\", work_tree_env);\n> @@ -392,8 +395,10 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n>  \toffset = len = strlen(cwd);\n>  \tfor (;;) {\n>  \t\tgitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);\n> -\t\tif (gitfile_dir || is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) {\n> -\t\t\tif (gitfile_dir && set_git_dir(gitfile_dir))\n> +\t\tif (!gitfile_dir && is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))\n> +\t\t\tgitfile_dir = DEFAULT_GIT_DIR_ENVIRONMENT;\n> +\t\tif (gitfile_dir) {\n> +\t\t\tif (set_git_dir(gitfile_dir))\n>  \t\t\t\tdie(\"Repository setup failed\");\n>  \t\t\tinside_git_dir = 0;\n>  \t\t\tif (!work_tree_env)\n\n\nYes, you have more calls to set_git_dir() than before.  But it is not\nexplained why \"calling set_git_dir explicitly\" is a good thing anywhere in\nthe series.\n\nCan you write down the set of rules for your setup sequence, the goal\nafter everything from this 37-patch series is applied?  Something along\nthe lines of (just illustrating the kinds of things to be described):\n\n - Definition.\n\n   The following state variables belong to the setup system:\n\n   - git_dir: holds the location of $GIT_DIR as a path relative to cwd\n   - is_bare_repository(): returns foo;\n   - is_inside_working_tree(): returns bar;\n   - ...\n\n - Rule for the callers of the setup system:\n\n   Once main() starts, this and that needs to be called in this order\n   before trying to access any of the above state.  Specifically, the\n   following call could access state variables, and should not be called\n   before this set-up is done:\n\n    - git_config(): needs to know where git_dir is;\n    - setup_revisions(), parse_options(), ...: needs to know the prefix;\n    - ...\n\n - Rule for the implementation of the setup system:\n\n   Upon the first call the caller makes into the setup system:\n\n   1. If the caller does not care about being in a git repository,\n      skip everything up to step #n.\n\n   2. First inspect GIT_DIR; if set, go to step #5.\n\n   3. Otherwise try to find GIT_DIR by checking .git or \".\" is a git\n      directory; repeat this step by checking one directory closer to the\n      root level until GIT_DIR is found or CEILING is reached.\n\n   4. If there is no GIT_DIR, then chdir back to the original location,\n      and skip everything up to step #m.\n\n   5. As a side effect of finding the GIT_DIR, the configuration file is\n      read from there, and we will know the value of core.worktree.\n\n   6. Inspect GIT_WORK_TREE; if set then git_work_tree is known.\n      Otherwise, if core.worktree is found in step #5, that is the value\n      of git_work_tree.  Otherwise determine git_work_tree in this\n      fashon. ...\n\n   ...\n\n\nWithout seeing such a design that you can clearly explain to others, the\nseries looks nothing more than a sequence of \"I noticed this so am\napplying this random patch\" and is impossible to review.\n"},{"id":"136633","messageId":"fcaeb9bf1003111935k7719c2a8r3e925f8ad2c12276@mail.gmail.com","threadId":"22985","inReplyTo":"1268313754-28179-7-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH 06/16] Use git_config_early() instead of git_config() during repo setup","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-12T03:35:25Z","receivedAt":"2010-03-12T03:35:25Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"2010/3/11 Nguyễn Thái Ngọc Duy <pclouds@gmail.com>:\n> @@ -411,6 +425,8 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n>                        inside_git_dir = 1;\n>                        if (!work_tree_env)\n>                                inside_work_tree = 0;\n> +                       if (check_repository_format_gently(gitfile_dir, nongit_ok))\n> +                               return NULL;\n>                        if (offset != len) {\n>                                root_len = offset_1st_component(cwd);\n>                                cwd[offset > root_len ? offset : root_len] = '\\0';\n\nSomething went wrong in rebases and conflicts. gitfile_dir in the\nabove call should be replaced by \".\"\n\nif (check_repository_format_gently(\".\", nongit_ok))\n-- \nDuy\n"},{"id":"137316","messageId":"fcaeb9bf1003200110w721903e7v7a5823cb312cbc71@mail.gmail.com","threadId":"22985","inReplyTo":"fcaeb9bf1003111645p54f42aaetbb622f8bde0ec8ad@mail.gmail.com","subject":"Re: [PATCH 04/16] worktree setup: call set_git_dir explicitly","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-20T08:10:46Z","receivedAt":"2010-03-20T08:10:46Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Hmm.. I did not notice I did not sent this to git@vger.\n\nOn 3/12/10, Nguyen Thai Ngoc Duy <pclouds@gmail.com> wrote:\n> On 3/12/10, Junio C Hamano <gitster@pobox.com> wrote:\n>  > Yes, you have more calls to set_git_dir() than before.  But it is not\n>  >  explained why \"calling set_git_dir explicitly\" is a good thing anywhere in\n>  >  the series.\n>\n>\n> OK. Let me try in mail first.\n>\n>  Goal: do not rely on setup_git_env() to set git_dir to \".git\". That\n>  means set_git_dir() explicitly.\n>\n>\n>  >   - Definition.\n>  >\n>  >    The following state variables belong to the setup system:\n>  >\n>  >    - git_dir: holds the location of $GIT_DIR as a path relative to cwd\n>  >    - is_bare_repository(): returns foo;\n>  >    - is_inside_working_tree(): returns bar;\n>  >    - ...\n>\n>\n> Best described in 7/16 and 14/16, function unset_git_directory() and\n>  unset_git_env():\n>\n>   - git_dir and other git_*_dir in environment.c for object store path...\n>   - shared_repository, is_bare_repository_cfg, git_work_tree_cfg,\n>  repository_format:  initial configuration used during setup.\n>   - is_inside_work_tree: whether cwd is inside a working directory\n>   - is_inside_git_dir: whether cwd is inside $GIT_DIR\n>   - startup_info->prefix: relative path from new cwd to original cwd\n>  when main() is called\n>   - startup_info->have_repository: whether a repository is found by setup system\n>   - current working directory: directory base for git_dir, work_tree\n>  and stuff if they are relative\n>\n>\n>  >   - Rule for the callers of the setup system:\n>\n>\n> Once main() starts. startup_info should be pointed to a struct. This\n>  struct will be used by various part of libgit. If startup_info is\n>  NULL, everything works like before.\n>\n>  Since startup_info initialization until calling setup functions,\n>  access to repository will not be allowed, patch 16/16. \"access to\n>  repository\" is everything that calls git_path() and friends.\n>\n>  One of the setup functions is called. These functions are\n>  setup_git_directory*, enter_repo() or init_db(). Only one setup call\n>  can be made for the rest of program's life time. unset_git_directory()\n>  can be called to undo setup and allow setup functions to be called\n>  once more. But it should be avoided.\n>\n>  If setup system fails to find a useable repository, all involved\n>  states are restored. Refer to definition part for those states.\n>\n>  After setup function is called:\n>\n>   - git_path() and friends are allowed if startup_info->have_repository is true.\n>\n>   - Current directory directory may be moved. Programs need to be aware\n>  of \"prefix\" (**) to calculate original cwd.\n>\n>   - Changing cwd is not allowed, as some directory settings may be\n>  relative to cwd.\n>\n>   - The current working directory may or may not be at worktree's top\n>  directory (*). If a command needs worktree, it must call\n>  setup_work_tree() to move cwd (and adjust prefix along the way).\n>  enter_repo() will never set worktree.\n>\n>  (*) setup_git_directory() does the setup_work_tree()-equivalent part\n>  automatically. Users of setup_git_directory_gently() must call\n>  setup_work_tree().\n>\n>  (**) The implicit rule of prefix is, it does not contain any \"../\". So\n>  if original cwd is outside worktree, using this prefix alone is not\n>  enough to get files from command line arguments.\n>\n>\n>  >   - Rule for the implementation of the setup system:\n>  >\n>  >    Upon the first call the caller makes into the setup system:\n>\n>\n> This is hard. Let's see. Prerequisites for entering setup system:\n>\n>   - No setup function has been called.\n>\n>  The main rule is, when a repo candidate is found, these must be done in order:\n>   - setup is_inside_work_tree and is_inside_git_dir\n>   - check_repo_format_gently(), if fails, call unset_git_dir and finish.\n>   - set_git_dir()\n>   - calculate prefix, then finish.\n>\n>  Setup procedure:\n>\n>   1. Check for GIT_DIR and GIT_WORK_TREE environment variables, if these are set:\n>    1.1. if GIT_WORK_TREE is not set (only GIT_DIR is set), make cwd\n>  GIT_WORK_TREE\n>    1.2. adjust is_inside_work_tree and is_inside_git_dir accordingly\n>    1.3. check repository config for repo format version, if\n>  incompatible format is found, roll back using unset_git_directory and\n>  return.\n>    1.4. set_git_dir() to save GIT_DIR (this is irreversible, until patch 14/16)\n>    1.5. calculate prefix, set startup_info->have_repository, finish.\n>\n>  2. Get current working directory, check whether $(cwd)/.git or $(cwd)\n>  looks like a repository, going up until GIT_CEILING_DIRECTORIES (or /)\n>  is reached, then finish.\n>\n>    2.1. If one dir looks like a repository:\n>       2.1.1. adjust is_inside_work_tree and is_inside_git_dir accordingly\n>       2.1.2. check repository config for repo format version, if\n>  incompatible format is found, roll back using unset_git_directory and\n>  return.\n>       2.1.3. set_git_dir() to save GIT_DIR\n>       2.1.4. calculate prefix, set ->have_repository, finish.\n>\n>    2.2. If no repository is found and GIT_CEILING_DIRECTORIES is\n>  reached, moving back to original cwd, finish.\n>\n>  During setup procedure, normal access to repository is not allowed.\n>  Though in theory, it could be allowed after set_git_dir() step.\n>   --\n>\n> Duy\n>\n\n\n-- \nDuy\n"}]}