Skip to content

Commit

Permalink
Allow arguments for diff-highlight
Browse files Browse the repository at this point in the history
This allows users to specify arguments in their ~/.tigrc when using an
alternate `diff-highlight` program.
  • Loading branch information
koutcher committed Feb 13, 2025
1 parent b5f6193 commit c016cce
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 21 deletions.
7 changes: 7 additions & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Release notes
=============

master
------

Improvements:

- Allow arguments for `diff-highlight`.

tig-2.5.12
----------
Expand Down
2 changes: 1 addition & 1 deletion include/tig/apps.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct app_external {
* diff-highlight
*/

struct app_external *app_diff_highlight_load(const char *query);
struct app_external *app_diff_highlight_load(const char **query);

#endif

Expand Down
2 changes: 1 addition & 1 deletion include/tig/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef struct view_column *view_settings;
_(diff_context, int, VIEW_DIFF_LIKE) \
_(diff_noprefix, bool, VIEW_NO_FLAGS) \
_(diff_options, const char **, VIEW_DIFF_LIKE) \
_(diff_highlight, const char *, VIEW_DIFF_LIKE) \
_(diff_highlight, const char **, VIEW_DIFF_LIKE) \
_(word_diff, bool, VIEW_DIFF_LIKE) \
_(diff_view, view_settings, VIEW_NO_FLAGS) \
_(editor_line_number, bool, VIEW_NO_FLAGS) \
Expand Down
9 changes: 6 additions & 3 deletions src/apps.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ app_diff_highlight_path_search(char *dest, size_t destlen, const char *query)
}

struct app_external
*app_diff_highlight_load(const char *query)
*app_diff_highlight_load(const char **query)
{
static struct app_external dhlt_app = { { NULL }, { "GIT_CONFIG=/dev/null", NULL } };
static bool did_search = false;
Expand All @@ -104,11 +104,14 @@ struct app_external
static char perl_include[SIZEOF_STR];

if (!did_search
&& app_diff_highlight_path_search(dhlt_path, sizeof(dhlt_path), query)
&& app_diff_highlight_path_search(dhlt_path, sizeof(dhlt_path), *query)
&& *dhlt_path) {
if (suffixcmp(dhlt_path, strlen(dhlt_path), "/diff-highlight.perl")) {
int i;
dhlt_app.argv[0] = dhlt_path;
dhlt_app.argv[1] = NULL;
for (i = 1; query[i] && i < SIZEOF_ARG; i++)
dhlt_app.argv[i] = query[i];
dhlt_app.argv[i] = NULL;
} else if (path_search(perl_path, sizeof(perl_path), "perl", getenv("PATH"), X_OK)) {
/* if the package manager failed to "make install" within the contrib dir, rescue via */
/* perl -MDiffHighlight -I/path/containing /path/containing/diff-highlight.perl */
Expand Down
4 changes: 2 additions & 2 deletions src/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ diff_init_highlight(struct view *view, struct diff_state *state)
app->argv[0] = "";

if (!io_exec(&io, IO_RP, view->dir, app->env, app->argv, view->io.pipe))
return error("Failed to run %s", opt_diff_highlight);
return error("Failed to run %s", *opt_diff_highlight);

state->view_io = view->io;
view->io = io;
Expand Down Expand Up @@ -521,7 +521,7 @@ diff_read(struct view *view, struct buffer *buf, bool force_stop)
if (!buf) {
if (!diff_done_highlight(state)) {
if (!force_stop)
report("Failed to run the diff-highlight program: %s", opt_diff_highlight);
report("Failed to run the diff-highlight program: %s", *opt_diff_highlight);
return false;
}

Expand Down
24 changes: 12 additions & 12 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,18 @@ parse_encoding(struct encoding **encoding_ref, const char *arg, bool priority)
static enum status_code
parse_args(const char ***args, const char *argv[])
{
if (args == &opt_diff_highlight && *argv) {
bool enabled = false;

if (parse_bool(&enabled, *argv) == SUCCESS) {
if (!enabled) {
argv_free(*args);
return SUCCESS;
}
*argv = "diff-highlight";
}
}

if (!argv_copy(args, argv))
return ERROR_OUT_OF_MEMORY;
return SUCCESS;
Expand Down Expand Up @@ -733,18 +745,6 @@ parse_option(struct option_info *option, const char *prefix, const char *arg)
const char **value = option->value;
char *alloc = NULL;

if (option->value == &opt_diff_highlight) {
bool enabled = false;

if (parse_bool(&enabled, arg) == SUCCESS) {
if (!enabled) {
*value = NULL;
return SUCCESS;
}
arg = "diff-highlight";
}
}

if (strlen(arg)) {
if (arg[0] == '"' && arg[strlen(arg) - 1] == '"')
alloc = strndup(arg + 1, strlen(arg + 1) - 1);
Expand Down
2 changes: 1 addition & 1 deletion src/pager.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pager_read(struct view *view, struct buffer *buf, bool force_stop)
if (!buf) {
if (!diff_done_highlight(view->private)) {
if (!force_stop)
report("Failed to run the diff-highlight program: %s", opt_diff_highlight);
report("Failed to run the diff-highlight program: %s", *opt_diff_highlight);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/stage.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ stage_read(struct view *view, struct buffer *buf, bool force_stop)
if (!buf) {
if (!diff_done_highlight(&state->diff)) {
if (!force_stop)
report("Failed to run the diff-highlight program: %s", opt_diff_highlight);
report("Failed to run the diff-highlight program: %s", *opt_diff_highlight);
return false;
}

Expand Down

0 comments on commit c016cce

Please sign in to comment.