get_source_date_epoch.sh 777 B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env bash
  2. export LANG=C
  3. export LC_ALL=C
  4. if [ -n "$TOPDIR" ]; then
  5. cd "$TOPDIR" || exit 1
  6. fi
  7. SOURCE="${1:-.}"
  8. try_version() {
  9. [ -f "$SOURCE/version.date" ] || return 1
  10. SOURCE_DATE_EPOCH=$(cat "$SOURCE/version.date")
  11. [ -n "$SOURCE_DATE_EPOCH" ]
  12. }
  13. try_git() {
  14. SOURCE_DATE_EPOCH=$(git -C "$SOURCE" log -1 --no-show-signature \
  15. --format=format:%ct "$SOURCE" 2>/dev/null)
  16. [ -n "$SOURCE_DATE_EPOCH" ]
  17. }
  18. try_hg() {
  19. SOURCE_DATE_EPOCH=$(hg --cwd "$SOURCE" log --template '{date}' -l 1 \
  20. "$SOURCE" 2>/dev/null | cut -d. -f1)
  21. [ -n "$SOURCE_DATE_EPOCH" ]
  22. }
  23. try_mtime() {
  24. SOURCE_DATE_EPOCH=$(perl -e 'print((stat $ARGV[0])[9])' "$0")
  25. [ -n "$SOURCE_DATE_EPOCH" ]
  26. }
  27. try_version || try_git || try_hg || try_mtime || SOURCE_DATE_EPOCH=""
  28. echo "$SOURCE_DATE_EPOCH"