package uk.co.pols.bamboo.gitplugin.client.commands;

import com.atlassian.bamboo.commit.Commit;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class ExecutorGitLogCommand implements GitLogCommand {
    private static final Log log = LogFactory.getLog(ExecutorGitLogCommand.class);

    private String gitExe;
    private File sourceCodeDirectory;
    private String lastRevisionChecked;
    private CommandExecutor commandExecutor;

    public ExecutorGitLogCommand(String gitExe, File sourceCodeDirectory, String lastRevisionChecked, CommandExecutor commandExecutor) {
        this.gitExe = gitExe;
        this.sourceCodeDirectory = sourceCodeDirectory;
        this.lastRevisionChecked = lastRevisionChecked;
        this.commandExecutor = commandExecutor;
    }

    public List<Commit> extractCommits() throws IOException {
        String logText = commandExecutor.execute(getCommandLine(), sourceCodeDirectory);

        log.info(logText);
        GitLogParser logParser = new GitLogParser(logText);

        List<Commit> commits = logParser.extractCommits(lastRevisionChecked);
        lastRevisionChecked = logParser.getMostRecentCommitDate();

        return commits;
    }

    public String getLastRevisionChecked() {
        return lastRevisionChecked;
    }

    private String[] getCommandLine() {
        if (lastRevisionChecked != null) {
            return new String[]{gitExe, "log", "--numstat", "--date=iso8601", "--since=\"" + lastRevisionChecked + "\""};
        }
        return new String[]{gitExe, "log", "-1", "--numstat", "--date=iso8601"};
    }
}
@@ -803,16 +803,60 @@
 hecked =
+ %22null%22.equals(lastRevisionChecked) ? null :
  lastRev
@@ -1505,9 +1505,9 @@
 ked 
-!
+=
 = nu
@@ -1558,16 +1558,107 @@
 , %22log%22,
+ %22-1%22, %22--numstat%22, %22--date=iso8601%22%7D;%0A        %7D%0A        return new String%5B%5D%7BgitExe, %22log%22,
  %22--nums
@@ -1729,99 +1729,8 @@
 %22%7D;%0A
-        %7D%0A        return new String%5B%5D%7BgitExe, %22log%22, %22-1%22, %22--numstat%22, %22--date=iso8601%22%7D;%0A
     

Handle the case where Bamboo's lastRevisionChecked equals the string "null" (probably shouldn't happen, but I was experimenting and something went wrong :)