src/soc/intel/braswell/cpu.c: Set up local APIC
[coreboot.git] / util / board_status / getrevision.sh
blob168c972f303cdb893d03606975ccbbefc5ed83b6
1 #!/bin/sh
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.
20 EXIT_SUCCESS=0
21 EXIT_FAILURE=1
23 # Make sure we don't get translated output
24 export LC_ALL=C
25 # nor local times or dates
26 export TZ=UTC0
28 # Helper functions
29 git_has_local_changes() {
30 git update-index -q --refresh >/dev/null
31 ! git diff-index --quiet HEAD -- "$1"
34 git_last_commit() {
35 git log --pretty=format:"%h" -1 -- "$1"
38 git_is_file_tracked() {
39 git ls-files --error-unmatch -- "$1" >/dev/null 2>&1
42 is_file_tracked() {
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
49 git_url() {
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
57 scm_url() {
58 local url
60 url="$(git_url "$1")"
62 echo "${url}"
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.
68 timestamp() {
69 local t
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);;
89 t=$(date -d "$(git log --pretty=format:%cD -1 -- $2)" -u "$1" 2>/dev/null);;
90 esac
92 else
93 t=$(date -u "$1")
96 if [ -z "$t" ]; then
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.
107 local_revision() {
108 local r
110 if git_is_file_tracked "$1" ; then
111 r=$(git_last_commit "$1")
113 if git_has_local_changes "$1" ; then
114 r="$r-dirty"
116 else
117 return ${EXIT_FAILURE}
120 echo "${r}"
123 # Similar to local_revision but uses "git describe" instead of "git log" which
124 # includes number of commits since most recent tag.
125 tagged_revision() {
126 local r
128 if git_is_file_tracked "$1" ; then
129 r=$(git describe --tags --dirty)
130 else
131 return ${EXIT_FAILURE}
134 echo "${r}"
137 upstream_revision() {
138 local r=
140 r=$(git log remotes/origin/master -1 --format=format:%h)
142 if [ -z "$r" ]; then
143 r="unknown" # default to unknown
145 echo "${r}"
148 show_help() {
149 echo "Usage:
150 ${0} <command> [path]
152 Commands
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
159 -T or --tags
160 similar to -l, but uses \"git describe\" to obtain revision info with tags
161 -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
168 return
171 check_action() {
172 if [ -n "$action" ]; then
173 echo "Error: Multiple actions given.">&2
174 exit ${EXIT_FAILURE}
178 main() {
179 local query_path=
180 local action=
182 # The is the main loop
183 while [ $# -gt 0 ];
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;;
193 -T|--tags)
194 check_action $1
195 action=tagged_revision
196 shift;;
197 -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;;
214 show_help;
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
221 exit ${EXIT_FAILURE}
223 query_path=$1
224 else
225 echo "Warning: Ignoring over-abundant paramter: \"${1}\"">&2
227 shift;;
228 esac;
229 done
231 # default to current directory (usually equals the whole repository)
232 if [ -z "$query_path" ] ; then
233 query_path=.
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
239 show_help
240 echo "Error: No actions specified"
241 exit ${EXIT_FAILURE}
244 $action "$query_path"
247 main $@