feat(user/bkfeh):Add wrapper for feh
[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
3# Usage: git-bk-find-file [string regex]
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.0.1
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 local commit results;
28
29 commit="$1"; shift;
30
31 # Decide if need to show commit at all
32 if results="$(git ls-tree -r --name-only "$commit" | grep "$1")"; then
33 commit="${commit:(-8)}"; # get last 8 chars
34 # Prepend results with commit
35 results="$( echo "$results" | sed "s/^/$commit: /" )";
36 printf "%s\n" "$results";
37 fi;
38
39 return 0;
40}; # display commit
41main() {
42 check_depends;
43 check_args "$@";
44 export -f yell die must display_commit;
45 git rev-list --all | parallel --jobs=75% display_commit "{}" "$@";
46};
47
48main "$@";
49
50# Author: Steven Baltakatei Sandoval
51# License: GPLv3+