package gitflow;

import com.intellij.execution.ExecutionException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

import git4idea.GitVcs;
import git4idea.commands.GitCommand;
import git4idea.commands.GitLineHandler;


public class GitInitLineHandler extends GitLineHandler {
    @NotNull private final GitVcs myVcs;

    private BufferedWriter writer;
    GitflowInitOptions _initOptions;

    public GitInitLineHandler(GitflowInitOptions initOptions, @NotNull Project project, @NotNull VirtualFile vcsRoot, @NotNull GitCommand command) {
        super(project, vcsRoot, command);
        myVcs = GitVcs.getInstance(project);
        _initOptions = initOptions;
    }

    @Nullable
    @Override
    protected Process startProcess() throws ExecutionException {
        Process p = super.startProcess();
        writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
        return p;
    }

    protected void processTerminated(final int exitCode) {
        super.processTerminated(exitCode);
    }


    @Override
    protected void onTextAvailable(String s, Key key) {
        super.onTextAvailable(s, key);
        try {
            if (s.contains("name for production releases")) {
                writer.write(_initOptions.getProductionBranch());
                myVcs.showCommandLine(_initOptions.getProductionBranch());
                writer.newLine();
                writer.flush();
            }

            if (s.contains("name for \"next release\"")) {
                writer.write(_initOptions.getDevelopmentBranch());
                myVcs.showCommandLine(_initOptions.getDevelopmentBranch());
                writer.newLine();
                writer.flush();
            }


            if (s.contains("Feature branches")) {
                writer.write(_initOptions.getFeaturePrefix());
                myVcs.showCommandLine(_initOptions.getFeaturePrefix());
                writer.newLine();
                writer.flush();
            }
            if (s.contains("Bugfix branches")) {
                writer.write(_initOptions.getBugfixPrefix());
                myVcs.showCommandLine(_initOptions.getBugfixPrefix());
                writer.newLine();
                writer.flush();
            }
            if (s.contains("Release branches")) {
                writer.write(_initOptions.getReleasePrefix());
                myVcs.showCommandLine(_initOptions.getReleasePrefix());
                writer.newLine();
                writer.flush();
            }
            if (s.contains("Hotfix branches")) {
                writer.write(_initOptions.getHotfixPrefix());
                myVcs.showCommandLine(_initOptions.getHotfixPrefix());
                writer.newLine();
                writer.flush();
            }
            if (s.contains("Support branches")) {
                writer.write(_initOptions.getSupportPrefix());
                myVcs.showCommandLine(_initOptions.getSupportPrefix());
                writer.newLine();
                writer.flush();
            }
            if (s.contains("Version tag")) {
                writer.write(_initOptions.getVersionPrefix());
                myVcs.showCommandLine(_initOptions.getVersionPrefix());
                writer.newLine();
                writer.flush();
            }
            if (s.contains("Hooks and filters")) {
                writer.newLine();
                writer.flush();
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

@@ -185,24 +185,65 @@
 irtualFile;%0A
+import git4idea.util.GitVcsConsoleWriter;
 %0Aimport org.
@@ -416,32 +416,8 @@
 r;%0A%0A
-import git4idea.GitVcs;%0A
 impo
@@ -553,25 +553,16 @@
 r %7B%0A    
-@NotNull 
 private 
@@ -573,22 +573,43 @@
 l GitVcs
- myVcs
+ConsoleWriter consoleWriter
 ;%0A%0A    p
@@ -737,16 +737,28 @@
 Options,
+%0A           
  @NotNul
@@ -805,16 +805,28 @@
 vcsRoot,
+%0A           
  @NotNul
@@ -903,22 +903,43 @@
     
-myVcs = GitVcs
+consoleWriter = GitVcsConsoleWriter
 .get
@@ -1246,118 +1246,8 @@
  %7D%0A%0A
-    protected void processTerminated(final int exitCode) %7B%0A        super.processTerminated(exitCode);%0A    %7D%0A%0A%0A
     
@@ -1439,35 +1439,52 @@
             
-writer.writ
+consoleWriter.showCommandLin
 e(_initOptio
@@ -1502,32 +1502,33 @@
 ctionBranch());%0A
+%0A
                 
@@ -1527,36 +1527,27 @@
         
-myVcs.showCommandLin
+writer.writ
 e(_initO
@@ -1729,35 +1729,52 @@
             
-writer.writ
+consoleWriter.showCommandLin
 e(_initOptio
@@ -1793,32 +1793,33 @@
 pmentBranch());%0A
+%0A
                 
@@ -1818,36 +1818,27 @@
         
-myVcs.showCommandLin
+writer.writ
 e(_initO
@@ -1942,33 +1942,32 @@
 %0A            %7D%0A%0A
-%0A
             if (
@@ -2012,35 +2012,52 @@
             
-writer.writ
+consoleWriter.showCommandLin
 e(_initOptio
@@ -2072,32 +2072,33 @@
 aturePrefix());%0A
+%0A
                 
@@ -2097,36 +2097,27 @@
         
-myVcs.showCommandLin
+writer.writ
 e(_initO
@@ -2285,35 +2285,52 @@
             
-writer.writ
+consoleWriter.showCommandLin
 e(_initOptio
@@ -2344,32 +2344,33 @@
 ugfixPrefix());%0A
+%0A
                 
@@ -2369,36 +2369,27 @@
         
-myVcs.showCommandLin
+writer.writ
 e(_initO
@@ -2557,35 +2557,52 @@
             
-writer.writ
+consoleWriter.showCommandLin
 e(_initOptio
@@ -2617,32 +2617,33 @@
 leasePrefix());%0A
+%0A
                 
@@ -2642,36 +2642,27 @@
         
-myVcs.showCommandLin
+writer.writ
 e(_initO
@@ -2830,35 +2830,52 @@
             
-writer.writ
+consoleWriter.showCommandLin
 e(_initOptio
@@ -2889,32 +2889,33 @@
 otfixPrefix());%0A
+%0A
                 
@@ -2914,36 +2914,27 @@
         
-myVcs.showCommandLin
+writer.writ
 e(_initO
@@ -3102,35 +3102,52 @@
             
-writer.writ
+consoleWriter.showCommandLin
 e(_initOptio
@@ -3162,32 +3162,33 @@
 pportPrefix());%0A
+%0A
                 
@@ -3187,36 +3187,27 @@
         
-myVcs.showCommandLin
+writer.writ
 e(_initO
@@ -3371,35 +3371,52 @@
             
-writer.writ
+consoleWriter.showCommandLin
 e(_initOptio
@@ -3431,32 +3431,33 @@
 rsionPrefix());%0A
+%0A
                 
@@ -3460,28 +3460,19 @@
     
-myVcs.showCommandLin
+writer.writ
 e(_i

Fix deprecation warnings