blob: e1580e5eb2ae70d5676fa3e4968430133f2d81c9 [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 Tauner76347082016-11-27 17:45:49 +01008# Copyright (C) 2013-2016 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
Stefan Tauner76347082016-11-27 17:45:49 +010034# First argument is the path to inspect (usually optional; without
35# it the whole repository will be considered)
Stefan Taunerec7a35f2013-08-29 00:38:14 +000036git_has_local_changes() {
37 git update-index -q --refresh >/dev/null
38 ! git diff-index --quiet HEAD -- "$1"
David Hendricks36e9f4b2013-08-14 14:47:26 +000039}
40
Stefan Taunerec7a35f2013-08-29 00:38:14 +000041git_last_commit() {
Stefan Tauner76347082016-11-27 17:45:49 +010042 # git rev-parse --short HEAD would suffice if repository as a whole is of interest (no $1)
Stefan Taunerec7a35f2013-08-29 00:38:14 +000043 git log --pretty=format:"%h" -1 -- "$1"
David Hendricks36e9f4b2013-08-14 14:47:26 +000044}
45
Stefan Taunerec7a35f2013-08-29 00:38:14 +000046git_is_file_tracked() {
47 git ls-files --error-unmatch -- "$1" >/dev/null 2>&1
David Hendricks36e9f4b2013-08-14 14:47:26 +000048}
49
Stefan Taunerec7a35f2013-08-29 00:38:14 +000050is_file_tracked() {
Stefan Tauner76347082016-11-27 17:45:49 +010051 git_is_file_tracked "$1"
David Hendricks36e9f4b2013-08-14 14:47:26 +000052}
53
Stefan Taunerec7a35f2013-08-29 00:38:14 +000054# Tries to find a remote source for the changes committed locally.
55# This includes the URL of the remote repository including the last commit and a suitable branch name.
56# Takes one optional argument: the path to inspect
David Hendricks36e9f4b2013-08-14 14:47:26 +000057git_url() {
Stefan Taunerec7a35f2013-08-29 00:38:14 +000058 last_commit=$(git_last_commit "$1")
Stefan Tauner76347082016-11-27 17:45:49 +010059 # get all remote branches containing the last commit (excluding origin/HEAD)
Stefan Taunerec7a35f2013-08-29 00:38:14 +000060 branches=$(git branch -r --contains $last_commit | sed '/\//!d;/.*->.*/d;s/[\t ]*//')
61 if [ -z "$branches" ] ; then
62 echo "No remote branch contains a suitable commit">&2
63 return
64 fi
65
66 # find "nearest" branch
67 local mindiff=9000
68 local target=
69 for branch in $branches ; do
70 curdiff=$(git rev-list --count $last_commit..$branch)
71 if [ $curdiff -ge $mindiff ] ; then
72 continue
73 fi
74 mindiff=$curdiff
75 target=$branch
76 done
77
78 echo "$(git ls-remote --exit-code --get-url ${target%/*}) ${target#*/}"
David Hendricks36e9f4b2013-08-14 14:47:26 +000079}
80
Stefan Taunerec7a35f2013-08-29 00:38:14 +000081# Returns a string indicating where others can get the current source code (excluding uncommitted changes)
82# Takes one optional argument: the path to inspect
David Hendricks36e9f4b2013-08-14 14:47:26 +000083scm_url() {
Stefan Taunerec7a35f2013-08-29 00:38:14 +000084 local url=
David Hendricks36e9f4b2013-08-14 14:47:26 +000085
Stefan Tauner76347082016-11-27 17:45:49 +010086 if git_is_file_tracked "$1" ; then
Stefan Taunerec7a35f2013-08-29 00:38:14 +000087 url="$(git_url "$1")"
88 else
89 return ${EXIT_FAILURE}
David Hendricks36e9f4b2013-08-14 14:47:26 +000090 fi
91
92 echo "${url}"
93}
94
95# Retrieve timestamp since last modification. If the sources are pristine,
Stefan Taunerec7a35f2013-08-29 00:38:14 +000096# then the timestamp will match that of the SCM's most recent modification
David Hendricks36e9f4b2013-08-14 14:47:26 +000097# date.
98timestamp() {
99 local t
100
Stefan Taunerc65b8552013-09-12 15:48:39 +0000101 # date syntaxes are manifold:
102 # gnu date [-d input]... [+FORMAT]
103 # netbsd date [-ajnu] [-d date] [-r seconds] [+format] [[[[[[CC]yy]mm]dd]HH]MM[.SS]]
104 # freebsd date [-jnu] [-d dst] [-r seconds] [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format] [...]
105 # dragonflybsd date [-jnu] [-d dst] [-r seconds] [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format] [...]
106 # openbsd date [-aju] [-d dst] [-r seconds] [+format] [[[[[[cc]yy]mm]dd]HH]MM[.SS]] [...]
Stefan Tauner76347082016-11-27 17:45:49 +0100107 if git_is_file_tracked "$2" ; then
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000108 # are there local changes?
109 if git_has_local_changes "$2" ; then
110 t=$(date -u "${1}")
111 else
112 # No local changes, get date of the last commit
Stefan Taunerc65b8552013-09-12 15:48:39 +0000113 case $(uname) in
114 # Most BSD dates do not support parsing date values from user input with -d but all of
115 # them support parsing epoch seconds with -r. Thanks to git we can easily use that:
116 NetBSD|OpenBSD|DragonFly|FreeBSD)
117 t=$(date -u -r "$(git log --pretty=format:%ct -1 -- $2)" "$1" 2>/dev/null);;
118 *)
119 t=$(date -d "$(git log --pretty=format:%cD -1 -- $2)" -u "$1" 2>/dev/null);;
120 esac
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000121 fi
122 else
123 t=$(date -u "$1")
David Hendricks36e9f4b2013-08-14 14:47:26 +0000124 fi
125
Stefan Taunerc65b8552013-09-12 15:48:39 +0000126 if [ -z "$t" ]; then
127 echo "Warning: Could not determine timestamp." 2>/dev/null
128 fi
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000129 echo "${t}"
David Hendricks36e9f4b2013-08-14 14:47:26 +0000130}
131
Stefan Tauner76347082016-11-27 17:45:49 +0100132# Retrieve local revision info.
David Hendricks36e9f4b2013-08-14 14:47:26 +0000133local_revision() {
Stefan Tauner76347082016-11-27 17:45:49 +0100134 if git_is_file_tracked "$1" ; then
135 local r=$(git describe $(git_last_commit "$1"))
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000136 if git_has_local_changes "$1" ; then
137 r="$r-dirty"
138 fi
139 else
140 return ${EXIT_FAILURE}
David Hendricks36e9f4b2013-08-14 14:47:26 +0000141 fi
142
143 echo "${r}"
144}
145
Stefan Taunerd5ff8452015-01-10 09:32:07 +0000146is_tracked() {
147 is_file_tracked "$1"
148}
149
David Hendricks36e9f4b2013-08-14 14:47:26 +0000150show_help() {
151 echo "Usage:
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000152 ${0} <command> [path]
David Hendricks36e9f4b2013-08-14 14:47:26 +0000153
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000154Commands
David Hendricks36e9f4b2013-08-14 14:47:26 +0000155 -h or --help
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000156 this message
Stefan Taunerd5ff8452015-01-10 09:32:07 +0000157 -c or --check
158 test if path is under version control at all
David Hendricks36e9f4b2013-08-14 14:47:26 +0000159 -l or --local
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000160 local revision information including an indicator for uncommitted changes
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000161 -U or --url
162 URL associated with the latest commit
163 -d or --date
164 date of most recent modification
David Hendricks36e9f4b2013-08-14 14:47:26 +0000165 -t or --timestamp
166 timestamp of most recent modification
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000167"
David Hendricks36e9f4b2013-08-14 14:47:26 +0000168 return
169}
170
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000171check_action() {
172 if [ -n "$action" ]; then
173 echo "Error: Multiple actions given.">&2
David Hendricks36e9f4b2013-08-14 14:47:26 +0000174 exit ${EXIT_FAILURE}
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000175 fi
176}
David Hendricks36e9f4b2013-08-14 14:47:26 +0000177
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000178main() {
179 local query_path=
180 local action=
181
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000182 # Argument parser loop
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000183 while [ $# -gt 0 ];
184 do
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;;
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000193 -U|--url)
194 check_action $1
195 action=scm_url
196 shift;;
197 -d|--date)
198 check_action $1
199 action="timestamp +%Y-%m-%d" # refrain from suffixing 'Z' to indicate it's UTC
200 shift;;
201 -t|--timestamp)
202 check_action $1
203 action="timestamp +%Y-%m-%dT%H:%M:%SZ" # There is only one valid time format! ISO 8601
204 shift;;
Stefan Taunerd5ff8452015-01-10 09:32:07 +0000205 -c|--check)
Stefan Tauner76347082016-11-27 17:45:49 +0100206 check_action $1
207 action=is_tracked
Stefan Taunerd5ff8452015-01-10 09:32:07 +0000208 shift;;
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000209 -*)
210 show_help;
211 echo "Error: Invalid option: ${1}"
212 exit ${EXIT_FAILURE};;
213 *)
214 if [ -z "$query_path" ] ; then
215 if [ ! -e "$1" ] ; then
216 echo "Error: Path \"${1}\" does not exist.">&2
217 exit ${EXIT_FAILURE}
218 fi
219 query_path=$1
220 else
Stefan Taunerc2eec2c2014-05-03 21:33:01 +0000221 echo "Warning: Ignoring overabundant parameter: \"${1}\"">&2
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000222 fi
223 shift;;
224 esac;
225 done
226
227 # default to current directory (usually equals the whole repository)
228 if [ -z "$query_path" ] ; then
229 query_path=.
230 fi
Stefan Taunerec7a35f2013-08-29 00:38:14 +0000231 if [ -z "$action" ] ; then
232 show_help
233 echo "Error: No actions specified"
234 exit ${EXIT_FAILURE}
235 fi
236
237 $action "$query_path"
238}
239
240main $@