Skip to content

Commit 4e10738

Browse files
peffgitster
authored andcommitted
Allow per-command pager config
There is great debate over whether some commands should set up a pager automatically. This patch allows individuals to set their own pager preferences for each command, overriding the default. For example, to disable the pager for git status: git config pager.status false If "--pager" or "--no-pager" is specified on the command line, it takes precedence over the config option. There are two caveats: - you can turn on the pager for plumbing commands. Combined with "core.pager = always", this will probably break a lot of things. Don't do it. - This only works for builtin commands. The reason is somewhat complex: Calling git_config before we do setup_git_directory has bad side effects, because it wants to know where the git_dir is to find ".git/config". Unfortunately, we cannot call setup_git_directory indiscriminately, because some builtins (like "init") break if we do. For builtins, this is OK, since we can just wait until after we call setup_git_directory. But for aliases, we don't know until we expand (recursively) which command we're doing. This should not be a huge problem for aliases, which can simply use "--pager" or "--no-pager" in the alias as appropriate. For external commands, however, we don't know we even have an external command until we exec it, and by then it is too late to check the config. An alternative approach would be to have a config mode where we don't bother looking at .git/config, but only at the user and system config files. This would make the behavior consistent across builtins, aliases, and external commands, at the cost of not allowing per-repo pager config for at all. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 08b51f5 commit 4e10738

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

git.c

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,43 @@ const char git_usage_string[] =
99
const char git_more_info_string[] =
1010
"See 'git help COMMAND' for more information on a specific command.";
1111

12+
static int use_pager = -1;
13+
struct pager_config {
14+
const char *cmd;
15+
int val;
16+
};
17+
18+
static int pager_command_config(const char *var, const char *value, void *data)
19+
{
20+
struct pager_config *c = data;
21+
if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
22+
c->val = git_config_bool(var, value);
23+
return 0;
24+
}
25+
26+
/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
27+
int check_pager_config(const char *cmd)
28+
{
29+
struct pager_config c;
30+
c.cmd = cmd;
31+
c.val = -1;
32+
git_config(pager_command_config, &c);
33+
return c.val;
34+
}
35+
36+
static void commit_pager_choice(void) {
37+
switch (use_pager) {
38+
case 0:
39+
setenv("GIT_PAGER", "cat", 1);
40+
break;
41+
case 1:
42+
setup_pager();
43+
break;
44+
default:
45+
break;
46+
}
47+
}
48+
1249
static int handle_options(const char*** argv, int* argc, int* envchanged)
1350
{
1451
int handled = 0;
@@ -38,9 +75,9 @@ static int handle_options(const char*** argv, int* argc, int* envchanged)
3875
exit(0);
3976
}
4077
} else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
41-
setup_pager();
78+
use_pager = 1;
4279
} else if (!strcmp(cmd, "--no-pager")) {
43-
setenv("GIT_PAGER", "cat", 1);
80+
use_pager = 0;
4481
if (envchanged)
4582
*envchanged = 1;
4683
} else if (!strcmp(cmd, "--git-dir")) {
@@ -242,8 +279,13 @@ static int run_command(struct cmd_struct *p, int argc, const char **argv)
242279
prefix = NULL;
243280
if (p->option & RUN_SETUP)
244281
prefix = setup_git_directory();
245-
if (p->option & USE_PAGER)
246-
setup_pager();
282+
283+
if (use_pager == -1 && p->option & RUN_SETUP)
284+
use_pager = check_pager_config(p->cmd);
285+
if (use_pager == -1 && p->option & USE_PAGER)
286+
use_pager = 1;
287+
commit_pager_choice();
288+
247289
if (p->option & NEED_WORK_TREE)
248290
setup_work_tree();
249291

@@ -466,6 +508,7 @@ int main(int argc, const char **argv)
466508
argv++;
467509
argc--;
468510
handle_options(&argv, &argc, NULL);
511+
commit_pager_choice();
469512
if (argc > 0) {
470513
if (!prefixcmp(argv[0], "--"))
471514
argv[0] += 2;

0 commit comments

Comments
 (0)