Skip to content

Commit 5fbc848

Browse files
committed
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 several 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`. 3. In the presence of conflict markers or whitespace errors (as reported by `git diff --check`), the user likely wants `git jump ws`. 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>
1 parent 1c00d2d commit 5fbc848

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

contrib/git-jump/README

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,20 @@ git jump grep foo_bar
7575
# arbitrary grep options
7676
git jump grep -i foo_bar
7777

78+
# jump to places with conflict markers or whitespace errors
79+
# (as reported by # `git diff --check`)
80+
git jump ws
81+
7882
# use the silver searcher for git jump grep
7983
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, same as "auto"
91+
git jump
8092
--------------------------------------------------
8193

8294
You can use the optional argument '--stdout' to print the listing to

contrib/git-jump/git-jump

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
usage() {
44
cat <<\EOF
5-
usage: git jump [--stdout] <mode> [<args>]
5+
usage: git jump [--stdout] [<mode>] [<args>]
66
77
Jump to interesting elements in an editor.
8-
The <mode> parameter is one of:
8+
The <mode> parameter is one of the following,
9+
defaulting to "auto" if omitted:
910
1011
diff: elements are diff hunks. Arguments are given to diff.
1112
@@ -16,6 +17,10 @@ grep: elements are grep hits. Arguments are given to git grep or, if
1617
1718
ws: elements are whitespace errors. Arguments are given to diff --check.
1819
20+
auto: select one of the other modes based on worktree state;
21+
"merge" if there are unmerged paths, "diff" if there are
22+
unstaged changes, "ws" if there are whitespace errors.
23+
1924
If the optional argument `--stdout` is given, print the quickfix
2025
lines to standard output instead of feeding it to the editor.
2126
EOF
@@ -82,6 +87,23 @@ mode_ws() {
8287
git diff --check "$@"
8388
}
8489

90+
mode_auto() {
91+
if test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" != "true"; then
92+
usage >&2
93+
exit 1
94+
fi
95+
if test -n "$(git ls-files -u "$@")"; then
96+
mode_merge "$@"
97+
elif ! git diff --quiet "$@"; then
98+
mode_diff "$@"
99+
elif ! git diff --check >/dev/null 2>&1; then
100+
mode_ws "$@"
101+
else
102+
usage >&2
103+
exit 1
104+
fi
105+
}
106+
85107
use_stdout=
86108
while test $# -gt 0; do
87109
case "$1" in
@@ -99,8 +121,7 @@ while test $# -gt 0; do
99121
shift
100122
done
101123
if test $# -lt 1; then
102-
usage >&2
103-
exit 1
124+
set -- auto
104125
fi
105126
mode=$1; shift
106127
type "mode_$mode" >/dev/null 2>&1 || { usage >&2; exit 1; }

0 commit comments

Comments
 (0)