1#!/bin/bash
2# Copyright 2013 the V8 project authors. All rights reserved.
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7#     * Redistributions of source code must retain the above copyright
8#       notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above
10#       copyright notice, this list of conditions and the following
11#       disclaimer in the documentation and/or other materials provided
12#       with the distribution.
13#     * Neither the name of Google Inc. nor the names of its
14#       contributors may be used to endorse or promote products derived
15#       from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
30########## Global variable definitions
31
32BASE_URL="https://code.google.com/p/v8/source/list"
33VERSION="include/v8-version.h"
34MAJOR="V8_MAJOR_VERSION"
35MINOR="V8_MINOR_VERSION"
36BUILD="V8_BUILD_NUMBER"
37PATCH="V8_PATCH_LEVEL"
38
39V8="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
40
41########## Function definitions
42
43cd $V8
44
45usage() {
46cat << EOF
47usage: $0 OPTIONS
48
49Fetches V8 revision information from a git-svn checkout.
50
51OPTIONS:
52  -h    Show this message.
53
54  -i    Print revision info for all branches matching the V8 version.
55          Example usage: $0 -i 3.19.10$
56          Output format: [Git hash] [SVN revision] [V8 version]
57
58  -v    Print the V8 version tag for a trunk SVN revision.
59          Example usage: $0 -v 14981
60          Output format: [V8 version]
61
62  -m    Print all patches that were merged to the specified V8 branch.
63          Example usage: $0 -m 3.18
64          Output format: [V8 version] [SVN revision] [SVN patch merged]*.
65
66  -p    Print all patches merged to a specific V8 point-release.
67          Example usage: $0 -p 3.19.12.1
68          Output format: [SVN patch merged]*
69
70  -u    Print a link to all SVN revisions between two V8 revision tags.
71          Example usage: $0 -u 3.19.10:3.19.11
72EOF
73}
74
75tags() {
76  git for-each-ref --format="%(objectname) %(refname:short)" refs/remotes/svn
77}
78
79tag_revision() {
80  cut -d" " -f1
81}
82
83tag_log() {
84  git log --format="%h %ci %ce %s" -1 $1
85}
86
87v8_hash() {
88  tags | grep "svn/tags/$1$" | tag_revision
89}
90
91point_merges() {
92  echo $1 | grep -o "r[0-9]\+"
93}
94
95hash_to_svn() {
96  git svn log -1 --oneline $1 | cut -d" " -f1
97}
98
99tag_version() {
100  tags | grep svn/tags/$1 | while read tag; do
101    id=$(echo $tag | grep -o "[^/]*$")
102    rev=$(echo $tag | tag_revision)
103    svn=$(hash_to_svn $rev)
104    echo $rev $svn $id
105  done
106}
107
108svn_rev() {
109  git svn find-rev $2 svn/$1
110}
111
112v8_rev() {
113  cd $(git rev-parse --show-toplevel)
114  rev=$(git show $1:$VERSION \
115      | grep "#define" \
116      | grep "$MAJOR\|$MINOR\|$BUILD\|$PATCH" \
117      | grep -o "[0-9]\+$" \
118      | tr "\\n" ".")
119  echo ${rev%?}
120}
121
122merges_to_branch() {
123  git cherry -v svn/trunk svn/$1 | while read merge; do
124    h=$(echo $merge | cut -d" " -f2)
125    svn=$(svn_rev $1 $h)
126    merges=$(echo $merge | grep -o "r[0-9]\+")
127    rev=$(v8_rev $h)
128    echo $rev r$svn $merges
129  done
130}
131
132url_for() {
133  first=$(svn_rev trunk $(v8_hash $(echo $1 | cut -d":" -f1)))
134  last=$(svn_rev trunk $(v8_hash $(echo $1 | cut -d":" -f2)))
135  num=$[ $last - $first]
136  echo "$BASE_URL?num=$num&start=$last"
137}
138
139########## Option parsing
140
141while getopts ":hi:v:m:p:u:" OPTION ; do
142  case $OPTION in
143    h)  usage
144        exit 0
145        ;;
146    i)  tag_version $OPTARG
147        ;;
148    v)  v8_rev $(svn_rev trunk r$OPTARG)
149        ;;
150    m)  merges_to_branch $OPTARG
151        ;;
152    p)  echo $(point_merges "$(tag_log $(v8_hash $OPTARG)^1)")
153        ;;
154    u)  url_for $OPTARG
155        ;;
156    ?)  echo "Illegal option: -$OPTARG"
157        usage
158        exit 1
159        ;;
160  esac
161done
162