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 | # |
| 21 | # You should have received a copy of the GNU General Public License |
| 22 | # along with this program; if not, write to the Free Software |
| 23 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | # |
| 25 | |
| 26 | EXIT_SUCCESS=0 |
| 27 | EXIT_FAILURE=1 |
| 28 | |
Joerg Mayer | d31d3c3 | 2013-08-17 23:58:01 +0000 | [diff] [blame] | 29 | # Make sure we don't get translated output |
| 30 | export LC_ALL=C |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 31 | # nor local times or dates |
| 32 | export TZ=UTC0 |
Joerg Mayer | d31d3c3 | 2013-08-17 23:58:01 +0000 | [diff] [blame] | 33 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 34 | # Helper functions |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 35 | # First argument is the path to inspect (usually optional; without |
| 36 | # it the whole repository will be considered) |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 37 | git_has_local_changes() { |
| 38 | git update-index -q --refresh >/dev/null |
| 39 | ! git diff-index --quiet HEAD -- "$1" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 42 | git_last_commit() { |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 43 | # 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] | 44 | git log --pretty=format:"%h" -1 -- "$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 | git_is_file_tracked() { |
| 48 | git ls-files --error-unmatch -- "$1" >/dev/null 2>&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 | is_file_tracked() { |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 52 | git_is_file_tracked "$1" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 55 | # Tries to find a remote source for the changes committed locally. |
| 56 | # This includes the URL of the remote repository including the last commit and a suitable branch name. |
| 57 | # Takes one optional argument: the path to inspect |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 58 | git_url() { |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 59 | last_commit=$(git_last_commit "$1") |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 60 | # get all remote branches containing the last commit (excluding origin/HEAD) |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 61 | branches=$(git branch -r --contains $last_commit | sed '/\//!d;/.*->.*/d;s/[\t ]*//') |
| 62 | if [ -z "$branches" ] ; then |
| 63 | echo "No remote branch contains a suitable commit">&2 |
| 64 | return |
| 65 | fi |
| 66 | |
| 67 | # find "nearest" branch |
| 68 | local mindiff=9000 |
| 69 | local target= |
| 70 | for branch in $branches ; do |
| 71 | curdiff=$(git rev-list --count $last_commit..$branch) |
| 72 | if [ $curdiff -ge $mindiff ] ; then |
| 73 | continue |
| 74 | fi |
| 75 | mindiff=$curdiff |
| 76 | target=$branch |
| 77 | done |
| 78 | |
| 79 | echo "$(git ls-remote --exit-code --get-url ${target%/*}) ${target#*/}" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Stefan Tauner | 8eb1df6 | 2017-10-01 16:45:49 +0200 | [diff] [blame] | 82 | # 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] | 83 | # Takes one optional argument: the path to inspect |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 84 | scm_url() { |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 85 | local url= |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 86 | |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 87 | if git_is_file_tracked "$1" ; then |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 88 | url="$(git_url "$1")" |
| 89 | else |
| 90 | return ${EXIT_FAILURE} |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 91 | fi |
| 92 | |
| 93 | echo "${url}" |
| 94 | } |
| 95 | |
| 96 | # Retrieve timestamp since last modification. If the sources are pristine, |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 97 | # 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] | 98 | # date. |
| 99 | timestamp() { |
| 100 | local t |
| 101 | |
Stefan Tauner | c65b855 | 2013-09-12 15:48:39 +0000 | [diff] [blame] | 102 | # date syntaxes are manifold: |
| 103 | # gnu date [-d input]... [+FORMAT] |
| 104 | # netbsd date [-ajnu] [-d date] [-r seconds] [+format] [[[[[[CC]yy]mm]dd]HH]MM[.SS]] |
| 105 | # freebsd date [-jnu] [-d dst] [-r seconds] [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format] [...] |
| 106 | # dragonflybsd date [-jnu] [-d dst] [-r seconds] [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format] [...] |
| 107 | # 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] | 108 | if git_is_file_tracked "$2" ; then |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 109 | # are there local changes? |
| 110 | if git_has_local_changes "$2" ; then |
| 111 | t=$(date -u "${1}") |
| 112 | else |
| 113 | # No local changes, get date of the last commit |
Stefan Tauner | c65b855 | 2013-09-12 15:48:39 +0000 | [diff] [blame] | 114 | case $(uname) in |
| 115 | # Most BSD dates do not support parsing date values from user input with -d but all of |
| 116 | # them support parsing epoch seconds with -r. Thanks to git we can easily use that: |
| 117 | NetBSD|OpenBSD|DragonFly|FreeBSD) |
| 118 | t=$(date -u -r "$(git log --pretty=format:%ct -1 -- $2)" "$1" 2>/dev/null);; |
| 119 | *) |
| 120 | t=$(date -d "$(git log --pretty=format:%cD -1 -- $2)" -u "$1" 2>/dev/null);; |
| 121 | esac |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 122 | fi |
| 123 | else |
| 124 | t=$(date -u "$1") |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 125 | fi |
| 126 | |
Stefan Tauner | c65b855 | 2013-09-12 15:48:39 +0000 | [diff] [blame] | 127 | if [ -z "$t" ]; then |
| 128 | echo "Warning: Could not determine timestamp." 2>/dev/null |
| 129 | fi |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 130 | echo "${t}" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Stefan Tauner | 5bf6b85 | 2017-10-04 03:46:51 +0200 | [diff] [blame] | 133 | 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 |
Stefan Tauner | 5bf6b85 | 2017-10-04 03:46:51 +0200 | [diff] [blame] | 160 | -r or --revision |
| 161 | return unique revision information including an indicator for |
| 162 | uncommitted changes |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 163 | -U or --url |
| 164 | URL associated with the latest commit |
| 165 | -d or --date |
| 166 | date of most recent modification |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 167 | -t or --timestamp |
| 168 | timestamp of most recent modification |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 169 | " |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 170 | return |
| 171 | } |
| 172 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 173 | check_action() { |
| 174 | if [ -n "$action" ]; then |
| 175 | echo "Error: Multiple actions given.">&2 |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 176 | exit ${EXIT_FAILURE} |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 177 | fi |
| 178 | } |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 179 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 180 | main() { |
| 181 | local query_path= |
| 182 | local action= |
| 183 | |
Stefan Tauner | c2eec2c | 2014-05-03 21:33:01 +0000 | [diff] [blame] | 184 | # Argument parser loop |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 185 | while [ $# -gt 0 ]; |
| 186 | do |
| 187 | case ${1} in |
| 188 | -h|--help) |
| 189 | action=show_help; |
| 190 | shift;; |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 191 | -U|--url) |
| 192 | check_action $1 |
| 193 | action=scm_url |
| 194 | shift;; |
| 195 | -d|--date) |
| 196 | check_action $1 |
| 197 | action="timestamp +%Y-%m-%d" # refrain from suffixing 'Z' to indicate it's UTC |
| 198 | shift;; |
Stefan Tauner | 5bf6b85 | 2017-10-04 03:46:51 +0200 | [diff] [blame] | 199 | -r|--revision) |
| 200 | check_action $1 |
| 201 | action=revision |
| 202 | shift;; |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 203 | -t|--timestamp) |
| 204 | check_action $1 |
| 205 | action="timestamp +%Y-%m-%dT%H:%M:%SZ" # There is only one valid time format! ISO 8601 |
| 206 | shift;; |
Stefan Tauner | d5ff845 | 2015-01-10 09:32:07 +0000 | [diff] [blame] | 207 | -c|--check) |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 208 | check_action $1 |
| 209 | action=is_tracked |
Stefan Tauner | d5ff845 | 2015-01-10 09:32:07 +0000 | [diff] [blame] | 210 | shift;; |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 211 | -*) |
| 212 | show_help; |
| 213 | echo "Error: Invalid option: ${1}" |
| 214 | exit ${EXIT_FAILURE};; |
| 215 | *) |
| 216 | if [ -z "$query_path" ] ; then |
| 217 | if [ ! -e "$1" ] ; then |
| 218 | echo "Error: Path \"${1}\" does not exist.">&2 |
| 219 | exit ${EXIT_FAILURE} |
| 220 | fi |
| 221 | query_path=$1 |
| 222 | else |
Stefan Tauner | c2eec2c | 2014-05-03 21:33:01 +0000 | [diff] [blame] | 223 | echo "Warning: Ignoring overabundant parameter: \"${1}\"">&2 |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 224 | fi |
| 225 | shift;; |
| 226 | esac; |
| 227 | done |
| 228 | |
| 229 | # default to current directory (usually equals the whole repository) |
| 230 | if [ -z "$query_path" ] ; then |
| 231 | query_path=. |
| 232 | fi |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 233 | if [ -z "$action" ] ; then |
| 234 | show_help |
| 235 | echo "Error: No actions specified" |
| 236 | exit ${EXIT_FAILURE} |
| 237 | fi |
| 238 | |
| 239 | $action "$query_path" |
| 240 | } |
| 241 | |
| 242 | main $@ |