unshuffle_patch.sh revision 1236:bebfcf0b68ea
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=`dirname $0`
39UNSHUFFLE_LIST=$SCRIPT_DIR"/unshuffle_list.txt"
40
41if [ ! -f "$UNSHUFFLE_LIST" ] ; then
42  echo "FATAL: cannot find $UNSHUFFLE_LIST" >&2
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." >&2
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]." >&2
87  usage
88fi
89
90# Check given input/output files
91input="$2"
92if [ "x$input" = "x-" ] ; then
93  input="/dev/stdin"
94fi
95
96if [ ! -f $input -a "x$input" != "x/dev/stdin" ] ; then
97  echo "ERROR: Cannot find input patch file: $input" >&2
98  exit 1
99fi
100
101output="$3"
102if [ "x$output" = "x-" ] ; then
103  output="/dev/stdout"
104fi
105
106if [ -f $output -a "x$output" != "x/dev/stdout" ] ; then
107  echo "ERROR: Output patch already exists: $output" >&2
108  exit 1
109fi
110
111what=""  ## shuffle or unshuffle
112
113verbose() {
114  if [ ${vflag} = "true" ] ; then
115    echo "$@" >&2
116  fi
117}
118
119unshuffle() {
120  line=$@
121  verbose "Attempting to rewrite: \"$line\""
122
123  # Retrieve the file name
124  path=
125  if echo "$line" | egrep '^diff' > /dev/null ; then
126    if ! echo "$line" | egrep '\-\-git' > /dev/null ; then
127      echo "ERROR: Only git patches supported. Please use 'hg export --git ...'." >&2
128      exit 1
129    fi
130    path="`echo "$line" | sed -e s@'diff --git a/'@@ -e s@' b/.*$'@@`"
131  elif echo "$line" | egrep '^\-\-\-' > /dev/null ; then
132    path="`echo "$line" | sed -e s@'--- a/'@@`"
133  elif echo "$line" | egrep '^\+\+\+' > /dev/null ; then
134    path="`echo "$line" | sed s@'+++ b/'@@`"
135  fi
136  verbose "Extracted path: \"$path\""
137
138  # Only source can be shuffled, or unshuffled
139  if ! echo "$path" | egrep '^src/.*' > /dev/null ; then
140    verbose "Not a src path, skipping."
141    echo "$line" >> $output
142    return
143  fi
144
145  # Shuffle or unshuffle?
146  if [ "${what}" = "" ] ; then
147    if echo "$path" | egrep '^src/java\..*|^src/jdk\..*' > /dev/null ; then
148      what="unshuffle"
149    else
150      what="shuffle"
151    fi
152    verbose "Shuffle or unshuffle: $what"
153  fi
154
155  # Find the most specific matches in the shuffle list
156  matches=
157  matchpath="$repo"/"$path"/x
158  while [ "$matchpath" != "" ] ; do
159    matchpath="`echo $matchpath | sed s@'\(.*\)/.*$'@'\1'@`"
160
161    if [ "${what}" =  "shuffle" ] ; then
162      pattern=": $matchpath$"
163    else
164      pattern="^$matchpath :"
165    fi
166    verbose "Attempting to find \"$matchpath\""
167    matches=`egrep "$pattern" "$UNSHUFFLE_LIST"`
168    if ! [ "x${matches}" = "x" ] ; then
169      verbose "Got matches: [$matches]"
170      break;
171    fi
172
173    if ! echo "$matchpath" | egrep '.*/.*' > /dev/null ; then
174      break;
175    fi
176  done
177
178  # Rewrite the line, if we have a match
179  if ! [ "x${matches}" = "x" ] ; then
180    shuffled="`echo "$matches" | sed -e s@' : .*'@@g -e s@'^[a-z]*\/'@@`"
181    unshuffled="`echo "$matches" | sed -e s@'.* : '@@g -e s@'^[a-z]*\/'@@`"
182    if [ "${what}" =  "shuffle" ] ; then
183      newline="`echo "$line" | sed -e s@"$unshuffled"@"$shuffled"@g`"
184    else
185      newline="`echo "$line" | sed -e s@"$shuffled"@"$unshuffled"@g`"
186    fi
187    verbose "Rewriting to \"$newline\""
188    echo "$newline" >> $output
189  else
190    echo "WARNING: no match found for $path"
191    echo "$line" >> $output
192  fi
193}
194
195while IFS= read -r line
196do
197  if echo "$line" | egrep '^diff|^\-\-\-|^\+\+\+' > /dev/null ; then
198    unshuffle "$line"
199  else
200    printf "%s\n" "$line" >> $output
201  fi
202done < "$input"
203