chore(user/split_audiobook.sh):Add version number
[BK-2020-03.git] / user / git-bk-find-file
CommitLineData
68e86d25
SBS
1#!/usr/bin/env bash
2# Desc: Grep search filenames of all git committed work trees
e65cf1ee
SBS
3# Usage: git-bk-find-file [string grep pattern]
4# Example git-bk-find-file '.txt$'
68e86d25 5# Ref/Attrib: albfan "How can I search Git branches for a file or directory?" https://stackoverflow.com/a/16868704/10850071
7ea27886 6# Version: 0.2.0
68e86d25
SBS
7# Depends: GNU bash v5.1.16, GNU parallel 20210822
8
9yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
10die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
11must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
12check_depends() {
13 # Desc: Check dependencies
14 # Usage: check_depends
15 # Depends: GNU parallel 20210822
16 if ! command -v parallel 1>/dev/random 2>&1; then die "FATAL:Missing:parallel"; fi;
17 if ! command -v git 1>/dev/random 2>&1; then die "FATAL:Missing:git"; fi;
18};
19check_args() {
20 if [[ $# -le 0 ]]; then die "FATAL:Incorrect number of args:$#"; fi;
21}; # check arguments
22display_commit() {
23 # Show commit if filename in committed tree matches grep pattern
24 # Input: arg1: commit id
25 # args2+: passed to grep
26 # Output: stdout
e65cf1ee 27 # Depends: git 2.34.1
68e86d25
SBS
28 local commit results;
29
30 commit="$1"; shift;
31
32 # Decide if need to show commit at all
33 if results="$(git ls-tree -r --name-only "$commit" | grep "$1")"; then
e65cf1ee
SBS
34 # Get commit timestamp
35 time="$(git -c log.showSignature=false show -s --format=%cI "$commit")";
36
7ea27886
SBS
37 # Get first 8 chars of commit
38 short_commit="${commit:0:8}";
e65cf1ee
SBS
39
40 # Output results
41 while read -r line; do
42 if [[ -z "$line" ]]; then continue; fi; # skip blank lines
43 printf "%s %s %s\n" "$time" "$short_commit" "$line";
44 done < <(printf "%s\n" "$results");
68e86d25
SBS
45 fi;
46
47 return 0;
48}; # display commit
49main() {
50 check_depends;
51 check_args "$@";
52 export -f yell die must display_commit;
53 git rev-list --all | parallel --jobs=75% display_commit "{}" "$@";
54};
55
56main "$@";
57
58# Author: Steven Baltakatei Sandoval
59# License: GPLv3+