Skip to content

Commit

Permalink
git: add q script
Browse files Browse the repository at this point in the history
"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
  • Loading branch information
benknoble committed Nov 15, 2024
1 parent 91f9988 commit 79a27b6
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions links/bin/git-q
Original file line number Diff line number Diff line change
@@ -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] <spec-or-ref> [<ref>…]
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" "$@"

0 comments on commit 79a27b6

Please sign in to comment.