Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Created April 8, 2019 07:56
Show Gist options
  • Save kou1okada/0696bbb36803729f2c74e34fafbf3aee to your computer and use it in GitHub Desktop.
Save kou1okada/0696bbb36803729f2c74e34fafbf3aee to your computer and use it in GitHub Desktop.
git-get-date
#!/usr/bin/env bash
#
# git-get-date
# Copyright (c) 2019 Koichi OKADA. All rights reserved.
# This script is distributed under the MIT license.
#
function usage ()
{
cat <<-EOD
Usage: git get-date [<options>] [<revisions> ...]
-a, --author Show author date (default)
-c, --commit Show commit date
EOD
}
function git-get-date ()
{
local i TARGET=AuthorDate REV=() DATE
for i; do
case "$i" in
-a|--author) TARGET=AuthorDate ;;
-c|--commit) TARGET=CommitDate ;;
-h|--help) usage; exit ;;
-*) echo "Error: Unknown option: $i"; exit 1 ;;
*) REV+=( "$i" ) ;;
esac
done
for i in "${REV[@]:-HEAD}"; do
readarray -t TIME < <(
git log --pretty=fuller -1 "$i" \
| awk -vTARGET="$TARGET" '$1==TARGET":"&&(($1="")||1)'
)
if [ -z "$TIME" ]; then
echo "Error: Faild to get $TARGET for $i" >&2
else
ruby -r Time -e 'puts Time.parse(ARGV[0])' "$TIME"
fi
done
}
git-get-date "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment