1#!/bin/bash
2#
3# Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
4# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5#
6# This code is free software; you can redistribute it and/or modify it
7# under the terms of the GNU General Public License version 2 only, as
8# published by the Free Software Foundation.
9#
10# This code is distributed in the hope that it will be useful, but WITHOUT
11# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13# version 2 for more details (a copy is included in the LICENSE file that
14# accompanied this code).
15#
16# You should have received a copy of the GNU General Public License version
17# 2 along with this work; if not, write to the Free Software Foundation,
18# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21# or visit www.oracle.com if you need additional information or have any
22# questions.
23#
24
25# Script for updating a patch file as per the shuffled/unshuffled source location.
26
27usage() {
28  echo "Usage: $0 [-h|--help] [-v|--verbose] [-to9|-to10] [-r <repo>] <input_patch> <output_patch>"
29  echo "where:"
30  echo "  -to9            create patches appropriate for a JDK 9 source tree"
31  echo "                  When going to 9, the output patches will be suffixed with the"
32  echo "                  repo name"
33  echo "  -to10           create patches appropriate for a JDK 10 source tree"
34  echo "  -r <repo>       specify repo for source patch, set to 'top' for top repo"
35  echo "  <input_patch>   is the input patch file, that needs shuffling/unshuffling"
36  echo "  <output_patch>  is the updated patch file "
37  echo " "
38  exit 1
39}
40
41SCRIPT_DIR=`dirname $0`
42UNSHUFFLE_LIST=$SCRIPT_DIR"/unshuffle_list.txt"
43
44if [ ! -f "$UNSHUFFLE_LIST" ] ; then
45  echo "FATAL: cannot find $UNSHUFFLE_LIST" >&2
46  exit 1
47fi
48
49vflag="false"
50while [ $# -gt 0 ]
51do
52  case $1 in
53    -h | --help )
54      usage
55      ;;
56
57    -v | --verbose )
58      vflag="true"
59      ;;
60
61    -r)
62      repo="$2"
63      shift
64      ;;
65
66    -to9)
67      shuffle_to=9
68      ;;
69
70    -to10)
71      shuffle_to=10
72      ;;
73
74    -*)  # bad option
75      usage
76      ;;
77
78    * )  # non option
79      break
80      ;;
81  esac
82  shift
83done
84
85# Make sure we have the right number of arguments
86if [ ! $# -eq 2 ] ; then
87  echo "ERROR: Invalid number of arguments." >&2
88  usage
89fi
90
91# Check the given repo
92repos="top corba jaxp jaxws jdk langtools nashorn hotspot"
93found="false"
94if [ -n "$repo" ]; then
95  for r in $repos ; do
96    if [ $repo = "$r" ] ; then
97      found="true"
98      break;
99    fi
100  done
101  if [ $found = "false" ] ; then
102    echo "ERROR: Unknown repo: $repo. Should be one of [$repos]." >&2
103    usage
104  fi
105fi
106
107if [ "$shuffle_to" != "9" -a "$shuffle_to" != "10" ]; then
108  echo "ERROR: Must pick either -to9 or -to10"
109  exit 1
110fi
111
112# When going to 10, a repo must be specified for the source patch
113if [ "$shuffle_to" = "10" -a -z "$repo" ]; then
114  echo "ERROR: Must specify src repo for JDK 9 patch"
115  exit 1
116fi
117
118# Check given input/output files
119input="$1"
120if [ "x$input" = "x-" ] ; then
121  input="/dev/stdin"
122fi
123
124if [ ! -f $input -a "x$input" != "x/dev/stdin" ] ; then
125  echo "ERROR: Cannot find input patch file: $input" >&2
126  exit 1
127fi
128
129output="$2"
130if [ "x$output" = "x-" ] ; then
131  output="/dev/stdout"
132fi
133base_output="$output"
134
135if [ "$shuffle_to" = "10" ]; then
136  if [ -f $output -a "x$output" != "x/dev/stdout" ] ; then
137    echo "ERROR: Output patch already exists: $output" >&2
138    exit 1
139  fi
140else
141  for r in $repos; do
142    if [ -f "$output.$r" ]; then
143      echo "ERROR: Output patch already exists: $output.$r" >&2
144      exit 1
145    fi
146  done
147fi
148
149verbose() {
150  if [ ${vflag} = "true" ] ; then
151    echo "$@" >&2
152  fi
153}
154
155unshuffle() {
156  line=$@
157  verbose "Attempting to rewrite: \"$line\""
158
159  # Retrieve the file name
160  path=
161  if echo "$line" | egrep '^diff' > /dev/null ; then
162    if ! echo "$line" | egrep '\-\-git' > /dev/null ; then
163      echo "ERROR: Only git patches supported. Please use 'hg export --git ...'." >&2
164      exit 1
165    fi
166    path="`echo "$line" | sed -e s@'diff --git a/'@@ -e s@' b/.*$'@@`"
167  elif echo "$line" | egrep '^\-\-\-' > /dev/null ; then
168    path="`echo "$line" | sed -e s@'--- a/'@@`"
169  elif echo "$line" | egrep '^\+\+\+' > /dev/null ; then
170    path="`echo "$line" | sed s@'+++ b/'@@`"
171  fi
172  verbose "Extracted path: \"$path\""
173
174  # Find the most specific matches in the shuffle list
175  matches=
176  if [ -n "$repo" -a "$repo" != "top" ]; then
177    matchpath="$repo"/"$path"/x
178  else
179    matchpath="$path"/x
180  fi
181  while [ "$matchpath" != "" ] ; do
182    matchpath="`echo $matchpath | sed s@'\(.*\)/.*$'@'\1'@`"
183
184    if [ "$shuffle_to" =  "10" ] ; then
185      pattern=": $matchpath$"
186    else
187      pattern="^$matchpath :"
188    fi
189    verbose "Attempting to find \"$matchpath\""
190    matches=`egrep "$pattern" "$UNSHUFFLE_LIST"`
191    if ! [ "x${matches}" = "x" ] ; then
192      verbose "Got matches: [$matches]"
193      break;
194    fi
195
196    if ! echo "$matchpath" | egrep '.*/.*' > /dev/null ; then
197      break;
198    fi
199  done
200
201  # Rewrite the line, if we have a match
202  if ! [ "x${matches}" = "x" ] ; then
203    shuffled="${matches%% : *}"
204    unshuffled="${matches#* : }"
205    patch_suffix_9=""
206    for r in $repos; do
207      if [ "$unshuffled" != "${unshuffled#$r}" ]; then
208        unshuffled="${unshuffled#$r\/}"
209        patch_suffix_9=".$r"
210      fi
211    done
212    verbose "shuffled: $shuffled"
213    verbose "unshuffled: $unshuffled"
214    verbose "patch_suffix_9: $patch_suffix_9"
215    if [ "$shuffle_to" =  "10" ] ; then
216      newline="`echo "$line" | sed -e s@"$unshuffled"@"$shuffled"@g`"
217    else
218      newline="`echo "$line" | sed -e s@"$shuffled"@"$unshuffled"@g`"
219      output=$base_output$patch_suffix_9
220      verbose "Writing to $output"
221    fi
222    verbose "Rewriting to \"$newline\""
223    echo "$newline" >> $output
224  else
225    echo "WARNING: no match found for $path"
226    echo "$line" >> $output
227  fi
228}
229
230while IFS= read -r line
231do
232  if echo "$line" | egrep '^diff|^\-\-\-|^\+\+\+' > /dev/null ; then
233    unshuffle "$line"
234  else
235    printf "%s\n" "$line" >> $output
236  fi
237done < "$input"
238