package io.bdrc.xmltoldmigration;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.api.Status;

public class GitHelpers {

    public static final String gitignore = "# Ignore everything\n" + 
            "*\n" + 
            "# Don't ignore directories, so we can recurse into them\n" + 
            "!*/\n" + 
            "# Don't ignore .gitignore and *.foo files\n" + 
            "!.gitignore\n" + 
            "!*.ttl\n" + 
            "";
    
    public static Map<String,Repository> typeRepo = new HashMap<>();
    
    public static void ensureGitRepo(String type) {
        if (typeRepo.containsKey(type))
            return;
        String dirpath = MigrationApp.OUTPUT_DIR+type;
        MigrationApp.createDirIfNotExists(dirpath);
//        FileRepositoryBuilder builder = new FileRepositoryBuilder();
//        File dir = new File(dirpath);
//        try {
//            Repository repository = builder.setGitDir(dir)
//              .readEnvironment() // scan environment GIT_* variables
//              .findGitDir() // scan up the file system tree
//              .build();
//            if (!repository.getObjectDatabase().exists()) {
//                System.out.println("create git repository in "+dirpath);
//                repository.create();
//                PrintWriter out = new PrintWriter(dirpath+".gitignore");
//                out.println(gitignore);
//                out.close();
//            }
//            typeRepo.put(type, repository);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }
    
    public static Set<String> getChanges(String type) {
        Git git = new Git(typeRepo.get(type));
        Status status;
        Set<String> res = new HashSet<>();
        try {
            status = git.status().call();
        } catch (NoWorkTreeException | GitAPIException e) {
            e.printStackTrace();
            git.close();
            return null;
        }
        res.addAll(status.getModified());
        res.addAll(status.getAdded());
        git.close();
        return res;
    }
    
}

@@ -1116,34 +1116,32 @@
 xists(dirpath);%0A
-//
         FileRepo
@@ -1189,26 +1189,73 @@
 yBuilder();%0A
-//
+        File gitDir = new File(dirpath+%22/.git%22);%0A
         File
@@ -1255,17 +1255,19 @@
    File 
-d
+wtD
 ir = new
@@ -1274,34 +1274,32 @@
  File(dirpath);%0A
-//
         try %7B%0A//
@@ -1296,18 +1296,16 @@
   try %7B%0A
-//
         
@@ -1354,15 +1354,88 @@
 Dir(
-dir)%0A//
+gitDir)%0A              .setWorkTree(wtDir)%0A              //.setMustExist( true )%0A
     
@@ -1503,72 +1503,8 @@
 les%0A
-//              .findGitDir() // scan up the file system tree%0A//
     
@@ -1519,26 +1519,24 @@
   .build();%0A
-//
             
@@ -1579,26 +1579,24 @@
 exists()) %7B%0A
-//
             
@@ -1656,18 +1656,16 @@
 rpath);%0A
-//
         
@@ -1689,26 +1689,24 @@
 y.create();%0A
-//
             
@@ -1766,18 +1766,16 @@
 nore%22);%0A
-//
         
@@ -1806,18 +1806,16 @@
 gnore);%0A
-//
         
@@ -1831,26 +1831,24 @@
 ut.close();%0A
-//
             
@@ -1849,18 +1849,16 @@
       %7D%0A
-//
         
@@ -1893,18 +1893,16 @@
 itory);%0A
-//
         
@@ -1927,18 +1927,16 @@
 on e) %7B%0A
-//
         
@@ -1960,18 +1960,16 @@
 race();%0A
-//
         

fix repository creation