16
"to read about a specific subcommand or concept.");
17
18
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;
28
-
29
-static void save_env_before_alias(void)
30
-{
31
- int i;
32
-
33
- assert(save_restore_env_balance == 0);
34
- save_restore_env_balance = 1;
35
- orig_cwd = xgetcwd();
36
- for (i = 0; i < ARRAY_SIZE(env_names); i++) {
37
- orig_env[i] = getenv(env_names[i]);
38
- orig_env[i] = xstrdup_or_null(orig_env[i]);
39
- }
40
-}
41
-
42
-static void restore_env(int external_alias)
43
-{
44
- int i;
45
-
46
- assert(save_restore_env_balance == 1);
47
- save_restore_env_balance = 0;
48
- if (!external_alias && orig_cwd && chdir(orig_cwd))
49
- die_errno("could not move to %s", orig_cwd);
50
- free(orig_cwd);
51
- for (i = 0; i < ARRAY_SIZE(env_names); i++) {
52
- if (external_alias &&
53
- !strcmp(env_names[i], GIT_PREFIX_ENVIRONMENT))
54
- continue;
55
- if (orig_env[i]) {
56
- setenv(env_names[i], orig_env[i], 1);
57
- free(orig_env[i]);
58
- } else {
59
- unsetenv(env_names[i]);
60
- }
61
- }
62
-}
19
20
static void commit_pager_choice(void) {
21
switch (use_pager) {
206
const char **new_argv;
207
const char *alias_command;
208
char *alias_string;
253
- int unused_nongit;
254
-
255
- save_env_before_alias();
256
- setup_git_directory_gently(&unused_nongit);
209
210
alias_command = (*argv)[0];
211
alias_string = alias_lookup(alias_command);
212
if (alias_string) {
213
if (alias_string[0] == '!') {
214
struct child_process child = CHILD_PROCESS_INIT;
215
+ int nongit_ok;
216
+
217
+ /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */
218
+ setup_git_directory_gently(&nongit_ok);
219
220
commit_pager_choice();
265
- restore_env(1);
221
222
child.use_shell = 1;
223
argv_array_push(&child.args, alias_string + 1);
263
ret = 1;
264
}
265
311
- restore_env(0);
312
-
266
errno = saved_errno;
267
268
return ret;