package uk.me.viv.logmyride;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.PushCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.RemoteRefUpdate;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;


public class GitSite {

    private static final String BLANK_PASSWORD = "";

    private final String user;
    private final String email;
    private final String token;
    private final String remoteURL;

    public GitSite(String user, String email, String token, String remoteURL) {
        this.user = user;
        this.email = email;
        this.token = token;
        this.remoteURL = remoteURL;
    }

    public void update() {
        try {
            System.out.println("Preparing destination folder for cloned repository");
            File localPath = File.createTempFile("TestGitRepository", "");
            localPath.delete();

            System.out.println("Cloning " + this.remoteURL + " into " + localPath);
            Git result = Git.cloneRepository()
                    .setURI(this.remoteURL)
                    .setDirectory(localPath)
                    .call();
            result.close();

            Repository repository = result.getRepository();
            Git git = new Git(repository);

            System.out.println("Creating test file");
            File myfile = new File(repository.getDirectory().getParent(), "testfile");
            myfile.createNewFile();

            System.out.println("Adding test file");
            git.add()
                    .addFilepattern("testfile")
                    .call();

            git.commit()
                    .setCommitter(this.user, this.email)
                    .setMessage("Added testfile")
                    .call();

            System.out.println("Committed file " + myfile + " to repository at " + repository.getDirectory());

            System.out.println("Pushing to remote");
            CredentialsProvider cp = new UsernamePasswordCredentialsProvider(this.token, BLANK_PASSWORD);            
            PushCommand pc = git.push();
            pc.setCredentialsProvider(cp)
                    .setPushAll();
            try {
                Iterator<PushResult> it = pc.call().iterator();
                if(it.hasNext()){
                    PushResult pr = it.next();                    
                    for (RemoteRefUpdate ru : pr.getRemoteUpdates() ) {
                        System.out.println("Push Status: " + ru.getStatus());
                        System.out.println("Updated: " + ru.getTrackingRefUpdate().getLocalName());
                    }
                }
            } catch (InvalidRemoteException e) {
                e.printStackTrace();
            }

            repository.close();
            System.out.println("Removing local repository");
            FileUtils.deleteDirectory(localPath);

        }   catch (GitAPIException | IOException ex) {
            Logger.getLogger(GitSite.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
@@ -733,16 +733,101 @@
  = %22%22;%0A%0A
+    private static final Logger LOGGER = Logger.getLogger(GitSite.class.getName());%0A%0A
     priv
@@ -1208,34 +1208,27 @@
         
-System.out.println
+LOGGER.info
 (%22Prepar
@@ -1395,34 +1395,27 @@
         
-System.out.println
+LOGGER.info
 (%22Clonin
@@ -1770,34 +1770,27 @@
         
-System.out.println
+LOGGER.info
 (%22Creati
@@ -1941,34 +1941,27 @@
         
-System.out.println
+LOGGER.info
 (%22Adding
@@ -2248,34 +2248,27 @@
         
-System.out.println
+LOGGER.info
 (%22Commit
@@ -2353,34 +2353,27 @@
         
-System.out.println
+LOGGER.info
 (%22Pushin
@@ -2902,34 +2902,27 @@
         
-System.out.println
+LOGGER.info
 (%22Push S
@@ -2973,34 +2973,27 @@
         
-System.out.println
+LOGGER.info
 (%22Update
@@ -3231,26 +3231,19 @@
     
-System.out.println
+LOGGER.info
 (%22Re
@@ -3392,48 +3392,13 @@
    L
-ogger.getLogger(GitSite.class.getName())
+OGGER
 .log

Use logger for status output