blob: 098f63e9ad7924859b067c42b946ffdf9a1d28b2 [file] [log] [blame]
Joerg Mayerd31d3c32013-08-17 23:58:01 +00001#!/bin/bash
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
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#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22#
23
24EXIT_SUCCESS=0
25EXIT_FAILURE=1
26
Joerg Mayerd31d3c32013-08-17 23:58:01 +000027# Make sure we don't get translated output
28export LC_ALL=C
29
David Hendricks36e9f4b2013-08-14 14:47:26 +000030svn_revision() {
Joerg Mayerd31d3c32013-08-17 23:58:01 +000031 svnversion -cn . 2>/dev/null | \
David Hendricks36e9f4b2013-08-14 14:47:26 +000032 sed -e "s/.*://" -e "s/\([0-9]*\).*/\1/" | \
33 grep "[0-9]" ||
Joerg Mayerd31d3c32013-08-17 23:58:01 +000034 svn info . 2>/dev/null | \
David Hendricks36e9f4b2013-08-14 14:47:26 +000035 awk '/^Revision:/ {print $$2 }' | \
36 grep "[0-9]" ||
Joerg Mayerd31d3c32013-08-17 23:58:01 +000037 git svn info . 2>/dev/null | \
David Hendricks36e9f4b2013-08-14 14:47:26 +000038 awk '/^Revision:/ {print $$2 }' | \
39 grep "[0-9]" ||
40 echo ''
41}
42
43svn_url() {
Joerg Mayerd31d3c32013-08-17 23:58:01 +000044 echo $(svn info 2>/dev/null |
David Hendricks36e9f4b2013-08-14 14:47:26 +000045 grep URL: |
46 sed 's/.*URL:[[:blank:]]*//' |
47 grep ^.
48 )
49}
50
51svn_timestamp() {
52 local date_format="+%Y-%m-%d %H:%M:%S"
53 local timestamp
54
55 # are there local changes in the client?
56 if svn status | egrep '^ *[ADMR] *' > /dev/null ; then
57 timestamp=$(date "${date_format} +")
58 else
59 # No local changes, get date of the last log record.
Joerg Mayerd31d3c32013-08-17 23:58:01 +000060 local last_commit_date=$(svn info | grep '^Last Changed Date:' | awk '{print $4" "$5" "$6}')
David Hendricks36e9f4b2013-08-14 14:47:26 +000061 timestamp=$(date --utc --date "${last_commit_date}" \
62 "${date_format} UTC")
63 fi
64
65 echo "${timestamp}"
66}
67
68git_revision() {
69 echo $(git log --oneline | head -1 | awk '{print $1}')
70}
71
72# Retrieve svn revision using git log data (for git mirrors)
73gitsvn_revision() {
74 local r
75
76 git log|
77 grep git-svn-id|
78 sed 's/.*git-svn-id:[[:blank:]]*\([^@]\+\)@[0-9]\+[[:blank:]]\+\([^[:blank:]]\+\)/\1 \2/'|
79 sort|
80 uniq|
81 read url uuid
82
83 r=$(git log --grep="git-svn-id.*$uuid"| grep git-svn-id | \
84 sed 's/.*@//;s/[[:blank:]].*//'| \
85 sort -n | \
86 tail -1)
87
88 echo "${r}"
89}
90
91git_timestamp() {
92 local date_format="+%b %d %Y %H:%M:%S"
93 local timestamp
94
95 # are there local changes in the client?
96 if git status | \
97 egrep '^# Change(s to be committed|d but not updated):$' > /dev/null
98 then
99 timestamp=$(date "${date_format} +")
100 else
101 # No local changes, get date of the last log record.
102 local last_commit_date=$(git log | head -3 | grep '^Date:' | \
103 awk '{print $3" "$4" "$6" "$5" "$7}')
104 timestamp=$(date --utc --date "${last_commit_date}" \
105 "${date_format} UTC")
106 fi
107
108 echo "${timestamp}"
109}
110
111git_url() {
112 # Only the first line of `git remote' is considered.
Joerg Mayerd31d3c32013-08-17 23:58:01 +0000113 echo $(git remote show origin 2>/dev/null |
David Hendricks36e9f4b2013-08-14 14:47:26 +0000114 grep 'Fetch URL:' |
115 sed 's/.*URL:[[:blank:]]*//' |
116 grep ^.
117 )
118}
119
120scm_url() {
121 local url
122
123 if [ -d ".svn" ] ; then
124 url=$(svn_url)
125 elif [ -d ".git" ] ; then
126 url=$(git_url)
127 fi
128
129 echo "${url}"
130}
131
132# Retrieve timestamp since last modification. If the sources are pristine,
133# then the timestamp will match that of the SCM's more recent modification
134# date.
135timestamp() {
136 local t
137
138 if [ -d ".svn" ] ; then
139 t=$(svn_timestamp)
140 elif [ -d ".git" ] ; then
141 t=$(git_timestamp)
142 fi
143
144 echo ${t}
145}
146
147# Retrieve local SCM revision info. This is useful if we're working in
148# a even different SCM than upstream.
149#
150# If local copy is svn, then there is nothing to do here.
151# If local copy is git, then retrieve useful git revision info
152local_revision() {
153 local r
154
155 if [ -d ".git" ] ; then
156 r=$(git_revision)
157 fi
158
159 echo ${r}
160}
161
162# Get the upstream flashrom revision stored in SVN metadata.
163#
164# If the local copy is svn, then use svnversion
165# If the local copy is git, then scrape upstream revision from git logs
166upstream_revision() {
167 local r
168
169 if [ -d ".svn" ] ; then
170 r=$(svn_revision)
171 elif [ -d ".git" ] ; then
172 r=$(gitsvn_revision)
173 fi
174
175 echo "${r}"
176}
177
178show_help() {
179 echo "Usage:
180 ${0} <option>
181
182Options
183 -h or --help
184 Display this message.
185 -u or --upstream
186 upstream flashrom revision
187 -l or --local
188 local revision (if different from upstream)
189 -t or --timestamp
190 timestamp of most recent modification
191 -U or --url
192 url associated with local copy of flashrom
193 "
194 return
195}
196
197if [ ! -n "${1}" ]
198then
199 show_help;
200 echo "No options specified";
201 exit ${EXIT_SUCCESS}
202fi
203
204# The is the main loop
205while [[ ${1} = -* ]];
206do
207 case ${1} in
208 -h|--help)
209 show_help;
210 shift;;
211 -u|--upstream)
212 echo "$(upstream_revision)";
213 shift;;
214 -l|--local)
215 echo "$(local_revision)";
216 shift;;
217 -t|--timestamp)
218 echo "$(timestamp)";
219 shift;;
220 -U|--url)
221 echo "$(scm_url)";
222 shift;;
223 *)
224 show_help;
225 echo "invalid option: ${1}"
226 exit ${EXIT_FAILURE}
227 esac;
228done
229
230exit ${EXIT_SUCCESS}