Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 1 | #!/bin/sh |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 2 | # |
| 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 Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 8 | # Copyright (C) 2013-2016 Stefan Tauner |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 9 | # |
| 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 | |
| 25 | EXIT_SUCCESS=0 |
| 26 | EXIT_FAILURE=1 |
| 27 | |
Joerg Mayer | d31d3c3 | 2013-08-17 23:58:01 +0000 | [diff] [blame] | 28 | # Make sure we don't get translated output |
| 29 | export LC_ALL=C |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 30 | # nor local times or dates |
| 31 | export TZ=UTC0 |
Joerg Mayer | d31d3c3 | 2013-08-17 23:58:01 +0000 | [diff] [blame] | 32 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 33 | # Helper functions |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 34 | # First argument is the path to inspect (usually optional; without |
| 35 | # it the whole repository will be considered) |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 36 | git_has_local_changes() { |
| 37 | git update-index -q --refresh >/dev/null |
| 38 | ! git diff-index --quiet HEAD -- "$1" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 41 | git_last_commit() { |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 42 | # git rev-parse --short HEAD would suffice if repository as a whole is of interest (no $1) |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 43 | git log --pretty=format:"%h" -1 -- "$1" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 46 | git_is_file_tracked() { |
| 47 | git ls-files --error-unmatch -- "$1" >/dev/null 2>&1 |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 50 | is_file_tracked() { |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 51 | git_is_file_tracked "$1" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 54 | # 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 Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 57 | git_url() { |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 58 | last_commit=$(git_last_commit "$1") |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 59 | # get all remote branches containing the last commit (excluding origin/HEAD) |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 60 | 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 Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 81 | # 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 Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 83 | scm_url() { |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 84 | local url= |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 85 | |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 86 | if git_is_file_tracked "$1" ; then |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 87 | url="$(git_url "$1")" |
| 88 | else |
| 89 | return ${EXIT_FAILURE} |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 90 | fi |
| 91 | |
| 92 | echo "${url}" |
| 93 | } |
| 94 | |
| 95 | # Retrieve timestamp since last modification. If the sources are pristine, |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 96 | # then the timestamp will match that of the SCM's most recent modification |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 97 | # date. |
| 98 | timestamp() { |
| 99 | local t |
| 100 | |
Stefan Tauner | c65b855 | 2013-09-12 15:48:39 +0000 | [diff] [blame] | 101 | # 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 Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 107 | if git_is_file_tracked "$2" ; then |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 108 | # 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 Tauner | c65b855 | 2013-09-12 15:48:39 +0000 | [diff] [blame] | 113 | 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 Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 121 | fi |
| 122 | else |
| 123 | t=$(date -u "$1") |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 124 | fi |
| 125 | |
Stefan Tauner | c65b855 | 2013-09-12 15:48:39 +0000 | [diff] [blame] | 126 | if [ -z "$t" ]; then |
| 127 | echo "Warning: Could not determine timestamp." 2>/dev/null |
| 128 | fi |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 129 | echo "${t}" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 132 | # Retrieve local revision info. |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 133 | local_revision() { |
Stefan Tauner | 9620912 | 2017-10-01 16:41:35 +0200 | [diff] [blame^] | 134 | local r |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 135 | if git_is_file_tracked "$1" ; then |
Stefan Tauner | 9620912 | 2017-10-01 16:41:35 +0200 | [diff] [blame^] | 136 | r=$(git describe $(git_last_commit "$1")) |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 137 | if git_has_local_changes "$1" ; then |
| 138 | r="$r-dirty" |
| 139 | fi |
| 140 | else |
Stefan Tauner | 9620912 | 2017-10-01 16:41:35 +0200 | [diff] [blame^] | 141 | r="unknown" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 142 | fi |
| 143 | |
| 144 | echo "${r}" |
| 145 | } |
| 146 | |
Stefan Tauner | d5ff845 | 2015-01-10 09:32:07 +0000 | [diff] [blame] | 147 | is_tracked() { |
| 148 | is_file_tracked "$1" |
| 149 | } |
| 150 | |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 151 | show_help() { |
| 152 | echo "Usage: |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 153 | ${0} <command> [path] |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 154 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 155 | Commands |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 156 | -h or --help |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 157 | this message |
Stefan Tauner | d5ff845 | 2015-01-10 09:32:07 +0000 | [diff] [blame] | 158 | -c or --check |
| 159 | test if path is under version control at all |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 160 | -l or --local |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 161 | local revision information including an indicator for uncommitted changes |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 162 | -U or --url |
| 163 | URL associated with the latest commit |
| 164 | -d or --date |
| 165 | date of most recent modification |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 166 | -t or --timestamp |
| 167 | timestamp of most recent modification |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 168 | " |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 169 | return |
| 170 | } |
| 171 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 172 | check_action() { |
| 173 | if [ -n "$action" ]; then |
| 174 | echo "Error: Multiple actions given.">&2 |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 175 | exit ${EXIT_FAILURE} |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 176 | fi |
| 177 | } |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 178 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 179 | main() { |
| 180 | local query_path= |
| 181 | local action= |
| 182 | |
Stefan Tauner | c2eec2c | 2014-05-03 21:33:01 +0000 | [diff] [blame] | 183 | # Argument parser loop |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 184 | while [ $# -gt 0 ]; |
| 185 | do |
| 186 | case ${1} in |
| 187 | -h|--help) |
| 188 | action=show_help; |
| 189 | shift;; |
| 190 | -l|--local) |
| 191 | check_action $1 |
| 192 | action=local_revision |
| 193 | shift;; |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 194 | -U|--url) |
| 195 | check_action $1 |
| 196 | action=scm_url |
| 197 | shift;; |
| 198 | -d|--date) |
| 199 | check_action $1 |
| 200 | action="timestamp +%Y-%m-%d" # refrain from suffixing 'Z' to indicate it's UTC |
| 201 | shift;; |
| 202 | -t|--timestamp) |
| 203 | check_action $1 |
| 204 | action="timestamp +%Y-%m-%dT%H:%M:%SZ" # There is only one valid time format! ISO 8601 |
| 205 | shift;; |
Stefan Tauner | d5ff845 | 2015-01-10 09:32:07 +0000 | [diff] [blame] | 206 | -c|--check) |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 207 | check_action $1 |
| 208 | action=is_tracked |
Stefan Tauner | d5ff845 | 2015-01-10 09:32:07 +0000 | [diff] [blame] | 209 | shift;; |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 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 |
Stefan Tauner | c2eec2c | 2014-05-03 21:33:01 +0000 | [diff] [blame] | 222 | echo "Warning: Ignoring overabundant parameter: \"${1}\"">&2 |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 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 |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 232 | if [ -z "$action" ] ; then |
| 233 | show_help |
| 234 | echo "Error: No actions specified" |
| 235 | exit ${EXIT_FAILURE} |
| 236 | fi |
| 237 | |
| 238 | $action "$query_path" |
| 239 | } |
| 240 | |
| 241 | main $@ |