From: Greg Hurrell <greg.hurrell@datadoghq.com>
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>
---
git-jump: pick a mode automatically when invoked without arguments
Changes since v0:
* Added explicit "auto" keyword/mode.
* Updated additional detail to usage info and README.
* (Bonus) Added ws usage example to README.Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2108%2Fwincent%2Fauto-jump-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2108/wincent/auto-jump-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/2108
Range-diff vs v1:
1: 87fa66d233 ! 1: 5fbc8480ef git-jump: pick a mode automatically when invoked without arguments
@@ Commit message
arguments after `--stdout`) it currently prints usage and exits with
status 1.
- But there are two situations where we can usefully infer the most
+ 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 when they run `git jump` without arguments:
+ automatically:
1. When there are unmerged paths in the index, the user likely
wants `git jump merge`.
@@ Commit message
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.
+ 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>
## contrib/git-jump/README ##
-@@ contrib/git-jump/README: To use it, just drop git-jump in your PATH, and then invoke it like
- this:
+@@ contrib/git-jump/README: git jump grep foo_bar
+ # arbitrary grep options
+ git jump grep -i foo_bar
- --------------------------------------------------
++# jump to places with conflict markers or whitespace errors
++# (as reported by # `git diff --check`)
++git jump ws
++
+ # use the silver searcher for git jump grep
+ git config jump.grepCmd "ag --column"
++
+# pick a mode automatically: "merge" if there are unmerged paths,
-+# "diff" if the worktree has unstaged changes, otherwise show usage
-+git jump
++# "diff" if the worktree has unstaged changes, "ws" if there are
++# whitespace problems; otherwise show usage
++git jump auto
+
- # jump to changes not yet staged for commit
- git jump diff
++# with no explicit mode, same as "auto"
++git jump
+ --------------------------------------------------
+ You can use the optional argument '--stdout' to print the listing to
## contrib/git-jump/git-jump ##
@@
@@ contrib/git-jump/git-jump
+usage: git jump [--stdout] [<mode>] [<args>]
Jump to interesting elements in an editor.
- The <mode> parameter is one of:
-@@ contrib/git-jump/git-jump: while test $# -gt 0; do
- shift
- done
- if test $# -lt 1; then
-- usage >&2
-- exit 1
+-The <mode> parameter is one of:
++The <mode> parameter is one of the following,
++defaulting to "auto" if omitted:
+
+ diff: elements are diff hunks. Arguments are given to diff.
+
+@@ contrib/git-jump/git-jump: grep: elements are grep hits. Arguments are given to git grep or, if
+
+ ws: elements are whitespace errors. Arguments are given to diff --check.
+
++auto: select one of the other modes based on worktree state;
++ "merge" if there are unmerged paths, "diff" if there are
++ unstaged changes, "ws" if there are whitespace errors.
++
+ If the optional argument `--stdout` is given, print the quickfix
+ lines to standard output instead of feeding it to the editor.
+ EOF
+@@ contrib/git-jump/git-jump: mode_ws() {
+ git diff --check "$@"
+ }
+
++mode_auto() {
+ if test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" != "true"; then
+ usage >&2
+ exit 1
+ fi
-+ if test -n "$(git ls-files -u)"; then
-+ set -- merge
-+ elif ! git diff --quiet; then
-+ set -- diff
++ if test -n "$(git ls-files -u "$@")"; then
++ mode_merge "$@"
++ elif ! git diff --quiet "$@"; then
++ mode_diff "$@"
++ elif ! git diff --check >/dev/null 2>&1; then
++ mode_ws "$@"
+ else
+ usage >&2
+ exit 1
+ fi
++}
++
+ use_stdout=
+ while test $# -gt 0; do
+ case "$1" in
+@@ contrib/git-jump/git-jump: while test $# -gt 0; do
+ shift
+ done
+ if test $# -lt 1; then
+- usage >&2
+- exit 1
++ set -- auto
fi
mode=$1; shift
type "mode_$mode" >/dev/null 2>&1 || { usage >&2; exit 1; } contrib/git-jump/README | 12 ++++++++++++
contrib/git-jump/git-jump | 29 +++++++++++++++++++++++++----
2 files changed, 37 insertions(+), 4 deletions(-)
Show changes to 2 files +37 −4
contrib/git-jump/README, contrib/git-jump/git-jump
diff --git a/contrib/git-jump/README b/contrib/git-jump/README
index 3211841305..ac35792e55 100644
--- a/contrib/git-jump/README
+++ b/contrib/git-jump/README
@@ -75,8 +75,20 @@ git jump grep foo_bar
# arbitrary grep options
git jump grep -i foo_bar
+# jump to places with conflict markers or whitespace errors
+# (as reported by # `git diff --check`)
+git jump ws
+
# use the silver searcher for git jump grep
git config jump.grepCmd "ag --column"
+
+# pick a mode automatically: "merge" if there are unmerged paths,
+# "diff" if the worktree has unstaged changes, "ws" if there are
+# whitespace problems; otherwise show usage
+git jump auto
+
+# with no explicit mode, same as "auto"
+git jump
--------------------------------------------------
You can use the optional argument '--stdout' to print the listing to
diff --git a/contrib/git-jump/git-jump b/contrib/git-jump/git-jump
index 8d1d5d79a6..43d3b42a41 100755
--- a/contrib/git-jump/git-jump
+++ b/contrib/git-jump/git-jump
@@ -2,10 +2,11 @@
usage() {
cat <<\EOF
-usage: git jump [--stdout] <mode> [<args>]
+usage: git jump [--stdout] [<mode>] [<args>]
Jump to interesting elements in an editor.
-The <mode> parameter is one of:
+The <mode> parameter is one of the following,
+defaulting to "auto" if omitted:
diff: elements are diff hunks. Arguments are given to diff.
@@ -16,6 +17,10 @@ grep: elements are grep hits. Arguments are given to git grep or, if
ws: elements are whitespace errors. Arguments are given to diff --check.
+auto: select one of the other modes based on worktree state;
+ "merge" if there are unmerged paths, "diff" if there are
+ unstaged changes, "ws" if there are whitespace errors.
+
If the optional argument `--stdout` is given, print the quickfix
lines to standard output instead of feeding it to the editor.
EOF
@@ -82,6 +87,23 @@ mode_ws() {
git diff --check "$@"
}
+mode_auto() {
+ if test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" != "true"; then
+ usage >&2
+ exit 1
+ fi
+ if test -n "$(git ls-files -u "$@")"; then
+ mode_merge "$@"
+ elif ! git diff --quiet "$@"; then
+ mode_diff "$@"
+ elif ! git diff --check >/dev/null 2>&1; then
+ mode_ws "$@"
+ else
+ usage >&2
+ exit 1
+ fi
+}
+
use_stdout=
while test $# -gt 0; do
case "$1" in
@@ -99,8 +121,7 @@ while test $# -gt 0; do
shift
done
if test $# -lt 1; then
- usage >&2
- exit 1
+ set -- auto
fi
mode=$1; shift
type "mode_$mode" >/dev/null 2>&1 || { usage >&2; exit 1; }
base-commit: 1c00d2d8392f603a6263f11f1a50fde96ae5475e