From: Steven Baltakatei Sandoval Date: Sat, 7 Jan 2023 22:24:38 +0000 (+0000) Subject: feat(user/git-bk-find-file):Add script to search old tree filenames X-Git-Tag: 0.6.0^0 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/68e86d2510e563f5deb8e71c274584dd92360d46?ds=sidebyside feat(user/git-bk-find-file):Add script to search old tree filenames - Ref/Attrib: albfan "How can I search Git branches for a file or directory?" https://stackoverflow.com/a/16868704/10850071 --- diff --git a/user/git-bk-find-file b/user/git-bk-find-file new file mode 100644 index 0000000..c31b31c --- /dev/null +++ b/user/git-bk-find-file @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +# Desc: Grep search filenames of all git committed work trees +# Usage: git-bk-find-file [string regex] +# Example git-bk-find-file '*.txt' +# Ref/Attrib: albfan "How can I search Git branches for a file or directory?" https://stackoverflow.com/a/16868704/10850071 +# Version: 0.0.1 +# Depends: GNU bash v5.1.16, GNU parallel 20210822 + +yell() { echo "$0: $*" >&2; } # print script path and all args to stderr +die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status +must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails +check_depends() { + # Desc: Check dependencies + # Usage: check_depends + # Depends: GNU parallel 20210822 + if ! command -v parallel 1>/dev/random 2>&1; then die "FATAL:Missing:parallel"; fi; + if ! command -v git 1>/dev/random 2>&1; then die "FATAL:Missing:git"; fi; +}; +check_args() { + if [[ $# -le 0 ]]; then die "FATAL:Incorrect number of args:$#"; fi; +}; # check arguments +display_commit() { + # Show commit if filename in committed tree matches grep pattern + # Input: arg1: commit id + # args2+: passed to grep + # Output: stdout + local commit results; + + commit="$1"; shift; + + # Decide if need to show commit at all + if results="$(git ls-tree -r --name-only "$commit" | grep "$1")"; then + commit="${commit:(-8)}"; # get last 8 chars + # Prepend results with commit + results="$( echo "$results" | sed "s/^/$commit: /" )"; + printf "%s\n" "$results"; + fi; + + return 0; +}; # display commit +main() { + check_depends; + check_args "$@"; + export -f yell die must display_commit; + git rev-list --all | parallel --jobs=75% display_commit "{}" "$@"; +}; + +main "$@"; + +# Author: Steven Baltakatei Sandoval +# License: GPLv3+