Skip to content

Commit af758dc

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 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>
1 parent aec3f58 commit af758dc

2 files changed

Lines changed: 35 additions & 3 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 and no args, 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: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
usage() {
44
cat <<\EOF
55
usage: git jump [--stdout] <mode> [<args>]
6+
or: git jump [--stdout]
67
78
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".
911
1012
diff: elements are diff hunks. Arguments are given to diff.
1113
@@ -16,6 +18,10 @@ grep: elements are grep hits. Arguments are given to git grep or, if
1618
1719
ws: elements are whitespace errors. Arguments are given to diff --check.
1820
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+
1925
If the optional argument `--stdout` is given, print the quickfix
2026
lines to standard output instead of feeding it to the editor.
2127
EOF
@@ -82,6 +88,21 @@ mode_ws() {
8288
git diff --check "$@"
8389
}
8490

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+
85106
use_stdout=
86107
while test $# -gt 0; do
87108
case "$1" in
@@ -99,8 +120,7 @@ while test $# -gt 0; do
99120
shift
100121
done
101122
if test $# -lt 1; then
102-
usage >&2
103-
exit 1
123+
set -- auto
104124
fi
105125
mode=$1; shift
106126
type "mode_$mode" >/dev/null 2>&1 || { usage >&2; exit 1; }

0 commit comments

Comments
 (0)