Skip to content

Commit 1c3d87c

Browse files
committed
Merge branch 'js/alias-early-config'
The code to pick up and execute command alias definition from the configuration used to switch to the top of the working tree and then come back when the expanded alias was executed, which was unnecessarilyl complex. Attempt to simplify the logic by using the early-config mechanism that does not chdir around. * js/alias-early-config: alias: use the early config machinery to expand aliases t7006: demonstrate a problem with aliases in subdirectories t1308: relax the test verifying that empty alias values are disallowed help: use early config when autocorrecting aliases config: report correct line number upon error discover_git_directory(): avoid setting invalid git_dir
2 parents 9bca0e5 + a9bcf65 commit 1c3d87c

File tree

8 files changed

+49
-61
lines changed

8 files changed

+49
-61
lines changed

alias.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
#include "cache.h"
22

3+
struct config_alias_data {
4+
const char *alias;
5+
char *v;
6+
};
7+
8+
static int config_alias_cb(const char *key, const char *value, void *d)
9+
{
10+
struct config_alias_data *data = d;
11+
const char *p;
12+
13+
if (skip_prefix(key, "alias.", &p) && !strcmp(p, data->alias))
14+
return git_config_string((const char **)&data->v, key, value);
15+
16+
return 0;
17+
}
18+
319
char *alias_lookup(const char *alias)
420
{
5-
char *v = NULL;
6-
struct strbuf key = STRBUF_INIT;
7-
strbuf_addf(&key, "alias.%s", alias);
8-
if (git_config_key_is_valid(key.buf))
9-
git_config_get_string(key.buf, &v);
10-
strbuf_release(&key);
11-
return v;
21+
struct config_alias_data data = { alias, NULL };
22+
23+
read_early_config(config_alias_cb, &data);
24+
25+
return data.v;
1226
}
1327

1428
#define SPLIT_CMDLINE_BAD_ENDING 1

config.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,8 @@ static int get_value(config_fn_t fn, void *data, struct strbuf *name)
604604
*/
605605
cf->linenr--;
606606
ret = fn(name->buf, value, data);
607-
cf->linenr++;
607+
if (ret >= 0)
608+
cf->linenr++;
608609
return ret;
609610
}
610611

git.c

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,9 @@ const char git_more_info_string[] =
1616
"to read about a specific subcommand or concept.");
1717

1818
static int use_pager = -1;
19-
static char *orig_cwd;
20-
static const char *env_names[] = {
21-
GIT_DIR_ENVIRONMENT,
22-
GIT_WORK_TREE_ENVIRONMENT,
23-
GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,
24-
GIT_PREFIX_ENVIRONMENT
25-
};
26-
static char *orig_env[4];
27-
static int save_restore_env_balance;
2819

2920
static void list_builtins(void);
3021

