Skip to content

Commit

Permalink
Allow to toggle revision filtering (#1173)
Browse files Browse the repository at this point in the history
"tig -- <pathspec>" shows only commits that touch files matching
<pathspec>.  The command ":toggle file-filter" (bound to %) can be
used to toggle this filtering dynamically.

"tig <revisions range>" shows only commits in the given range.
This patch adds ":toggle rev-filter" (bound to ^) to quickly toggle
this filter.

This feature is particularly useful with a new feature in Tig 2.5.2:
when pressing m in the blame view, Tig will show the blamed commit
centered in the main view, making it easy to explore adjacent
commits. Note that if there are many commits between the blamed commit
and HEAD, you'll need to wait a bit after pressing m.

[tk: fixed tab in manual.adoc and tweaked the log message]
  • Loading branch information
krobelus authored and koutcher committed Jan 7, 2022
1 parent 08f23f9 commit 19bb021
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 19 deletions.
1 change: 1 addition & 0 deletions doc/manual.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ Misc
|X |Toggle commit ID display on/off
|% |Toggle file filtering in order to see the full diff instead of only
the diff concerning the currently selected file.
|^ |Toggle revision filtering in the main view.
|$ |Toggle highlighting of commit title overflow.
|H |Go to the HEAD commit.
|: |Open prompt. This allows you to specify what command to run and also to
Expand Down
1 change: 1 addition & 0 deletions include/tig/argv.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ extern struct argv_env argv_env;
enum argv_flag {
argv_flag_first = 1 << 0,
argv_flag_file_filter = 1 << 1,
argv_flag_rev_filter = 1 << 2,
};

bool argv_format(struct argv_env *argv_env, const char ***dst_argv, const char *src_argv[], int flags);
Expand Down
1 change: 1 addition & 0 deletions include/tig/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ typedef struct view_column *view_settings;
_(refresh_mode, enum refresh_mode, VIEW_NO_FLAGS) \
_(refs_view, view_settings, VIEW_NO_FLAGS) \
_(rev_args, const char **, VIEW_LOG_LIKE) \
_(rev_filter, bool, VIEW_LOG_LIKE) \
_(send_child_enter, bool, VIEW_NO_FLAGS) \
_(show_changes, bool, VIEW_LOG_LIKE) \
_(show_notes, bool, VIEW_DIFF_LIKE | VIEW_LOG_LIKE) \
Expand Down
15 changes: 8 additions & 7 deletions include/tig/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ enum view_flag {
VIEW_BLAME_LIKE = 1 << 8,
VIEW_SEND_CHILD_ENTER = 1 << 9,
VIEW_FILE_FILTER = 1 << 10,
VIEW_LOG_LIKE = 1 << 11,
VIEW_STATUS_LIKE = 1 << 12,
VIEW_REFRESH = 1 << 13,
VIEW_GREP_LIKE = 1 << 14,
VIEW_SORTABLE = 1 << 15,
VIEW_FLEX_WIDTH = 1 << 16,
VIEW_RESET_DISPLAY = 1 << 17,
VIEW_REV_FILTER = 1 << 11,
VIEW_LOG_LIKE = 1 << 12,
VIEW_STATUS_LIKE = 1 << 13,
VIEW_REFRESH = 1 << 14,
VIEW_GREP_LIKE = 1 << 15,
VIEW_SORTABLE = 1 << 16,
VIEW_FLEX_WIDTH = 1 << 17,
VIEW_RESET_DISPLAY = 1 << 18,
};

#define view_has_flags(view, flag) ((view)->ops->flags & (flag))
Expand Down
4 changes: 2 additions & 2 deletions src/argv.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ argv_format(struct argv_env *argv_env, const char ***dst_argv, const char *src_a

} else if (!strcmp(arg, "%(revargs)") ||
((flags & argv_flag_first) && !strcmp(arg, "%(commit)"))) {
if (!argv_append_array(dst_argv, opt_rev_args))
if ((flags & argv_flag_rev_filter) && !argv_append_array(dst_argv, opt_rev_args))
break;

} else if (!format_append_arg(&format, dst_argv, arg)) {
Expand Down Expand Up @@ -608,7 +608,7 @@ argv_format_arg(struct argv_env *argv_env, const char *src_arg)
const char **dst_argv = NULL;
char *dst_arg = NULL;

if (argv_format(argv_env, &dst_argv, src_argv, argv_flag_file_filter))
if (argv_format(argv_env, &dst_argv, src_argv, argv_flag_file_filter | argv_flag_rev_filter))
dst_arg = (char *) dst_argv[0];

free(dst_argv);
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ main_select(struct view *view, struct line *line)
static struct view_ops main_ops = {
"commit",
argv_env.head,
VIEW_SEND_CHILD_ENTER | VIEW_FILE_FILTER | VIEW_LOG_LIKE | VIEW_REFRESH,
VIEW_SEND_CHILD_ENTER | VIEW_FILE_FILTER | VIEW_REV_FILTER | VIEW_LOG_LIKE | VIEW_REFRESH,
sizeof(struct main_state),
main_open,
main_read,
Expand Down
2 changes: 2 additions & 0 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,7 @@ option_bind_command(int argc, const char *argv[])
{ "toggle-date", "date" },
{ "toggle-files", "file-filter" },
{ "toggle-file-filter", "file-filter" },
{ "toggle-rev-filter", "rev-filter" },
{ "toggle-file-size", "file-size" },
{ "toggle-filename", "filename" },
{ "toggle-graphic", "show-graphic" },
Expand Down Expand Up @@ -1058,6 +1059,7 @@ load_options(void)
char buf[SIZEOF_STR];

opt_file_filter = true;
opt_rev_filter = true;
if (!find_option_info_by_value(&opt_diff_context)->seen)
opt_diff_context = -3;

Expand Down
6 changes: 3 additions & 3 deletions src/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ run_prompt_command(struct view *view, const char *argv[])

/* Trim the leading '!'. */
argv[0] = cmd + 1;
copied = argv_format(view->env, &next->argv, argv, argv_flag_file_filter);
copied = argv_format(view->env, &next->argv, argv, argv_flag_file_filter | argv_flag_rev_filter);
argv[0] = cmd;

if (!copied) {
Expand Down Expand Up @@ -969,7 +969,7 @@ run_prompt_command(struct view *view, const char *argv[])

if (argv[1]
&& strlen(argv[1]) > 0
&& (!argv_format(view->env, &fmt_argv, &argv[1], argv_flag_file_filter)
&& (!argv_format(view->env, &fmt_argv, &argv[1], argv_flag_file_filter | argv_flag_rev_filter)
|| !argv_to_string(fmt_argv, text, sizeof(text), " ")
)) {
report("Failed to format echo string");
Expand Down Expand Up @@ -1104,7 +1104,7 @@ exec_run_request(struct view *view, struct run_request *req)

if (!argv_to_string(req->argv, cmd, sizeof(cmd), " ")
|| !argv_from_string_no_quotes(req_argv, &req_argc, cmd)
|| !argv_format(view->env, &argv, req_argv, argv_flag_file_filter)
|| !argv_format(view->env, &argv, req_argv, argv_flag_file_filter | argv_flag_rev_filter)
|| !argv) {
report("Failed to format arguments");
return REQ_NONE;
Expand Down
1 change: 1 addition & 0 deletions src/tig.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ view_request(struct view *view, enum request request)
_('C', "local change display", "show-changes"), \
_('X', "commit ID display", "id"), \
_('%', "file filtering", "file-filter"), \
_('^', "revision filtering", "rev-filter"), \
_('$', "commit title overflow display", "commit-title-overflow"), \
_('d', "untracked directory info", "status-show-untracked-dirs"), \
_('|', "view split", "vertical-split"), \
Expand Down
2 changes: 2 additions & 0 deletions src/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,8 @@ begin_update(struct view *view, const char *dir, const char **argv, enum open_fl
flags |= argv_flag_first;
if (!view_has_flags(view, VIEW_FILE_FILTER) || opt_file_filter)
flags |= argv_flag_file_filter;
if (!view_has_flags(view, VIEW_REV_FILTER) || opt_rev_filter)
flags |= argv_flag_rev_filter;

view->dir = dir;
if (!argv_format(view->env, &view->argv, argv, flags))
Expand Down
2 changes: 1 addition & 1 deletion test/help/all-keybindings-test
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
. libtest.sh

export COLUMNS=90
export LINES=129
export LINES=130

steps '
:view-help
Expand Down
3 changes: 2 additions & 1 deletion test/help/all-keybindings-test.expected
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Option toggling:
X :toggle id
$ :toggle commit-title-overflow
% :toggle file-filter
^ :toggle rev-filter
[-] search bindings
View manipulation
<Ctrl-C> view-close Close the current view
Expand Down Expand Up @@ -125,4 +126,4 @@ External commands:
[-] pager bindings
Internal commands:
@ :/^@@
[help] - line 1 of 127 100%
[help] - line 1 of 128 100%
4 changes: 2 additions & 2 deletions test/help/default-test
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ View manipulation
R, <F5> refresh Reload and refresh view
O maximize Maximize the current view
q view-close Close the current view
[help] - line 1 of 127 22%
[help] - line 1 of 128 21%
EOF

assert_equals 'help-search.screen' <<EOF
Expand Down Expand Up @@ -83,7 +83,7 @@ View manipulation
R, <F5> refresh Reload and refresh view
O maximize Maximize the current view
q view-close Close the current view
[help] - line 19 of 127 22%
[help] - line 19 of 128 21%
EOF

assert_equals 'help-collapsed.screen' <<EOF
Expand Down
2 changes: 1 addition & 1 deletion test/help/user-command-test
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ Searching
[-] main bindings
Cursor navigation
G move-last-line Move cursor to last line
[help] - line 80 of 154 69%
[help] - line 81 of 155 69%
EOF
3 changes: 2 additions & 1 deletion tigrc
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ bind generic $ :toggle commit-title-overflow
# bind generic ??? :toggle status-show-untracked-dirs
# Toggle display of file in untracked directories
# bind generic ??? :toggle vertical-split # Toggle vertical split
bind generic % :toggle file-filter
bind generic % :toggle file-filter # Toggle filtering by pathspecs in file-args
bind generic ^ :toggle rev-filter # Toggle filtering by revisions in rev-args

# Misc
bind generic e edit # Open in editor
Expand Down

0 comments on commit 19bb021

Please sign in to comment.