Skip to content

Commit 87fa66d

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 when they run `git jump` without arguments: 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`. Detect these two cases and dispatch to the corresponding mode automatically, falling back to the existing usage-and-exit behavior when neither holds. Signed-off-by: Greg Hurrell <greg.hurrell@datadoghq.com>
1 parent 94f0577 commit 87fa66d

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

contrib/git-jump/README

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ To use it, just drop git-jump in your PATH, and then invoke it like
5555
this:
5656

5757
--------------------------------------------------
58+
# pick a mode automatically: "merge" if there are unmerged paths,
59+
# "diff" if the worktree has unstaged changes, otherwise show usage
60+
git jump
61+
5862
# jump to changes not yet staged for commit
5963
git jump diff
6064

contrib/git-jump/git-jump

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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.
88
The <mode> parameter is one of:
@@ -99,8 +99,18 @@ while test $# -gt 0; do
9999
shift
100100
done
101101
if test $# -lt 1; then
102-
usage >&2
103-
exit 1
102+
if test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" != "true"; then
103+
usage >&2
104+
exit 1
105+
fi
106+
if test -n "$(git ls-files -u)"; then
107+
set -- merge
108+
elif ! git diff --quiet; then
109+
set -- diff
110+
else
111+
usage >&2
112+
exit 1
113+
fi
104114
fi
105115
mode=$1; shift
106116
type "mode_$mode" >/dev/null 2>&1 || { usage >&2; exit 1; }

0 commit comments

Comments
 (0)