All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Greg Hurrell via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Jeff King <peff@peff.net>, Greg Hurrell <greg@hurrell.net>,
	Erik Cervin Edin <erik@cervined.in>,
	Junio C Hamano <gitster@pobox.com>,
	Greg Hurrell <greg.hurrell@datadoghq.com>,
	Greg Hurrell <greg.hurrell@datadoghq.com>
Subject: [PATCH v3] git-jump: pick a mode automatically when invoked without arguments
Date: Thu, 21 May 2026 13:45:09 +0000	[thread overview]
Message-ID: <pull.2108.v3.git.1779371110195.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2108.v2.git.1779280307112.gitgitgadget@gmail.com>

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 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>
---
    git-jump: pick a mode automatically when invoked without arguments
    
    Changes since v2; all of these in response to feedback from Junio:
    
     * Removed stray # from README.
     * Don't both teaching "auto" to select "ws" mode, because it is always
       subsumed by "diff".
     * Update usage string to make clear that git jump --stdout foo is not a
       synonym for git jump --stdout auto foo, because distinguishing
       between foo as <mode> and foo as <arg> is fraught with ambiguity.
    
    In answer to Junio's question:
    
    > If more than one interesting cases apply, what happens, and what
    > should happen?
    
    it's an ordered choice (merge > diff).

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2108%2Fwincent%2Fauto-jump-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2108/wincent/auto-jump-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/2108

Range-diff vs v2:

 1:  5fbc8480ef ! 1:  af758dcfd2 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 several situations where we can usefully infer the most
     +    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:
      
     @@ Commit message
          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
     @@ contrib/git-jump/README: git jump grep foo_bar
       git jump grep -i foo_bar
       
      +# jump to places with conflict markers or whitespace errors
     -+# (as reported by # `git diff --check`)
     ++# (as reported by `git diff --check`)
      +git jump ws
      +
       # use the silver searcher for git jump grep
     @@ contrib/git-jump/README: git jump grep foo_bar
      +# whitespace problems; otherwise show usage
      +git jump auto
      +
     -+# with no explicit mode, same as "auto"
     ++# with no explicit mode and no args, same as "auto"
      +git jump
       --------------------------------------------------
       
     @@ contrib/git-jump/README: git jump grep foo_bar
      
       ## contrib/git-jump/git-jump ##
      @@
     - 
       usage() {
       	cat <<\EOF
     --usage: git jump [--stdout] <mode> [<args>]
     -+usage: git jump [--stdout] [<mode>] [<args>]
     + usage: git jump [--stdout] <mode> [<args>]
     ++   or: git jump [--stdout]
       
       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:
     ++The <mode> parameter is one of the following.
     ++With no <mode> and no <args>, it defaults to "auto".
       
       diff: elements are diff hunks. Arguments are given to diff.
       
     @@ contrib/git-jump/git-jump: mode_ws() {
      +		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


 contrib/git-jump/README   | 12 ++++++++++++
 contrib/git-jump/git-jump | 26 +++++++++++++++++++++++---
 2 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/contrib/git-jump/README b/contrib/git-jump/README
index 3211841305..aabec4a756 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 and no args, 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..79286d8112 100755
--- a/contrib/git-jump/git-jump
+++ b/contrib/git-jump/git-jump
@@ -3,9 +3,11 @@
 usage() {
 	cat <<\EOF
 usage: git jump [--stdout] <mode> [<args>]
+   or: git jump [--stdout]
 
 Jump to interesting elements in an editor.
-The <mode> parameter is one of:
+The <mode> parameter is one of the following.
+With no <mode> and no <args>, it defaults to "auto".
 
 diff: elements are diff hunks. Arguments are given to diff.
 
@@ -16,6 +18,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 +88,21 @@ 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 "$@"
+	else
+		usage >&2
+		exit 1
+	fi
+}
+
 use_stdout=
 while test $# -gt 0; do
 	case "$1" in
@@ -99,8 +120,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: aec3f587505a472db67e9462d0702e7d463a449d
-- 
gitgitgadget

  parent reply	other threads:[~2026-05-21 13:45 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-08  9:07 [PATCH] git-jump: pick a mode automatically when invoked without arguments Greg Hurrell via GitGitGadget
2026-05-08 14:13 ` Jeff King
2026-05-08 14:30   ` Greg Hurrell
2026-05-08 17:52     ` Jeff King
2026-05-14 15:40       ` Erik Cervin Edin
2026-05-19  9:03         ` Greg Hurrell
2026-05-19 21:22           ` Jeff King
2026-05-20 12:31 ` [PATCH v2] " Greg Hurrell via GitGitGadget
2026-05-21  1:22   ` Junio C Hamano
2026-05-21  1:30     ` Junio C Hamano
2026-05-21 13:45   ` Greg Hurrell via GitGitGadget [this message]
2026-05-21 14:00     ` [PATCH v3] " Junio C Hamano
2026-05-22  5:28     ` Jeff King
2026-05-22  7:33       ` Greg Hurrell
2026-05-26 21:33       ` Erik Cervin Edin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.2108.v3.git.1779371110195.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=erik@cervined.in \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=greg.hurrell@datadoghq.com \
    --cc=greg@hurrell.net \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.