{"thread":{"id":"23117","subject":"[PATCH v2 00/19] nd/setup part two, second round","startedAt":"2010-03-21T10:30:27Z","lastAt":"2010-03-21T10:30:46Z","messageCount":20,"participants":["Nguyễn Thái Ngọc Duy"],"isPatch":true,"patchVersion":2,"patchTotal":19},"messages":[{"id":"137415","messageId":"1269167446-7799-1-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":null,"subject":"[PATCH v2 00/19] nd/setup part two, second round","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-21T10:30:27Z","receivedAt":"2010-03-21T10:30:27Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Again, this series is to make repository setup more predictable, i.e. must be done\nin either setup_git_dir*, enter_repo or init_db. Compared to the first version [1]:\n\n - More detail in commit messages, mostly extracted from [2]\n - Fix \"git foo -h\" code path (that skips repo setup) in patches 14-16\n\n[1] http://mid.gmane.org/1268313754-28179-1-git-send-email-pclouds@gmail.com\n[2] http://mid.gmane.org/fcaeb9bf1003200110w721903e7v7a5823cb312cbc71@mail.gmail.com\n\nNguyễn Thái Ngọc Duy (19):\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  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  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  run_builtin(): save \"-h\" detection result for later use\n  builtins: utilize startup_info->help where possible\n  builtins: check for startup_info->help, print and exit early\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  Guard unallowed access to repository when it's not set up\n\n attr.c                     |    5 +-\n builtin/apply.c            |    2 +-\n builtin/branch.c           |    3 +\n builtin/check-ref-format.c |    2 +-\n builtin/checkout-index.c   |    3 +\n builtin/clone.c            |    3 +-\n builtin/commit.c           |    6 ++\n builtin/config.c           |    9 ++-\n builtin/gc.c               |    3 +\n builtin/grep.c             |    2 +-\n builtin/index-pack.c       |    2 +-\n builtin/init-db.c          |   10 ++-\n builtin/log.c              |    7 +-\n builtin/ls-files.c         |    3 +\n builtin/merge-ours.c       |    2 +-\n builtin/merge.c            |    3 +\n builtin/pack-redundant.c   |    2 +-\n builtin/rev-parse.c        |    8 ++\n builtin/show-ref.c         |    2 +-\n builtin/update-index.c     |    3 +\n builtin/upload-archive.c   |    7 +-\n cache.h                    |    7 ++-\n config.c                   |   22 ++++-\n dir.c                      |    8 +-\n environment.c              |   33 +++++++-\n git.c                      |   33 +++++---\n path.c                     |   91 ---------------------\n setup.c                    |  191 +++++++++++++++++++++++++++++++++++++++++---\n t/t1300-repo-config.sh     |   14 +++\n t/t1302-repo-version.sh    |    2 +-\n t/t7002-grep.sh            |   24 ++++++\n 31 files changed, 360 insertions(+), 152 deletions(-)\n"},{"id":"137416","messageId":"1269167446-7799-2-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 01/19] Move enter_repo() to setup.c","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-21T10:30:28Z","receivedAt":"2010-03-21T10:30:28Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"enter_repo() is to set up a repository, it fits better in setup.c\n\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.2.425.gb99f1\n"},{"id":"137417","messageId":"1269167446-7799-3-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 02/19] 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-21T10:30:29Z","receivedAt":"2010-03-21T10:30:29Z","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.2.425.gb99f1\n"},{"id":"137418","messageId":"1269167446-7799-4-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 03/19] rev-parse --git-dir: print relative gitdir correctly","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-21T10:30:30Z","receivedAt":"2010-03-21T10:30:30Z","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.2.425.gb99f1\n"},{"id":"137421","messageId":"1269167446-7799-5-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 04/19] worktree setup: call set_git_dir explicitly","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-21T10:30:31Z","receivedAt":"2010-03-21T10:30:31Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Currently setup_git_env() can automatically set git directory to\n\".git\", relative to current working directory, no matter where it\nis. Any access to repository may call setup_git_env() automatically if\nthere is no repository found yet. This leads to obscure faults where\nwe don't expect a repository setup to happen.\n\nThe ultimate goal (**) is:\n\n - Git repository will be searched and set up by either one of these\n   functions (and no other functions):\n\n   + setup_git_directory_gently()\n   + setup_git_directory()\n   + enter_repo()\n   + init_db()\n\n - Repository setup can't be called more than once. If it is really\n   needed, then un-setup the previous repo before moving on with\n   another one.\n\n - Any attempts to access repository when it's not found/set up should\n   be caught.\n\nThis patch is the first step to accomplish this goal. It ensures that\nafter the \"setup function\" (mentioned above) is called, if a\nrepository is found, then setup_git_env() must be called already.\n\nThe two other cases (no setup function called,\nsetup_git_directory_gently() called but no repo found) will be covered\nby the next patches, to ensure setup_git_env() will not be triggered.\n\n(**) This only applies to builtin commands, for now.\n\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.2.425.gb99f1\n"},{"id":"137419","messageId":"1269167446-7799-6-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 05/19] Add git_config_early()","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-21T10:30:32Z","receivedAt":"2010-03-21T10:30:32Z","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.2.425.gb99f1\n"},{"id":"137420","messageId":"1269167446-7799-7-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 06/19] 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-21T10:30:33Z","receivedAt":"2010-03-21T10:30:33Z","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..2c1b64f 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(\".\", 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.2.425.gb99f1\n"},{"id":"137422","messageId":"1269167446-7799-8-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 07/19] worktree setup: restore original state when things go wrong","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-21T10:30:34Z","receivedAt":"2010-03-21T10:30:34Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"In current state of setup_git_directory_gently(), when a repository is\nnot found, many things will not be the same as when setup procedure is\nstarted (while they should be). For one thing, current working\ndirectory will not be the same in certain cases.\n\nThis patch reorders actions in clear steps and rolls back if one step\nis wrong. (Actually the last step, setting git_dir, can't be rolled\nback by this patch).\n\nThe steps and their state change are:\n\n 1. Looking in working directory for repository candidates (or $GIT_DIR\n    if it is set)\n\n    This step may move $(cwd) to somewhere.\n\n 2. Set internal variables to record repository features, based only\n    on repository location and environment variables.\n\n    This step sets variables:\n    - inside_git_dir\n    - inside_work_tree\n\n 3. Inspect $GIT_DIR/config to see if it's a valid repository.\n\n    This step may change variables:\n    - repository_format_version\n    - shared_repository\n    - is_bare_repository_cfg\n    - git_work_tree_cfg\n\n 4. Save the repository location for later use.\n\n    This step calls setup_git_dir() to set many variables in environment.c\n\n 5. Calculate prefix (relative path to the original current working\n    directory)\n\nSetup procedure is completed at step 5. Stopping at any other steps\nis considered a setup failure.\n\nIn case of setup failure, if it's not fatal and nongit_ok is not NULL,\nprefix must be calculated before returning to caller. In other words:\n\n  if (!fatal && gently)\n    goto step_5;\n\nThings that go wrong before step 4 will be cleaned up by\nunset_git_directory(). Step 4 is irreversible, for now.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n cache.h |    1 +\n setup.c |   46 ++++++++++++++++++++++++++++++++++++++++++----\n 2 files changed, 43 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 2c1b64f..4de7bf0 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -323,6 +323,33 @@ const char *read_gitfile_gently(const char *path)\n \treturn path;\n }\n \n+void unset_git_directory(const char *prefix)\n+{\n+\t/*\n+\t * FIXME:\n+\t * chdir(prefix) may be enough for most of cases,\n+\t * if original cwd is outside worktree, prefix\n+\t * will always be set NULL, thus impossible to move\n+\t * back to orignal cwd\n+\t */\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 +430,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 +444,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 +461,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(\".\", nongit_ok))\n+\t\t\tif (check_repository_format_gently(\".\", 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.2.425.gb99f1\n"},{"id":"137423","messageId":"1269167446-7799-9-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 08/19] init/clone: turn on startup->have_repository properly","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-21T10:30:35Z","receivedAt":"2010-03-21T10:30:35Z","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.2.425.gb99f1\n"},{"id":"137424","messageId":"1269167446-7799-10-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 09/19] 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-21T10:30:36Z","receivedAt":"2010-03-21T10:30:36Z","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.2.425.gb99f1\n"},{"id":"137425","messageId":"1269167446-7799-11-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 10/19] 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-21T10:30:37Z","receivedAt":"2010-03-21T10:30:37Z","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.2.425.gb99f1\n"},{"id":"137426","messageId":"1269167446-7799-12-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 11/19] 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-21T10:30:38Z","receivedAt":"2010-03-21T10:30:38Z","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.2.425.gb99f1\n"},{"id":"137428","messageId":"1269167446-7799-13-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 12/19] 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-21T10:30:39Z","receivedAt":"2010-03-21T10:30:39Z","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.2.425.gb99f1\n"},{"id":"137427","messageId":"1269167446-7799-14-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 13/19] 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-21T10:30:40Z","receivedAt":"2010-03-21T10:30:40Z","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.2.425.gb99f1\n"},{"id":"137429","messageId":"1269167446-7799-15-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 14/19] run_builtin(): save \"-h\" detection result for later use","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-21T10:30:41Z","receivedAt":"2010-03-21T10:30:41Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"When run_builtin() sees \"-h\" as the first argument, it assumes that:\n - it's the call for help usage\n - the real git command will only print help usage then exit\n\nSo it skips all setup in this case. Unfortunately some commands may do\nmore things before calling parse_options(), which is where help usage\nis printed. Some of those things may touch repository, which has yet\nto be set up.\n\nUntil now, the only safe thing that can be called before\nparse_options() is git_config(). This function is aware of repository\nfindind status, and will skip reading $GIT_DIR/config if no repository\nis found.\n\nMake real commands aware of this fast path so that they can handle it\nproperly (i.e. print help usage then exist immediately) if they do\nmore initialization than git_config().\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n cache.h |    1 +\n git.c   |    8 ++++----\n 2 files changed, 5 insertions(+), 4 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex dd27e11..e397e1d 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1060,6 +1060,7 @@ int split_cmdline(char *cmdline, const char ***argv);\n struct startup_info {\n \tconst char *prefix;\n \tint have_repository;\n+\tint help;\n };\n extern struct startup_info *startup_info;\n \ndiff --git a/git.c b/git.c\nindex 746470f..7e30498 100644\n--- a/git.c\n+++ b/git.c\n@@ -237,13 +237,13 @@ struct cmd_struct {\n \n static int run_builtin(struct cmd_struct *p, int argc, const char **argv)\n {\n-\tint status, help;\n+\tint status;\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+\tstartup_info->help = argc == 2 && !strcmp(argv[1], \"-h\");\n+\tif (!startup_info->help) {\n \t\tif (p->option & RUN_SETUP)\n \t\t\tsetup_git_directory();\n \t\tif (p->option & RUN_SETUP_GENTLY) {\n@@ -261,7 +261,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)\n \t}\n \tcommit_pager_choice();\n \n-\tif (!help && p->option & NEED_WORK_TREE)\n+\tif (!startup_info->help && p->option & NEED_WORK_TREE)\n \t\tsetup_work_tree();\n \n \ttrace_argv_printf(argv, \"trace: built-in: git\");\n-- \n1.7.0.2.425.gb99f1\n"},{"id":"137430","messageId":"1269167446-7799-16-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 15/19] builtins: utilize startup_info->help where possible","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-21T10:30:42Z","receivedAt":"2010-03-21T10:30:42Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"It helps reduce false alarms while I'm looking for \"git foo -h\" code\npath that accesses repository. Anyway it looks like a good thing to\ndo.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/check-ref-format.c |    2 +-\n builtin/grep.c             |    2 +-\n builtin/index-pack.c       |    2 +-\n builtin/log.c              |    7 ++-----\n builtin/merge-ours.c       |    2 +-\n builtin/pack-redundant.c   |    2 +-\n builtin/show-ref.c         |    2 +-\n 7 files changed, 8 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/check-ref-format.c b/builtin/check-ref-format.c\nindex b106c65..c13f90a 100644\n--- a/builtin/check-ref-format.c\n+++ b/builtin/check-ref-format.c\n@@ -35,7 +35,7 @@ static void collapse_slashes(char *dst, const char *src)\n \n int cmd_check_ref_format(int argc, const char **argv, const char *prefix)\n {\n-\tif (argc == 2 && !strcmp(argv[1], \"-h\"))\n+\tif (startup_info->help)\n \t\tusage(builtin_check_ref_format_usage);\n \n \tif (argc == 3 && !strcmp(argv[1], \"--branch\")) {\ndiff --git a/builtin/grep.c b/builtin/grep.c\nindex fb82ff8..a7bdd7b 100644\n--- a/builtin/grep.c\n+++ b/builtin/grep.c\n@@ -857,7 +857,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \t * 'git grep -h', unlike 'git grep -h <pattern>', is a request\n \t * to show usage information and exit.\n \t */\n-\tif (argc == 2 && !strcmp(argv[1], \"-h\"))\n+\tif (startup_info->help)\n \t\tusage_with_options(grep_usage, options);\n \n \tmemset(&opt, 0, sizeof(opt));\ndiff --git a/builtin/index-pack.c b/builtin/index-pack.c\nindex 9aa6a13..44375a2 100644\n--- a/builtin/index-pack.c\n+++ b/builtin/index-pack.c\n@@ -880,7 +880,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)\n \tstruct pack_idx_entry **idx_objects;\n \tunsigned char pack_sha1[20];\n \n-\tif (argc == 2 && !strcmp(argv[1], \"-h\"))\n+\tif (startup_info->help)\n \t\tusage(index_pack_usage);\n \n \t/*\ndiff --git a/builtin/log.c b/builtin/log.c\nindex e0d5caa..382d4a3 100644\n--- a/builtin/log.c\n+++ b/builtin/log.c\n@@ -50,12 +50,9 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,\n \tif (default_date_mode)\n \t\trev->date_mode = parse_date_format(default_date_mode);\n \n-\t/*\n-\t * Check for -h before setup_revisions(), or \"git log -h\" will\n-\t * fail when run without a git directory.\n-\t */\n-\tif (argc == 2 && !strcmp(argv[1], \"-h\"))\n+\tif (startup_info->help)\n \t\tusage(builtin_log_usage);\n+\n \targc = setup_revisions(argc, argv, rev, \"HEAD\");\n \n \tif (!rev->show_notes_given && !rev->pretty_given)\ndiff --git a/builtin/merge-ours.c b/builtin/merge-ours.c\nindex 6844116..8e0777b 100644\n--- a/builtin/merge-ours.c\n+++ b/builtin/merge-ours.c\n@@ -20,7 +20,7 @@ static const char *diff_index_args[] = {\n \n int cmd_merge_ours(int argc, const char **argv, const char *prefix)\n {\n-\tif (argc == 2 && !strcmp(argv[1], \"-h\"))\n+\tif (startup_info->help)\n \t\tusage(builtin_merge_ours_usage);\n \n \t/*\ndiff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c\nindex 41e1615..3f090b2 100644\n--- a/builtin/pack-redundant.c\n+++ b/builtin/pack-redundant.c\n@@ -601,7 +601,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix)\n \tunsigned char *sha1;\n \tchar buf[42]; /* 40 byte sha1 + \\n + \\0 */\n \n-\tif (argc == 2 && !strcmp(argv[1], \"-h\"))\n+\tif (startup_info->help)\n \t\tusage(pack_redundant_usage);\n \n \tfor (i = 1; i < argc; i++) {\ndiff --git a/builtin/show-ref.c b/builtin/show-ref.c\nindex 17ada88..59f90df 100644\n--- a/builtin/show-ref.c\n+++ b/builtin/show-ref.c\n@@ -204,7 +204,7 @@ static const struct option show_ref_options[] = {\n \n int cmd_show_ref(int argc, const char **argv, const char *prefix)\n {\n-\tif (argc == 2 && !strcmp(argv[1], \"-h\"))\n+\tif (startup_info->help)\n \t\tusage_with_options(show_ref_usage, show_ref_options);\n \n \targc = parse_options(argc, argv, prefix, show_ref_options,\n-- \n1.7.0.2.425.gb99f1\n"},{"id":"137433","messageId":"1269167446-7799-17-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 16/19] builtins: check for startup_info->help, print and exit early","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-03-21T10:30:43Z","receivedAt":"2010-03-21T10:30:43Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"These commands need more than just git_config() before parsing\ncommmand line arguments. Some of these activities will unconditionally\nlook into a repository. When startup_info->help is TRUE, no repository\nis set up and the caller expects callees to print help usage and exit,\nno more.\n\nDo as expected.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/branch.c         |    3 +++\n builtin/checkout-index.c |    3 +++\n builtin/commit.c         |    6 ++++++\n builtin/gc.c             |    3 +++\n builtin/ls-files.c       |    3 +++\n builtin/merge.c          |    3 +++\n builtin/update-index.c   |    3 +++\n builtin/upload-archive.c |    7 ++++---\n 8 files changed, 28 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex 6cf7e72..cc0053b 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -651,6 +651,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)\n \t\tOPT_END(),\n \t};\n \n+\tif (startup_info->help)\n+\t\tusage_with_options(builtin_branch_usage, options);\n+\n \tgit_config(git_branch_config, NULL);\n \n \tif (branch_use_color == -1)\ndiff --git a/builtin/checkout-index.c b/builtin/checkout-index.c\nindex a7a5ee1..7f25cd7 100644\n--- a/builtin/checkout-index.c\n+++ b/builtin/checkout-index.c\n@@ -241,6 +241,9 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)\n \t\tOPT_END()\n \t};\n \n+\tif (startup_info->help)\n+\t\tusage_with_options(builtin_checkout_index_usage, builtin_checkout_index_options);\n+\n \tgit_config(git_default_config, NULL);\n \tstate.base_dir = \"\";\n \tprefix_length = prefix ? strlen(prefix) : 0;\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex f4c7344..b1a496e 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -1032,6 +1032,9 @@ int cmd_status(int argc, const char **argv, const char *prefix)\n \t\tOPT_END(),\n \t};\n \n+\tif (startup_info->help)\n+\t\tusage_with_options(builtin_status_usage, builtin_status_options);\n+\n \tif (null_termination && status_format == STATUS_FORMAT_LONG)\n \t\tstatus_format = STATUS_FORMAT_PORCELAIN;\n \n@@ -1172,6 +1175,9 @@ int cmd_commit(int argc, const char **argv, const char *prefix)\n \tint allow_fast_forward = 1;\n \tstruct wt_status s;\n \n+\tif (startup_info->help)\n+\t\tusage_with_options(builtin_commit_usage, builtin_commit_options);\n+\n \twt_status_prepare(&s);\n \tgit_config(git_commit_config, &s);\n \tin_merge = file_exists(git_path(\"MERGE_HEAD\"));\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex c304638..f040171 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -191,6 +191,9 @@ int cmd_gc(int argc, const char **argv, const char *prefix)\n \n \tgit_config(gc_config, NULL);\n \n+\tif (startup_info->help)\n+\t\tusage_with_options(builtin_gc_usage, builtin_gc_options);\n+\n \tif (pack_refs < 0)\n \t\tpack_refs = !is_bare_repository();\n \ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex b065061..5125560 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -518,6 +518,9 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)\n \t\tOPT_END()\n \t};\n \n+\tif (startup_info->help)\n+\t\tusage_with_options(ls_files_usage, builtin_ls_files_options);\n+\n \tmemset(&dir, 0, sizeof(dir));\n \tif (prefix)\n \t\tprefix_offset = strlen(prefix);\ndiff --git a/builtin/merge.c b/builtin/merge.c\nindex 3aaec7b..11fadac 100644\n--- a/builtin/merge.c\n+++ b/builtin/merge.c\n@@ -886,6 +886,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)\n \tconst char *best_strategy = NULL, *wt_strategy = NULL;\n \tstruct commit_list **remotes = &remoteheads;\n \n+\tif (startup_info->help)\n+\t\tusage_with_options(builtin_merge_usage, builtin_merge_options);\n+\n \tif (read_cache_unmerged()) {\n \t\tdie_resolve_conflict(\"merge\");\n \t}\ndiff --git a/builtin/update-index.c b/builtin/update-index.c\nindex 3ab214d..46a53f5 100644\n--- a/builtin/update-index.c\n+++ b/builtin/update-index.c\n@@ -589,6 +589,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)\n \tint lock_error = 0;\n \tstruct lock_file *lock_file;\n \n+\tif (startup_info->help)\n+\t\tusage(update_index_usage);\n+\n \tgit_config(git_default_config, NULL);\n \n \t/* We can't free this memory, it becomes part of a linked list parsed atexit() */\ndiff --git a/builtin/upload-archive.c b/builtin/upload-archive.c\nindex 73f788e..d4f4741 100644\n--- a/builtin/upload-archive.c\n+++ b/builtin/upload-archive.c\n@@ -26,9 +26,6 @@ static int run_upload_archive(int argc, const char **argv, const char *prefix)\n \tint sent_argc;\n \tint len;\n \n-\tif (argc != 2)\n-\t\tusage(upload_archive_usage);\n-\n \tif (strlen(argv[1]) + 1 > sizeof(buf))\n \t\tdie(\"insanely long repository name\");\n \n@@ -98,6 +95,10 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)\n {\n \tpid_t writer;\n \tint fd1[2], fd2[2];\n+\n+\tif (startup_info->help || argc != 2)\n+\t\tusage(upload_archive_usage);\n+\n \t/*\n \t * Set up sideband subprocess.\n \t *\n-- \n1.7.0.2.425.gb99f1\n"},{"id":"137431","messageId":"1269167446-7799-18-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 17/19] 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-21T10:30:44Z","receivedAt":"2010-03-21T10:30:44Z","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() sets internal\nstate itself. Even worse, set_git_dir() may override $GIT_DIR env\nvariable.\n\nAdd unset_git_env() to undo set_git_dir(), allow unset_git_directory()\nto undo a succesful setup_git_directory_gently().\n\nWhile at there, fix alias handling code regarding git_dir {,un-}setup,\nuse unset_git_directory() instead of just chdir() back.\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 e397e1d..b1ed150 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 7e30498..db5bd95 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;\n \tstruct stat st;\n \n-\tmemset(&git_startup_info, 0, sizeof(git_startup_info));\n-\tstartup_info = &git_startup_info;\n \tstartup_info->help = argc == 2 && !strcmp(argv[1], \"-h\");\n \tif (!startup_info->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 4de7bf0..1808ebe 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -336,6 +336,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.2.425.gb99f1\n"},{"id":"137432","messageId":"1269167446-7799-19-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 18/19] 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-21T10:30:45Z","receivedAt":"2010-03-21T10:30:45Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Given that about 80% builtin commands will need to search for a\nrepository, we should keep the repository found in alias handling\ncode. Until we are clear there's no need for a repository, then we can\nundo 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 db5bd95..88aaf13 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 \tstartup_info->help = argc == 2 && !strcmp(argv[1], \"-h\");\n \tif (!startup_info->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.2.425.gb99f1\n"},{"id":"137434","messageId":"1269167446-7799-20-git-send-email-pclouds@gmail.com","threadId":"23117","inReplyTo":"1269167446-7799-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 19/19] 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-21T10:30:46Z","receivedAt":"2010-03-21T10:30:46Z","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 git.c             |    3 +++\n setup.c           |   13 +++++++++++++\n 6 files changed, 31 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 b1ed150..417a744 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 \tint help;\n };\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/git.c b/git.c\nindex 88aaf13..5f7f410 100644\n--- a/git.c\n+++ b/git.c\n@@ -260,6 +260,9 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)\n \t\t\tuse_pager = 1;\n \t\t}\n \t}\n+\telse\n+\t\t/* Stop git_config() from complaining that no repository found. */\n+\t\tstartup_info->have_run_setup_gitdir = 1;\n \tcommit_pager_choice();\n \n \tif (!startup_info->help && p->option & NEED_WORK_TREE)\ndiff --git a/setup.c b/setup.c\nindex 1808ebe..d9bb616 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@@ -340,6 +350,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@@ -506,6 +517,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@@ -600,6 +612,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.2.425.gb99f1\n"}]}