31-
static void save_env_before_alias(void)
32-
{
33-
int i;
34-
35-
assert(save_restore_env_balance == 0);
36-
save_restore_env_balance = 1;
37-
orig_cwd = xgetcwd();
38-
for (i = 0; i < ARRAY_SIZE(env_names); i++) {
39-
orig_env[i] = getenv(env_names[i]);
40-
orig_env[i] = xstrdup_or_null(orig_env[i]);
41-
}
42-
}
43-
44-
static void restore_env(int external_alias)
45-
{
46-
int i;
47-
48-
assert(save_restore_env_balance == 1);
49-
save_restore_env_balance = 0;
50-
if (!external_alias && orig_cwd && chdir(orig_cwd))
51-
die_errno("could not move to %s", orig_cwd);
52-
free(orig_cwd);
53-
for (i = 0; i < ARRAY_SIZE(env_names); i++) {
54-
if (external_alias &&
55-
!strcmp(env_names[i], GIT_PREFIX_ENVIRONMENT))
56-
continue;
57-
if (orig_env[i]) {
58-
setenv(env_names[i], orig_env[i], 1);
59-
free(orig_env[i]);
60-
} else {
61-
unsetenv(env_names[i]);
62-
}
63-
}
64-
}
65-
6622
static void commit_pager_choice(void) {
6723
switch (use_pager) {
6824
case 0:
@@ -255,19 +211,18 @@ static int handle_alias(int *argcp, const char ***argv)
255211
const char **new_argv;
256212
const char *alias_command;
257213
char *alias_string;
258-
int unused_nongit;
259-
260-
save_env_before_alias();
261-
setup_git_directory_gently(&unused_nongit);
262214

263215
alias_command = (*argv)[0];
264216
alias_string = alias_lookup(alias_command);
265217
if (alias_string) {
266218
if (alias_string[0] == '!') {
267219
struct child_process child = CHILD_PROCESS_INIT;
220+
int nongit_ok;
221+
222+
/* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */
223+
setup_git_directory_gently(&nongit_ok);
268224

269225
commit_pager_choice();
270-
restore_env(1);
271226

272227
child.use_shell = 1;
273228
argv_array_push(&child.args, alias_string + 1);
@@ -313,8 +268,6 @@ static int handle_alias(int *argcp, const char ***argv)
313268
ret = 1;
314269
}
315270

316-
restore_env(0);
317-
318271
errno = saved_errno;
319272

320273
return ret;

help.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ const char *help_unknown_cmd(const char *cmd)
290290
memset(&other_cmds, 0, sizeof(other_cmds));
291291
memset(&aliases, 0, sizeof(aliases));
292292

293-
git_config(git_unknown_cmd_config, NULL);
293+
read_early_config(git_unknown_cmd_config, NULL);
294294

295295
load_command_list("git-", &main_cmds, &other_cmds);
296296

setup.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@ const char *discover_git_directory(struct strbuf *gitdir)
10041004
warning("ignoring git dir '%s': %s",
10051005
gitdir->buf + gitdir_offset, err.buf);
10061006
strbuf_release(&err);
1007+
strbuf_setlen(gitdir, gitdir_offset);
10071008
return NULL;
10081009
}
10091010

t/t1300-repo-config.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,12 @@ test_expect_success 'invalid unit' '
703703
test_i18ngrep "bad numeric config value .1auto. for .aninvalid.unit. in file .git/config: invalid unit" actual
704704
'
705705

706+
test_expect_success 'line number is reported correctly' '
707+
printf "[bool]\n\tvar\n" >invalid &&
708+
test_must_fail git config -f invalid --path bool.var 2>actual &&
709+
test_i18ngrep "line 2" actual
710+
'
711+
706712
test_expect_success 'invalid stdin config' '
707713
echo "[broken" | test_must_fail git config --list --file - >output 2>&1 &&
708714
test_i18ngrep "bad config line 1 in standard input" output

t/t1308-config-set.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ test_expect_success 'check line errors for malformed values' '
226226
br
227227
EOF
228228
test_expect_code 128 git br 2>result &&
229-
test_i18ngrep "fatal: .*alias\.br.*\.git/config.*line 2" result
229+
test_i18ngrep "missing value for .alias\.br" result &&
230+
test_i18ngrep "fatal: .*\.git/config" result &&
231+
test_i18ngrep "fatal: .*line 2" result
230232
'
231233

232234
test_expect_success 'error on modifying repo config without repo' '

t/t7006-pager.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,17 @@ test_expect_success TTY 'core.pager in repo config works and retains cwd' '
391391
)
392392
'
393393

394+
test_expect_success TTY 'core.pager is found via alias in subdirectory' '
395+
sane_unset GIT_PAGER &&
396+
test_config core.pager "cat >via-alias" &&
397+
(
398+
cd sub &&
399+
rm -f via-alias &&
400+
test_terminal git -c alias.r="-p rev-parse" r HEAD &&
401+
test_path_is_file via-alias
402+
)
403+
'
404+
394405
test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
395406

396407
test_pager_choices 'git shortlog'

0 commit comments

Comments
 (0)