From 70214760c7f8f9bde7a8018165f2e9d402612afe Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Sun, 9 Jan 2022 00:20:52 -0500 Subject: [PATCH] Add git-grep alias and git-grep-editor tools This is a fun exploration of UNIX as an IDE. The problem: I want to be able to quickly jump to occurrences as specified in git-grep. The solution? I either need to embed git-grep in an IDE (vim), or I need to write a tool that parses git-grep and knows HOW to open an editor. This patch adds such a tool, to stay true to the UNIX philosophy, warts and all. The tool has two smaller bits--one to parse the "file:number:desc" and launch the editor, and another to parse git-grep. --- .gitconfig | 1 + bin/fned | 30 ++++++++++++++++++++++++++++++ bin/gge | 24 ++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100755 bin/fned create mode 100755 bin/gge diff --git a/.gitconfig b/.gitconfig index c69e14f..ac57e06 100644 --- a/.gitconfig +++ b/.gitconfig @@ -17,6 +17,7 @@ rs = reset d = diff c = commit + g = grep -n [pull] ff = only diff --git a/bin/fned b/bin/fned new file mode 100755 index 0000000..88c8f7b --- /dev/null +++ b/bin/fned @@ -0,0 +1,30 @@ +#!/bin/sh +# fned -- file:number editor +# reads file:number:blah (grep-style) from stdin or ARGV and opens +# the file in $EDITOR at that point (for vi/m). Other editors just +# open the file & print the line +# +# example: +# +# fned "$(git grep -n 'pattern' | fzf)" +# +# see also: gge(1) +# +die() { + echo "$*" 1>&2 + exit 1 +} +[ -z "$1" ] && die "usage: fned 'file:line:desc'" + +fstr="$*" +file=${fstr%%:*} +nofile=${fstr##$file:} +line=${nofile%%:*} + +case ${EDITOR:-vi} in + vi*) $EDITOR -c "$line" "$file" ;; + *) + echo "fned: $EDITOR doesn't support line jumps. Go to $line" + $EDITOR "$file" + ;; +esac diff --git a/bin/gge b/bin/gge new file mode 100755 index 0000000..7a88a99 --- /dev/null +++ b/bin/gge @@ -0,0 +1,24 @@ +#!/bin/sh +# gge -- git grep -> editor +# +# small ease-of-use tool that combines git-grep and fned +die() { + echo "$*" 1>&2 + exit 1 +} + +# Reuse last pattern +if [ -z "$1" ]; then + if [ ! -e /tmp/gge ]; then + die "No previous search, run with 'gge PATTERN'" + fi +else + git grep -n "$@" > /tmp/gge +fi + +[ ! -s /tmp/gge ] && die "no matches, exiting" + +choice=$(fzf < /tmp/gge) +[ -z "$choice" ] && exit 1 + +fned "$choice" -- libgit2 1.1.1