package de.oppermann.pomutils;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.oppermann.pomutils.util.POM;

/**
 * 
 * @author Sven Oppermann <sven.oppermann@gmail.com>
 * 
 */

public class PomMergeDriver {

	private final Logger logger = LoggerFactory.getLogger(PomMergeDriver.class);

	private POM basePom;
	private POM ourPom;
	private POM theirPom;

	public PomMergeDriver(String basePomFile, String ourPomFile, String theirPomFile) {
		basePom = new POM(basePomFile);
		ourPom = new POM(ourPomFile);
		theirPom = new POM(theirPomFile);
	}

	public void adjustTheirPomVersion() {
		adjustParentVersion();
		adjustProjectVersion();
		theirPom.savePom();
	}

	public int doGitMerge() {
		ProcessBuilder processBuilder = new ProcessBuilder("git", "merge-file", "-L", "our", "-L", "base", "-L", "theirs", ourPom.getPath(),
		        basePom.getPath(), theirPom.getPath());
		processBuilder.redirectErrorStream(true);
		try {
			final Process p = processBuilder.start();

			consumeGitOutput(p);

			return p.waitFor();
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	private void adjustParentVersion() {
		String ourParentVersion = ourPom.getParentVersion();
		String theirParentVersion = theirPom.getParentVersion();

		if (ourParentVersion != null && !ourParentVersion.equals(theirParentVersion)) {
			theirPom.setParentVersion(ourParentVersion);
		}
	}

	private void adjustProjectVersion() {
		String ourVersion = ourPom.getProjectVersion();
		String theirVersion = theirPom.getProjectVersion();

		if (ourVersion != null && !ourVersion.equals(theirVersion)) {
			theirPom.setProjectVersion(ourVersion);
		}
	}

	private void consumeGitOutput(final Process p) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

		logger.debug("Git merge output:");
		logger.debug("=====================================");
		String line;
		while ((line = reader.readLine()) != null) {
			logger.debug(line);
		}
		reader.close();
		logger.debug("=====================================");
	}

}

@@ -853,22 +853,27 @@
 Buffered
-Reader
+InputStream
 ;%0Aimport
@@ -898,42 +898,48 @@
 on;%0A
+%0A
 import 
-java.io.InputStreamReader;%0A
+org.codehaus.plexus.util.IOUtil;
 %0Aimp
@@ -2711,32 +2711,43 @@
 %7B%0A%09%09
-BufferedReader reader = 
+%0A%09%09String output = IOUtil.toString(
 new 
@@ -2758,19 +2758,8 @@
 ered
-Reader(new 
 Inpu
@@ -2769,14 +2769,8 @@
 ream
-Reader
 (p.g
@@ -2784,20 +2784,27 @@
 Stream()
+, 256
 ));%0A
+%09%09
 %0A%09%09logge
@@ -2833,230 +2833,21 @@
 put:
-%22);%0A%09%09logger.debug(%22=====================================%22);%0A%09%09String line;%0A%09%09while ((line = reader.readLine()) != null) %7B%0A%09%09%09logger.debug(line);%0A%09%09%7D%0A%09%09reader.close();%0A%09%09logger.debug(%22=====================================%22
+%5Cn%7B%7D%22, output
 );%0A%09

simplify git output logging