unshuffle_patch.sh revision 1120:ba5645f2735b
1#!/bin/sh
2#
3# Copyright (c) 2014, 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] <repo> <input_patch> <output_patch>"
29      echo "where:"
30      echo "  <repo>          is one of: corba, jaxp, jaxws, jdk, langtools, nashorn"
31      echo "                  [Note: patches from other repos do not need updating]"
32      echo "  <input_patch>   is the input patch file, that needs shuffling/unshuffling"
33      echo "  <output_patch>  is the updated patch file "
34      echo " "
35      exit 1
36}
37
38SCRIPT_DIR=`pwd`/`dirname $0`
39UNSHUFFLE_LIST=$SCRIPT_DIR"/unshuffle_list.txt"
40
41if [ ! -f "$UNSHUFFLE_LIST" ] ; then
42  echo "FATAL: cannot find $UNSHUFFLE_LIST"
43  exit 1
44fi
45
46vflag="false"
47while [ $# -gt 0 ]
48do
49  case $1 in
50    -h | --help )
51      usage
52      ;;
53
54    -v | --verbose )
55      vflag="true"
56      ;;
57
58    -*)  # bad option
59      usage
60      ;;
61
62     * )  # non option
63      break
64      ;;
65  esac
66  shift
67done
68
69# Make sure we have the right number of arguments
70if [ ! $# -eq 3 ] ; then
71  echo "ERROR: Invalid number of arguments."
72  usage
73fi
74
75# Check the given repo
76repos="corba jaxp jaxws jdk langtools nashorn"
77repo="$1"
78found="false"
79for r in $repos ; do
80  if [ $repo = "$r" ] ; then
81    found="true"
82    break;
83  fi 
84done
85if [ $found = "false" ] ; then
86  echo "ERROR: Unknown repo: $repo. Should be one of [$repos]."
87  usage
88fi
89
90# Check given input/output files
91input="$2"
92output="$3"
93
94if [ ! -f $input ] ; then
95  echo "ERROR: Cannot find input patch file: $input"
96  exit 1
97fi
98
99if [ -f $output ] ; then
100  echo "ERROR: Output patch already exists: $output"
101  exit 1
102fi
103
104what=""  ## shuffle or unshuffle
105
106verbose() {
107  if [ ${vflag} = "true" ] ; then
108    echo "$@"
109  fi
110}
111
112unshuffle() {
113  line=$@
114  verbose "Attempting to rewrite: \"$line\""
115
116  # Retrieve the file name
117  path=
118  if echo "$line" | egrep '^diff' > /dev/null ; then
119    if ! echo "$line" | egrep '\-\-git' > /dev/null ; then
120      echo "ERROR: Only git patches supported. Please use 'hg export --git ...'."
121      exit 1
122    fi
123    path="`echo "$line" | sed -e s@'diff --git a/'@@ -e s@' b/.*$'@@`"
124  elif echo "$line" | egrep '^\-\-\-' > /dev/null ; then
125    path="`echo "$line" | sed -e s@'--- a/'@@`"
126  elif echo "$line" | egrep '^\+\+\+' > /dev/null ; then
127    path="`echo "$line" | sed s@'+++ b/'@@`"
128  fi
129  verbose "Extracted path: \"$path\""
130
131  # Only source can be shuffled, or unshuffled
132  if ! echo "$path" | egrep '^src/.*' > /dev/null ; then
133    verbose "Not a src path, skipping."
134    echo "$line" >> $output
135    return
136  fi
137
138  # Shuffle or unshuffle?
139  if [ "${what}" = "" ] ; then
140    if echo "$path" | egrep '^src/java\..*|^src/jdk\..*' > /dev/null ; then
141      what="unshuffle"
142    else
143      what="shuffle"
144    fi
145    verbose "Shuffle or unshuffle: $what"
146  fi
147
148  # Find the most specific matches in the shuffle list
149  matches=
150  matchpath="$repo"/"$path"/x
151  while [ "$matchpath" != "" ] ; do
152    matchpath="`echo $matchpath | sed s@'\(.*\)/.*$'@'\1'@`"
153
154    if [ "${what}" =  "shuffle" ] ; then
155      pattern=": $matchpath$"
156    else
157      pattern="^$matchpath :"
158    fi
159    verbose "Attempting to find \"$matchpath\""
160    matches=`egrep "$pattern" "$UNSHUFFLE_LIST"`
161    if ! [ "x${matches}" = "x" ] ; then
162      verbose "Got matches: [$matches]"
163      break;
164    fi
165
166    if ! echo "$matchpath" | egrep '.*/.*' > /dev/null ; then
167      break;
168    fi
169  done
170
171  # Rewrite the line, if we have a match
172  if ! [ "x${matches}" = "x" ] ; then
173    shuffled="`echo "$matches" | sed -e s@' : .*'@@g -e s@'^[a-z]*\/'@@`"
174    unshuffled="`echo "$matches" | sed -e s@'.* : '@@g -e s@'^[a-z]*\/'@@`"
175    if [ "${what}" =  "shuffle" ] ; then
176      newline="`echo "$line" | sed -e s@"$unshuffled"@"$shuffled"@g`"
177    else
178      newline="`echo "$line" | sed -e s@"$shuffled"@"$unshuffled"@g`"
179    fi
180    verbose "Rewriting to \"$newline\""
181    echo "$newline" >> $output
182  else
183    echo "WARNING: no match found for $path"
184    echo "$line" >> $output
185  fi
186}
187
188while IFS= read -r line
189do
190  if echo "$line" | egrep '^diff|^\-\-\-|^\+\+\+' > /dev/null ; then
191    unshuffle "$line"
192  else
193    printf "%s\n" "$line" >> $output
194  fi
195done < "$input"
196
197