blob: 101205860efabc6484799006ff4b41fbc5953218 [file] [log] [blame]
Stefan Taunerec7a35f2013-08-29 00:38:14 +00001#!/bin/sh
David Hendricks36e9f4b2013-08-14 14:47:26 +00002#
3# This file is part of the flashrom project.
4#
5# Copyright (C) 2005 coresystems GmbH <stepan@coresystems.de>
6# Copyright (C) 2009,2010 Carl-Daniel Hailfinger
7# Copyright (C) 2010 Chromium OS Authors
Stefan Taunerec7a35f2013-08-29 00:38:14 +00008# Copyright (C) 2013 Stefan Tauner
David Hendricks36e9f4b2013-08-14 14:47:26 +00009#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23#
24
25EXIT_SUCCESS=0
26EXIT_FAILURE=1
27
Joerg Mayerd31d3c32013-08-17 23:58:01 +000028# Make sure we don't get translated output
29export LC_ALL=C
Stefan Taunerec7a35f2013-08-29 00:38:14 +000030# nor local times or dates
31export TZ=UTC0
Joerg Mayerd31d3c32013-08-17 23:58:01 +000032
Stefan Taunerec7a35f2013-08-29 00:38:14 +000033# Helper functions
34# First argument is the path to inspect (usually optional; w/o it the whole repository will be considered)
35svn_has_local_changes() {
36 svn status "$1" | egrep '^ *[ADMR] *' >/dev/null
David Hendricks36e9f4b2013-08-14 14:47:26 +000037}
38
Stefan Taunerec7a35f2013-08-29 00:38:14 +000039git_has_local_changes() {
40 git update-index -q --refresh >/dev/null
41 ! git diff-index --quiet HEAD -- "$1"
David Hendricks36e9f4b2013-08-14 14:47:26 +000042}
43
Stefan Taunerec7a35f2013-08-29 00:38:14 +000044git_last_commit() {
45 git log --pretty=format:"%h" -1 -- "$1"
David Hendricks36e9f4b2013-08-14 14:47:26 +000046}
47
Stefan Taunerec7a35f2013-08-29 00:38:14 +000048svn_is_file_tracked() {
49 svn info "$1" >/dev/null 2>&1
David Hendricks36e9f4b2013-08-14 14:47:26 +000050}
51
Stefan Taunerec7a35f2013-08-29 00:38:14 +000052git_is_file_tracked() {
53 git ls-files --error-unmatch -- "$1" >/dev/null 2>&1
David Hendricks36e9f4b2013-08-14 14:47:26 +000054}
55
Stefan Taunerec7a35f2013-08-29 00:38:14 +000056is_file_tracked() {
57 svn_is_file_tracked "$1" || git_is_file_tracked "$1"
David Hendricks36e9f4b2013-08-14 14:47:26 +000058}
59
Stefan Taunerec7a35f2013-08-29 00:38:14 +000060# Tries to find a remote source for the changes committed locally.
61# This includes the URL of the remote repository including the last commit and a suitable branch name.
62# Takes one optional argument: the path to inspect
David Hendricks36e9f4b2013-08-14 14:47:26 +000063git_url() {
Stefan Taunerec7a35f2013-08-29 00:38:14 +000064 last_commit=$(git_last_commit "$1")
65 # get all remote branches containing the last commit (excluding origin/HEAD and git-svn branches/tags)
66 branches=$(git branch -r --contains $last_commit | sed '/\//!d;/.*->.*/d;s/[\t ]*//')
67 if [ -z "$branches" ] ; then
68 echo "No remote branch contains a suitable commit">&2
69 return
70 fi
71
72 # find "nearest" branch
73 local mindiff=9000
74 local target=
75 for branch in $branches ; do
76 curdiff=$(git rev-list --count $last_commit..$branch)
77 if [ $curdiff -ge $mindiff ] ; then
78 continue
79 fi
80 mindiff=$curdiff
81 target=$branch
82 done
83
84 echo "$(git ls-remote --exit-code --get-url ${target%/*}) ${target#*/}"
David Hendricks36e9f4b2013-08-14 14:47:26 +000085}
86
Stefan Taunerec7a35f2013-08-29 00:38:14 +000087# Returns a string indicating where others can get the current source code (excluding uncommitted changes)
88# Takes one optional argument: the path to inspect
David Hendricks36e9f4b2013-08-14 14:47:26 +000089scm_url() {
Stefan Taunerec7a35f2013-08-29 00:38:14 +000090 local url=
David Hendricks36e9f4b2013-08-14 14:47:26 +000091
Stefan Taunerec7a35f2013-08-29 00:38:14 +000092 # for a primitive VCS like subversion finding the URL is easy: there is only one upstream host
93 if svn_is_file_tracked "$1" ; then
94 url="$(svn info "$1" 2>/dev/null |
95 grep URL: |
96 sed 's/.*URL:[[:blank:]]*//;s/:\/\/.*@/:\/\//' |
97 grep ^.)"
98 elif git_is_file_tracked "$1" ; then
99 url="$(git_url "$1")"
100 else
101 return ${EXIT_FAILURE}
David Hendricks36e9f4b2013-08-14 14:47:26 +0000102 fi
103
104 echo "${url}"
105}
106
107# Retrieve timestamp since last modification. If the sources are pristine,
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000108# then the timestamp will match that of the SCM's most recent modification
David Hendricks36e9f4b2013-08-14 14:47:26 +0000109# date.
110timestamp() {
111 local t
112
Stefan Taunerc65b8552013-09-12 15:48:39 +0000113 # date syntaxes are manifold:
114 # gnu date [-d input]... [+FORMAT]
115 # netbsd date [-ajnu] [-d date] [-r seconds] [+format] [[[[[[CC]yy]mm]dd]HH]MM[.SS]]
116 # freebsd date [-jnu] [-d dst] [-r seconds] [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format] [...]
117 # dragonflybsd date [-jnu] [-d dst] [-r seconds] [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format] [...]
118 # openbsd date [-aju] [-d dst] [-r seconds] [+format] [[[[[[cc]yy]mm]dd]HH]MM[.SS]] [...]
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000119 if svn_is_file_tracked "$2" ; then
120 if svn_has_local_changes "$2"; then
121 t=$(date -u "$1")
122 else
Stefan Taunerc65b8552013-09-12 15:48:39 +0000123 # No local changes, get date of the last log record. Subversion provides that in
124 # ISO 8601 format when using the --xml switch. The sed call extracts that ignoring any
125 # fractional parts started by a comma or a dot.
126 local last_commit_date="$(svn info --xml "$2"| \
127 sed -n -e 's/<date>\([^,\.]*\)\([\.,].*\)*Z<\/date>/\1Z/p')"
128
129 case $(uname) in
130 # Most BSD dates do not support parsing date values from user input with -d but all of
131 # them support parsing the syntax with [[[[[[cc]yy]mm]dd]HH]MM[.ss]]. We have to
132 # transform the ISO8601 date first though.
133 NetBSD|OpenBSD|DragonFly|FreeBSD)
134 last_commit_date="$(echo ${last_commit_date} | \
135 sed -n -e 's/\(....\)-\(..\)-\(..\)T\(..\):\(..\):\(..\)Z/\1\2\3\4\5\.\6/p')"
136 t=$(date -u -j "${last_commit_date}" "$1" 2>/dev/null);;
137 *)
138 t=$(date -u -d "${last_commit_date}" "$1" 2>/dev/null);;
139 esac
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000140 fi
141 elif git_is_file_tracked "$2" ; then
142 # are there local changes?
143 if git_has_local_changes "$2" ; then
144 t=$(date -u "${1}")
145 else
146 # No local changes, get date of the last commit
Stefan Taunerc65b8552013-09-12 15:48:39 +0000147 case $(uname) in
148 # Most BSD dates do not support parsing date values from user input with -d but all of
149 # them support parsing epoch seconds with -r. Thanks to git we can easily use that:
150 NetBSD|OpenBSD|DragonFly|FreeBSD)
151 t=$(date -u -r "$(git log --pretty=format:%ct -1 -- $2)" "$1" 2>/dev/null);;
152 *)
153 t=$(date -d "$(git log --pretty=format:%cD -1 -- $2)" -u "$1" 2>/dev/null);;
154 esac
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000155 fi
156 else
157 t=$(date -u "$1")
David Hendricks36e9f4b2013-08-14 14:47:26 +0000158 fi
159
Stefan Taunerc65b8552013-09-12 15:48:39 +0000160 if [ -z "$t" ]; then
161 echo "Warning: Could not determine timestamp." 2>/dev/null
162 fi
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000163 echo "${t}"
David Hendricks36e9f4b2013-08-14 14:47:26 +0000164}
165
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000166# Retrieve local SCM revision info. This is useful if we're working in a different SCM than upstream and/or
167# have local changes.
David Hendricks36e9f4b2013-08-14 14:47:26 +0000168local_revision() {
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000169 local r=
David Hendricks36e9f4b2013-08-14 14:47:26 +0000170
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000171 if svn_is_file_tracked "$1" ; then
172 r=$(svn_has_local_changes "$1" && echo "dirty")
173 elif git_is_file_tracked "$1" ; then
174 r=$(git_last_commit "$1")
David Hendricks36e9f4b2013-08-14 14:47:26 +0000175
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000176 local svn_base=$(git log --grep=git-svn-id -1 --format='%h')
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000177 if [ "$svn_base" != "" ] ; then
178 local diff_to_svn=$(git rev-list --count ${svn_base}..${r})
179 if [ "$diff_to_svn" -gt 0 ] ; then
180 r="$r-$diff_to_svn"
181 fi
182 fi
David Hendricks36e9f4b2013-08-14 14:47:26 +0000183
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000184 if git_has_local_changes "$1" ; then
185 r="$r-dirty"
186 fi
187 else
188 return ${EXIT_FAILURE}
David Hendricks36e9f4b2013-08-14 14:47:26 +0000189 fi
190
191 echo "${r}"
192}
193
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000194# Get the upstream flashrom revision stored in SVN metadata.
195upstream_revision() {
196 local r=
197
198 if svn_is_file_tracked "$1" ; then
199 r=$(svn info "$1" 2>/dev/null | \
200 grep "Last Changed Rev:" | \
201 sed -e "s/^Last Changed Rev: *//" -e "s/\([0-9]*\).*/r\1/" | \
202 grep "r[0-9]")
203 elif git_is_file_tracked "$1" ; then
204 # If this is a "native" git-svn clone we could use git svn log:
205 # git svn log --oneline -1 | sed 's/^r//;s/[[:blank:]].*//' or even git svn find-rev
206 # but it is easier to just grep for the git-svn-id unconditionally
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000207 r=$(git log --grep=git-svn-id -1 -- "$1" | \
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000208 grep git-svn-id | \
209 sed 's/.*@/r/;s/[[:blank:]].*//')
210 fi
211
212 if [ -z "$r" ]; then
213 r="unknown" # default to unknown
214 fi
215 echo "${r}"
216}
217
Stefan Taunerd5ff8452015-01-10 09:32:07 +0000218is_tracked() {
219 is_file_tracked "$1"
220}
221
David Hendricks36e9f4b2013-08-14 14:47:26 +0000222show_help() {
223 echo "Usage:
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000224 ${0} <command> [path]
David Hendricks36e9f4b2013-08-14 14:47:26 +0000225
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000226Commands
David Hendricks36e9f4b2013-08-14 14:47:26 +0000227 -h or --help
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000228 this message
Stefan Taunerd5ff8452015-01-10 09:32:07 +0000229 -c or --check
230 test if path is under version control at all
David Hendricks36e9f4b2013-08-14 14:47:26 +0000231 -l or --local
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000232 local revision information including an indicator for uncommitted changes
233 -u or --upstream
234 upstream revision
235 -U or --url
236 URL associated with the latest commit
237 -d or --date
238 date of most recent modification
David Hendricks36e9f4b2013-08-14 14:47:26 +0000239 -t or --timestamp
240 timestamp of most recent modification
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000241"
David Hendricks36e9f4b2013-08-14 14:47:26 +0000242 return
243}
244
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000245check_action() {
246 if [ -n "$action" ]; then
247 echo "Error: Multiple actions given.">&2
David Hendricks36e9f4b2013-08-14 14:47:26 +0000248 exit ${EXIT_FAILURE}
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000249 fi
250}
David Hendricks36e9f4b2013-08-14 14:47:26 +0000251
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000252main() {
253 local query_path=
254 local action=
255
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000256 # Argument parser loop
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000257 while [ $# -gt 0 ];
258 do
259 case ${1} in
260 -h|--help)
261 action=show_help;
262 shift;;
263 -l|--local)
264 check_action $1
265 action=local_revision
266 shift;;
267 -u|--upstream)
268 check_action $1
269 action=upstream_revision
270 shift;;
271 -U|--url)
272 check_action $1
273 action=scm_url
274 shift;;
275 -d|--date)
276 check_action $1
277 action="timestamp +%Y-%m-%d" # refrain from suffixing 'Z' to indicate it's UTC
278 shift;;
279 -t|--timestamp)
280 check_action $1
281 action="timestamp +%Y-%m-%dT%H:%M:%SZ" # There is only one valid time format! ISO 8601
282 shift;;
Stefan Taunerd5ff8452015-01-10 09:32:07 +0000283 -c|--check)
284 check_action=$1
285 action="is_tracked"
286 shift;;
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000287 -*)
288 show_help;
289 echo "Error: Invalid option: ${1}"
290 exit ${EXIT_FAILURE};;
291 *)
292 if [ -z "$query_path" ] ; then
293 if [ ! -e "$1" ] ; then
294 echo "Error: Path \"${1}\" does not exist.">&2
295 exit ${EXIT_FAILURE}
296 fi
297 query_path=$1
298 else
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000299 echo "Warning: Ignoring overabundant parameter: \"${1}\"">&2
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000300 fi
301 shift;;
302 esac;
303 done
304
305 # default to current directory (usually equals the whole repository)
306 if [ -z "$query_path" ] ; then
307 query_path=.
308 fi
309 if ! is_file_tracked "$query_path" ; then
310 echo "Warning: Path \"${query_path}\" is not under version control.">&2
311 fi
312 if [ -z "$action" ] ; then
313 show_help
314 echo "Error: No actions specified"
315 exit ${EXIT_FAILURE}
316 fi
317
318 $action "$query_path"
319}
320
321main $@