Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 1 | #!/bin/sh |
Stefan Tauner | 8eb1df6 | 2017-10-01 16:45:49 +0200 | [diff] [blame] | 2 | # NB: Supposed to be POSIX compatible but at least the usage of 'local' is not. |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 3 | # |
| 4 | # This file is part of the flashrom project. |
| 5 | # |
| 6 | # Copyright (C) 2005 coresystems GmbH <stepan@coresystems.de> |
| 7 | # Copyright (C) 2009,2010 Carl-Daniel Hailfinger |
| 8 | # Copyright (C) 2010 Chromium OS Authors |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 9 | # Copyright (C) 2013-2016 Stefan Tauner |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 10 | # |
| 11 | # This program is free software; you can redistribute it and/or modify |
| 12 | # it under the terms of the GNU General Public License as published by |
| 13 | # the Free Software Foundation; either version 2 of the License, or |
| 14 | # (at your option) any later version. |
| 15 | # |
| 16 | # This program is distributed in the hope that it will be useful, |
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | # GNU General Public License for more details. |
| 20 | # |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 21 | |
| 22 | EXIT_SUCCESS=0 |
| 23 | EXIT_FAILURE=1 |
| 24 | |
Joerg Mayer | d31d3c3 | 2013-08-17 23:58:01 +0000 | [diff] [blame] | 25 | # Make sure we don't get translated output |
| 26 | export LC_ALL=C |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 27 | # nor local times or dates |
| 28 | export TZ=UTC0 |
Joerg Mayer | d31d3c3 | 2013-08-17 23:58:01 +0000 | [diff] [blame] | 29 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 30 | # Helper functions |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 31 | # First argument is the path to inspect (usually optional; without |
| 32 | # it the whole repository will be considered) |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 33 | git_has_local_changes() { |
| 34 | git update-index -q --refresh >/dev/null |
| 35 | ! git diff-index --quiet HEAD -- "$1" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 38 | git_last_commit() { |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 39 | # 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] | 40 | git log --pretty=format:"%h" -1 -- "$1" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 43 | git_is_file_tracked() { |
| 44 | git ls-files --error-unmatch -- "$1" >/dev/null 2>&1 |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 47 | is_file_tracked() { |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 48 | git_is_file_tracked "$1" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 51 | # Tries to find a remote source for the changes committed locally. |
| 52 | # This includes the URL of the remote repository including the last commit and a suitable branch name. |
| 53 | # Takes one optional argument: the path to inspect |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 54 | git_url() { |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 55 | last_commit=$(git_last_commit "$1") |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 56 | # get all remote branches containing the last commit (excluding origin/HEAD) |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 57 | branches=$(git branch -r --contains $last_commit | sed '/\//!d;/.*->.*/d;s/[\t ]*//') |
| 58 | if [ -z "$branches" ] ; then |
| 59 | echo "No remote branch contains a suitable commit">&2 |
| 60 | return |
| 61 | fi |
| 62 | |
| 63 | # find "nearest" branch |
| 64 | local mindiff=9000 |
| 65 | local target= |
| 66 | for branch in $branches ; do |
| 67 | curdiff=$(git rev-list --count $last_commit..$branch) |
| 68 | if [ $curdiff -ge $mindiff ] ; then |
| 69 | continue |
| 70 | fi |
| 71 | mindiff=$curdiff |
| 72 | target=$branch |
| 73 | done |
| 74 | |
| 75 | echo "$(git ls-remote --exit-code --get-url ${target%/*}) ${target#*/}" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Stefan Tauner | 8eb1df6 | 2017-10-01 16:45:49 +0200 | [diff] [blame] | 78 | # Returns a string indicating where others can get the current source code (excluding uncommitted changes). |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 79 | # Takes one optional argument: the path to inspect |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 80 | scm_url() { |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 81 | local url= |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 82 | |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 83 | if git_is_file_tracked "$1" ; then |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 84 | url="$(git_url "$1")" |
| 85 | else |
| 86 | return ${EXIT_FAILURE} |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 87 | fi |
| 88 | |
| 89 | echo "${url}" |
| 90 | } |
| 91 | |
| 92 | # Retrieve timestamp since last modification. If the sources are pristine, |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 93 | # 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] | 94 | # date. |
| 95 | timestamp() { |
| 96 | local t |
| 97 | |
Stefan Tauner | c65b855 | 2013-09-12 15:48:39 +0000 | [diff] [blame] | 98 | # date syntaxes are manifold: |
| 99 | # gnu date [-d input]... [+FORMAT] |
| 100 | # netbsd date [-ajnu] [-d date] [-r seconds] [+format] [[[[[[CC]yy]mm]dd]HH]MM[.SS]] |
| 101 | # freebsd date [-jnu] [-d dst] [-r seconds] [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format] [...] |
| 102 | # dragonflybsd date [-jnu] [-d dst] [-r seconds] [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format] [...] |
| 103 | # 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] | 104 | if git_is_file_tracked "$2" ; then |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 105 | # are there local changes? |
| 106 | if git_has_local_changes "$2" ; then |
| 107 | t=$(date -u "${1}") |
| 108 | else |
| 109 | # No local changes, get date of the last commit |
Stefan Tauner | c65b855 | 2013-09-12 15:48:39 +0000 | [diff] [blame] | 110 | case $(uname) in |
| 111 | # Most BSD dates do not support parsing date values from user input with -d but all of |
| 112 | # them support parsing epoch seconds with -r. Thanks to git we can easily use that: |
| 113 | NetBSD|OpenBSD|DragonFly|FreeBSD) |
| 114 | t=$(date -u -r "$(git log --pretty=format:%ct -1 -- $2)" "$1" 2>/dev/null);; |
| 115 | *) |
| 116 | t=$(date -d "$(git log --pretty=format:%cD -1 -- $2)" -u "$1" 2>/dev/null);; |
| 117 | esac |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 118 | fi |
| 119 | else |
| 120 | t=$(date -u "$1") |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 121 | fi |
| 122 | |
Stefan Tauner | c65b855 | 2013-09-12 15:48:39 +0000 | [diff] [blame] | 123 | if [ -z "$t" ]; then |
| 124 | echo "Warning: Could not determine timestamp." 2>/dev/null |
| 125 | fi |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 126 | echo "${t}" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Stefan Tauner | 5bf6b85 | 2017-10-04 03:46:51 +0200 | [diff] [blame] | 129 | revision() { |
Stefan Tauner | 9620912 | 2017-10-01 16:41:35 +0200 | [diff] [blame] | 130 | local r |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 131 | if git_is_file_tracked "$1" ; then |
Stefan Tauner | 9620912 | 2017-10-01 16:41:35 +0200 | [diff] [blame] | 132 | r=$(git describe $(git_last_commit "$1")) |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 133 | if git_has_local_changes "$1" ; then |
| 134 | r="$r-dirty" |
| 135 | fi |
| 136 | else |
Stefan Tauner | 9620912 | 2017-10-01 16:41:35 +0200 | [diff] [blame] | 137 | r="unknown" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 138 | fi |
| 139 | |
| 140 | echo "${r}" |
| 141 | } |
| 142 | |
Stefan Tauner | d5ff845 | 2015-01-10 09:32:07 +0000 | [diff] [blame] | 143 | is_tracked() { |
| 144 | is_file_tracked "$1" |
| 145 | } |
| 146 | |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 147 | show_help() { |
| 148 | echo "Usage: |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 149 | ${0} <command> [path] |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 150 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 151 | Commands |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 152 | -h or --help |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 153 | this message |
Stefan Tauner | d5ff845 | 2015-01-10 09:32:07 +0000 | [diff] [blame] | 154 | -c or --check |
| 155 | test if path is under version control at all |
Stefan Tauner | 5bf6b85 | 2017-10-04 03:46:51 +0200 | [diff] [blame] | 156 | -r or --revision |
| 157 | return unique revision information including an indicator for |
| 158 | uncommitted changes |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 159 | -U or --url |
| 160 | URL associated with the latest commit |
| 161 | -d or --date |
| 162 | date of most recent modification |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 163 | -t or --timestamp |
| 164 | timestamp of most recent modification |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 165 | " |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 166 | return |
| 167 | } |
| 168 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 169 | check_action() { |
| 170 | if [ -n "$action" ]; then |
| 171 | echo "Error: Multiple actions given.">&2 |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 172 | exit ${EXIT_FAILURE} |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 173 | fi |
| 174 | } |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 175 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 176 | main() { |
| 177 | local query_path= |
| 178 | local action= |
| 179 | |
Stefan Tauner | c2eec2c | 2014-05-03 21:33:01 +0000 | [diff] [blame] | 180 | # Argument parser loop |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 181 | while [ $# -gt 0 ]; |
| 182 | do |
| 183 | case ${1} in |
| 184 | -h|--help) |
| 185 | action=show_help; |
| 186 | shift;; |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 187 | -U|--url) |
| 188 | check_action $1 |
| 189 | action=scm_url |
| 190 | shift;; |
| 191 | -d|--date) |
| 192 | check_action $1 |
| 193 | action="timestamp +%Y-%m-%d" # refrain from suffixing 'Z' to indicate it's UTC |
| 194 | shift;; |
Stefan Tauner | 5bf6b85 | 2017-10-04 03:46:51 +0200 | [diff] [blame] | 195 | -r|--revision) |
| 196 | check_action $1 |
| 197 | action=revision |
| 198 | shift;; |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 199 | -t|--timestamp) |
| 200 | check_action $1 |
| 201 | action="timestamp +%Y-%m-%dT%H:%M:%SZ" # There is only one valid time format! ISO 8601 |
| 202 | shift;; |
Stefan Tauner | d5ff845 | 2015-01-10 09:32:07 +0000 | [diff] [blame] | 203 | -c|--check) |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 204 | check_action $1 |
| 205 | action=is_tracked |
Stefan Tauner | d5ff845 | 2015-01-10 09:32:07 +0000 | [diff] [blame] | 206 | shift;; |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 207 | -*) |
| 208 | show_help; |
| 209 | echo "Error: Invalid option: ${1}" |
| 210 | exit ${EXIT_FAILURE};; |
| 211 | *) |
| 212 | if [ -z "$query_path" ] ; then |
| 213 | if [ ! -e "$1" ] ; then |
| 214 | echo "Error: Path \"${1}\" does not exist.">&2 |
| 215 | exit ${EXIT_FAILURE} |
| 216 | fi |
| 217 | query_path=$1 |
| 218 | else |
Stefan Tauner | c2eec2c | 2014-05-03 21:33:01 +0000 | [diff] [blame] | 219 | echo "Warning: Ignoring overabundant parameter: \"${1}\"">&2 |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 220 | fi |
| 221 | shift;; |
| 222 | esac; |
| 223 | done |
| 224 | |
| 225 | # default to current directory (usually equals the whole repository) |
| 226 | if [ -z "$query_path" ] ; then |
| 227 | query_path=. |
| 228 | fi |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 229 | if [ -z "$action" ] ; then |
| 230 | show_help |
| 231 | echo "Error: No actions specified" |
| 232 | exit ${EXIT_FAILURE} |
| 233 | fi |
| 234 | |
| 235 | $action "$query_path" |
| 236 | } |
| 237 | |
| 238 | main $@ |