3 # This file is part of the coreboot project. It originated in the
4 # flashrom project but has been heavily modified since then.
6 # Copyright (C) 2013 Stefan Tauner
7 # Copyright (C) 2013 Google Inc.
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.
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.
23 # Make sure we don't get translated output
25 # nor local times or dates
29 git_has_local_changes
() {
30 git update-index
-q --refresh >/dev
/null
31 ! git diff-index
--quiet HEAD
-- "$1"
35 git log
--pretty=format
:"%h" -1 -- "$1"
38 git_is_file_tracked
() {
39 git ls-files
--error-unmatch -- "$1" >/dev
/null
2>&1
43 git_is_file_tracked
"$1"
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
50 # Note: This may not work as expected if multiple remotes are fetched from.
51 echo $
(git remote
-v |
grep "^origin\>" | \
52 awk '/fetch/ {print $2; exit 0}' |
sed "s,^.*@,,")
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
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
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
82 # No local changes, get date of the last commit
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
);;
89 t
=$
(date -d "$(git log --pretty=format:%cD -1 -- $2)" -u "$1" 2>/dev
/null
);;
97 echo "Warning: Could not determine timestamp." 2>/dev
/null
100 # output the time, changing colons to underscores.
101 # gnu make doesn't work in directories with colons
102 echo "${t}" |
tr ':' '_'
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.
110 if git_is_file_tracked
"$1" ; then
111 r
=$
(git_last_commit
"$1")
113 if git_has_local_changes
"$1" ; then
117 return ${EXIT_FAILURE}
123 # Similar to local_revision but uses "git describe" instead of "git log" which
124 # includes number of commits since most recent tag.
128 if git_is_file_tracked
"$1" ; then
129 r
=$
(git describe
--tags --dirty)
131 return ${EXIT_FAILURE}
137 upstream_revision
() {
140 r
=$
(git log remotes
/origin
/master
-1 --format=format
:%h
)
143 r
="unknown" # default to unknown
150 ${0} <command> [path]
156 local revision information including an indicator for uncommitted changes
160 similar to -l, but uses \"git describe\" to obtain revision info with tags
162 URL associated with the latest commit
164 date of most recent modification
166 timestamp of most recent modification
172 if [ -n "$action" ]; then
173 echo "Error: Multiple actions given.">&2
182 # The is the main loop
191 action
=local_revision
195 action
=tagged_revision
199 action
=upstream_revision
207 action
="timestamp +%Y-%m-%d" # refrain from suffixing 'Z' to indicate it's UTC
211 action
="timestamp +%Y-%m-%dT%H:%M:%SZ" # There is only one valid time format! ISO 8601
215 echo "Error: Invalid option: ${1}"
216 exit ${EXIT_FAILURE};;
218 if [ -z "$query_path" ] ; then
219 if [ ! -e "$1" ] ; then
220 echo "Error: Path \"${1}\" does not exist.">&2
225 echo "Warning: Ignoring over-abundant paramter: \"${1}\"">&2
231 # default to current directory (usually equals the whole repository)
232 if [ -z "$query_path" ] ; then
235 if ! is_file_tracked
"$query_path" ; then
236 echo "Warning: Path \"${query_path}\" is not under version control.">&2
238 if [ -z "$action" ] ; then
240 echo "Error: No actions specified"
244 $action "$query_path"