{"thread":{"id":"23339","subject":"[PATCH 00/43] nd/setup update","startedAt":"2010-04-05T18:40:45Z","lastAt":"2010-04-06T07:17:25Z","messageCount":50,"participants":["Nguyễn Thái Ngọc Duy","Jonathan Nieder","Nguyen Thai Ngoc Duy","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":43},"messages":[{"id":"138609","messageId":"1270492888-26589-1-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":null,"subject":"[PATCH 00/43] nd/setup update","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:40:45Z","receivedAt":"2010-04-05T18:40:45Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Cleanup series, rebased on master.\n\nExcept 15/43 (which is now a comment only patch), no changes from pu 9f72b82.\n\nJonathan Nieder (2):\n  help: take note why this command is not applicable for\n    RUN_SETUP_GENTLY\n  t0001: Add test cases for \"git init\" with aliases\n\nNguyễn Thái Ngọc Duy (41):\n  builtin: introduce startup_info struct\n  builtin: Support RUN_SETUP_GENTLY to set up repository early if found\n  config: use RUN_SETUP_GENTLY\n  hash-object: use RUN_SETUP_GENTLY\n  shortlog: use RUN_SETUP_GENTLY\n  grep: use RUN_SETUP_GENTLY\n  builtin: USE_PAGER should not be used without RUN_SETUP*\n  archive: use RUN_SETUP_GENTLY\n  mailinfo: use RUN_SETUP_GENTLY\n  check-ref-format: use RUN_SETUP_GENTLY\n  verify-pack: use RUN_SETUP_GENTLY\n  apply: use RUN_SETUP_GENTLY\n  bundle: use RUN_SETUP_GENTLY\n  diff: use RUN_SETUP_GENTLY\n  ls-remote: use RUN_SETUP_GENTLY\n  var: use RUN_SETUP_GENTLY\n  merge-file: use RUN_SETUP_GENTLY\n  worktree setup: calculate prefix even if no worktree is found\n  index-pack: trust the prefix returned by setup_git_directory_gently()\n  index-pack: use RUN_SETUP_GENTLY\n  Move enter_repo() to setup.c\n  enter_repo(): initialize other variables as\n    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\n    code)\n  alias: keep repository found while collecting aliases as long as\n    possible\n  Guard unallowed access to repository when it's not set up\n  builtins: setup repository before print unknown command error\n  builtins: do not commit pager choice early\n\n attr.c                     |    5 +-\n builtin/apply.c            |    9 +-\n builtin/archive.c          |    2 +-\n builtin/branch.c           |    3 +\n builtin/bundle.c           |    6 +-\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           |   15 ++--\n builtin/diff.c             |    6 +-\n builtin/gc.c               |    3 +\n builtin/grep.c             |   11 +--\n builtin/hash-object.c      |    9 +-\n builtin/help.c             |    5 +\n builtin/index-pack.c       |   21 +---\n builtin/init-db.c          |   10 +-\n builtin/log.c              |    6 +-\n builtin/ls-files.c         |    3 +\n builtin/ls-remote.c        |    3 -\n builtin/mailinfo.c         |    3 -\n builtin/merge-file.c       |    4 +-\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/shortlog.c         |    4 +-\n builtin/show-ref.c         |    2 +-\n builtin/update-index.c     |    3 +\n builtin/upload-archive.c   |    7 +-\n builtin/var.c              |    2 -\n cache.h                    |   14 +++-\n config.c                   |   22 ++++-\n dir.c                      |    8 +-\n environment.c              |   34 +++++++-\n git.c                      |   99 ++++++++++++-------\n help.c                     |    4 +\n path.c                     |   91 ------------------\n setup.c                    |  223 ++++++++++++++++++++++++++++++++++++++++----\n t/t0001-init.sh            |   52 ++++++++++\n t/t1300-repo-config.sh     |   14 +++\n t/t1302-repo-version.sh    |    2 +-\n t/t1500-rev-parse.sh       |    2 +-\n t/t1501-worktree.sh        |    5 +\n t/t7002-grep.sh            |   24 +++++\n 45 files changed, 525 insertions(+), 240 deletions(-)\n"},{"id":"138613","messageId":"1270492888-26589-2-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 01/43] builtin: introduce startup_info struct","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:40:46Z","receivedAt":"2010-04-05T18:40:46Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"The purpose of this struct is to make it easier to extend parameters\npassed to builtin commands from run_builtin(), and let libgit aware of\nsome global states in future.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n cache.h       |    6 ++++++\n environment.c |    1 +\n git.c         |    9 +++++----\n setup.c       |   12 +++++++++++-\n 4 files changed, 23 insertions(+), 5 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex 6dcb100..8d8f8ee 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1061,4 +1061,10 @@ int split_cmdline(char *cmdline, const char ***argv);\n /* builtin/merge.c */\n int checkout_fast_forward(const unsigned char *from, const unsigned char *to);\n \n+/* git.c */\n+struct startup_info {\n+\tconst char *prefix;\n+};\n+extern struct startup_info *startup_info;\n+\n #endif /* CACHE_H */\ndiff --git a/environment.c b/environment.c\nindex 876c5e5..c36c902 100644\n--- a/environment.c\n+++ b/environment.c\n@@ -52,6 +52,7 @@ enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;\n char *notes_ref_name;\n int grafts_replace_parents = 1;\n int core_apply_sparse_checkout;\n+struct startup_info *startup_info;\n \n /* Parallel index stat data preload? */\n int core_preload_index = 0;\ndiff --git a/git.c b/git.c\nindex 6bae305..4be34e4 100644\n--- a/git.c\n+++ b/git.c\n@@ -13,6 +13,7 @@ const char git_usage_string[] =\n const char git_more_info_string[] =\n \t\"See 'git help COMMAND' for more information on a specific command.\";\n \n+static struct startup_info git_startup_info;\n static int use_pager = -1;\n struct pager_config {\n \tconst char *cmd;\n@@ -237,13 +238,13 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)\n {\n \tint status, help;\n \tstruct stat st;\n-\tconst char *prefix;\n \n-\tprefix = NULL;\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-\t\t\tprefix = setup_git_directory();\n+\t\t\tsetup_git_directory();\n \n \t\tif (use_pager == -1 && p->option & RUN_SETUP)\n \t\t\tuse_pager = check_pager_config(p->cmd);\n@@ -257,7 +258,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)\n \n \ttrace_argv_printf(argv, \"trace: built-in: git\");\n \n-\tstatus = p->fn(argc, argv, prefix);\n+\tstatus = p->fn(argc, argv, startup_info->prefix);\n \tif (status)\n \t\treturn status;\n \ndiff --git a/setup.c b/setup.c\nindex 5716d90..cf1b37d 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -315,7 +315,7 @@ const char *read_gitfile_gently(const char *path)\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  */\n-const char *setup_git_directory_gently(int *nongit_ok)\n+static const char *setup_git_directory_gently_1(int *nongit_ok)\n {\n \tconst char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);\n \tconst char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);\n@@ -443,6 +443,16 @@ const char *setup_git_directory_gently(int *nongit_ok)\n \treturn cwd + offset;\n }\n \n+const char *setup_git_directory_gently(int *nongit_ok)\n+{\n+\tconst char *prefix;\n+\n+\tprefix = setup_git_directory_gently_1(nongit_ok);\n+\tif (startup_info)\n+\t\tstartup_info->prefix = prefix;\n+\treturn prefix;\n+}\n+\n int git_config_perm(const char *var, const char *value)\n {\n \tint i;\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138611","messageId":"1270492888-26589-3-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 02/43] builtin: Support RUN_SETUP_GENTLY to set up repository early if found","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:40:47Z","receivedAt":"2010-04-05T18:40:47Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Attempts to access gitdir are everywhere, even before cmd_*() is\ncalled. Because repository has not been found, repository-specific\ninformation (i.e. $GIT_DIR/config) may not be read. This leads to\nobscure bugs.\n\nSo the sooner we setup gitdir, the less trouble we may have to deal with.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n cache.h |    1 +\n git.c   |   11 ++++++++---\n setup.c |    4 +++-\n 3 files changed, 12 insertions(+), 4 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex 8d8f8ee..fa13bf7 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1064,6 +1064,7 @@ int checkout_fast_forward(const unsigned char *from, const unsigned char *to);\n /* git.c */\n struct startup_info {\n \tconst char *prefix;\n+\tint have_repository;\n };\n extern struct startup_info *startup_info;\n \ndiff --git a/git.c b/git.c\nindex 4be34e4..9e0e2d0 100644\n--- a/git.c\n+++ b/git.c\n@@ -220,13 +220,14 @@ static int handle_alias(int *argcp, const char ***argv)\n \n const char git_version_string[] = GIT_VERSION;\n \n-#define RUN_SETUP\t(1<<0)\n-#define USE_PAGER\t(1<<1)\n+#define RUN_SETUP\t\t(1<<0)\n+#define USE_PAGER\t\t(1<<1)\n /*\n  * require working tree to be present -- anything uses this needs\n  * RUN_SETUP for reading from the configuration file.\n  */\n-#define NEED_WORK_TREE\t(1<<2)\n+#define NEED_WORK_TREE\t\t(1<<2)\n+#define RUN_SETUP_GENTLY\t(1<<3)\n \n struct cmd_struct {\n \tconst char *cmd;\n@@ -245,6 +246,10 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)\n \tif (!help) {\n \t\tif (p->option & RUN_SETUP)\n \t\t\tsetup_git_directory();\n+\t\tif (p->option & RUN_SETUP_GENTLY) {\n+\t\t\tint nongit_ok;\n+\t\t\tsetup_git_directory_gently(&nongit_ok);\n+\t\t}\n \n \t\tif (use_pager == -1 && p->option & RUN_SETUP)\n \t\t\tuse_pager = check_pager_config(p->cmd);\ndiff --git a/setup.c b/setup.c\nindex cf1b37d..bb3648c 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -448,8 +448,10 @@ const char *setup_git_directory_gently(int *nongit_ok)\n \tconst char *prefix;\n \n \tprefix = setup_git_directory_gently_1(nongit_ok);\n-\tif (startup_info)\n+\tif (startup_info) {\n \t\tstartup_info->prefix = prefix;\n+\t\tstartup_info->have_repository = !nongit_ok || !*nongit_ok;\n+\t}\n \treturn prefix;\n }\n \n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138620","messageId":"1270492888-26589-4-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 03/43] config: use RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:40:48Z","receivedAt":"2010-04-05T18:40:48Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/config.c |    6 ++----\n git.c            |    4 ++--\n 2 files changed, 4 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/config.c b/builtin/config.c\nindex 4bc46b1..ecc8f87 100644\n--- a/builtin/config.c\n+++ b/builtin/config.c\n@@ -326,11 +326,9 @@ static int get_colorbool(int print)\n \t\treturn get_colorbool_found ? 0 : 1;\n }\n \n-int cmd_config(int argc, const char **argv, const char *unused_prefix)\n+int cmd_config(int argc, const char **argv, const char *prefix)\n {\n-\tint nongit;\n \tchar *value;\n-\tconst char *prefix = setup_git_directory_gently(&nongit);\n \n \tconfig_exclusive_filename = getenv(CONFIG_ENVIRONMENT);\n \n@@ -409,7 +407,7 @@ int cmd_config(int argc, const char **argv, const char *unused_prefix)\n \t}\n \telse if (actions == ACTION_EDIT) {\n \t\tcheck_argc(argc, 0, 0);\n-\t\tif (!config_exclusive_filename && nongit)\n+\t\tif (!config_exclusive_filename && !startup_info->have_repository)\n \t\t\tdie(\"not in a git directory\");\n \t\tgit_config(git_default_config, NULL);\n \t\tlaunch_editor(config_exclusive_filename ?\ndiff --git a/git.c b/git.c\nindex 9e0e2d0..4c99319 100644\n--- a/git.c\n+++ b/git.c\n@@ -309,7 +309,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"clean\", cmd_clean, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"commit\", cmd_commit, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"commit-tree\", cmd_commit_tree, RUN_SETUP },\n-\t\t{ \"config\", cmd_config },\n+\t\t{ \"config\", cmd_config, RUN_SETUP_GENTLY },\n \t\t{ \"count-objects\", cmd_count_objects, RUN_SETUP },\n \t\t{ \"describe\", cmd_describe, RUN_SETUP },\n \t\t{ \"diff\", cmd_diff },\n@@ -366,7 +366,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"reflog\", cmd_reflog, RUN_SETUP },\n \t\t{ \"remote\", cmd_remote, RUN_SETUP },\n \t\t{ \"replace\", cmd_replace, RUN_SETUP },\n-\t\t{ \"repo-config\", cmd_config },\n+\t\t{ \"repo-config\", cmd_config, RUN_SETUP_GENTLY },\n \t\t{ \"rerere\", cmd_rerere, RUN_SETUP },\n \t\t{ \"reset\", cmd_reset, RUN_SETUP },\n \t\t{ \"rev-list\", cmd_rev_list, RUN_SETUP },\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138616","messageId":"1270492888-26589-5-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 04/43] hash-object: use RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:40:49Z","receivedAt":"2010-04-05T18:40:49Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"hash-object needs to inspect repository's config, so it must try to\nfind a repository regardless the object will be written in object\nstore.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/hash-object.c |    9 +++++----\n git.c                 |    2 +-\n 2 files changed, 6 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/hash-object.c b/builtin/hash-object.c\nindex 080af1a..1f7fd6a 100644\n--- a/builtin/hash-object.c\n+++ b/builtin/hash-object.c\n@@ -78,7 +78,7 @@ static const struct option hash_object_options[] = {\n int cmd_hash_object(int argc, const char **argv, const char *prefix)\n {\n \tint i;\n-\tint prefix_length = -1;\n+\tint prefix_length;\n \tconst char *errstr = NULL;\n \n \ttype = blob_type;\n@@ -86,9 +86,10 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)\n \targc = parse_options(argc, argv, NULL, hash_object_options,\n \t\t\t     hash_object_usage, 0);\n \n+\tprefix_length = prefix ? strlen(prefix) : 0;\n \tif (write_object) {\n-\t\tprefix = setup_git_directory();\n-\t\tprefix_length = prefix ? strlen(prefix) : 0;\n+\t\tif (!startup_info->have_repository)\n+\t\t\tdie(\"No repository found\");\n \t\tif (vpath && prefix)\n \t\t\tvpath = prefix_filename(prefix, prefix_length, vpath);\n \t}\n@@ -121,7 +122,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)\n \tfor (i = 0 ; i < argc; i++) {\n \t\tconst char *arg = argv[i];\n \n-\t\tif (0 <= prefix_length)\n+\t\tif (prefix_length)\n \t\t\targ = prefix_filename(prefix, prefix_length, arg);\n \t\thash_object(arg, type, write_object,\n \t\t\t    no_filters ? NULL : vpath ? vpath : arg);\ndiff --git a/git.c b/git.c\nindex 4c99319..2069ef3 100644\n--- a/git.c\n+++ b/git.c\n@@ -327,7 +327,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"gc\", cmd_gc, RUN_SETUP },\n \t\t{ \"get-tar-commit-id\", cmd_get_tar_commit_id },\n \t\t{ \"grep\", cmd_grep, USE_PAGER },\n-\t\t{ \"hash-object\", cmd_hash_object },\n+\t\t{ \"hash-object\", cmd_hash_object, RUN_SETUP_GENTLY },\n \t\t{ \"help\", cmd_help },\n \t\t{ \"index-pack\", cmd_index_pack },\n \t\t{ \"init\", cmd_init_db },\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138612","messageId":"1270492888-26589-6-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 05/43] shortlog: use RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:40:50Z","receivedAt":"2010-04-05T18:40:50Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"shortlog has USE_PAGER set. setup_pager() may read config even\nsetup_git_dir* is not run yet. This tries to find a repository before\nthe config is read.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/shortlog.c |    4 +---\n git.c              |    2 +-\n 2 files changed, 2 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/shortlog.c b/builtin/shortlog.c\nindex 06320f5..bdf3402 100644\n--- a/builtin/shortlog.c\n+++ b/builtin/shortlog.c\n@@ -249,7 +249,6 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)\n {\n \tstatic struct shortlog log;\n \tstatic struct rev_info rev;\n-\tint nongit;\n \n \tstatic const struct option options[] = {\n \t\tOPT_BOOLEAN('n', \"numbered\", &log.sort_by_number,\n@@ -265,7 +264,6 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)\n \n \tstruct parse_opt_ctx_t ctx;\n \n-\tprefix = setup_git_directory_gently(&nongit);\n \tgit_config(git_default_config, NULL);\n \tshortlog_init(&log);\n \tinit_revisions(&rev, prefix);\n@@ -292,7 +290,7 @@ parse_done:\n \tlog.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;\n \n \t/* assume HEAD if from a tty */\n-\tif (!nongit && !rev.pending.nr && isatty(0))\n+\tif (startup_info->have_repository && !rev.pending.nr && isatty(0))\n \t\tadd_head_to_pending(&rev);\n \tif (rev.pending.nr == 0) {\n \t\tif (isatty(0))\ndiff --git a/git.c b/git.c\nindex 2069ef3..29489e6 100644\n--- a/git.c\n+++ b/git.c\n@@ -374,7 +374,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"revert\", cmd_revert, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"rm\", cmd_rm, RUN_SETUP },\n \t\t{ \"send-pack\", cmd_send_pack, RUN_SETUP },\n-\t\t{ \"shortlog\", cmd_shortlog, USE_PAGER },\n+\t\t{ \"shortlog\", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },\n \t\t{ \"show-branch\", cmd_show_branch, RUN_SETUP },\n \t\t{ \"show\", cmd_show, RUN_SETUP | USE_PAGER },\n \t\t{ \"status\", cmd_status, RUN_SETUP | NEED_WORK_TREE },\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138610","messageId":"1270492888-26589-7-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 06/43] grep: use RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:40:51Z","receivedAt":"2010-04-05T18:40:51Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"grep has USE_PAGER set. setup_pager() may read config even\nsetup_git_dir* is not run yet. This tries to find a repository before\nconfig is read.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/grep.c |    9 +++------\n git.c          |    2 +-\n 2 files changed, 4 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/grep.c b/builtin/grep.c\nindex 9d30ddb..a4ea0c9 100644\n--- a/builtin/grep.c\n+++ b/builtin/grep.c\n@@ -779,7 +779,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \tconst char **paths = NULL;\n \tint i;\n \tint dummy;\n-\tint nongit = 0, use_index = 1;\n+\tint use_index = 1;\n \tstruct option options[] = {\n \t\tOPT_BOOLEAN(0, \"cached\", &cached,\n \t\t\t\"search in index instead of in the work tree\"),\n@@ -867,8 +867,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \t\tOPT_END()\n \t};\n \n-\tprefix = setup_git_directory_gently(&nongit);\n-\n \t/*\n \t * 'git grep -h', unlike 'git grep -h <pattern>', is a request\n \t * to show usage information and exit.\n@@ -913,9 +911,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \t\t\t     PARSE_OPT_STOP_AT_NON_OPTION |\n \t\t\t     PARSE_OPT_NO_INTERNAL_HELP);\n \n-\tif (use_index && nongit)\n-\t\t/* die the same way as if we did it at the beginning */\n-\t\tsetup_git_directory();\n+\tif (use_index && !startup_info->have_repository)\n+\t\tdie(\"No git repository found\");\n \n \t/*\n \t * skip a -- separator; we know it cannot be\ndiff --git a/git.c b/git.c\nindex 29489e6..3904fe2 100644\n--- a/git.c\n+++ b/git.c\n@@ -326,7 +326,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"fsck-objects\", cmd_fsck, RUN_SETUP },\n \t\t{ \"gc\", cmd_gc, RUN_SETUP },\n \t\t{ \"get-tar-commit-id\", cmd_get_tar_commit_id },\n-\t\t{ \"grep\", cmd_grep, USE_PAGER },\n+\t\t{ \"grep\", cmd_grep, RUN_SETUP_GENTLY | USE_PAGER },\n \t\t{ \"hash-object\", cmd_hash_object, RUN_SETUP_GENTLY },\n \t\t{ \"help\", cmd_help },\n \t\t{ \"index-pack\", cmd_index_pack },\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138617","messageId":"1270492888-26589-8-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 07/43] builtin: USE_PAGER should not be used without RUN_SETUP*","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:40:52Z","receivedAt":"2010-04-05T18:40:52Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"USE_PAGER may need core.pager from repository. So a repository search\nshould be done before core.pager is read.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n git.c |    5 ++++-\n 1 files changed, 4 insertions(+), 1 deletions(-)\n\ndiff --git a/git.c b/git.c\nindex 3904fe2..84dd78e 100644\n--- a/git.c\n+++ b/git.c\n@@ -253,8 +253,11 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)\n \n \t\tif (use_pager == -1 && p->option & RUN_SETUP)\n \t\t\tuse_pager = check_pager_config(p->cmd);\n-\t\tif (use_pager == -1 && p->option & USE_PAGER)\n+\t\tif (use_pager == -1 && p->option & USE_PAGER) {\n+\t\t\tif ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) == 0)\n+\t\t\t\tdie(\"Internal error: USE_PAGER must be together with RUN_SETUP*\");\n \t\t\tuse_pager = 1;\n+\t\t}\n \t}\n \tcommit_pager_choice();\n \n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138618","messageId":"1270492888-26589-9-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 08/43] archive: use RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:40:53Z","receivedAt":"2010-04-05T18:40:53Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/archive.c |    2 +-\n git.c             |    4 ++--\n 2 files changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/archive.c b/builtin/archive.c\nindex 6a887f5..ef0bef8 100644\n--- a/builtin/archive.c\n+++ b/builtin/archive.c\n@@ -125,5 +125,5 @@ int cmd_archive(int argc, const char **argv, const char *prefix)\n \n \tsetvbuf(stderr, NULL, _IOLBF, BUFSIZ);\n \n-\treturn write_archive(argc, argv, prefix, 1);\n+\treturn write_archive(argc, argv, prefix, 0);\n }\ndiff --git a/git.c b/git.c\nindex 84dd78e..2b59f06 100644\n--- a/git.c\n+++ b/git.c\n@@ -295,7 +295,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"stage\", cmd_add, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"annotate\", cmd_annotate, RUN_SETUP },\n \t\t{ \"apply\", cmd_apply },\n-\t\t{ \"archive\", cmd_archive },\n+\t\t{ \"archive\", cmd_archive, RUN_SETUP_GENTLY },\n \t\t{ \"bisect--helper\", cmd_bisect__helper, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"blame\", cmd_blame, RUN_SETUP },\n \t\t{ \"branch\", cmd_branch, RUN_SETUP },\n@@ -384,7 +384,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"stripspace\", cmd_stripspace },\n \t\t{ \"symbolic-ref\", cmd_symbolic_ref, RUN_SETUP },\n \t\t{ \"tag\", cmd_tag, RUN_SETUP },\n-\t\t{ \"tar-tree\", cmd_tar_tree },\n+\t\t{ \"tar-tree\", cmd_tar_tree, RUN_SETUP_GENTLY },\n \t\t{ \"unpack-file\", cmd_unpack_file, RUN_SETUP },\n \t\t{ \"unpack-objects\", cmd_unpack_objects, RUN_SETUP },\n \t\t{ \"update-index\", cmd_update_index, RUN_SETUP },\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138615","messageId":"1270492888-26589-10-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 09/43] mailinfo: use RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:40:54Z","receivedAt":"2010-04-05T18:40:54Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"mailinfo may use repo config, so setup gitdir first.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/mailinfo.c |    3 ---\n git.c              |    2 +-\n 2 files changed, 1 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/mailinfo.c b/builtin/mailinfo.c\nindex ce2ef6b..3d2c650 100644\n--- a/builtin/mailinfo.c\n+++ b/builtin/mailinfo.c\n@@ -1026,9 +1026,6 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)\n {\n \tconst char *def_charset;\n \n-\t/* NEEDSWORK: might want to do the optional .git/ directory\n-\t * discovery\n-\t */\n \tgit_config(git_mailinfo_config, NULL);\n \n \tdef_charset = (git_commit_encoding ? git_commit_encoding : \"UTF-8\");\ndiff --git a/git.c b/git.c\nindex 2b59f06..326f7c7 100644\n--- a/git.c\n+++ b/git.c\n@@ -339,7 +339,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"ls-files\", cmd_ls_files, RUN_SETUP },\n \t\t{ \"ls-tree\", cmd_ls_tree, RUN_SETUP },\n \t\t{ \"ls-remote\", cmd_ls_remote },\n-\t\t{ \"mailinfo\", cmd_mailinfo },\n+\t\t{ \"mailinfo\", cmd_mailinfo, RUN_SETUP_GENTLY },\n \t\t{ \"mailsplit\", cmd_mailsplit },\n \t\t{ \"merge\", cmd_merge, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"merge-base\", cmd_merge_base, RUN_SETUP },\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138621","messageId":"1270492888-26589-11-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 10/43] check-ref-format: use RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:40:55Z","receivedAt":"2010-04-05T18:40:55Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"When --branch is used, check-ref-format will look into repository for\nbranch name. Therefore repository search is needed.\n\nThe call flow is:\n - cmd_check_ref_format\n - strbuf_check_branch_ref\n - strbuf_branchname\n - interpret_branch_name\n - branch_get\n - read_config\n - resolve_ref\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n git.c |    2 +-\n 1 files changed, 1 insertions(+), 1 deletions(-)\n\ndiff --git a/git.c b/git.c\nindex 326f7c7..a8660f4 100644\n--- a/git.c\n+++ b/git.c\n@@ -304,7 +304,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"checkout\", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"checkout-index\", cmd_checkout_index,\n \t\t\tRUN_SETUP | NEED_WORK_TREE},\n-\t\t{ \"check-ref-format\", cmd_check_ref_format },\n+\t\t{ \"check-ref-format\", cmd_check_ref_format, RUN_SETUP_GENTLY },\n \t\t{ \"check-attr\", cmd_check_attr, RUN_SETUP },\n \t\t{ \"cherry\", cmd_cherry, RUN_SETUP },\n \t\t{ \"cherry-pick\", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138614","messageId":"1270492888-26589-12-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 11/43] verify-pack: use RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:40:56Z","receivedAt":"2010-04-05T18:40:56Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n git.c |    2 +-\n 1 files changed, 1 insertions(+), 1 deletions(-)\n\ndiff --git a/git.c b/git.c\nindex a8660f4..0e052c8 100644\n--- a/git.c\n+++ b/git.c\n@@ -396,7 +396,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"version\", cmd_version },\n \t\t{ \"whatchanged\", cmd_whatchanged, RUN_SETUP | USE_PAGER },\n \t\t{ \"write-tree\", cmd_write_tree, RUN_SETUP },\n-\t\t{ \"verify-pack\", cmd_verify_pack },\n+\t\t{ \"verify-pack\", cmd_verify_pack, RUN_SETUP_GENTLY },\n \t\t{ \"show-ref\", cmd_show_ref, RUN_SETUP },\n \t\t{ \"pack-refs\", cmd_pack_refs, RUN_SETUP },\n \t};\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138619","messageId":"1270492888-26589-13-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 12/43] apply: use RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:40:57Z","receivedAt":"2010-04-05T18:40:57Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/apply.c |    7 +++----\n git.c           |    2 +-\n 2 files changed, 4 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 7ca9047..bb957d4 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -3621,7 +3621,6 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)\n {\n \tint i;\n \tint errs = 0;\n-\tint is_not_gitdir;\n \tint binary;\n \tint force_apply = 0;\n \n@@ -3694,7 +3693,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)\n \t\tOPT_END()\n \t};\n \n-\tprefix = setup_git_directory_gently(&is_not_gitdir);\n+\tprefix = startup_info->prefix;\n \tprefix_length = prefix ? strlen(prefix) : 0;\n \tgit_config(git_apply_config, NULL);\n \tif (apply_default_whitespace)\n@@ -3709,10 +3708,10 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)\n \t\tapply = apply_verbosely = 1;\n \tif (!force_apply && (diffstat || numstat || summary || check || fake_ancestor))\n \t\tapply = 0;\n-\tif (check_index && is_not_gitdir)\n+\tif (check_index && !startup_info->have_repository)\n \t\tdie(\"--index outside a repository\");\n \tif (cached) {\n-\t\tif (is_not_gitdir)\n+\t\tif (!startup_info->have_repository)\n \t\t\tdie(\"--cached outside a repository\");\n \t\tcheck_index = 1;\n \t}\ndiff --git a/git.c b/git.c\nindex 0e052c8..7820d22 100644\n--- a/git.c\n+++ b/git.c\n@@ -294,7 +294,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"add\", cmd_add, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"stage\", cmd_add, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"annotate\", cmd_annotate, RUN_SETUP },\n-\t\t{ \"apply\", cmd_apply },\n+\t\t{ \"apply\", cmd_apply, RUN_SETUP_GENTLY },\n \t\t{ \"archive\", cmd_archive, RUN_SETUP_GENTLY },\n \t\t{ \"bisect--helper\", cmd_bisect__helper, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"blame\", cmd_blame, RUN_SETUP },\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138624","messageId":"1270492888-26589-14-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 13/43] bundle: use RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:40:58Z","receivedAt":"2010-04-05T18:40:58Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/bundle.c |    6 ++----\n git.c            |    2 +-\n 2 files changed, 3 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/bundle.c b/builtin/bundle.c\nindex 2006cc5..80649ba 100644\n--- a/builtin/bundle.c\n+++ b/builtin/bundle.c\n@@ -18,7 +18,6 @@ static const char builtin_bundle_usage[] =\n int cmd_bundle(int argc, const char **argv, const char *prefix)\n {\n \tstruct bundle_header header;\n-\tint nongit;\n \tconst char *cmd, *bundle_file;\n \tint bundle_fd = -1;\n \tchar buffer[PATH_MAX];\n@@ -31,7 +30,6 @@ int cmd_bundle(int argc, const char **argv, const char *prefix)\n \targc -= 2;\n \targv += 2;\n \n-\tprefix = setup_git_directory_gently(&nongit);\n \tif (prefix && bundle_file[0] != '/') {\n \t\tsnprintf(buffer, sizeof(buffer), \"%s/%s\", prefix, bundle_file);\n \t\tbundle_file = buffer;\n@@ -54,11 +52,11 @@ int cmd_bundle(int argc, const char **argv, const char *prefix)\n \t\treturn !!list_bundle_refs(&header, argc, argv);\n \t}\n \tif (!strcmp(cmd, \"create\")) {\n-\t\tif (nongit)\n+\t\tif (!startup_info->have_repository)\n \t\t\tdie(\"Need a repository to create a bundle.\");\n \t\treturn !!create_bundle(&header, bundle_file, argc, argv);\n \t} else if (!strcmp(cmd, \"unbundle\")) {\n-\t\tif (nongit)\n+\t\tif (!startup_info->have_repository)\n \t\t\tdie(\"Need a repository to unbundle.\");\n \t\treturn !!unbundle(&header, bundle_fd) ||\n \t\t\tlist_bundle_refs(&header, argc, argv);\ndiff --git a/git.c b/git.c\nindex 7820d22..a90e0c3 100644\n--- a/git.c\n+++ b/git.c\n@@ -299,7 +299,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"bisect--helper\", cmd_bisect__helper, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"blame\", cmd_blame, RUN_SETUP },\n \t\t{ \"branch\", cmd_branch, RUN_SETUP },\n-\t\t{ \"bundle\", cmd_bundle },\n+\t\t{ \"bundle\", cmd_bundle, RUN_SETUP_GENTLY },\n \t\t{ \"cat-file\", cmd_cat_file, RUN_SETUP },\n \t\t{ \"checkout\", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"checkout-index\", cmd_checkout_index,\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138622","messageId":"1270492888-26589-15-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 14/43] diff: use RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:40:59Z","receivedAt":"2010-04-05T18:40:59Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/diff.c |    6 ++----\n git.c          |    2 +-\n 2 files changed, 3 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/diff.c b/builtin/diff.c\nindex ffcdd05..e4bd855 100644\n--- a/builtin/diff.c\n+++ b/builtin/diff.c\n@@ -252,7 +252,6 @@ int cmd_diff(int argc, const char **argv, const char *prefix)\n \tint ents = 0, blobs = 0, paths = 0;\n \tconst char *path = NULL;\n \tstruct blobinfo blob[2];\n-\tint nongit;\n \tint result = 0;\n \n \t/*\n@@ -278,7 +277,6 @@ int cmd_diff(int argc, const char **argv, const char *prefix)\n \t * Other cases are errors.\n \t */\n \n-\tprefix = setup_git_directory_gently(&nongit);\n \tgit_config(git_diff_ui_config, NULL);\n \n \tif (diff_use_color_default == -1)\n@@ -287,7 +285,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)\n \tinit_revisions(&rev, prefix);\n \n \t/* If this is a no-index diff, just run it and exit there. */\n-\tdiff_no_index(&rev, argc, argv, nongit, prefix);\n+\tdiff_no_index(&rev, argc, argv, !startup_info->have_repository, prefix);\n \n \t/* Otherwise, we are doing the usual \"git\" diff */\n \trev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;\n@@ -296,7 +294,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)\n \tDIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);\n \tDIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);\n \n-\tif (nongit)\n+\tif (!startup_info->have_repository)\n \t\tdie(\"Not a git repository\");\n \targc = setup_revisions(argc, argv, &rev, NULL);\n \tif (!rev.diffopt.output_format) {\ndiff --git a/git.c b/git.c\nindex a90e0c3..02ba3b2 100644\n--- a/git.c\n+++ b/git.c\n@@ -315,7 +315,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"config\", cmd_config, RUN_SETUP_GENTLY },\n \t\t{ \"count-objects\", cmd_count_objects, RUN_SETUP },\n \t\t{ \"describe\", cmd_describe, RUN_SETUP },\n-\t\t{ \"diff\", cmd_diff },\n+\t\t{ \"diff\", cmd_diff, RUN_SETUP_GENTLY },\n \t\t{ \"diff-files\", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"diff-index\", cmd_diff_index, RUN_SETUP },\n \t\t{ \"diff-tree\", cmd_diff_tree, RUN_SETUP },\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138626","messageId":"1270492888-26589-16-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 15/43] help: take note why this command is not applicable for RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:00Z","receivedAt":"2010-04-05T18:41:00Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"From: Jonathan Nieder <jrnieder@gmail.com>\n\n\"git help\" calls setup_git_directory_gently(), thus a candidate for\nRUN_SETUP_GENTLY. However RUN_SETUP_GENTLY should not be used for\nperformance reason, as follows.\n\nIn automount setups like that which prompted v1.6.0-rc0~121^2~1 (Add\nsupport for GIT_CEILING_DIRECTORIES, 2008-05-19), if\nGIT_CEILING_DIRECTORIES is unset, then probing for the Git directory\ncan take a long time.  Thus unnecessarily searching for a git\ndirectory can slow down 'git help -a' (and thus bash completion).\n\n'git help' does not use RUN_SETUP or USE_PAGER, and neither option\nparsing nor producing output for plain 'git help' or 'git help -a'\nrequires access to the git configuration.  Therefore it is safe to not\nsearch for the git directory early in this case.\n\nAlso add some comments to document the requirements this places on\nlist_commands() and list_common_cmds_help().\n\nSigned-off-by: Jonathan Nieder <jrnieder@gmail.com>\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/help.c |    5 +++++\n help.c         |    4 ++++\n 2 files changed, 9 insertions(+), 0 deletions(-)\n\ndiff --git a/builtin/help.c b/builtin/help.c\nindex 3182a2b..1626251 100644\n--- a/builtin/help.c\n+++ b/builtin/help.c\n@@ -271,6 +271,11 @@ static int git_help_config(const char *var, const char *value, void *cb)\n \n static struct cmdnames main_cmds, other_cmds;\n \n+/*\n+ * Used for plain 'git' and 'git help'.\n+ * Therefore this should not use git_config(), nor any other function\n+ * that requires searching for a repository.\n+ */\n void list_common_cmds_help(void)\n {\n \tint i, longest = 0;\ndiff --git a/help.c b/help.c\nindex 7f4928e..d4c3165 100644\n--- a/help.c\n+++ b/help.c\n@@ -221,6 +221,10 @@ void load_command_list(const char *prefix,\n \texclude_cmds(other_cmds, main_cmds);\n }\n \n+/*\n+ * Used for 'git help -a'.  Therefore this should not use git_config(),\n+ * nor any other function that requires searching for a repository.\n+ */\n void list_commands(const char *title, struct cmdnames *main_cmds,\n \t\t   struct cmdnames *other_cmds)\n {\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138623","messageId":"1270492888-26589-17-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 16/43] ls-remote: use RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:01Z","receivedAt":"2010-04-05T18:41:01Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/ls-remote.c |    3 ---\n git.c               |    4 ++--\n 2 files changed, 2 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/ls-remote.c b/builtin/ls-remote.c\nindex 70f5622..998f2c8 100644\n--- a/builtin/ls-remote.c\n+++ b/builtin/ls-remote.c\n@@ -31,7 +31,6 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n {\n \tint i;\n \tconst char *dest = NULL;\n-\tint nongit;\n \tunsigned flags = 0;\n \tconst char *uploadpack = NULL;\n \tconst char **pattern = NULL;\n@@ -40,8 +39,6 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n \tstruct transport *transport;\n \tconst struct ref *ref;\n \n-\tsetup_git_directory_gently(&nongit);\n-\n \tfor (i = 1; i < argc; i++) {\n \t\tconst char *arg = argv[i];\n \ndiff --git a/git.c b/git.c\nindex 02ba3b2..257ddde 100644\n--- a/git.c\n+++ b/git.c\n@@ -338,7 +338,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"log\", cmd_log, RUN_SETUP | USE_PAGER },\n \t\t{ \"ls-files\", cmd_ls_files, RUN_SETUP },\n \t\t{ \"ls-tree\", cmd_ls_tree, RUN_SETUP },\n-\t\t{ \"ls-remote\", cmd_ls_remote },\n+\t\t{ \"ls-remote\", cmd_ls_remote, RUN_SETUP_GENTLY },\n \t\t{ \"mailinfo\", cmd_mailinfo, RUN_SETUP_GENTLY },\n \t\t{ \"mailsplit\", cmd_mailsplit },\n \t\t{ \"merge\", cmd_merge, RUN_SETUP | NEED_WORK_TREE },\n@@ -359,7 +359,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"pack-objects\", cmd_pack_objects, RUN_SETUP },\n \t\t{ \"pack-redundant\", cmd_pack_redundant, RUN_SETUP },\n \t\t{ \"patch-id\", cmd_patch_id },\n-\t\t{ \"peek-remote\", cmd_ls_remote },\n+\t\t{ \"peek-remote\", cmd_ls_remote, RUN_SETUP_GENTLY },\n \t\t{ \"pickaxe\", cmd_blame, RUN_SETUP },\n \t\t{ \"prune\", cmd_prune, RUN_SETUP },\n \t\t{ \"prune-packed\", cmd_prune_packed, RUN_SETUP },\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138625","messageId":"1270492888-26589-18-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 17/43] var: use RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:02Z","receivedAt":"2010-04-05T18:41:02Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\"git var\" needs to read repository config, so setup gitdir first.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/var.c |    2 --\n git.c         |    2 +-\n 2 files changed, 1 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/var.c b/builtin/var.c\nindex 70fdb4d..26324ef 100644\n--- a/builtin/var.c\n+++ b/builtin/var.c\n@@ -75,12 +75,10 @@ static int show_config(const char *var, const char *value, void *cb)\n int cmd_var(int argc, const char **argv, const char *prefix)\n {\n \tconst char *val;\n-\tint nongit;\n \tif (argc != 2) {\n \t\tusage(var_usage);\n \t}\n \n-\tsetup_git_directory_gently(&nongit);\n \tval = NULL;\n \n \tif (strcmp(argv[1], \"-l\") == 0) {\ndiff --git a/git.c b/git.c\nindex 257ddde..567d533 100644\n--- a/git.c\n+++ b/git.c\n@@ -391,7 +391,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"update-ref\", cmd_update_ref, RUN_SETUP },\n \t\t{ \"update-server-info\", cmd_update_server_info, RUN_SETUP },\n \t\t{ \"upload-archive\", cmd_upload_archive },\n-\t\t{ \"var\", cmd_var },\n+\t\t{ \"var\", cmd_var, RUN_SETUP_GENTLY },\n \t\t{ \"verify-tag\", cmd_verify_tag, RUN_SETUP },\n \t\t{ \"version\", cmd_version },\n \t\t{ \"whatchanged\", cmd_whatchanged, RUN_SETUP | USE_PAGER },\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138632","messageId":"1270492888-26589-19-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 18/43] merge-file: use RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:03Z","receivedAt":"2010-04-05T18:41:03Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/merge-file.c |    4 +---\n git.c                |    2 +-\n 2 files changed, 2 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/merge-file.c b/builtin/merge-file.c\nindex 69cc683..493ff76 100644\n--- a/builtin/merge-file.c\n+++ b/builtin/merge-file.c\n@@ -28,7 +28,6 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)\n \txmparam_t xmp = {{XDF_NEED_MINIMAL}};\n \tint ret = 0, i = 0, to_stdout = 0;\n \tint quiet = 0;\n-\tint nongit;\n \tstruct option options[] = {\n \t\tOPT_BOOLEAN('p', \"stdout\", &to_stdout, \"send results to standard output\"),\n \t\tOPT_SET_INT(0, \"diff3\", &xmp.style, \"use a diff3 based merge\", XDL_MERGE_DIFF3),\n@@ -50,8 +49,7 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)\n \txmp.style = 0;\n \txmp.favor = 0;\n \n-\tprefix = setup_git_directory_gently(&nongit);\n-\tif (!nongit) {\n+\tif (startup_info->have_repository) {\n \t\t/* Read the configuration file */\n \t\tgit_config(git_xmerge_config, NULL);\n \t\tif (0 <= git_xmerge_style)\ndiff --git a/git.c b/git.c\nindex 567d533..96c2d21 100644\n--- a/git.c\n+++ b/git.c\n@@ -343,7 +343,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"mailsplit\", cmd_mailsplit },\n \t\t{ \"merge\", cmd_merge, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"merge-base\", cmd_merge_base, RUN_SETUP },\n-\t\t{ \"merge-file\", cmd_merge_file },\n+\t\t{ \"merge-file\", cmd_merge_file, RUN_SETUP_GENTLY },\n \t\t{ \"merge-index\", cmd_merge_index, RUN_SETUP },\n \t\t{ \"merge-ours\", cmd_merge_ours, RUN_SETUP },\n \t\t{ \"merge-recursive\", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138643","messageId":"1270492888-26589-20-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 19/43] worktree setup: calculate prefix even if no worktree is found","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:04Z","receivedAt":"2010-04-05T18:41:04Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Commit a672ea6 (rehabilitate 'git index-pack' inside the object\nstore - 2008-10-20) worked around a fault in\nsetup_git_directory_gently(). When walking up from inside a git\nrepository, we will return NULL as prefix.  We should have returned a\ncorrect prefix anyway because cwd has been moved.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n setup.c              |   19 ++++++++-----------\n t/t1500-rev-parse.sh |    2 +-\n t/t1501-worktree.sh  |    5 +++++\n 3 files changed, 14 insertions(+), 12 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex bb3648c..8796c6f 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -392,13 +392,16 @@ 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) {\n-\t\t\tif (set_git_dir(gitfile_dir))\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\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\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 \t\t}\n-\t\tif (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))\n-\t\t\tbreak;\n \t\tif (is_git_directory(\".\")) {\n \t\t\tinside_git_dir = 1;\n \t\t\tif (!work_tree_env)\n@@ -409,8 +412,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)\n \t\t\t\tset_git_dir(cwd);\n \t\t\t} else\n \t\t\t\tset_git_dir(\".\");\n-\t\t\tcheck_repository_format_gently(nongit_ok);\n-\t\t\treturn NULL;\n+\t\t\tbreak;\n \t\t}\n \t\twhile (--offset > ceil_offset && cwd[offset] != '/');\n \t\tif (offset <= ceil_offset) {\n@@ -426,11 +428,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-\tinside_git_dir = 0;\n-\tif (!work_tree_env)\n-\t\tinside_work_tree = 1;\n-\troot_len = offset_1st_component(cwd);\n-\tgit_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);\n \tif (check_repository_format_gently(nongit_ok))\n \t\treturn NULL;\n \tif (offset == len)\ndiff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh\nindex 48ee077..4ee92c7 100755\n--- a/t/t1500-rev-parse.sh\n+++ b/t/t1500-rev-parse.sh\n@@ -42,7 +42,7 @@ test_rev_parse toplevel false false true '' .git\n cd .git || exit 1\n test_rev_parse .git/ false true false '' .\n cd objects || exit 1\n-test_rev_parse .git/objects/ false true false '' \"$ROOT/.git\"\n+test_rev_parse .git/objects/ false true false 'objects/' \"$ROOT/.git\"\n cd ../.. || exit 1\n \n mkdir -p sub/dir || exit 1\ndiff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh\nindex 9df3012..c72b578 100755\n--- a/t/t1501-worktree.sh\n+++ b/t/t1501-worktree.sh\n@@ -28,6 +28,11 @@ test_rev_parse() {\n \t[ $# -eq 0 ] && return\n }\n \n+cd .git/objects || exit 1\n+say \"subdirectory inside .git\"\n+test_rev_parse 'inside'       false true false objects/\n+cd ../.. || exit 1\n+\n EMPTY_TREE=$(git write-tree)\n mkdir -p work/sub/dir || exit 1\n mv .git repo.git || exit 1\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138644","messageId":"1270492888-26589-21-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 20/43] index-pack: trust the prefix returned by setup_git_directory_gently()","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:05Z","receivedAt":"2010-04-05T18:41:05Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/index-pack.c |   21 ++++++---------------\n 1 files changed, 6 insertions(+), 15 deletions(-)\n\ndiff --git a/builtin/index-pack.c b/builtin/index-pack.c\nindex b4cf8c5..2a6359d 100644\n--- a/builtin/index-pack.c\n+++ b/builtin/index-pack.c\n@@ -879,29 +879,20 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)\n \tchar *index_name_buf = NULL, *keep_name_buf = NULL;\n \tstruct pack_idx_entry **idx_objects;\n \tunsigned char pack_sha1[20];\n+\tint nongit;\n \n \tif (argc == 2 && !strcmp(argv[1], \"-h\"))\n \t\tusage(index_pack_usage);\n \n \t/*\n-\t * We wish to read the repository's config file if any, and\n-\t * for that it is necessary to call setup_git_directory_gently().\n+\t * We wish to read the repository's config file if any.\n \t * However if the cwd was inside .git/objects/pack/ then we need\n \t * to go back there or all the pack name arguments will be wrong.\n-\t * And in that case we cannot rely on any prefix returned by\n-\t * setup_git_directory_gently() either.\n \t */\n-\t{\n-\t\tchar cwd[PATH_MAX+1];\n-\t\tint nongit;\n-\n-\t\tif (!getcwd(cwd, sizeof(cwd)-1))\n-\t\t\tdie(\"Unable to get current working directory\");\n-\t\tsetup_git_directory_gently(&nongit);\n-\t\tgit_config(git_index_pack_config, NULL);\n-\t\tif (chdir(cwd))\n-\t\t\tdie(\"Cannot come back to cwd\");\n-\t}\n+\tprefix = setup_git_directory_gently(&nongit);\n+\tgit_config(git_index_pack_config, NULL);\n+\tif (prefix && chdir(prefix))\n+\t\tdie(\"Cannot come back to cwd\");\n \n \tfor (i = 1; i < argc; i++) {\n \t\tconst char *arg = argv[i];\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138635","messageId":"1270492888-26589-22-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 21/43] index-pack: use RUN_SETUP_GENTLY","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:06Z","receivedAt":"2010-04-05T18:41:06Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/index-pack.c |    2 --\n git.c                |    2 +-\n 2 files changed, 1 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/index-pack.c b/builtin/index-pack.c\nindex 2a6359d..9aa6a13 100644\n--- a/builtin/index-pack.c\n+++ b/builtin/index-pack.c\n@@ -879,7 +879,6 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)\n \tchar *index_name_buf = NULL, *keep_name_buf = NULL;\n \tstruct pack_idx_entry **idx_objects;\n \tunsigned char pack_sha1[20];\n-\tint nongit;\n \n \tif (argc == 2 && !strcmp(argv[1], \"-h\"))\n \t\tusage(index_pack_usage);\n@@ -889,7 +888,6 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)\n \t * However if the cwd was inside .git/objects/pack/ then we need\n \t * to go back there or all the pack name arguments will be wrong.\n \t */\n-\tprefix = setup_git_directory_gently(&nongit);\n \tgit_config(git_index_pack_config, NULL);\n \tif (prefix && chdir(prefix))\n \t\tdie(\"Cannot come back to cwd\");\ndiff --git a/git.c b/git.c\nindex 96c2d21..fac3d1c 100644\n--- a/git.c\n+++ b/git.c\n@@ -332,7 +332,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"grep\", cmd_grep, RUN_SETUP_GENTLY | USE_PAGER },\n \t\t{ \"hash-object\", cmd_hash_object, RUN_SETUP_GENTLY },\n \t\t{ \"help\", cmd_help },\n-\t\t{ \"index-pack\", cmd_index_pack },\n+\t\t{ \"index-pack\", cmd_index_pack, RUN_SETUP_GENTLY },\n \t\t{ \"init\", cmd_init_db },\n \t\t{ \"init-db\", cmd_init_db },\n \t\t{ \"log\", cmd_log, RUN_SETUP | USE_PAGER },\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138627","messageId":"1270492888-26589-23-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 22/43] Move enter_repo() to setup.c","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:07Z","receivedAt":"2010-04-05T18:41:07Z","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>\nSigned-off-by: Junio C Hamano <gitster@pobox.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.rc1.541.g2da82.dirty\n"},{"id":"138639","messageId":"1270492888-26589-24-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 23/43] 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-04-05T18:41:08Z","receivedAt":"2010-04-05T18:41:08Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.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.rc1.541.g2da82.dirty\n"},{"id":"138637","messageId":"1270492888-26589-25-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 24/43] rev-parse --git-dir: print relative gitdir correctly","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:09Z","receivedAt":"2010-04-05T18:41:09Z","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>\nSigned-off-by: Junio C Hamano <gitster@pobox.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.rc1.541.g2da82.dirty\n"},{"id":"138638","messageId":"1270492888-26589-26-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 25/43] worktree setup: call set_git_dir explicitly","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:10Z","receivedAt":"2010-04-05T18:41:10Z","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>\nSigned-off-by: Junio C Hamano <gitster@pobox.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.rc1.541.g2da82.dirty\n"},{"id":"138628","messageId":"1270492888-26589-27-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 26/43] Add git_config_early()","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:11Z","receivedAt":"2010-04-05T18:41:11Z","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>\nSigned-off-by: Junio C Hamano <gitster@pobox.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 fa13bf7..f4935b8 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -937,6 +937,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.rc1.541.g2da82.dirty\n"},{"id":"138630","messageId":"1270492888-26589-28-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 27/43] 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-04-05T18:41:12Z","receivedAt":"2010-04-05T18:41:12Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.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.rc1.541.g2da82.dirty\n"},{"id":"138629","messageId":"1270492888-26589-29-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 28/43] worktree setup: restore original state when things go wrong","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:13Z","receivedAt":"2010-04-05T18:41:13Z","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 |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++----\n 2 files changed, 52 insertions(+), 4 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex f4935b8..f53fb32 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -421,6 +421,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..858cf6b 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -323,6 +323,42 @@ 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+\t/*\n+\t * repository_format_version is zero for two reasons:\n+\t * - For really old repos, there won't be core.repositoryformatversion\n+\t *   we treat it as supported version zero.\n+\t * - For repo creating commands like \"git init\" or \"git clone\"\n+\t *   $GIT_DIR/config may not be there, just let it pass, the\n+\t *   repository will be properly initialized later.\n+\t * For history of this setting see commits 4f62953 and 96f1e58\n+\t */\n+\trepository_format_version = 0;\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 +439,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 +453,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 +470,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.rc1.541.g2da82.dirty\n"},{"id":"138631","messageId":"1270492888-26589-30-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 29/43] init/clone: turn on startup->have_repository properly","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:14Z","receivedAt":"2010-04-05T18:41:14Z","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>\nSigned-off-by: Junio C Hamano <gitster@pobox.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 05f8fb4..3badb44 100644\n--- a/builtin/clone.c\n+++ b/builtin/clone.c\n@@ -459,9 +459,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_verbosity < 0) ? INIT_DB_QUIET : 0);\n+\tinit_db(git_dir, option_template, (option_verbosity < 0) ? 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 f53fb32..2980741 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -430,7 +430,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.rc1.541.g2da82.dirty\n"},{"id":"138634","messageId":"1270492888-26589-31-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 30/43] 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-04-05T18:41:15Z","receivedAt":"2010-04-05T18:41:15Z","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>\nSigned-off-by: Junio C Hamano <gitster@pobox.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.rc1.541.g2da82.dirty\n"},{"id":"138653","messageId":"1270492888-26589-32-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 31/43] 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-04-05T18:41:16Z","receivedAt":"2010-04-05T18:41:16Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.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 cb83332..0bd5d9f 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -1040,9 +1040,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.rc1.541.g2da82.dirty\n"},{"id":"138652","messageId":"1270492888-26589-33-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 32/43] 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-04-05T18:41:17Z","receivedAt":"2010-04-05T18:41:17Z","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>\nSigned-off-by: Junio C Hamano <gitster@pobox.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.rc1.541.g2da82.dirty\n"},{"id":"138633","messageId":"1270492888-26589-34-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 33/43] apply: do not check sha1 if there is no repository","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:18Z","receivedAt":"2010-04-05T18:41:18Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.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 bb957d4..0f269aa 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -2546,7 +2546,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.rc1.541.g2da82.dirty\n"},{"id":"138636","messageId":"1270492888-26589-35-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 34/43] 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-04-05T18:41:19Z","receivedAt":"2010-04-05T18:41:19Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.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.rc1.541.g2da82.dirty\n"},{"id":"138641","messageId":"1270492888-26589-36-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 35/43] run_builtin(): save \"-h\" detection result for later use","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:20Z","receivedAt":"2010-04-05T18:41:20Z","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>\nSigned-off-by: Junio C Hamano <gitster@pobox.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 2980741..bdcd1ec 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1067,6 +1067,7 @@ int checkout_fast_forward(const unsigned char *from, const unsigned char *to);\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 fac3d1c..d60f792 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.rc1.541.g2da82.dirty\n"},{"id":"138640","messageId":"1270492888-26589-37-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 36/43] builtins: utilize startup_info->help where possible","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:21Z","receivedAt":"2010-04-05T18:41:21Z","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>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/check-ref-format.c |    2 +-\n builtin/grep.c             |    2 +-\n builtin/index-pack.c       |    2 +-\n builtin/log.c              |    6 +-----\n builtin/merge-ours.c       |    2 +-\n builtin/pack-redundant.c   |    2 +-\n builtin/show-ref.c         |    2 +-\n 7 files changed, 7 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 a4ea0c9..7b5e416 100644\n--- a/builtin/grep.c\n+++ b/builtin/grep.c\n@@ -871,7 +871,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 542ecc7..294402b 100644\n--- a/builtin/log.c\n+++ b/builtin/log.c\n@@ -50,11 +50,7 @@ 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 \targc = setup_revisions(argc, argv, rev, opt);\n \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.rc1.541.g2da82.dirty\n"},{"id":"138642","messageId":"1270492888-26589-38-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 37/43] builtins: check for startup_info->help, print and exit early","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:22Z","receivedAt":"2010-04-05T18:41:22Z","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>\nSigned-off-by: Junio C Hamano <gitster@pobox.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 c5ab683..bccbbef 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -1034,6 +1034,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@@ -1208,6 +1211,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 c043066..3cced9e 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.rc1.541.g2da82.dirty\n"},{"id":"138649","messageId":"1270492888-26589-39-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 38/43] 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-04-05T18:41:23Z","receivedAt":"2010-04-05T18:41:23Z","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>\nSigned-off-by: Junio C Hamano <gitster@pobox.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 bdcd1ec..137edac 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -422,6 +422,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 d60f792..17482dd 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 858cf6b..b0269aa 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.rc1.541.g2da82.dirty\n"},{"id":"138646","messageId":"1270492888-26589-40-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 39/43] 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-04-05T18:41:24Z","receivedAt":"2010-04-05T18:41:24Z","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>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n git.c |   15 ++++++++++-----\n 1 files changed, 10 insertions(+), 5 deletions(-)\n\ndiff --git a/git.c b/git.c\nindex 17482dd..d6513f1 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.rc1.541.g2da82.dirty\n"},{"id":"138647","messageId":"1270492888-26589-41-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 40/43] 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-04-05T18:41:25Z","receivedAt":"2010-04-05T18:41:25Z","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             |   16 +++++++++++-----\n setup.c           |   13 +++++++++++++\n 6 files changed, 39 insertions(+), 7 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 137edac..1e6fd56 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1067,6 +1067,7 @@ int checkout_fast_forward(const unsigned char *from, const unsigned char *to);\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 d6513f1..43cebd7 100644\n--- a/git.c\n+++ b/git.c\n@@ -245,11 +245,14 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)\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\telse if (startup_info->have_run_setup_gitdir) {\n+\t\t\tif (startup_info->have_repository) {\n+\t\t\t\tif (p->option & (RUN_SETUP_GENTLY | RUN_SETUP))\n+\t\t\t\t\t; /* done already */\n+\t\t\t\telse\n+\t\t\t\t\tunset_git_directory(startup_info->prefix);\n+\t\t\t}\n+\t\t\tstartup_info->have_run_setup_gitdir = 0;\n \t\t}\n \n \t\tif (use_pager == -1 && p->option & RUN_SETUP)\n@@ -260,6 +263,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 b0269aa..89757fc 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@@ -515,6 +526,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@@ -609,6 +621,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.rc1.541.g2da82.dirty\n"},{"id":"138648","messageId":"1270492888-26589-42-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 41/43] t0001: Add test cases for \"git init\" with aliases","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:26Z","receivedAt":"2010-04-05T18:41:26Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"From: Jonathan Nieder <jrnieder@gmail.com>\n\nSigned-off-by: Jonathan Nieder <jrnieder@gmail.com>\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n t/t0001-init.sh |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++\n 1 files changed, 52 insertions(+), 0 deletions(-)\n\ndiff --git a/t/t0001-init.sh b/t/t0001-init.sh\nindex 6757734..dd66352 100755\n--- a/t/t0001-init.sh\n+++ b/t/t0001-init.sh\n@@ -33,6 +33,58 @@ test_expect_success 'plain' '\n \tcheck_config plain/.git false unset\n '\n \n+test_expect_success 'plain nested in bare' '\n+\t(\n+\t\tunset GIT_DIR GIT_WORK_TREE &&\n+\t\tgit init --bare bare-ancestor.git &&\n+\t\tcd bare-ancestor.git &&\n+\t\tmkdir plain-nested &&\n+\t\tcd plain-nested &&\n+\t\tgit init\n+\t) &&\n+\tcheck_config bare-ancestor.git/plain-nested/.git false unset\n+'\n+\n+test_expect_success 'plain through aliased command' '\n+\t(\n+\t\tunset GIT_DIR GIT_WORK_TREE GIT_CONFIG_NOGLOBAL &&\n+\t\tHOME=$(pwd)/alias-config &&\n+\t\texport HOME &&\n+\t\tmkdir alias-config &&\n+\t\techo \"[alias] aliasedinit = init\" >alias-config/.gitconfig &&\n+\t\tmkdir plain-aliased &&\n+\t\tcd plain-aliased &&\n+\t\tgit aliasedinit\n+\t) &&\n+\tcheck_config plain-aliased/.git false unset\n+'\n+\n+test_expect_success 'plain nested through aliased command' '\n+\t(\n+\t\tunset GIT_DIR GIT_WORK_TREE &&\n+\t\tgit init plain-ancestor-aliased &&\n+\t\tcd plain-ancestor-aliased &&\n+\t\techo \"[alias] aliasedinit = init\" >>.git/config &&\n+\t\tmkdir plain-nested &&\n+\t\tcd plain-nested &&\n+\t\tgit aliasedinit\n+\t) &&\n+\tcheck_config plain-ancestor-aliased/plain-nested/.git false unset\n+'\n+\n+test_expect_success 'plain nested in bare through aliased command' '\n+\t(\n+\t\tunset GIT_DIR GIT_WORK_TREE &&\n+\t\tgit init --bare bare-ancestor-aliased.git &&\n+\t\tcd bare-ancestor-aliased.git &&\n+\t\techo \"[alias] aliasedinit = init\" >>config &&\n+\t\tmkdir plain-nested &&\n+\t\tcd plain-nested &&\n+\t\tgit aliasedinit\n+\t) &&\n+\tcheck_config bare-ancestor-aliased.git/plain-nested/.git false unset\n+'\n+\n test_expect_success 'plain with GIT_WORK_TREE' '\n \tif (\n \t\tunset GIT_DIR\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138645","messageId":"1270492888-26589-43-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 42/43] builtins: setup repository before print unknown command error","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:27Z","receivedAt":"2010-04-05T18:41:27Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"help_unknown_cmd() will need to look into repository's config, in\norder to collect all possible commands/aliases and give a\nsuggestion. So, repository must be set up before this function is\ncalled.\n\nAs it is now, because\n - alias handling will always be done before help_unknown_cmd()\n - alias handling code will search and set up repository if found\n - alias handline code will not undo repository setup\n\nThese ensure that repository will always be set up (or attempted to\nset up) before help_unknown_cmd(), so there is no issue. But the setup\ndependency here is subtle. It may break some day if someone reorders\nthe loop, for example.\n\nMake a note about this.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nReviewed-by: Jonathan Nieder <jrnieder@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n git.c |    7 +++++++\n 1 files changed, 7 insertions(+), 0 deletions(-)\n\ndiff --git a/git.c b/git.c\nindex 43cebd7..1fb478f 100644\n--- a/git.c\n+++ b/git.c\n@@ -555,6 +555,13 @@ int main(int argc, const char **argv)\n \t\t\texit(1);\n \t\t}\n \t\tif (!done_help) {\n+\t\t\t/*\n+\t\t\t * help_unknown_cmd() requires that a repository has been\n+\t\t\t * searched for and set up if found.\n+\t\t\t * Luckily, the alias handling code already took care of this.\n+\t\t\t */\n+\t\t\tif (!startup_info->have_run_setup_gitdir)\n+\t\t\t\tdie(\"internal error: handling unknown command\");\n \t\t\tcmd = argv[0] = help_unknown_cmd(cmd);\n \t\t\tdone_help = 1;\n \t\t} else\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138650","messageId":"1270492888-26589-44-git-send-email-pclouds@gmail.com","threadId":"23339","inReplyTo":"1270492888-26589-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 43/43] builtins: do not commit pager choice early","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-05T18:41:28Z","receivedAt":"2010-04-05T18:41:28Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Committing pager choice may require setting up the pager, which\nwill need access to repository.\n\nAt the time after handle_options() is called, the repository has not\nbeen found yet. As a result, unallowed access to repository may\nhappen.\n\nThere are several possible code path after\nhandle_options()/commit_pager_choice() is called:\n\n1. list_common_cmds_help()\n2. run_argv()\n3. help_unknown_cmd()\n\nCase 2. will have commit_pager_choice() called inside run_builtin() if\na command is found. Case 1. and 3. won't need a pager, it's short\nprintout and should be fitted within a screen. So, removing\ncommit_pager_choice() call after handle_options() is safe.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n git.c |    1 -\n 1 files changed, 0 insertions(+), 1 deletions(-)\n\ndiff --git a/git.c b/git.c\nindex 1fb478f..060cfe8 100644\n--- a/git.c\n+++ b/git.c\n@@ -521,7 +521,6 @@ int main(int argc, const char **argv)\n \targv++;\n \targc--;\n \thandle_options(&argv, &argc, NULL);\n-\tcommit_pager_choice();\n \tif (argc > 0) {\n \t\tif (!prefixcmp(argv[0], \"--\"))\n \t\t\targv[0] += 2;\n-- \n1.7.0.rc1.541.g2da82.dirty\n"},{"id":"138699","messageId":"20100406040145.GA30403@progeny.tock","threadId":"23339","inReplyTo":"1270492888-26589-44-git-send-email-pclouds@gmail.com","subject":"[PATCH 0/2] fix weird git --paginate behavior","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2010-04-06T04:01:46Z","receivedAt":"2010-04-06T04:01:46Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Hi,\n\nNguyễn Thái Ngọc Duy wrote:\n\n> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n\nThanks for posting this series.  I had not read it in full before.\n\nThe series is very long and it is hard to make certain it does not\nintroduce regressions.  Here is my idea for working around this: the\nidea is to grab patches from the series to form short, independently\njustifiable mini-series.  Once most of the functional changes are\nmerged, it should be a lot easier to evaluate the core code change\nthat makes setup more brittle (which I like a lot).\n\nWhat do you think?  Is it worth the trouble?\n\nHere’s an example mini-series.  Patch 1 adds a test case for\nsome weird git -p behavior.  Patch 2 fixes it.\n\nJonathan Nieder (1):\n  t7006: test an edge case for git --paginate\n\nNguyễn Thái Ngọc Duy (1):\n  builtins: do not commit pager choice early\n\n git.c            |    1 -\n t/t7006-pager.sh |   32 ++++++++++++++++++++++++++++++++\n 2 files changed, 32 insertions(+), 1 deletions(-)\n"},{"id":"138700","messageId":"20100406040350.GB30403@progeny.tock","threadId":"23339","inReplyTo":"20100406040145.GA30403@progeny.tock","subject":"[PATCH 1/2] t7006: test core.pager configuration in subdir of toplevel","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2010-04-06T04:03:50Z","receivedAt":"2010-04-06T04:03:50Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"If git is passed the --paginate option, committing the pager choice\nwill require setting up the pager, which requires access to repository\nfor the core.pager configuration.\n\nUnfortunately, the --paginate option is handled before git has a\nchance to search for a git directory.  The effect is that with\n--paginate and only with --paginate, a repository-local core.pager\nsetting does not take effect unless the git directory is simply .git\nin the cwd (or GIT_DIR or GIT_CONFIG was explicitly set).\n\nAdd a test to demonstrate this problem.\n\nNoticed while reading over a patch by Nguyễn Thái Ngọc Duy that\nfixes it.\n\nCc: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Jonathan Nieder <jrnieder@gmail.com>\n---\n t/t7006-pager.sh |   32 ++++++++++++++++++++++++++++++++\n 1 files changed, 32 insertions(+), 0 deletions(-)\n\ndiff --git a/t/t7006-pager.sh b/t/t7006-pager.sh\nindex d9202d5..4f804ed 100755\n--- a/t/t7006-pager.sh\n+++ b/t/t7006-pager.sh\n@@ -164,6 +164,38 @@ test_expect_success TTY 'core.pager overrides PAGER' '\n \ttest -e core.pager_used\n '\n \n+unset GIT_PAGER\n+rm -f core.pager_used\n+rm -fr sub\n+test_expect_success TTY 'core.pager in subdir' '\n+\tPAGER=wc &&\n+\tstampname=$(pwd)/core.pager_used &&\n+\texport PAGER stampname &&\n+\tgit config core.pager \"wc > \\\"\\$stampname\\\"\" &&\n+\tmkdir sub &&\n+\t(\n+\t\tcd sub &&\n+\t\ttest_terminal git log\n+\t) &&\n+\ttest -e \"$stampname\"\n+'\n+\n+unset GIT_PAGER\n+rm -f core.pager_used\n+rm -fr sub\n+test_expect_failure TTY 'core.pager in subdir with --paginate' '\n+\tPAGER=wc &&\n+\tstampname=$(pwd)/core.pager_used &&\n+\texport PAGER stampname &&\n+\tgit config core.pager \"wc > \\\"\\$stampname\\\"\" &&\n+\tmkdir sub &&\n+\t(\n+\t\tcd sub &&\n+\t\ttest_terminal git --paginate log\n+\t) &&\n+\ttest -e \"$stampname\"\n+'\n+\n rm -f GIT_PAGER_used\n test_expect_success TTY 'GIT_PAGER overrides core.pager' '\n \tgit config core.pager wc &&\n-- \n1.7.0.4.369.g62d9d\n"},{"id":"138702","messageId":"20100406040612.GC30403@progeny.tock","threadId":"23339","inReplyTo":"20100406040145.GA30403@progeny.tock","subject":"[PATCH 2/2] builtins: do not commit pager choice early","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2010-04-06T04:06:12Z","receivedAt":"2010-04-06T04:06:12Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"If git is passed the --paginate option, committing the pager choice\nwill require setting up the pager, which requires access to repository\nfor the core.pager configuration.\n\nAfter handle_options() is called, the repository has not been searched\nfor yet.  Unless GIT_DIR or GIT_CONFIG is set, attempting to access\nthe configuration at this point results in git_dir being set to .git,\nwhich is almost certainly not what was wanted.\n\nFor example, this breaks git --paginate when used from a subdirectory\nof the toplevel directory in a repository with core.pager set.\n\nThere are several possible code paths after handle_options() and\ncommit_pager_choice() are called:\n\n1. list_common_cmds_help() does not need a pager: it’s a printout of\n   only 29 lines, and most operating systems have some sort of\n   scrollback that can handle this.\n\n2. run_argv() will run commit_pager_choice() inside run_builtin() if a\n   command is found.\n\n3. help_unknown_cmd() prints out only a few lines.\n\nSo removing the commit_pager_choice() after handle_options() is safe.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Jonathan Nieder <jrnieder@gmail.com>\n---\nThat’s the end of the series (based against master).\n\nThoughts welcome.\n\n git.c            |    1 -\n t/t7006-pager.sh |    2 +-\n 2 files changed, 1 insertions(+), 2 deletions(-)\n\ndiff --git a/git.c b/git.c\nindex 6bae305..75bd387 100644\n--- a/git.c\n+++ b/git.c\n@@ -502,7 +502,6 @@ int main(int argc, const char **argv)\n \targv++;\n \targc--;\n \thandle_options(&argv, &argc, NULL);\n-\tcommit_pager_choice();\n \tif (argc > 0) {\n \t\tif (!prefixcmp(argv[0], \"--\"))\n \t\t\targv[0] += 2;\ndiff --git a/t/t7006-pager.sh b/t/t7006-pager.sh\nindex 4f804ed..c373342 100755\n--- a/t/t7006-pager.sh\n+++ b/t/t7006-pager.sh\n@@ -183,7 +183,7 @@ test_expect_success TTY 'core.pager in subdir' '\n unset GIT_PAGER\n rm -f core.pager_used\n rm -fr sub\n-test_expect_failure TTY 'core.pager in subdir with --paginate' '\n+test_expect_success TTY 'core.pager in subdir with --paginate' '\n \tPAGER=wc &&\n \tstampname=$(pwd)/core.pager_used &&\n \texport PAGER stampname &&\n-- \n1.7.0.4.369.g62d9d\n"},{"id":"138703","messageId":"20100406041651.GD30403@progeny.tock","threadId":"23339","inReplyTo":"20100406040612.GC30403@progeny.tock","subject":"Re: [PATCH 2/2] builtins: do not commit pager choice early","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2010-04-06T04:16:51Z","receivedAt":"2010-04-06T04:16:51Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Jonathan Nieder wrote:\n\n> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>\n\nI forgot a pseudo-header field:\n\n  From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nSorry for the trouble.\n"},{"id":"138717","messageId":"x2zfcaeb9bf1004060000s7672df7dw5804ad54cb272a18@mail.gmail.com","threadId":"23339","inReplyTo":"20100406040145.GA30403@progeny.tock","subject":"Re: [PATCH 0/2] fix weird git --paginate behavior","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-04-06T07:00:03Z","receivedAt":"2010-04-06T07:00:03Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"2010/4/6 Jonathan Nieder <jrnieder@gmail.com>:\n> Hi,\n>\n> Nguyễn Thái Ngọc Duy wrote:\n>\n>> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n>> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n>\n> Thanks for posting this series.  I had not read it in full before.\n>\n> The series is very long and it is hard to make certain it does not\n> introduce regressions.  Here is my idea for working around this: the\n> idea is to grab patches from the series to form short, independently\n> justifiable mini-series.  Once most of the functional changes are\n> merged, it should be a lot easier to evaluate the core code change\n> that makes setup more brittle (which I like a lot).\n>\n> What do you think?  Is it worth the trouble?\n\nI don't know. There are few independent patches like this. Most of\nthem will need startup_info struct, introduced in 01/43. I splitted\nthe series in two parts, with hope that the first (simpler) part could\ngraduate early. But as you pointed out, even the first part introduced\na regression.\n\nAnyway if you want to pick them out, candidates are:\n - 19-20/43: calculate prefix anyway and remove the hack in index-pack\n - 22-23/43: moving enter_repo(). This makes enter_repo() more\nconsistent with setup_git_dir*, no real gain.\n - 24/43: the bug only exposes when set_git_dir() is used extensively\nin setup_git_dir*, so this patch alone seems useless.\n\nSo that's 7 patches shorter if all of them are sent independently.\n-- \nDuy\n"},{"id":"138719","messageId":"7v1vetjbi2.fsf@alter.siamese.dyndns.org","threadId":"23339","inReplyTo":"20100406040145.GA30403@progeny.tock","subject":"Re: [PATCH 0/2] fix weird git --paginate behavior","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-04-06T07:17:25Z","receivedAt":"2010-04-06T07:17:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jonathan Nieder <jrnieder@gmail.com> writes:\n\n> Nguyễn Thái Ngọc Duy wrote:\n>\n>> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n>> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n>\n> Thanks for posting this series.  I had not read it in full before.\n>\n> The series is very long and it is hard to make certain it does not\n> introduce regressions.  Here is my idea for working around this: the\n> idea is to grab patches from the series to form short, independently\n> justifiable mini-series.\n\nThanks for trying out your idea.\n\nI've been having trouble reviewing this series.  I can read bits and\npieces and find things that I mostly agree with, or things that I find\nquestionable.  But in either case, I cannot be sure about my judgement\nwithout having a clear idea of where the series is trying to go upfront.\n\nWhile a re-roll will certainly give us a chance to review the series\n\"cover-to-cover\", it would make it much easier to swallow if this can be\nsplit into independently digestable bite-sized pieces.  I am somewhat\nskeptical if a major part of the series will become that smaller,\nthough...\n"}]}