unshuffle_patch.sh revision 1180:654b0f026152
1152909Sanholt#!/bin/sh
2145132Sanholt#
3152909Sanholt# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
4145132Sanholt# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5145132Sanholt#
6182080Srnoland# This code is free software; you can redistribute it and/or modify it
7152909Sanholt# under the terms of the GNU General Public License version 2 only, as
8152909Sanholt# published by the Free Software Foundation.
9152909Sanholt#
10152909Sanholt# This code is distributed in the hope that it will be useful, but WITHOUT
11152909Sanholt# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12152909Sanholt# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13152909Sanholt# version 2 for more details (a copy is included in the LICENSE file that
14182080Srnoland# accompanied this code).
15152909Sanholt#
16152909Sanholt# You should have received a copy of the GNU General Public License version
17152909Sanholt# 2 along with this work; if not, write to the Free Software Foundation,
18182080Srnoland# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19152909Sanholt#
20152909Sanholt# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21152909Sanholt# or visit www.oracle.com if you need additional information or have any
22152909Sanholt# questions.
23152909Sanholt#
24152909Sanholt
25152909Sanholt# Script for updating a patch file as per the shuffled/unshuffled source location.
26182080Srnoland
27152909Sanholtusage() {
28145132Sanholt      echo "Usage: $0 [-h|--help] [-v|--verbose] <repo> <input_patch> <output_patch>"
29152909Sanholt      echo "where:"
30152909Sanholt      echo "  <repo>          is one of: corba, jaxp, jaxws, jdk, langtools, nashorn"
31145132Sanholt      echo "                  [Note: patches from other repos do not need updating]"
32152909Sanholt      echo "  <input_patch>   is the input patch file, that needs shuffling/unshuffling"
33152909Sanholt      echo "  <output_patch>  is the updated patch file "
34152909Sanholt      echo " "
35152909Sanholt      exit 1
36152909Sanholt}
37145132Sanholt
38145132SanholtSCRIPT_DIR=`dirname $0`
39189049SrnolandUNSHUFFLE_LIST=$SCRIPT_DIR"/unshuffle_list.txt"
40189049Srnoland
41189049Srnolandif [ ! -f "$UNSHUFFLE_LIST" ] ; then
42189049Srnoland  echo "FATAL: cannot find $UNSHUFFLE_LIST" >&2
43189049Srnoland  exit 1
44189049Srnolandfi
45183573Srnoland
46190021Srnolandvflag="false"
47190021Srnolandwhile [ $# -gt 0 ]
48183573Srnolanddo
49189049Srnoland  case $1 in
50190021Srnoland    -h | --help )
51189049Srnoland      usage
52189049Srnoland      ;;
53190021Srnoland
54190021Srnoland    -v | --verbose )
55189049Srnoland      vflag="true"
56190021Srnoland      ;;
57190021Srnoland
58190021Srnoland    -*)  # bad option
59183573Srnoland      usage
60189049Srnoland      ;;
61183573Srnoland
62189049Srnoland     * )  # non option
63183573Srnoland      break
64183573Srnoland      ;;
65183573Srnoland  esac
66183573Srnoland  shift
67183573Srnolanddone
68183573Srnoland
69183573Srnoland# Make sure we have the right number of arguments
70183573Srnolandif [ ! $# -eq 3 ] ; then
71189049Srnoland  echo "ERROR: Invalid number of arguments." >&2
72183573Srnoland  usage
73189049Srnolandfi
74183573Srnoland
75183573Srnoland# Check the given repo
76183573Srnolandrepos="corba jaxp jaxws jdk langtools nashorn"
77183573Srnolandrepo="$1"
78183573Srnolandfound="false"
79183573Srnolandfor r in $repos ; do
80183573Srnoland  if [ $repo = "$r" ] ; then
81189049Srnoland    found="true"
82189049Srnoland    break;
83145132Sanholt  fi 
84189049Srnolanddone
85189049Srnolandif [ $found = "false" ] ; then
86189049Srnoland  echo "ERROR: Unknown repo: $repo. Should be one of [$repos]." >&2
87189049Srnoland  usage
88189049Srnolandfi
89189049Srnoland
90145132Sanholt# Check given input/output files
91189049Srnolandinput="$2"
92189049Srnolandif [ "x$input" = "x-" ] ; then
93189049Srnoland  input="/dev/stdin"
94189049Srnolandfi
95189049Srnoland
96145132Sanholtif [ ! -f $input -a "x$input" != "x/dev/stdin" ] ; then
97189049Srnoland  echo "ERROR: Cannot find input patch file: $input" >&2
98189049Srnoland  exit 1
99189049Srnolandfi
100189049Srnoland
101189049Srnolandoutput="$3"
102182080Srnolandif [ "x$output" = "x-" ] ; then
103157617Sanholt  output="/dev/stdout"
104189049Srnolandfi
105189049Srnoland
106182080Srnolandif [ -f $output -a "x$output" != "x/dev/stdout" ] ; then
107189049Srnoland  echo "ERROR: Output patch already exists: $output" >&2
108189049Srnoland  exit 1
109189049Srnolandfi
110189049Srnoland
111189049Srnolandwhat=""  ## shuffle or unshuffle
112189049Srnoland
113189049Srnolandverbose() {
114182080Srnoland  if [ ${vflag} = "true" ] ; then
115145132Sanholt    echo "$@" >&2
116182080Srnoland  fi
117182080Srnoland}
118182080Srnoland
119182080Srnolandunshuffle() {
120182080Srnoland  line=$@
121182080Srnoland  verbose "Attempting to rewrite: \"$line\""
122182080Srnoland
123182080Srnoland  # Retrieve the file name
124182080Srnoland  path=
125182080Srnoland  if echo "$line" | egrep '^diff' > /dev/null ; then
126182080Srnoland    if ! echo "$line" | egrep '\-\-git' > /dev/null ; then
127182080Srnoland      echo "ERROR: Only git patches supported. Please use 'hg export --git ...'." >&2
128182080Srnoland      exit 1
129182080Srnoland    fi
130182080Srnoland    path="`echo "$line" | sed -e s@'diff --git a/'@@ -e s@' b/.*$'@@`"
131182080Srnoland  elif echo "$line" | egrep '^\-\-\-' > /dev/null ; then
132182080Srnoland    path="`echo "$line" | sed -e s@'--- a/'@@`"
133182080Srnoland  elif echo "$line" | egrep '^\+\+\+' > /dev/null ; then
134182080Srnoland    path="`echo "$line" | sed s@'+++ b/'@@`"
135182080Srnoland  fi
136182080Srnoland  verbose "Extracted path: \"$path\""
137189049Srnoland
138189049Srnoland  # Only source can be shuffled, or unshuffled
139189049Srnoland  if ! echo "$path" | egrep '^src/.*' > /dev/null ; then
140189049Srnoland    verbose "Not a src path, skipping."
141182080Srnoland    echo "$line" >> $output
142182080Srnoland    return
143182080Srnoland  fi
144182080Srnoland
145182080Srnoland  # Shuffle or unshuffle?
146182080Srnoland  if [ "${what}" = "" ] ; then
147182080Srnoland    if echo "$path" | egrep '^src/java\..*|^src/jdk\..*' > /dev/null ; then
148182080Srnoland      what="unshuffle"
149182080Srnoland    else
150182080Srnoland      what="shuffle"
151194986Srnoland    fi
152189049Srnoland    verbose "Shuffle or unshuffle: $what"
153182080Srnoland  fi
154182080Srnoland
155182080Srnoland  # Find the most specific matches in the shuffle list
156182080Srnoland  matches=
157182080Srnoland  matchpath="$repo"/"$path"/x
158182080Srnoland  while [ "$matchpath" != "" ] ; do
159182080Srnoland    matchpath="`echo $matchpath | sed s@'\(.*\)/.*$'@'\1'@`"
160182080Srnoland
161182080Srnoland    if [ "${what}" =  "shuffle" ] ; then
162182080Srnoland      pattern=": $matchpath$"
163182080Srnoland    else
164182080Srnoland      pattern="^$matchpath :"
165182080Srnoland    fi
166182080Srnoland    verbose "Attempting to find \"$matchpath\""
167182080Srnoland    matches=`egrep "$pattern" "$UNSHUFFLE_LIST"`
168182080Srnoland    if ! [ "x${matches}" = "x" ] ; then
169182080Srnoland      verbose "Got matches: [$matches]"
170182080Srnoland      break;
171182080Srnoland    fi
172182080Srnoland
173182080Srnoland    if ! echo "$matchpath" | egrep '.*/.*' > /dev/null ; then
174194540Srnoland      break;
175189054Srnoland    fi
176189054Srnoland  done
177189054Srnoland
178189054Srnoland  # Rewrite the line, if we have a match
179189054Srnoland  if ! [ "x${matches}" = "x" ] ; then
180194986Srnoland    shuffled="`echo "$matches" | sed -e s@' : .*'@@g -e s@'^[a-z]*\/'@@`"
181189054Srnoland    unshuffled="`echo "$matches" | sed -e s@'.* : '@@g -e s@'^[a-z]*\/'@@`"
182189054Srnoland    if [ "${what}" =  "shuffle" ] ; then
183189054Srnoland      newline="`echo "$line" | sed -e s@"$unshuffled"@"$shuffled"@g`"
184189054Srnoland    else
185189054Srnoland      newline="`echo "$line" | sed -e s@"$shuffled"@"$unshuffled"@g`"
186189054Srnoland    fi
187182080Srnoland    verbose "Rewriting to \"$newline\""
188182080Srnoland    echo "$newline" >> $output
189182080Srnoland  else
190182080Srnoland    echo "WARNING: no match found for $path"
191189049Srnoland    echo "$line" >> $output
192189049Srnoland  fi
193190021Srnoland}
194190021Srnoland
195190021Srnolandwhile IFS= read -r line
196189049Srnolanddo
197190021Srnoland  if echo "$line" | egrep '^diff|^\-\-\-|^\+\+\+' > /dev/null ; then
198183831Srnoland    unshuffle "$line"
199190021Srnoland  else
200194986Srnoland    printf "%s\n" "$line" >> $output
201190021Srnoland  fi
202190021Srnolanddone < "$input"
203190021Srnoland
204190021Srnoland