blob: 6559df3eb6aaf6b58db779b1a8bc3e62eca0fe3c [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
99 echo "${t}"
100}
101
102# Retrieve local SCM revision info. This is useful if we're working in a different SCM than upstream and/or
103# have local changes.
104local_revision() {
105 local r
106
107 if git_is_file_tracked "$1" ; then
108 r=$(git_last_commit "$1")
109
110 if git_has_local_changes "$1" ; then
111 r="$r-dirty"
112 fi
113 else
114 return ${EXIT_FAILURE}
115 fi
116
117 echo "${r}"
118}
119
David Hendricks1b6e7a62013-11-11 18:44:05 -0800120# Similar to local_revision but uses "git describe" instead of "git log" which
121# includes number of commits since most recent tag.
122tagged_revision() {
123 local r
124
125 if git_is_file_tracked "$1" ; then
126 r=$(git describe --tags --dirty)
127 else
128 return ${EXIT_FAILURE}
129 fi
130
131 echo "${r}"
132}
133
David Hendricks6583a812013-11-01 19:37:44 -0700134upstream_revision() {
135 local r=
136
137 r=$(git log remotes/origin/master -1 --format=format:%h)
138
139 if [ -z "$r" ]; then
140 r="unknown" # default to unknown
141 fi
142 echo "${r}"
143}
144
145show_help() {
146 echo "Usage:
147 ${0} <command> [path]
148
149Commands
150 -h or --help
151 this message
152 -l or --local
153 local revision information including an indicator for uncommitted changes
154 -u or --upstream
155 upstream revision
David Hendricks1b6e7a62013-11-11 18:44:05 -0800156 -T or --tags
157 similar to -l, but uses \"git describe\" to obtain revision info with tags
David Hendricks6583a812013-11-01 19:37:44 -0700158 -U or --url
159 URL associated with the latest commit
160 -d or --date
161 date of most recent modification
162 -t or --timestamp
163 timestamp of most recent modification
164"
165 return
166}
167
168check_action() {
169 if [ -n "$action" ]; then
170 echo "Error: Multiple actions given.">&2
171 exit ${EXIT_FAILURE}
172 fi
173}
174
175main() {
176 local query_path=
177 local action=
178
179 # The is the main loop
180 while [ $# -gt 0 ];
181 do
182 case ${1} in
183 -h|--help)
184 action=show_help;
185 shift;;
186 -l|--local)
187 check_action $1
188 action=local_revision
189 shift;;
David Hendricks1b6e7a62013-11-11 18:44:05 -0800190 -T|--tags)
191 check_action $1
192 action=tagged_revision
193 shift;;
David Hendricks6583a812013-11-01 19:37:44 -0700194 -u|--upstream)
195 check_action $1
196 action=upstream_revision
197 shift;;
198 -U|--url)
199 check_action $1
200 action=scm_url
201 shift;;
202 -d|--date)
203 check_action $1
204 action="timestamp +%Y-%m-%d" # refrain from suffixing 'Z' to indicate it's UTC
205 shift;;
206 -t|--timestamp)
207 check_action $1
208 action="timestamp +%Y-%m-%dT%H:%M:%SZ" # There is only one valid time format! ISO 8601
209 shift;;
210 -*)
211 show_help;
212 echo "Error: Invalid option: ${1}"
213 exit ${EXIT_FAILURE};;
214 *)
215 if [ -z "$query_path" ] ; then
216 if [ ! -e "$1" ] ; then
217 echo "Error: Path \"${1}\" does not exist.">&2
218 exit ${EXIT_FAILURE}
219 fi
220 query_path=$1
221 else
222 echo "Warning: Ignoring over-abundant paramter: \"${1}\"">&2
223 fi
224 shift;;
225 esac;
226 done
227
228 # default to current directory (usually equals the whole repository)
229 if [ -z "$query_path" ] ; then
230 query_path=.
231 fi
232 if ! is_file_tracked "$query_path" ; then
233 echo "Warning: Path \"${query_path}\" is not under version control.">&2
234 fi
235 if [ -z "$action" ] ; then
236 show_help
237 echo "Error: No actions specified"
238 exit ${EXIT_FAILURE}
239 fi
240
241 $action "$query_path"
242}
243
244main $@