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