blob: 168c972f303cdb893d03606975ccbbefc5ed83b6 [file] [log] [blame]
David Hendricks6583a812013-11-01 19:37:44 -07001#!/bin/sh
2#
3# This file is part of the coreboot project. It originated in the
4# flashrom project but has been heavily modified since then.
5#
6# Copyright (C) 2013 Stefan Tauner
7# Copyright (C) 2013 Google Inc.
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
David Hendricks6583a812013-11-01 19:37:44 -070019
20EXIT_SUCCESS=0
21EXIT_FAILURE=1
22
23# Make sure we don't get translated output
24export LC_ALL=C
25# nor local times or dates
26export TZ=UTC0
27
28# Helper functions
29git_has_local_changes() {
30 git update-index -q --refresh >/dev/null
31 ! git diff-index --quiet HEAD -- "$1"
32}
33
34git_last_commit() {
35 git log --pretty=format:"%h" -1 -- "$1"
36}
37
38git_is_file_tracked() {
39 git ls-files --error-unmatch -- "$1" >/dev/null 2>&1
40}
41
42is_file_tracked() {
43 git_is_file_tracked "$1"
44}
45
46# Tries to find a remote source for the changes committed locally.
47# This includes the URL of the remote repository including the last commit and a suitable branch name.
48# Takes one optional argument: the path to inspect
49git_url() {
50 # Note: This may not work as expected if multiple remotes are fetched from.
Patrick Georgif60fc822015-04-28 17:49:30 +020051 echo $(git remote -v | grep "^origin\>" | \
52 awk '/fetch/ {print $2; exit 0}' | sed "s,^.*@,,")
David Hendricks6583a812013-11-01 19:37:44 -070053}
54
55# Returns a string indicating where others can get the current source code (excluding uncommitted changes)
56# Takes one optional argument: the path to inspect
57scm_url() {
58 local url
59
60 url="$(git_url "$1")"
61
62 echo "${url}"
63}
64
65# Retrieve timestamp since last modification. If the sources are pristine,
66# then the timestamp will match that of the SCM's most recent modification
67# date.
68timestamp() {
69 local t
70
71 # date syntaxes are manifold:
72 # gnu date [-d input]... [+FORMAT]
73 # netbsd date [-ajnu] [-d date] [-r seconds] [+format] [[[[[[CC]yy]mm]dd]HH]MM[.SS]]
74 # freebsd date [-jnu] [-d dst] [-r seconds] [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format] [...]
75 # dragonflybsd date [-jnu] [-d dst] [-r seconds] [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format] [...]
76 # openbsd date [-aju] [-d dst] [-r seconds] [+format] [[[[[[cc]yy]mm]dd]HH]MM[.SS]] [...]
77 if git_is_file_tracked "$2" ; then
78 # are there local changes?
79 if git_has_local_changes "$2" ; then
80 t=$(date -u "${1}")
81 else
82 # No local changes, get date of the last commit
83 case $(uname) in
84 # Most BSD dates do not support parsing date values from user input with -d but all of
85 # them support parsing epoch seconds with -r. Thanks to git we can easily use that:
86 NetBSD|OpenBSD|DragonFly|FreeBSD)
87 t=$(date -u -r "$(git log --pretty=format:%ct -1 -- $2)" "$1" 2>/dev/null);;
88 *)
89 t=$(date -d "$(git log --pretty=format:%cD -1 -- $2)" -u "$1" 2>/dev/null);;
90 esac
91 fi
92 else
93 t=$(date -u "$1")
94 fi
95
96 if [ -z "$t" ]; then
97 echo "Warning: Could not determine timestamp." 2>/dev/null
98 fi
Martin Rothb2cc1292016-01-27 09:53:45 -070099
100 # output the time, changing colons to underscores.
101 # gnu make doesn't work in directories with colons
102 echo "${t}" | tr ':' '_'
David Hendricks6583a812013-11-01 19:37:44 -0700103}
104
105# Retrieve local SCM revision info. This is useful if we're working in a different SCM than upstream and/or
106# have local changes.
107local_revision() {
108 local r
109
110 if git_is_file_tracked "$1" ; then
111 r=$(git_last_commit "$1")
112
113 if git_has_local_changes "$1" ; then
114 r="$r-dirty"
115 fi
116 else
117 return ${EXIT_FAILURE}
118 fi
119
120 echo "${r}"
121}
122
David Hendricks1b6e7a62013-11-11 18:44:05 -0800123# Similar to local_revision but uses "git describe" instead of "git log" which
124# includes number of commits since most recent tag.
125tagged_revision() {
126 local r
127
128 if git_is_file_tracked "$1" ; then
129 r=$(git describe --tags --dirty)
130 else
131 return ${EXIT_FAILURE}
132 fi
133
134 echo "${r}"
135}
136
David Hendricks6583a812013-11-01 19:37:44 -0700137upstream_revision() {
138 local r=
139
140 r=$(git log remotes/origin/master -1 --format=format:%h)
141
142 if [ -z "$r" ]; then
143 r="unknown" # default to unknown
144 fi
145 echo "${r}"
146}
147
148show_help() {
149 echo "Usage:
150 ${0} <command> [path]
151
152Commands
153 -h or --help
154 this message
155 -l or --local
156 local revision information including an indicator for uncommitted changes
157 -u or --upstream
158 upstream revision
David Hendricks1b6e7a62013-11-11 18:44:05 -0800159 -T or --tags
160 similar to -l, but uses \"git describe\" to obtain revision info with tags
David Hendricks6583a812013-11-01 19:37:44 -0700161 -U or --url
162 URL associated with the latest commit
163 -d or --date
164 date of most recent modification
165 -t or --timestamp
166 timestamp of most recent modification
167"
168 return
169}
170
171check_action() {
172 if [ -n "$action" ]; then
173 echo "Error: Multiple actions given.">&2
174 exit ${EXIT_FAILURE}
175 fi
176}
177
178main() {
179 local query_path=
180 local action=
181
182 # The is the main loop
183 while [ $# -gt 0 ];
184 do
185 case ${1} in
186 -h|--help)
187 action=show_help;
188 shift;;
189 -l|--local)
190 check_action $1
191 action=local_revision
192 shift;;
David Hendricks1b6e7a62013-11-11 18:44:05 -0800193 -T|--tags)
194 check_action $1
195 action=tagged_revision
196 shift;;
David Hendricks6583a812013-11-01 19:37:44 -0700197 -u|--upstream)
198 check_action $1
199 action=upstream_revision
200 shift;;
201 -U|--url)
202 check_action $1
203 action=scm_url
204 shift;;
205 -d|--date)
206 check_action $1
207 action="timestamp +%Y-%m-%d" # refrain from suffixing 'Z' to indicate it's UTC
208 shift;;
209 -t|--timestamp)
210 check_action $1
211 action="timestamp +%Y-%m-%dT%H:%M:%SZ" # There is only one valid time format! ISO 8601
212 shift;;
213 -*)
214 show_help;
215 echo "Error: Invalid option: ${1}"
216 exit ${EXIT_FAILURE};;
217 *)
218 if [ -z "$query_path" ] ; then
219 if [ ! -e "$1" ] ; then
220 echo "Error: Path \"${1}\" does not exist.">&2
221 exit ${EXIT_FAILURE}
222 fi
223 query_path=$1
224 else
225 echo "Warning: Ignoring over-abundant paramter: \"${1}\"">&2
226 fi
227 shift;;
228 esac;
229 done
230
231 # default to current directory (usually equals the whole repository)
232 if [ -z "$query_path" ] ; then
233 query_path=.
234 fi
235 if ! is_file_tracked "$query_path" ; then
236 echo "Warning: Path \"${query_path}\" is not under version control.">&2
237 fi
238 if [ -z "$action" ] ; then
239 show_help
240 echo "Error: No actions specified"
241 exit ${EXIT_FAILURE}
242 fi
243
244 $action "$query_path"
245}
246
247main $@