package de.tum.in.www1.exerciseapp.service;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;

@Service
@Transactional
public class GitService {

    private final Logger log = LoggerFactory.getLogger(GitService.class);

    @Value("${exerciseapp.bitbucket.url}")
    private URL BITBUCKET_SERVER;

    @Value("${exerciseapp.bitbucket.user}")
    private String BITBUCKET_USER;

    @Value("${exerciseapp.bitbucket.password}")
    private String BITBUCKET_PASSWORD;

    /**
     * Checks out the repository with the given URL to the file system
     */
    public Git checkoutRepository(String projectKey, String repoUrl) {
        File repo = new File("/home/muench/exercise-application/repos/" + projectKey);
        if (!Files.exists(repo.toPath())) {
            System.out.println("Repo doesn't exist, cloning...");
            try {
                Git.cloneRepository()
                    .setURI(repoUrl)
                    .setCredentialsProvider(new UsernamePasswordCredentialsProvider(BITBUCKET_USER, BITBUCKET_PASSWORD))
                    .setDirectory(repo).call();
            } catch (GitAPIException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("Repo exists.");
        }
        try {
            return new Git(new FileRepository(repo.getPath() + "/.git"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void doEmptyCommit(String projectKey, String newRemote) {
        Git git = checkoutRepository(projectKey, newRemote);
        try {
            StoredConfig config = git.getRepository().getConfig();
            config.setString("remote", "origin", "url", newRemote);
            config.save();
            git.commit().setMessage("Setup").setAllowEmpty(true).call();
            git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(BITBUCKET_USER, BITBUCKET_PASSWORD)).call();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (GitAPIException e) {
            e.printStackTrace();
        }
    }
}

@@ -38,16 +38,56 @@
 rvice;%0A%0A
+import org.apache.commons.io.FileUtils;%0A
 import o
@@ -664,16 +664,39 @@
 e.Files;
+%0Aimport java.util.UUID;
 %0A%0A@Servi
@@ -1159,19 +1159,20 @@
  public 
-Git
+File
  checkou
@@ -1367,32 +1367,39 @@
     
-System.out.println(%22Repo
+log.info(%22Repository for key %7B%7D
  doe
@@ -1421,16 +1421,28 @@
 ning...%22
+, projectKey
 );%0A     
@@ -1824,39 +1824,37 @@
     
-System.out.println(%22Repo exists
+log.info(%22Repo already cloned
 .%22);
@@ -1876,336 +1876,430 @@
     
-try %7B
+return repo;
 %0A    
- 
+%7D%0A%0A
     
-   return new Git(new FileRepository(repo.getPath() + %22/.git%22));%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return null;%0A    %7D%0A%0A    public void doEmptyCommit(String projectKey, String newRemote) %7B%0A        Git git = checkoutRepository(projectKey, newRemote);%0A        try %7B
+public void doEmptyCommit(String projectKey, String newRemote) %7B%0A        File sourceFolder = checkoutRepository(projectKey, newRemote);%0A        try %7B%0A            File tmpFolder = new File(sourceFolder.getParentFile().getPath() + %22/%22 + UUID.randomUUID());%0A            FileUtils.copyDirectory(sourceFolder, tmpFolder);%0A            Git git = new Git(new FileRepository(tmpFolder.getPath() + %22/.git%22));
 %0A   
@@ -2653,32 +2653,82 @@
 SWORD)).call();%0A
+            FileUtils.deleteDirectory(tmpFolder);%0A
         %7D catch 

Copy repository folder before changing remote while doing empty commit workaround