David Hendricks | 36e9f4b | 2013-08-14 14:47:26 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 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 |
| 8 | # |
| 9 | # This program is free software; you can redistribute it and/or modify |
| 10 | # it under the terms of the GNU General Public License as published by |
| 11 | # the Free Software Foundation; either version 2 of the License, or |
| 12 | # (at your option) any later version. |
| 13 | # |
| 14 | # This program is distributed in the hope that it will be useful, |
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | # GNU General Public License for more details. |
| 18 | # |
| 19 | # You should have received a copy of the GNU General Public License |
| 20 | # along with this program; if not, write to the Free Software |
| 21 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | # |
| 23 | |
| 24 | EXIT_SUCCESS=0 |
| 25 | EXIT_FAILURE=1 |
| 26 | |
| 27 | svn_revision() { |
| 28 | LC_ALL=C svnversion -cn . 2>/dev/null | \ |
| 29 | sed -e "s/.*://" -e "s/\([0-9]*\).*/\1/" | \ |
| 30 | grep "[0-9]" || |
| 31 | LC_ALL=C svn info . 2>/dev/null | \ |
| 32 | awk '/^Revision:/ {print $$2 }' | \ |
| 33 | grep "[0-9]" || |
| 34 | LC_ALL=C git svn info . 2>/dev/null | \ |
| 35 | awk '/^Revision:/ {print $$2 }' | \ |
| 36 | grep "[0-9]" || |
| 37 | echo '' |
| 38 | } |
| 39 | |
| 40 | svn_url() { |
| 41 | echo $(LC_ALL=C svn info 2>/dev/null | |
| 42 | grep URL: | |
| 43 | sed 's/.*URL:[[:blank:]]*//' | |
| 44 | grep ^. |
| 45 | ) |
| 46 | } |
| 47 | |
| 48 | svn_timestamp() { |
| 49 | local date_format="+%Y-%m-%d %H:%M:%S" |
| 50 | local timestamp |
| 51 | |
| 52 | # are there local changes in the client? |
| 53 | if svn status | egrep '^ *[ADMR] *' > /dev/null ; then |
| 54 | timestamp=$(date "${date_format} +") |
| 55 | else |
| 56 | # No local changes, get date of the last log record. |
| 57 | local last_commit_date=$(svn info | grep '^Last Changed Date:' | \ |
| 58 | awk '{print $4" "$5" "$6}') |
| 59 | timestamp=$(date --utc --date "${last_commit_date}" \ |
| 60 | "${date_format} UTC") |
| 61 | fi |
| 62 | |
| 63 | echo "${timestamp}" |
| 64 | } |
| 65 | |
| 66 | git_revision() { |
| 67 | echo $(git log --oneline | head -1 | awk '{print $1}') |
| 68 | } |
| 69 | |
| 70 | # Retrieve svn revision using git log data (for git mirrors) |
| 71 | gitsvn_revision() { |
| 72 | local r |
| 73 | |
| 74 | git log| |
| 75 | grep git-svn-id| |
| 76 | sed 's/.*git-svn-id:[[:blank:]]*\([^@]\+\)@[0-9]\+[[:blank:]]\+\([^[:blank:]]\+\)/\1 \2/'| |
| 77 | sort| |
| 78 | uniq| |
| 79 | read url uuid |
| 80 | |
| 81 | r=$(git log --grep="git-svn-id.*$uuid"| grep git-svn-id | \ |
| 82 | sed 's/.*@//;s/[[:blank:]].*//'| \ |
| 83 | sort -n | \ |
| 84 | tail -1) |
| 85 | |
| 86 | echo "${r}" |
| 87 | } |
| 88 | |
| 89 | git_timestamp() { |
| 90 | local date_format="+%b %d %Y %H:%M:%S" |
| 91 | local timestamp |
| 92 | |
| 93 | # are there local changes in the client? |
| 94 | if git status | \ |
| 95 | egrep '^# Change(s to be committed|d but not updated):$' > /dev/null |
| 96 | then |
| 97 | timestamp=$(date "${date_format} +") |
| 98 | else |
| 99 | # No local changes, get date of the last log record. |
| 100 | local last_commit_date=$(git log | head -3 | grep '^Date:' | \ |
| 101 | awk '{print $3" "$4" "$6" "$5" "$7}') |
| 102 | timestamp=$(date --utc --date "${last_commit_date}" \ |
| 103 | "${date_format} UTC") |
| 104 | fi |
| 105 | |
| 106 | echo "${timestamp}" |
| 107 | } |
| 108 | |
| 109 | git_url() { |
| 110 | # Only the first line of `git remote' is considered. |
| 111 | echo $(LC_ALL=C git remote show origin 2>/dev/null | |
| 112 | grep 'Fetch URL:' | |
| 113 | sed 's/.*URL:[[:blank:]]*//' | |
| 114 | grep ^. |
| 115 | ) |
| 116 | } |
| 117 | |
| 118 | scm_url() { |
| 119 | local url |
| 120 | |
| 121 | if [ -d ".svn" ] ; then |
| 122 | url=$(svn_url) |
| 123 | elif [ -d ".git" ] ; then |
| 124 | url=$(git_url) |
| 125 | fi |
| 126 | |
| 127 | echo "${url}" |
| 128 | } |
| 129 | |
| 130 | # Retrieve timestamp since last modification. If the sources are pristine, |
| 131 | # then the timestamp will match that of the SCM's more recent modification |
| 132 | # date. |
| 133 | timestamp() { |
| 134 | local t |
| 135 | |
| 136 | if [ -d ".svn" ] ; then |
| 137 | t=$(svn_timestamp) |
| 138 | elif [ -d ".git" ] ; then |
| 139 | t=$(git_timestamp) |
| 140 | fi |
| 141 | |
| 142 | echo ${t} |
| 143 | } |
| 144 | |
| 145 | # Retrieve local SCM revision info. This is useful if we're working in |
| 146 | # a even different SCM than upstream. |
| 147 | # |
| 148 | # If local copy is svn, then there is nothing to do here. |
| 149 | # If local copy is git, then retrieve useful git revision info |
| 150 | local_revision() { |
| 151 | local r |
| 152 | |
| 153 | if [ -d ".git" ] ; then |
| 154 | r=$(git_revision) |
| 155 | fi |
| 156 | |
| 157 | echo ${r} |
| 158 | } |
| 159 | |
| 160 | # Get the upstream flashrom revision stored in SVN metadata. |
| 161 | # |
| 162 | # If the local copy is svn, then use svnversion |
| 163 | # If the local copy is git, then scrape upstream revision from git logs |
| 164 | upstream_revision() { |
| 165 | local r |
| 166 | |
| 167 | if [ -d ".svn" ] ; then |
| 168 | r=$(svn_revision) |
| 169 | elif [ -d ".git" ] ; then |
| 170 | r=$(gitsvn_revision) |
| 171 | fi |
| 172 | |
| 173 | echo "${r}" |
| 174 | } |
| 175 | |
| 176 | show_help() { |
| 177 | echo "Usage: |
| 178 | ${0} <option> |
| 179 | |
| 180 | Options |
| 181 | -h or --help |
| 182 | Display this message. |
| 183 | -u or --upstream |
| 184 | upstream flashrom revision |
| 185 | -l or --local |
| 186 | local revision (if different from upstream) |
| 187 | -t or --timestamp |
| 188 | timestamp of most recent modification |
| 189 | -U or --url |
| 190 | url associated with local copy of flashrom |
| 191 | " |
| 192 | return |
| 193 | } |
| 194 | |
| 195 | if [ ! -n "${1}" ] |
| 196 | then |
| 197 | show_help; |
| 198 | echo "No options specified"; |
| 199 | exit ${EXIT_SUCCESS} |
| 200 | fi |
| 201 | |
| 202 | # The is the main loop |
| 203 | while [[ ${1} = -* ]]; |
| 204 | do |
| 205 | case ${1} in |
| 206 | -h|--help) |
| 207 | show_help; |
| 208 | shift;; |
| 209 | -u|--upstream) |
| 210 | echo "$(upstream_revision)"; |
| 211 | shift;; |
| 212 | -l|--local) |
| 213 | echo "$(local_revision)"; |
| 214 | shift;; |
| 215 | -t|--timestamp) |
| 216 | echo "$(timestamp)"; |
| 217 | shift;; |
| 218 | -U|--url) |
| 219 | echo "$(scm_url)"; |
| 220 | shift;; |
| 221 | *) |
| 222 | show_help; |
| 223 | echo "invalid option: ${1}" |
| 224 | exit ${EXIT_FAILURE} |
| 225 | esac; |
| 226 | done |
| 227 | |
| 228 | exit ${EXIT_SUCCESS} |