View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  import java.io.BufferedReader;
20  import java.io.BufferedWriter;
21  import java.io.File;
22  import java.io.FileReader;
23  import java.io.FileWriter;
24  import java.util.HashSet;
25  import java.util.regex.Pattern;
26  
27  public class PartialMergeHelper {
28  
29      static final Pattern fileBreakPattern = Pattern.compile("^Index: ");
30  
31      public static void main(String[] args) throws Exception {
32          String partialMergeDiffFileName = args[0];
33          String completeMergeDiffFileName = args[1];
34          String mergeRemainderDiffFileName = args[2];
35  
36          HashSet<String> filesInPartialMerge = new HashSet<String>();
37  
38          // first, go through the partial merge file and extract all the updated file names
39          // build a list of the file break patterns and save in a HashSet
40          File partialMergeFile = new File( partialMergeDiffFileName );
41          System.out.println( "Partial Merge file: " + partialMergeFile.getAbsolutePath() );
42          BufferedReader r = new BufferedReader(new FileReader( partialMergeFile ) );
43          String line;
44          while ( (line = r.readLine()) != null ) {
45              if ( fileBreakPattern.matcher(line).find() ) {
46                  System.out.println( "Adding to partial merge file list: " + line );
47                  filesInPartialMerge.add(line);
48              }
49          }
50  
51          // second, loop over the complete merge file, suppressing any entries which appear in the
52          // first merge file
53          File inputFile = new File( completeMergeDiffFileName );
54          System.out.println( "Attemping to read from file: " + inputFile.getAbsolutePath() );
55          File outputFile = new File ( mergeRemainderDiffFileName );
56          System.out.println( "Output file: " + outputFile.getAbsolutePath() );
57          r = new BufferedReader(new FileReader( inputFile ) );
58          BufferedWriter w = new BufferedWriter( new FileWriter(outputFile) );
59          boolean inFileSuppressionMode = false;
60          int suppressionLineCount = 0;
61          while ( (line = r.readLine()) != null ) {
62              if ( fileBreakPattern.matcher(line).find() ) {
63  //                if ( inFileSuppressionMode ) {
64  //                    System.out.println( "Completing File Suppression.  Lines Removed: " + suppressionLineCount );
65  //                }
66                  inFileSuppressionMode = false;
67                  System.out.println( "Found File Break: " + line );
68                  if ( filesInPartialMerge.contains(line) ) {
69                      System.out.println( "    HAS ALREADY BEEN MERGED - SUPPRESSING OUTPUT" );
70                      inFileSuppressionMode = true;
71                      suppressionLineCount = 0;
72                  }
73              }
74              if ( !inFileSuppressionMode ) {
75                  w.write(line);
76                  w.newLine();
77              } else {
78                  suppressionLineCount++;
79              }
80          }
81          if ( inFileSuppressionMode ) {
82              System.out.println( "Completing File Suppression.  Lines Removed: " + suppressionLineCount );
83          }
84          r.close();
85          w.close();
86      }
87  
88  }