fix(unitproc/bkshuf):Make executable
[BK-2020-03.git] / user / git-bk-find-file
... / ...
CommitLineData
1#!/usr/bin/env bash
2# Desc: Grep search filenames of all git committed work trees
3# Usage: git-bk-find-file [string grep pattern]
4# Example git-bk-find-file '.txt$'
5# Ref/Attrib: albfan "How can I search Git branches for a file or directory?" https://stackoverflow.com/a/16868704/10850071
6# Version: 0.2.0
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
27 # Depends: git 2.34.1
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
34 # Get commit timestamp
35 time="$(git -c log.showSignature=false show -s --format=%cI "$commit")";
36
37 # Get first 8 chars of commit
38 short_commit="${commit:0:8}";
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");
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+