From 69743f9b4f8411853e347fbd392221552b43fd12 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 14 Jun 2017 13:35:26 +0200 Subject: discover_git_directory(): avoid setting invalid git_dir When discovering a .git/ directory, we take pains to ensure that its repository format version matches Git's expectations, and we return NULL otherwise. However, we still appended the invalid path to the strbuf passed as argument. Let's just reset the strbuf to the state before we appended the .git/ directory that was eventually rejected. There is another early return path in that function, when setup_git_directory_gently_1() returns GIT_DIR_NONE or an error. In that case, the gitdir parameter has not been touched, therefore there is no need for an equivalent change in that code path. Signed-off-by: Johannes Schindelin Reviewed-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/setup.c b/setup.c index 0309c27..ba3241b 100644 --- a/setup.c +++ b/setup.c @@ -977,6 +977,7 @@ const char *discover_git_directory(struct strbuf *gitdir) warning("ignoring git dir '%s': %s", gitdir->buf + gitdir_offset, err.buf); strbuf_release(&err); + strbuf_setlen(gitdir, gitdir_offset); return NULL; } -- cgit v0.10.2-6-g49f6 From e2e142510762712b4b005dca6c7a9676f93a3278 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 14 Jun 2017 13:35:46 +0200 Subject: config: report correct line number upon error When get_value() parses a key/value pair, it is possible that the line number is decreased (because the \n has been consumed already) before the key/value pair is passed to the callback function, to allow for the correct line to be attributed in case of an error. However, when git_parse_source() asks get_value() to parse the key/value pair, the error reporting is performed *after* get_value() returns. Which means that we have to be careful not to increase the line number in get_value() after the callback function returned an error. Signed-off-by: Johannes Schindelin Reviewed-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/config.c b/config.c index b4a3205..3df7515 100644 --- a/config.c +++ b/config.c @@ -588,7 +588,8 @@ static int get_value(config_fn_t fn, void *data, struct strbuf *name) */ cf->linenr--; ret = fn(name->buf, value, data); - cf->linenr++; + if (ret >= 0) + cf->linenr++; return ret; } diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh index afcca0d..f664bfc 100755 --- a/t/t1300-repo-config.sh +++ b/t/t1300-repo-config.sh @@ -703,6 +703,12 @@ test_expect_success 'invalid unit' ' test_i18ngrep "bad numeric config value .1auto. for .aninvalid.unit. in file .git/config: invalid unit" actual ' +test_expect_success 'line number is reported correctly' ' + printf "[bool]\n\tvar\n" >invalid && + test_must_fail git config -f invalid --path bool.var 2>actual && + test_i18ngrep "line 2" actual +' + test_expect_success 'invalid stdin config' ' echo "[broken" | test_must_fail git config --list --file - >output 2>&1 && test_i18ngrep "bad config line 1 in standard input" output -- cgit v0.10.2-6-g49f6 From 659fef199fb52d4a5cb24f1dfd2b272980fc5038 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 14 Jun 2017 13:35:50 +0200 Subject: help: use early config when autocorrecting aliases Git has this feature which suggests similar commands (including aliases) in case the user specified an unknown command. This feature currently relies on a side effect of the way we expand aliases right now: when a command is not a builtin, we use the regular config machinery (meaning: discovering the .git/ directory and initializing global state such as the config cache) to see whether the command refers to an alias. However, we will change the way aliases are expanded in the next commits, to use the early config instead. That means that the autocorrect feature can no longer discover the available aliases by looking at the config cache (because it has not yet been initialized). So let's just use the early config machinery instead. This is slightly less performant than the previous way, as the early config is used *twice*: once to see whether the command refers to an alias, and then to see what aliases are most similar. However, this is hardly a performance-critical code path, so performance is less important here. Signed-off-by: Johannes Schindelin Reviewed-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/help.c b/help.c index bc6cd19..91ec8ab 100644 --- a/help.c +++ b/help.c @@ -330,7 +330,7 @@ const char *help_unknown_cmd(const char *cmd) memset(&other_cmds, 0, sizeof(other_cmds)); memset(&aliases, 0, sizeof(aliases)); - git_config(git_unknown_cmd_config, NULL); + read_early_config(git_unknown_cmd_config, NULL); load_command_list("git-", &main_cmds, &other_cmds); -- cgit v0.10.2-6-g49f6 From e4feff4898f4705cc8df50e3dc152ed45ef06db6 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 14 Jun 2017 13:35:53 +0200 Subject: t1308: relax the test verifying that empty alias values are disallowed We are about to change the way aliases are expanded, to use the early config machinery. This machinery reports errors in a slightly different manner than the cached config machinery. Let's not get hung up by the precise wording of the message mentioning the line number. It is really sufficient to verify that all the relevant information is given to the user. Signed-off-by: Johannes Schindelin Reviewed-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/t/t1308-config-set.sh b/t/t1308-config-set.sh index ff50960..69a0aa5 100755 --- a/t/t1308-config-set.sh +++ b/t/t1308-config-set.sh @@ -215,7 +215,9 @@ test_expect_success 'check line errors for malformed values' ' br EOF test_expect_code 128 git br 2>result && - test_i18ngrep "fatal: .*alias\.br.*\.git/config.*line 2" result + test_i18ngrep "missing value for .alias\.br" result && + test_i18ngrep "fatal: .*\.git/config" result && + test_i18ngrep "fatal: .*line 2" result ' test_expect_success 'error on modifying repo config without repo' ' -- cgit v0.10.2-6-g49f6 From 3f9c5dfb7118256747de5efbaa4b5cd3f0e02331 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 14 Jun 2017 13:35:56 +0200 Subject: t7006: demonstrate a problem with aliases in subdirectories When expanding aliases, the git_dir is set during the alias expansion (by virtue of running setup_git_directory_gently()). This git_dir may be relative to the current working directory, and indeed often is simply ".git/". When the alias expands to a shell command, we restore the original working directory, though, yet we do not reset git_dir. As a consequence, subsequent read_early_config() runs will mistake the git_dir to be populated properly and not find the correct config. Demonstrate this problem by adding a test case. Signed-off-by: Johannes Schindelin Reviewed-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/t/t7006-pager.sh b/t/t7006-pager.sh index 4f3794d..83881ec 100755 --- a/t/t7006-pager.sh +++ b/t/t7006-pager.sh @@ -391,6 +391,17 @@ test_expect_success TTY 'core.pager in repo config works and retains cwd' ' ) ' +test_expect_failure TTY 'core.pager is found via alias in subdirectory' ' + sane_unset GIT_PAGER && + test_config core.pager "cat >via-alias" && + ( + cd sub && + rm -f via-alias && + test_terminal git -c alias.r="-p rev-parse" r HEAD && + test_path_is_file via-alias + ) +' + test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense' test_pager_choices 'git shortlog' -- cgit v0.10.2-6-g49f6 From a9bcf6586d1a4888aea91553d73cda20494b8335 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 14 Jun 2017 13:36:00 +0200 Subject: alias: use the early config machinery to expand aliases Instead of discovering the .git/ directory, reading the config and then trying to painstakingly reset all the global state if we did not find a matching alias, let's use the early config machinery instead. It may look like unnecessary work to discover the .git/ directory in the early config machinery and then call setup_git_directory_gently() in the case of a shell alias, repeating the very same discovery *again*. However, we have to do this as the early config machinery takes pains *not* to touch any global state, while shell aliases expect a possibly changed working directory and at least the GIT_PREFIX and GIT_DIR variables to be set. This change also fixes a known issue where Git tried to read the pager config from an incorrect path in a subdirectory of a Git worktree if an alias expanded to a shell command. Signed-off-by: Johannes Schindelin Reviewed-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/alias.c b/alias.c index 3b90397..0526304 100644 --- a/alias.c +++ b/alias.c @@ -1,14 +1,28 @@ #include "cache.h" +struct config_alias_data { + const char *alias; + char *v; +}; + +static int config_alias_cb(const char *key, const char *value, void *d) +{ + struct config_alias_data *data = d; + const char *p; + + if (skip_prefix(key, "alias.", &p) && !strcmp(p, data->alias)) + return git_config_string((const char **)&data->v, key, value); + + return 0; +} + char *alias_lookup(const char *alias) { - char *v = NULL; - struct strbuf key = STRBUF_INIT; - strbuf_addf(&key, "alias.%s", alias); - if (git_config_key_is_valid(key.buf)) - git_config_get_string(key.buf, &v); - strbuf_release(&key); - return v; + struct config_alias_data data = { alias, NULL }; + + read_early_config(config_alias_cb, &data); + + return data.v; } #define SPLIT_CMDLINE_BAD_ENDING 1 diff --git a/git.c b/git.c index 8ff44f0..58ef570 100644 --- a/git.c +++ b/git.c @@ -16,50 +16,6 @@ const char git_more_info_string[] = "to read about a specific subcommand or concept."); static int use_pager = -1; -static char *orig_cwd; -static const char *env_names[] = { - GIT_DIR_ENVIRONMENT, - GIT_WORK_TREE_ENVIRONMENT, - GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, - GIT_PREFIX_ENVIRONMENT -}; -static char *orig_env[4]; -static int save_restore_env_balance; - -static void save_env_before_alias(void) -{ - int i; - - assert(save_restore_env_balance == 0); - save_restore_env_balance = 1; - orig_cwd = xgetcwd(); - for (i = 0; i < ARRAY_SIZE(env_names); i++) { - orig_env[i] = getenv(env_names[i]); - orig_env[i] = xstrdup_or_null(orig_env[i]); - } -} - -static void restore_env(int external_alias) -{ - int i; - - assert(save_restore_env_balance == 1); - save_restore_env_balance = 0; - if (!external_alias && orig_cwd && chdir(orig_cwd)) - die_errno("could not move to %s", orig_cwd); - free(orig_cwd); - for (i = 0; i < ARRAY_SIZE(env_names); i++) { - if (external_alias && - !strcmp(env_names[i], GIT_PREFIX_ENVIRONMENT)) - continue; - if (orig_env[i]) { - setenv(env_names[i], orig_env[i], 1); - free(orig_env[i]); - } else { - unsetenv(env_names[i]); - } - } -} static void commit_pager_choice(void) { switch (use_pager) { @@ -250,19 +206,18 @@ static int handle_alias(int *argcp, const char ***argv) const char **new_argv; const char *alias_command; char *alias_string; - int unused_nongit; - - save_env_before_alias(); - setup_git_directory_gently(&unused_nongit); alias_command = (*argv)[0]; alias_string = alias_lookup(alias_command); if (alias_string) { if (alias_string[0] == '!') { struct child_process child = CHILD_PROCESS_INIT; + int nongit_ok; + + /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */ + setup_git_directory_gently(&nongit_ok); commit_pager_choice(); - restore_env(1); child.use_shell = 1; argv_array_push(&child.args, alias_string + 1); @@ -308,8 +263,6 @@ static int handle_alias(int *argcp, const char ***argv) ret = 1; } - restore_env(0); - errno = saved_errno; return ret; diff --git a/t/t7006-pager.sh b/t/t7006-pager.sh index 83881ec..20b4d83 100755 --- a/t/t7006-pager.sh +++ b/t/t7006-pager.sh @@ -391,7 +391,7 @@ test_expect_success TTY 'core.pager in repo config works and retains cwd' ' ) ' -test_expect_failure TTY 'core.pager is found via alias in subdirectory' ' +test_expect_success TTY 'core.pager is found via alias in subdirectory' ' sane_unset GIT_PAGER && test_config core.pager "cat >via-alias" && ( -- cgit v0.10.2-6-g49f6