Move setup_diff_pager to libgit.a

This is used by diff-no-index.c, part of libgit.a while it stays in builtin/diff.c. Move it to diff.c so that we won't get undefined reference if a program that uses libgit.a happens to pull it in. While at it, move check_pager from git.c to pager.c. It makes more sense there and pager.c is also part of libgit.a Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Jeff King <peff@peff.net>

Nguyễn Thái Ngọc Duy committed Oct 26, 2012 at 22:53 UTC 4914c9629c046f7f5abf4109ad756040f9ebe2bf
7 files changed +52 -53
builtin.h
-4
@@ -37,10 +37,6 @@ int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
37 const unsigned char *from_obj, const unsigned char *to_obj);
38 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c);
39
40 -extern int check_pager_config(const char *cmd);
41 -struct diff_options;
42 -extern void setup_diff_pager(struct diff_options *);
43 -
40 extern int textconv_object(const char *path, unsigned mode, const unsigned char *sha1, int sha1_valid, char **buf, unsigned long *buf_size);
41
42 extern int cmd_add(int argc, const char **argv, const char *prefix);
builtin/diff.c
-16
@@ -418,19 +418,3 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
418 refresh_index_quietly();
419 return result;
420 }
421 -
422 -void setup_diff_pager(struct diff_options *opt)
423 -{
424 - /*
425 - * If the user asked for our exit code, then either they want --quiet
426 - * or --exit-code. We should definitely not bother with a pager in the
427 - * former case, as we will generate no output. Since we still properly
428 - * report our exit code even when a pager is run, we _could_ run a
429 - * pager with --exit-code. But since we have not done so historically,
430 - * and because it is easy to find people oneline advising "git diff
431 - * --exit-code" in hooks and other scripts, we do not do so.
432 - */
433 - if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
434 - check_pager_config("diff") != 0)
435 - setup_pager();
436 -}
cache.h
+1
@@ -1183,6 +1183,7 @@ extern int pager_in_use(void);
1183 extern int pager_use_color;
1184 extern int term_columns(void);
1185 extern int decimal_width(int);
1186 +extern int check_pager_config(const char *cmd);
1187
1188 extern const char *editor_program;
1189 extern const char *askpass_program;
diff.c
+16
@@ -4878,3 +4878,19 @@ size_t fill_textconv(struct userdiff_driver *driver,
4878
4879 return size;
4880 }
4881 +
4882 +void setup_diff_pager(struct diff_options *opt)
4883 +{
4884 + /*
4885 + * If the user asked for our exit code, then either they want --quiet
4886 + * or --exit-code. We should definitely not bother with a pager in the
4887 + * former case, as we will generate no output. Since we still properly
4888 + * report our exit code even when a pager is run, we _could_ run a
4889 + * pager with --exit-code. But since we have not done so historically,
4890 + * and because it is easy to find people oneline advising "git diff
4891 + * --exit-code" in hooks and other scripts, we do not do so.
4892 + */
4893 + if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
4894 + check_pager_config("diff") != 0)
4895 + setup_pager();
4896 +}
diff.h
+1
@@ -335,5 +335,6 @@ extern int parse_rename_score(const char **cp_p);
335
336 extern int print_stat_summary(FILE *fp, int files,
337 int insertions, int deletions);
338 +extern void setup_diff_pager(struct diff_options *);
339
340 #endif /* DIFF_H */
git.c
-33
@@ -17,39 +17,6 @@ const char git_more_info_string[] =
17
18 static struct startup_info git_startup_info;
19 static int use_pager = -1;
20 -struct pager_config {
21 - const char *cmd;
22 - int want;
23 - char *value;
24 -};
25 -
26 -static int pager_command_config(const char *var, const char *value, void *data)
27 -{
28 - struct pager_config *c = data;
29 - if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd)) {
30 - int b = git_config_maybe_bool(var, value);
31 - if (b >= 0)
32 - c->want = b;
33 - else {
34 - c->want = 1;
35 - c->value = xstrdup(value);
36 - }
37 - }
38 - return 0;
39 -}
40 -
41 -/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
42 -int check_pager_config(const char *cmd)
43 -{
44 - struct pager_config c;
45 - c.cmd = cmd;
46 - c.want = -1;
47 - c.value = NULL;
48 - git_config(pager_command_config, &c);
49 - if (c.value)
50 - pager_program = c.value;
51 - return c.want;
52 -}
20
21 static void commit_pager_choice(void) {
22 switch (use_pager) {
pager.c
+34
@@ -6,6 +6,12 @@
6 #define DEFAULT_PAGER "less"
7 #endif
8
9 +struct pager_config {
10 + const char *cmd;
11 + int want;
12 + char *value;
13 +};
14 +
15 /*
16 * This is split up from the rest of git so that we can do
17 * something different on Windows.
@@ -141,3 +147,31 @@ int decimal_width(int number)
147 i *= 10;
148 return width;
149 }
150 +
151 +static int pager_command_config(const char *var, const char *value, void *data)
152 +{
153 + struct pager_config *c = data;
154 + if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd)) {
155 + int b = git_config_maybe_bool(var, value);
156 + if (b >= 0)
157 + c->want = b;
158 + else {
159 + c->want = 1;
160 + c->value = xstrdup(value);
161 + }
162 + }
163 + return 0;
164 +}
165 +
166 +/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
167 +int check_pager_config(const char *cmd)
168 +{
169 + struct pager_config c;
170 + c.cmd = cmd;
171 + c.want = -1;
172 + c.value = NULL;
173 + git_config(pager_command_config, &c);
174 + if (c.value)
175 + pager_program = c.value;
176 + return c.want;
177 +}