Skip to content

Instantly share code, notes, and snippets.

@gukandrew
Created November 23, 2020 10:14
Show Gist options
  • Save gukandrew/bd055fe762a8d8ea104965028dc09330 to your computer and use it in GitHub Desktop.
Save gukandrew/bd055fe762a8d8ea104965028dc09330 to your computer and use it in GitHub Desktop.
List files of directory(-ies) inside git repository along with their last commit
#!/bin/sh
# USAGE:
# ~/gw.sh [optional: <file1> <file2> ... otherwise current dir will be used]
# complex example
# bash -c "~/gw.sh $(git ls-tree -r --name-only HEAD app/*/web/main.js | tr '\n' ' ')"
#
# SAMPLE OUTPUT:
# ~/gw.sh
# Running on current dir
# .editorconfig -- 8 weeks ago 81bbe6d Initial commit 2.42
# .github -- 8 weeks ago 81bbe6d Initial commit 2.42
# .gitignore -- 8 weeks ago 81bbe6d Initial commit 2.42
# .gitkeep -- 8 weeks ago 81bbe6d Initial commit 2.42
# .htaccess -- 8 weeks ago 81bbe6d Initial commit 2.42
# .htaccess.sample -- 8 weeks ago 81bbe6d Initial commit 2.42
# .user.ini -- 8 weeks ago 81bbe6d Initial commit 2.42
# CHANGELOG.md -- 8 weeks ago 81bbe6d Initial commit 2.42
# COPYING.txt -- 8 weeks ago 81bbe6d Initial commit 2.42
# Gruntfile.js.sample -- 8 weeks ago 81bbe6d Initial commit 2.42
# LICENSE.txt -- 8 weeks ago 81bbe6d Initial commit 2.42
# LICENSE_AFL.txt -- 8 weeks ago 81bbe6d Initial commit 2.42
# SECURITY.md -- 8 weeks ago 81bbe6d Initial commit 2.42
# app -- 8 weeks ago 81bbe6d Initial commit 2.42
FILES=("$@")
if [ $# -eq 0 ]; then
echo "Running on current dir"
FILES="$(git ls-tree --name-only HEAD .)"
fi
MAXLEN=0
IFS="$(printf "\n\b")"
for f in ${FILES[@]}; do
if [ ${#f} -gt $MAXLEN ]; then
MAXLEN=${#f}
fi
done
for f in ${FILES[@]}; do
str="$(git log -1 --pretty=format:"%C(green)%cr%Creset %x09 %C(cyan)%h%Creset %s %C(yellow)(%cn)%Creset" $f)"
printf "%-${MAXLEN}s -- %s\n" "$f" "$str"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment