{"thread":{"id":"51801","subject":"[PATCH 0/1] contrib/git-jump: support alias expansion","startedAt":"2019-09-04T17:55:04Z","lastAt":"2019-09-05T06:04:30Z","messageCount":4,"participants":["Taylor Blau","Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":1},"messages":[{"id":"381806","messageId":"cover.1567619579.git.me@ttaylorr.com","threadId":"51801","inReplyTo":null,"subject":"[PATCH 0/1] contrib/git-jump: support alias expansion","fromName":"Taylor Blau","fromEmail":"me@ttaylorr.com","sentAt":"2019-09-04T17:55:00Z","receivedAt":"2019-09-04T17:55:04Z","isPatch":true,"sender":{"key":"me@ttaylorr.com","avatar":"https://avatars.githubusercontent.com/u/301000140?v=4"},"body":"Hi,\n\nI have been meaning to send this patch for a while, which teaches the\n'git-jump' script how to respect user aliases for commands like 'diff',\n'merge', and 'grep'.\n\nI often find myself parsing the output of 'git diff' (which I spell 'g\ndi') in less, and then wanting to jump to some specific diff in a file,\ni.e., by running 'git jump diff -- <filename>'.\n\nBut, I am so used to typing 'di' instead of 'diff', that I often write\nthe later invocation as 'g jump di -- <filename>', and 'git-jump'\ncomplains that it doesn't know what 'di' means.\n\nLet's rectify that by teaching it how to expand alises, which is\nimplemented in the patch below.\n\nThanks,\nTaylor\n\nTaylor Blau (1):\n  contrib/git-jump/git-jump: support alias expansion\n\n contrib/git-jump/README   | 4 ++++\n contrib/git-jump/git-jump | 4 +++-\n 2 files changed, 7 insertions(+), 1 deletion(-)\n\n--\n2.22.0\n"},{"id":"381807","messageId":"473a7c7b241ad2d449d3bcb6daeb77a179c7e45f.1567619579.git.me@ttaylorr.com","threadId":"51801","inReplyTo":"cover.1567619579.git.me@ttaylorr.com","subject":"[PATCH 1/1] contrib/git-jump/git-jump: support alias expansion","fromName":"Taylor Blau","fromEmail":"me@ttaylorr.com","sentAt":"2019-09-04T17:55:03Z","receivedAt":"2019-09-04T17:55:06Z","isPatch":true,"sender":{"key":"me@ttaylorr.com","avatar":"https://avatars.githubusercontent.com/u/301000140?v=4"},"body":"When a caller of 'git-jump' has, say, the alias 'di' assigned to 'diff',\nit can be cumbersome to remember to type 'git jump diff' instead of the\nshorthand '... di' that they are used to.\n\nLet's teach 'git-jump' to expand these aliases before calling the\nmode-specific subroutine. Do so by fetching the configuration value of\n'alias.$1', defaulting to \"$1\" in the case that no alias is set.\n\nSigned-off-by: Taylor Blau <me@ttaylorr.com>\n---\n contrib/git-jump/README   | 4 ++++\n contrib/git-jump/git-jump | 4 +++-\n 2 files changed, 7 insertions(+), 1 deletion(-)\n\ndiff --git a/contrib/git-jump/README b/contrib/git-jump/README\nindex 2f618a7f97..9e59990ba0 100644\n--- a/contrib/git-jump/README\n+++ b/contrib/git-jump/README\n@@ -74,6 +74,10 @@ git jump grep -i foo_bar\n \n # use the silver searcher for git jump grep\n git config jump.grepCmd \"ag --column\"\n+\n+# jump to changes via an alias of 'git diff', assuming you have set\n+# `git config alias.diff di`\n+git jump di\n --------------------------------------------------\n \n \ndiff --git a/contrib/git-jump/git-jump b/contrib/git-jump/git-jump\nindex 931b0fe3a9..15e129b350 100755\n--- a/contrib/git-jump/git-jump\n+++ b/contrib/git-jump/git-jump\n@@ -15,6 +15,8 @@ grep: elements are grep hits. Arguments are given to git grep or, if\n       configured, to the command in `jump.grepCmd`.\n \n ws: elements are whitespace errors. Arguments are given to diff --check.\n+\n+Aliases of any of the above are expanded automatically.\n EOF\n }\n \n@@ -68,7 +70,7 @@ if test $# -lt 1; then\n \tusage >&2\n \texit 1\n fi\n-mode=$1; shift\n+mode=\"$(git config --default \"$1\" --get -- \"alias.$1\")\"; shift\n \n trap 'rm -f \"$tmp\"' 0 1 2 3 15\n tmp=`mktemp -t git-jump.XXXXXX` || exit 1\n-- \n2.22.0\n"},{"id":"381830","messageId":"20190904205116.GA20056@syl.local","threadId":"51801","inReplyTo":"473a7c7b241ad2d449d3bcb6daeb77a179c7e45f.1567619579.git.me@ttaylorr.com","subject":"Re: [PATCH 1/1] contrib/git-jump/git-jump: support alias expansion","fromName":"Taylor Blau","fromEmail":"me@ttaylorr.com","sentAt":"2019-09-04T20:51:16Z","receivedAt":"2019-09-04T20:51:20Z","isPatch":true,"sender":{"key":"me@ttaylorr.com","avatar":"https://avatars.githubusercontent.com/u/301000140?v=4"},"body":"On Wed, Sep 04, 2019 at 01:55:03PM -0400, Taylor Blau wrote:\n> @@ -68,7 +70,7 @@ if test $# -lt 1; then\n>  \tusage >&2\n>  \texit 1\n>  fi\n> -mode=$1; shift\n> +mode=\"$(git config --default \"$1\" --get -- \"alias.$1\")\"; shift\n>\n>  trap 'rm -f \"$tmp\"' 0 1 2 3 15\n>  tmp=`mktemp -t git-jump.XXXXXX` || exit 1\n\nI guess it's worth noting that this does _not_ respect extra options\ngiven to the various modes. For example, if I alias 'diff' to 'diff\n--minimal', we will try and invoke the function \"mode_diff --minimal\",\nwhich doesn't make sense.\n\nPerhaps we could take the output of this through \"| awk '{ print $1 }'\"\nto discard any extra options, but it feels like a bit of a hack.\n\nPersonally, I'm not bothered by this, but I also don't use aliases to\nadd \"default\" options to any git sub-commands. But, I don't know if\nother people do, in which case they may want to chime in here.\n\nThanks in advance for your thoughts.\n\n> --\n> 2.22.0\n\nThanks,\nTaylor\n"},{"id":"381854","messageId":"20190905060427.GA21450@sigill.intra.peff.net","threadId":"51801","inReplyTo":"cover.1567619579.git.me@ttaylorr.com","subject":"Re: [PATCH 0/1] contrib/git-jump: support alias expansion","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2019-09-05T06:04:27Z","receivedAt":"2019-09-05T06:04:30Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Sep 04, 2019 at 01:55:00PM -0400, Taylor Blau wrote:\n\n> I often find myself parsing the output of 'git diff' (which I spell 'g\n> di') in less, and then wanting to jump to some specific diff in a file,\n> i.e., by running 'git jump diff -- <filename>'.\n> \n> But, I am so used to typing 'di' instead of 'diff', that I often write\n> the later invocation as 'g jump di -- <filename>', and 'git-jump'\n> complains that it doesn't know what 'di' means.\n\nHmm. I'm not exactly _opposed_ to this patch, but it does feel like it's\nweirdly conflating git commands with jump modes. Which just happen to\nuse some of the same verbs, but not not always (e.g., \"mode_ws\").\n\nAnd as you note, aliases may carry along options which do not make any\nsense for jump modes (or they might; we pass command-line options to\ndiff and grep, so it's possible the alias could do something useful).\n\nYour case would be equally helped if the tool allowed any non-ambiguous\nprefix to be used. But it wouldn't if somebody had \"alias.foo = diff\" or\nsomething. So I dunno.\n\nI solved this for myself long ago with a mix of git and shell aliases:\n\n  $ git help vgrep\n  'vgrep' is aliased to 'jump grep'\n  \n  $ type d\n  d is aliased to `git jump diff'\n  \n  $ type m\n  m is aliased to `git jump merge'\n\nFor fun, here's a patch that does the prefix thing (though the $0\nhackery may be too much; we list the modes by hand in the usage, after\nall).\n\ndiff --git a/contrib/git-jump/git-jump b/contrib/git-jump/git-jump\nindex 931b0fe3a9..c2a092806e 100755\n--- a/contrib/git-jump/git-jump\n+++ b/contrib/git-jump/git-jump\n@@ -64,15 +64,35 @@ mode_ws() {\n \tgit diff --check \"$@\"\n }\n \n+list_modes() {\n+\tperl -lne '/^mode_([a-z]+)[ (]/ and print $1' \"$0\"\n+}\n+\n if test $# -lt 1; then\n \tusage >&2\n \texit 1\n fi\n mode=$1; shift\n \n+if ! type \"mode_$mode\" >/dev/null 2>&1; then\n+\tfound=\n+\tfor i in $(list_modes | grep \"^$mode\"); do\n+\t\tif test -n \"$found\"; then\n+\t\t\techo >&2 \"ambiguous mode: $mode (matches $found and $i)\"\n+\t\t\texit 1\n+\t\tfi\n+\t\tfound=$i\n+\tdone\n+\tif test -n \"$found\"; then\n+\t\tmode=$found\n+\telse\n+\t\tusage >&2\n+\t\texit 1\n+\tfi\n+fi\n+\n trap 'rm -f \"$tmp\"' 0 1 2 3 15\n tmp=`mktemp -t git-jump.XXXXXX` || exit 1\n-type \"mode_$mode\" >/dev/null 2>&1 || { usage >&2; exit 1; }\n \"mode_$mode\" \"$@\" >\"$tmp\"\n test -s \"$tmp\" || exit 0\n open_editor \"$tmp\"\n"}]}