Skip to content

Commit

Permalink
Move setup_diff_pager to libgit.a
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Jeff King <[email protected]>
  • Loading branch information
pclouds authored and peff committed Oct 29, 2012
1 parent efc7df4 commit 4914c96
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 53 deletions.
4 changes: 0 additions & 4 deletions builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
const unsigned char *from_obj, const unsigned char *to_obj);
void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c);

extern int check_pager_config(const char *cmd);
struct diff_options;
extern void setup_diff_pager(struct diff_options *);

extern int textconv_object(const char *path, unsigned mode, const unsigned char *sha1, int sha1_valid, char **buf, unsigned long *buf_size);

extern int cmd_add(int argc, const char **argv, const char *prefix);
Expand Down
16 changes: 0 additions & 16 deletions builtin/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,19 +418,3 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
refresh_index_quietly();
return result;
}

void setup_diff_pager(struct diff_options *opt)
{
/*
* If the user asked for our exit code, then either they want --quiet
* or --exit-code. We should definitely not bother with a pager in the
* former case, as we will generate no output. Since we still properly
* report our exit code even when a pager is run, we _could_ run a
* pager with --exit-code. But since we have not done so historically,
* and because it is easy to find people oneline advising "git diff
* --exit-code" in hooks and other scripts, we do not do so.
*/
if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
check_pager_config("diff") != 0)
setup_pager();
}
1 change: 1 addition & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,7 @@ extern int pager_in_use(void);
extern int pager_use_color;
extern int term_columns(void);
extern int decimal_width(int);
extern int check_pager_config(const char *cmd);

extern const char *editor_program;
extern const char *askpass_program;
Expand Down
16 changes: 16 additions & 0 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -4878,3 +4878,19 @@ size_t fill_textconv(struct userdiff_driver *driver,

return size;
}

void setup_diff_pager(struct diff_options *opt)
{
/*
* If the user asked for our exit code, then either they want --quiet
* or --exit-code. We should definitely not bother with a pager in the
* former case, as we will generate no output. Since we still properly
* report our exit code even when a pager is run, we _could_ run a
* pager with --exit-code. But since we have not done so historically,
* and because it is easy to find people oneline advising "git diff
* --exit-code" in hooks and other scripts, we do not do so.
*/
if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
check_pager_config("diff") != 0)
setup_pager();
}
1 change: 1 addition & 0 deletions diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,5 +335,6 @@ extern int parse_rename_score(const char **cp_p);

extern int print_stat_summary(FILE *fp, int files,
int insertions, int deletions);
extern void setup_diff_pager(struct diff_options *);

#endif /* DIFF_H */
33 changes: 0 additions & 33 deletions git.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,6 @@ const char git_more_info_string[] =

static struct startup_info git_startup_info;
static int use_pager = -1;
struct pager_config {
const char *cmd;
int want;
char *value;
};

static int pager_command_config(const char *var, const char *value, void *data)
{
struct pager_config *c = data;
if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd)) {
int b = git_config_maybe_bool(var, value);
if (b >= 0)
c->want = b;
else {
c->want = 1;
c->value = xstrdup(value);
}
}
return 0;
}

/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
int check_pager_config(const char *cmd)
{
struct pager_config c;
c.cmd = cmd;
c.want = -1;
c.value = NULL;
git_config(pager_command_config, &c);
if (c.value)
pager_program = c.value;
return c.want;
}

static void commit_pager_choice(void) {
switch (use_pager) {
Expand Down
34 changes: 34 additions & 0 deletions pager.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#define DEFAULT_PAGER "less"
#endif

struct pager_config {
const char *cmd;
int want;
char *value;
};

/*
* This is split up from the rest of git so that we can do
* something different on Windows.
Expand Down Expand Up @@ -141,3 +147,31 @@ int decimal_width(int number)
i *= 10;
return width;
}

static int pager_command_config(const char *var, const char *value, void *data)
{
struct pager_config *c = data;
if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd)) {
int b = git_config_maybe_bool(var, value);
if (b >= 0)
c->want = b;
else {
c->want = 1;
c->value = xstrdup(value);
}
}
return 0;
}

/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
int check_pager_config(const char *cmd)
{
struct pager_config c;
c.cmd = cmd;
c.want = -1;
c.value = NULL;
git_config(pager_command_config, &c);
if (c.value)
pager_program = c.value;
return c.want;
}

0 comments on commit 4914c96

Please sign in to comment.