git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 0/1] contrib/git-jump: support alias expansion
@ 2019-09-04 17:55 Taylor Blau
  2019-09-04 17:55 ` [PATCH 1/1] contrib/git-jump/git-jump: " Taylor Blau
  2019-09-05  6:04 ` [PATCH 0/1] contrib/git-jump: " Jeff King
  0 siblings, 2 replies; 4+ messages in thread
From: Taylor Blau @ 2019-09-04 17:55 UTC (permalink / raw)
  To: git; +Cc: peff

Hi,

I have been meaning to send this patch for a while, which teaches the
'git-jump' script how to respect user aliases for commands like 'diff',
'merge', and 'grep'.

I often find myself parsing the output of 'git diff' (which I spell 'g
di') in less, and then wanting to jump to some specific diff in a file,
i.e., by running 'git jump diff -- <filename>'.

But, I am so used to typing 'di' instead of 'diff', that I often write
the later invocation as 'g jump di -- <filename>', and 'git-jump'
complains that it doesn't know what 'di' means.

Let's rectify that by teaching it how to expand alises, which is
implemented in the patch below.

Thanks,
Taylor

Taylor Blau (1):
  contrib/git-jump/git-jump: support alias expansion

 contrib/git-jump/README   | 4 ++++
 contrib/git-jump/git-jump | 4 +++-
 2 files changed, 7 insertions(+), 1 deletion(-)

--
2.22.0

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/1] contrib/git-jump/git-jump: support alias expansion
  2019-09-04 17:55 [PATCH 0/1] contrib/git-jump: support alias expansion Taylor Blau
@ 2019-09-04 17:55 ` Taylor Blau
  2019-09-04 20:51   ` Taylor Blau
  2019-09-05  6:04 ` [PATCH 0/1] contrib/git-jump: " Jeff King
  1 sibling, 1 reply; 4+ messages in thread
From: Taylor Blau @ 2019-09-04 17:55 UTC (permalink / raw)
  To: git; +Cc: peff

When a caller of 'git-jump' has, say, the alias 'di' assigned to 'diff',
it can be cumbersome to remember to type 'git jump diff' instead of the
shorthand '... di' that they are used to.

Let's teach 'git-jump' to expand these aliases before calling the
mode-specific subroutine. Do so by fetching the configuration value of
'alias.$1', defaulting to "$1" in the case that no alias is set.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 contrib/git-jump/README   | 4 ++++
 contrib/git-jump/git-jump | 4 +++-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/contrib/git-jump/README b/contrib/git-jump/README
index 2f618a7f97..9e59990ba0 100644
--- a/contrib/git-jump/README
+++ b/contrib/git-jump/README
@@ -74,6 +74,10 @@ git jump grep -i foo_bar
 
 # use the silver searcher for git jump grep
 git config jump.grepCmd "ag --column"
+
+# jump to changes via an alias of 'git diff', assuming you have set
+# `git config alias.diff di`
+git jump di
 --------------------------------------------------
 
 
diff --git a/contrib/git-jump/git-jump b/contrib/git-jump/git-jump
index 931b0fe3a9..15e129b350 100755
--- a/contrib/git-jump/git-jump
+++ b/contrib/git-jump/git-jump
@@ -15,6 +15,8 @@ grep: elements are grep hits. Arguments are given to git grep or, if
       configured, to the command in `jump.grepCmd`.
 
 ws: elements are whitespace errors. Arguments are given to diff --check.
+
+Aliases of any of the above are expanded automatically.
 EOF
 }
 
@@ -68,7 +70,7 @@ if test $# -lt 1; then
 	usage >&2
 	exit 1
 fi
-mode=$1; shift
+mode="$(git config --default "$1" --get -- "alias.$1")"; shift
 
 trap 'rm -f "$tmp"' 0 1 2 3 15
 tmp=`mktemp -t git-jump.XXXXXX` || exit 1
-- 
2.22.0

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] contrib/git-jump/git-jump: support alias expansion
  2019-09-04 17:55 ` [PATCH 1/1] contrib/git-jump/git-jump: " Taylor Blau
