git-jump: pick a mode automatically when invoked without arguments

When `git jump` is invoked with no positional arguments (and no arguments after `--stdout`) it currently prints usage and exits with status 1. But there are two situations where we can usefully infer the most valuable and likely mode that a user would want to use, and select it automatically: 1. When there are unmerged paths in the index, the user likely wants `git jump merge`. 2. When the working tree has unstaged changes, the user likely wants `git jump diff`. In this commit we teach `git jump` a new "auto" mode which detects these cases and dispatches to the corresponding mode automatically. The user can either explicitly spell out `git jump auto`, or just leave it at `git jump` (because "auto" is the default). If none of the interesting cases listed above applies, then auto mode falls back to the existing usage-and-exit behavior. Signed-off-by: Greg Hurrell <greg.hurrell@datadoghq.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Greg Hurrell committed May 21, 2026 at 13:45 UTC 74216ffe0aa02309e1fc510c0056ec6fd523898c
2 files changed +35 -3
contrib/git-jump/README
+12
@@ -75,8 +75,20 @@ git jump grep foo_bar
75 # arbitrary grep options
76 git jump grep -i foo_bar
77
78 +# jump to places with conflict markers or whitespace errors
79 +# (as reported by `git diff --check`)
80 +git jump ws
81 +
82 # use the silver searcher for git jump grep
83 git config jump.grepCmd "ag --column"
84 +
85 +# pick a mode automatically: "merge" if there are unmerged paths,
86 +# "diff" if the worktree has unstaged changes, "ws" if there are
87 +# whitespace problems; otherwise show usage
88 +git jump auto
89 +
90 +# with no explicit mode and no args, same as "auto"
91 +git jump
92 --------------------------------------------------
93
94 You can use the optional argument '--stdout' to print the listing to
contrib/git-jump/git-jump
+23 -3
@@ -3,9 +3,11 @@
3 usage() {
4 cat <<\EOF
5 usage: git jump [--stdout] <mode> [<args>]
6 + or: git jump [--stdout]
7
8 Jump to interesting elements in an editor.
8 -The <mode> parameter is one of:
9 +The <mode> parameter is one of the following.
10 +With no <mode> and no <args>, it defaults to "auto".
11
12 diff: elements are diff hunks. Arguments are given to diff.
13
@@ -16,6 +18,10 @@ grep: elements are grep hits. Arguments are given to git grep or, if
18
19 ws: elements are whitespace errors. Arguments are given to diff --check.
20
21 +auto: select one of the other modes based on worktree state;
22 + "merge" if there are unmerged paths, "diff" if there are
23 + unstaged changes, "ws" if there are whitespace errors.
24 +
25 If the optional argument `--stdout` is given, print the quickfix
26 lines to standard output instead of feeding it to the editor.
27 EOF
@@ -82,6 +88,21 @@ mode_ws() {
88 git diff --check "$@"
89 }
90
91 +mode_auto() {
92 + if test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" != "true"; then
93 + usage >&2
94 + exit 1
95 + fi
96 + if test -n "$(git ls-files -u "$@")"; then
97 + mode_merge "$@"
98 + elif ! git diff --quiet "$@"; then
99 + mode_diff "$@"
100 + else
101 + usage >&2
102 + exit 1
103 + fi
104 +}
105 +
106 use_stdout=
107 while test $# -gt 0; do
108 case "$1" in
@@ -99,8 +120,7 @@ while test $# -gt 0; do
120 shift
121 done
122 if test $# -lt 1; then
102 - usage >&2
103 - exit 1
123 + set -- auto
124 fi
125 mode=$1; shift
126 type "mode_$mode" >/dev/null 2>&1 || { usage >&2; exit 1; }