001/*
002 * The Kuali Financial System, a comprehensive financial management system for higher education.
003 * 
004 * Copyright 2005-2014 The Kuali Foundation
005 * 
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Affero General Public License as
008 * published by the Free Software Foundation, either version 3 of the
009 * License, or (at your option) any later version.
010 * 
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Affero General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Affero General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019import java.io.BufferedReader;
020import java.io.BufferedWriter;
021import java.io.File;
022import java.io.FileReader;
023import java.io.FileWriter;
024import java.util.HashSet;
025import java.util.regex.Pattern;
026
027public class PartialMergeHelper {
028
029    static final Pattern fileBreakPattern = Pattern.compile("^Index: ");
030
031    public static void main(String[] args) throws Exception {
032        String partialMergeDiffFileName = args[0];
033        String completeMergeDiffFileName = args[1];
034        String mergeRemainderDiffFileName = args[2];
035
036        HashSet<String> filesInPartialMerge = new HashSet<String>();
037
038        // first, go through the partial merge file and extract all the updated file names
039        // build a list of the file break patterns and save in a HashSet
040        File partialMergeFile = new File( partialMergeDiffFileName );
041        System.out.println( "Partial Merge file: " + partialMergeFile.getAbsolutePath() );
042        BufferedReader r = new BufferedReader(new FileReader( partialMergeFile ) );
043        String line;
044        while ( (line = r.readLine()) != null ) {
045            if ( fileBreakPattern.matcher(line).find() ) {
046                System.out.println( "Adding to partial merge file list: " + line );
047                filesInPartialMerge.add(line);
048            }
049        }
050
051        // second, loop over the complete merge file, suppressing any entries which appear in the
052        // first merge file
053        File inputFile = new File( completeMergeDiffFileName );
054        System.out.println( "Attemping to read from file: " + inputFile.getAbsolutePath() );
055        File outputFile = new File ( mergeRemainderDiffFileName );
056        System.out.println( "Output file: " + outputFile.getAbsolutePath() );
057        r = new BufferedReader(new FileReader( inputFile ) );
058        BufferedWriter w = new BufferedWriter( new FileWriter(outputFile) );
059        boolean inFileSuppressionMode = false;
060        int suppressionLineCount = 0;
061        while ( (line = r.readLine()) != null ) {
062            if ( fileBreakPattern.matcher(line).find() ) {
063//                if ( inFileSuppressionMode ) {
064//                    System.out.println( "Completing File Suppression.  Lines Removed: " + suppressionLineCount );
065//                }
066                inFileSuppressionMode = false;
067                System.out.println( "Found File Break: " + line );
068                if ( filesInPartialMerge.contains(line) ) {
069                    System.out.println( "    HAS ALREADY BEEN MERGED - SUPPRESSING OUTPUT" );
070                    inFileSuppressionMode = true;
071                    suppressionLineCount = 0;
072                }
073            }
074            if ( !inFileSuppressionMode ) {
075                w.write(line);
076                w.newLine();
077            } else {
078                suppressionLineCount++;
079            }
080        }
081        if ( inFileSuppressionMode ) {
082            System.out.println( "Completing File Suppression.  Lines Removed: " + suppressionLineCount );
083        }
084        r.close();
085        w.close();
086    }
087
088}