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 | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 8 | # Copyright (C) 2013 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 |
| 34 | # First argument is the path to inspect (usually optional; w/o it the whole repository will be considered) |
| 35 | svn_has_local_changes() { |
| 36 | svn status "$1" | egrep '^ *[ADMR] *' >/dev/null |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 39 | git_has_local_changes() { |
| 40 | git update-index -q --refresh >/dev/null |
| 41 | ! git diff-index --quiet HEAD -- "$1" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 44 | git_last_commit() { |
| 45 | git log --pretty=format:"%h" -1 -- "$1" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 48 | svn_is_file_tracked() { |
| 49 | svn info "$1" >/dev/null 2>&1 |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 52 | git_is_file_tracked() { |
| 53 | git ls-files --error-unmatch -- "$1" >/dev/null 2>&1 |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 56 | is_file_tracked() { |
| 57 | svn_is_file_tracked "$1" || git_is_file_tracked "$1" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 60 | # Tries to find a remote source for the changes committed locally. |
| 61 | # This includes the URL of the remote repository including the last commit and a suitable branch name. |
| 62 | # Takes one optional argument: the path to inspect |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 63 | git_url() { |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 64 | last_commit=$(git_last_commit "$1") |
| 65 | # get all remote branches containing the last commit (excluding origin/HEAD and git-svn branches/tags) |
| 66 | branches=$(git branch -r --contains $last_commit | sed '/\//!d;/.*->.*/d;s/[\t ]*//') |
| 67 | if [ -z "$branches" ] ; then |
| 68 | echo "No remote branch contains a suitable commit">&2 |
| 69 | return |
| 70 | fi |
| 71 | |
| 72 | # find "nearest" branch |
| 73 | local mindiff=9000 |
| 74 | local target= |
| 75 | for branch in $branches ; do |
| 76 | curdiff=$(git rev-list --count $last_commit..$branch) |
| 77 | if [ $curdiff -ge $mindiff ] ; then |
| 78 | continue |
| 79 | fi |
| 80 | mindiff=$curdiff |
| 81 | target=$branch |
| 82 | done |
| 83 | |
| 84 | echo "$(git ls-remote --exit-code --get-url ${target%/*}) ${target#*/}" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 87 | # Returns a string indicating where others can get the current source code (excluding uncommitted changes) |
| 88 | # Takes one optional argument: the path to inspect |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 89 | scm_url() { |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 90 | local url= |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 91 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 92 | # for a primitive VCS like subversion finding the URL is easy: there is only one upstream host |
| 93 | if svn_is_file_tracked "$1" ; then |
| 94 | url="$(svn info "$1" 2>/dev/null | |
| 95 | grep URL: | |
| 96 | sed 's/.*URL:[[:blank:]]*//;s/:\/\/.*@/:\/\//' | |
| 97 | grep ^.)" |
| 98 | elif git_is_file_tracked "$1" ; then |
| 99 | url="$(git_url "$1")" |
| 100 | else |
| 101 | return ${EXIT_FAILURE} |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 102 | fi |
| 103 | |
| 104 | echo "${url}" |
| 105 | } |
| 106 | |
| 107 | # Retrieve timestamp since last modification. If the sources are pristine, |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 108 | # 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] | 109 | # date. |
| 110 | timestamp() { |
| 111 | local t |
| 112 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 113 | if svn_is_file_tracked "$2" ; then |
| 114 | if svn_has_local_changes "$2"; then |
| 115 | t=$(date -u "$1") |
| 116 | else |
| 117 | # No local changes, get date of the last log record. |
| 118 | local last_commit_date="$(svn info "$2" | \ |
| 119 | grep '^Last Changed Date:' | \ |
| 120 | awk '{print $4" "$5" "$6}')" |
| 121 | t=$(date -d "${last_commit_date}" -u "$1") |
| 122 | fi |
| 123 | elif git_is_file_tracked "$2" ; then |
| 124 | # are there local changes? |
| 125 | if git_has_local_changes "$2" ; then |
| 126 | t=$(date -u "${1}") |
| 127 | else |
| 128 | # No local changes, get date of the last commit |
| 129 | t=$(date -d "$(git log --pretty=format:"%cD" -1 -- "$2")" -u "$1") |
| 130 | fi |
| 131 | else |
| 132 | t=$(date -u "$1") |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 133 | fi |
| 134 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 135 | echo "${t}" |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 138 | # Retrieve local SCM revision info. This is useful if we're working in a different SCM than upstream and/or |
| 139 | # have local changes. |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 140 | local_revision() { |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 141 | local r= |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 142 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 143 | if svn_is_file_tracked "$1" ; then |
| 144 | r=$(svn_has_local_changes "$1" && echo "dirty") |
| 145 | elif git_is_file_tracked "$1" ; then |
| 146 | r=$(git_last_commit "$1") |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 147 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 148 | local svn_base=$(git log --grep git-svn-id -1 --format='%h') |
| 149 | if [ "$svn_base" != "" ] ; then |
| 150 | local diff_to_svn=$(git rev-list --count ${svn_base}..${r}) |
| 151 | if [ "$diff_to_svn" -gt 0 ] ; then |
| 152 | r="$r-$diff_to_svn" |
| 153 | fi |
| 154 | fi |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 155 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 156 | if git_has_local_changes "$1" ; then |
| 157 | r="$r-dirty" |
| 158 | fi |
| 159 | else |
| 160 | return ${EXIT_FAILURE} |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 161 | fi |
| 162 | |
| 163 | echo "${r}" |
| 164 | } |
| 165 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 166 | # Get the upstream flashrom revision stored in SVN metadata. |
| 167 | upstream_revision() { |
| 168 | local r= |
| 169 | |
| 170 | if svn_is_file_tracked "$1" ; then |
| 171 | r=$(svn info "$1" 2>/dev/null | \ |
| 172 | grep "Last Changed Rev:" | \ |
| 173 | sed -e "s/^Last Changed Rev: *//" -e "s/\([0-9]*\).*/r\1/" | \ |
| 174 | grep "r[0-9]") |
| 175 | elif git_is_file_tracked "$1" ; then |
| 176 | # If this is a "native" git-svn clone we could use git svn log: |
| 177 | # git svn log --oneline -1 | sed 's/^r//;s/[[:blank:]].*//' or even git svn find-rev |
| 178 | # but it is easier to just grep for the git-svn-id unconditionally |
| 179 | r=$(git log --grep git-svn-id -1 -- "$1" | \ |
| 180 | grep git-svn-id | \ |
| 181 | sed 's/.*@/r/;s/[[:blank:]].*//') |
| 182 | fi |
| 183 | |
| 184 | if [ -z "$r" ]; then |
| 185 | r="unknown" # default to unknown |
| 186 | fi |
| 187 | echo "${r}" |
| 188 | } |
| 189 | |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 190 | show_help() { |
| 191 | echo "Usage: |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 192 | ${0} <command> [path] |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 193 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 194 | Commands |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 195 | -h or --help |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 196 | this message |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 197 | -l or --local |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 198 | local revision information including an indicator for uncommitted changes |
| 199 | -u or --upstream |
| 200 | upstream revision |
| 201 | -U or --url |
| 202 | URL associated with the latest commit |
| 203 | -d or --date |
| 204 | date of most recent modification |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 205 | -t or --timestamp |
| 206 | timestamp of most recent modification |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 207 | " |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 208 | return |
| 209 | } |
| 210 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 211 | check_action() { |
| 212 | if [ -n "$action" ]; then |
| 213 | echo "Error: Multiple actions given.">&2 |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 214 | exit ${EXIT_FAILURE} |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 215 | fi |
| 216 | } |
David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 217 | |
Stefan Tauner | ec7a35f | 2013-08-29 00:38:14 +0000 | [diff] [blame] | 218 | main() { |
| 219 | local query_path= |
| 220 | local action= |
| 221 | |
| 222 | # The is the main loop |
| 223 | while [ $# -gt 0 ]; |
| 224 | do |
| 225 | case ${1} in |
| 226 | -h|--help) |
| 227 | action=show_help; |
| 228 | shift;; |
| 229 | -l|--local) |
| 230 | check_action $1 |
| 231 | action=local_revision |
| 232 | shift;; |
| 233 | -u|--upstream) |
| 234 | check_action $1 |
| 235 | action=upstream_revision |
| 236 | shift;; |
| 237 | -U|--url) |
| 238 | check_action $1 |
| 239 | action=scm_url |
| 240 | shift;; |
| 241 | -d|--date) |
| 242 | check_action $1 |
| 243 | action="timestamp +%Y-%m-%d" # refrain from suffixing 'Z' to indicate it's UTC |
| 244 | shift;; |
| 245 | -t|--timestamp) |
| 246 | check_action $1 |
| 247 | action="timestamp +%Y-%m-%dT%H:%M:%SZ" # There is only one valid time format! ISO 8601 |
| 248 | shift;; |
| 249 | -*) |
| 250 | show_help; |
| 251 | echo "Error: Invalid option: ${1}" |
| 252 | exit ${EXIT_FAILURE};; |
| 253 | *) |
| 254 | if [ -z "$query_path" ] ; then |
| 255 | if [ ! -e "$1" ] ; then |
| 256 | echo "Error: Path \"${1}\" does not exist.">&2 |
| 257 | exit ${EXIT_FAILURE} |
| 258 | fi |
| 259 | query_path=$1 |
| 260 | else |
| 261 | echo "Warning: Ignoring over-abundant paramter: \"${1}\"">&2 |
| 262 | fi |
| 263 | shift;; |
| 264 | esac; |
| 265 | done |
| 266 | |
| 267 | # default to current directory (usually equals the whole repository) |
| 268 | if [ -z "$query_path" ] ; then |
| 269 | query_path=. |
| 270 | fi |
| 271 | if ! is_file_tracked "$query_path" ; then |
| 272 | echo "Warning: Path \"${query_path}\" is not under version control.">&2 |
| 273 | fi |
| 274 | if [ -z "$action" ] ; then |
| 275 | show_help |
| 276 | echo "Error: No actions specified" |
| 277 | exit ${EXIT_FAILURE} |
| 278 | fi |
| 279 | |
| 280 | $action "$query_path" |
| 281 | } |
| 282 | |
| 283 | main $@ |