From 79a27b666323494b0fdcc82dbb1d0b5f73b556e2 Mon Sep 17 00:00:00 2001 From: "D. Ben Knoble" Date: Fri, 15 Nov 2024 13:48:16 -0500 Subject: [PATCH] git: add q script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "Q" stands for "query": Inspired by MJD [1], add my own version. Mine shows symbolic refs like the input by default, but expands to short hashes with `-v`. It also collapses equivalent refs (because `show` does) instead of running a command for each input (which is expensive). Here's hyperfine output: hyperfine --output=pipe 'git q2 HEAD origin/master~{1..1000}' 'git q HEAD origin/master~{1..1000}' Benchmark 1: git q2 HEAD origin/master~{1..1000} Time (mean ± σ): 9.524 s ± 0.012 s [User: 4.540 s, System: 3.397 s] Range (min … max): 9.510 s … 9.547 s 10 runs Benchmark 2: git q HEAD origin/master~{1..1000} Time (mean ± σ): 163.7 ms ± 1.9 ms [User: 99.8 ms, System: 54.8 ms] Range (min … max): 160.8 ms … 168.2 ms 17 runs Summary git q HEAD origin/master~{1..1000} ran 58.18 ± 0.70 times faster than git q2 HEAD origin/master~{1..1000} The 2 commands produce identical output (although q2 used different lengths with -v sometimes), where q2 is the same as q with this patch: 38c38 < exec git show -s --format="$id $query" "$@" --- > for ref; do git show -s --format="$id $query" "$ref"; done [1]: https://blog.plover.com/prog/git-q.html --- links/bin/git-q | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 links/bin/git-q diff --git a/links/bin/git-q b/links/bin/git-q new file mode 100755 index 0000000..e4461ef --- /dev/null +++ b/links/bin/git-q @@ -0,0 +1,38 @@ +#! /bin/sh + +# Inspired by: https://github.com/mjdominus/git-util/blob/master/bin/git-q + +set -eu + +OPTIONS_SPEC="\ +$(basename -- "$0" | sed -e 's/-/ /') [-v] […] + +Query refs for data using pretty-format specs (default: %cd) +-- +v verbose: show commit IDs instead of input refs" +SUBDIRECTORY_OK=true + +# source git-sh-setup for some helpers +set +u +. "$(git --exec-path)"/git-sh-setup +set -u + +id=%S + +while test $# -gt 0; do + opt="$1" + shift + case "$opt" in + (-v) id=%h ;; + (--) break ;; + esac +done +test $# -eq 0 && usage + +query=%cd +case "$1" in + (%*) query="$1"; shift ;; +esac +test $# -eq 0 && usage + +exec git show -s --format="$id $query" "$@"