@ 2019-09-04 20:51   ` Taylor Blau
  0 siblings, 0 replies; 4+ messages in thread
From: Taylor Blau @ 2019-09-04 20:51 UTC (permalink / raw)
  To: Taylor Blau; +Cc: git, peff

On Wed, Sep 04, 2019 at 01:55:03PM -0400, Taylor Blau wrote:
> @@ -68,7 +70,7 @@ if test $# -lt 1; then
>  	usage >&2
>  	exit 1
>  fi
> -mode=$1; shift
> +mode="$(git config --default "$1" --get -- "alias.$1")"; shift
>
>  trap 'rm -f "$tmp"' 0 1 2 3 15
>  tmp=`mktemp -t git-jump.XXXXXX` || exit 1

I guess it's worth noting that this does _not_ respect extra options
given to the various modes. For example, if I alias 'diff' to 'diff
--minimal', we will try and invoke the function "mode_diff --minimal",
which doesn't make sense.

Perhaps we could take the output of this through "| awk '{ print $1 }'"
to discard any extra options, but it feels like a bit of a hack.

Personally, I'm not bothered by this, but I also don't use aliases to
add "default" options to any git sub-commands. But, I don't know if
other people do, in which case they may want to chime in here.

Thanks in advance for your thoughts.

> --
> 2.22.0

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/1] contrib/git-jump: support alias expansion
  2019-09-04 17:55 [PATCH 0/1] contrib/git-jump: support alias expansion Taylor Blau
  2019-09-04 17:55 ` [PATCH 1/1] contrib/git-jump/git-jump: " Taylor Blau
@ 2019-09-05  6:04 ` Jeff King
  1 sibling, 0 replies; 4+ messages in thread
From: Jeff King @ 2019-09-05  6:04 UTC (permalink / raw)
  To: Taylor Blau; +Cc: git

On Wed, Sep 04, 2019 at 01:55:00PM -0400, Taylor Blau wrote:

> I often find myself parsing the output of 'git diff' (which I spell 'g
> di') in less, and then wanting to jump to some specific diff in a file,
> i.e., by running 'git jump diff -- <filename>'.
> 
> But, I am so used to typing 'di' instead of 'diff', that I often write
> the later invocation as 'g jump di -- <filename>', and 'git-jump'
> complains that it doesn't know what 'di' means.

Hmm. I'm not exactly _opposed_ to this patch, but it does feel like it's
weirdly conflating git commands with jump modes. Which just happen to
use some of the same verbs, but not not always (e.g., "mode_ws").

And as you note, aliases may carry along options which do not make any
sense for jump modes (or they might; we pass command-line options to
diff and grep, so it's possible the alias could do something useful).

Your case would be equally helped if the tool allowed any non-ambiguous
prefix to be used. But it wouldn't if somebody had "alias.foo = diff" or
something. So I dunno.

I solved this for myself long ago with a mix of git and shell aliases:

  $ git help vgrep
  'vgrep' is aliased to 'jump grep'
  
  $ type d
  d is aliased to `git jump diff'
  
  $ type m
  m is aliased to `git jump merge'

For fun, here's a patch that does the prefix thing (though the $0
hackery may be too much; we list the modes by hand in the usage, after
all).

diff --git a/contrib/git-jump/git-jump b/contrib/git-jump/git-jump
index 931b0fe3a9..c2a092806e 100755
--- a/contrib/git-jump/git-jump
+++ b/contrib/git-jump/git-jump
@@ -64,15 +64,35 @@ mode_ws() {
 	git diff --check "$@"
 }
 
+list_modes() {
+	perl -lne '/^mode_([a-z]+)[ (]/ and print $1' "$0"
+}
+
 if test $# -lt 1; then
 	usage >&2
 	exit 1
 fi
 mode=$1; shift
 
+if ! type "mode_$mode" >/dev/null 2>&1; then
+	found=
+	for i in $(list_modes | grep "^$mode"); do
+		if test -n "$found"; then
+			echo >&2 "ambiguous mode: $mode (matches $found and $i)"
+			exit 1
+		fi
+		found=$i
+	done
+	if test -n "$found"; then
+		mode=$found
+	else
+		usage >&2
+		exit 1
+	fi
+fi
+
 trap 'rm -f "$tmp"' 0 1 2 3 15
 tmp=`mktemp -t git-jump.XXXXXX` || exit 1
-type "mode_$mode" >/dev/null 2>&1 || { usage >&2; exit 1; }
 "mode_$mode" "$@" >"$tmp"
 test -s "$tmp" || exit 0
 open_editor "$tmp"

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-09-05  6:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-04 17:55 [PATCH 0/1] contrib/git-jump: support alias expansion Taylor Blau
2019-09-04 17:55 ` [PATCH 1/1] contrib/git-jump/git-jump: " Taylor Blau
2019-09-04 20:51   ` Taylor Blau
2019-09-05  6:04 ` [PATCH 0/1] contrib/git-jump: " Jeff King

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).