build: get_source_date_epoch allow external repos
[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 SOURCE="${1:-.}"
10
11 try_version() {
12 [ -f "$SOURCE/version.date" ] || return 1
13 SOURCE_DATE_EPOCH=$(cat "$SOURCE/version.date")
14 [ -n "$SOURCE_DATE_EPOCH" ]
15 }
16
17 try_git() {
18 SOURCE_DATE_EPOCH=$(git -C "$SOURCE" log -1 --format=format:%ct \
19 "$SOURCE" 2>/dev/null)
20 [ -n "$SOURCE_DATE_EPOCH" ]
21 }
22
23 try_hg() {
24 SOURCE_DATE_EPOCH=$(hg --cwd "$SOURCE" log --template '{date}' -l 1 \
25 "$SOURCE" 2>/dev/null | cut -d. -f1)
26 [ -n "$SOURCE_DATE_EPOCH" ]
27 }
28
29 try_mtime() {
30 SOURCE_DATE_EPOCH=$(perl -e 'print((stat $ARGV[0])[9])' "$0")
31 [ -n "$SOURCE_DATE_EPOCH" ]
32 }
33
34 try_version || try_git || try_hg || try_mtime || SOURCE_DATE_EPOCH=""
35 echo "$SOURCE_DATE_EPOCH"