DocAsCodeRepository.java

package org.docascode.utils;

import org.docascode.git.ConfigCtrl;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.SystemReader;

import java.io.File;
import java.io.IOException;

import static org.docascode.utils.DocAsCodeRepository.ConfigLevel.LOCAL;

public class DocAsCodeRepository{
    public enum ConfigLevel {
        LOCAL,
        PROJECT,
        GLOBAL,
        SYSTEM
    }

    private File directory = null;

    public DocAsCodeRepository(File directory) throws DocAsCodeException{
        this.directory = directory;
    }

    public File getTopLevel(){
        FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
        Repository repository = null;
        try {
            repository = repositoryBuilder.findGitDir().build();
            return repository.getDirectory().getParentFile();
        } catch (IOException e) {
            return this.directory;
        }

    }

    public File getLocalConfig()
    {
        return new File(getTopLevel().getAbsolutePath()+"/.git/config");
    }

    public File getProjectConfig()
    {
        return new File(getTopLevel().getAbsolutePath()+"/.docascode/config");
    }

    public StoredConfig getConfig(ConfigLevel level){
        StoredConfig config = null;
        switch (level){
            case LOCAL:
                config = new FileBasedConfig(getLocalConfig(),FS.detect());
                break;
            case PROJECT:
                config = new FileBasedConfig(getProjectConfig(),FS.detect());
                break;
            case GLOBAL:
                config = SystemReader.getInstance().openUserConfig(null,FS.detect());
                break;
            case SYSTEM:
                config = SystemReader.getInstance().openSystemConfig(null,FS.detect());
                break;
        }
        return config;
    }

    public File getPreCommit() {
        return new File(getTopLevel().getAbsolutePath()+"/.git/hooks/pre-commit");
    }

    public File getPostCommit() {
        return new File(getTopLevel().getAbsolutePath()+"/.git/hooks/post-commit");
    }

    public File getDeliveryXML() {
        return new File(getTopLevel().getAbsolutePath()+"/delivery.xml");
    }

    public File getDeliveryProperties() {
        return new File(getTopLevel().getAbsolutePath()+"/delivery.properties");
    }

    public File getDocAsCodeFolder(){
        return new File(getTopLevel().getAbsolutePath()+"/.docascode");
    }

    public File getDocAsCodeXML(){
        return new File(getDocAsCodeFolder().getAbsolutePath()+"/docascode.xml");
    }

    public File getChronoXML(){
        return new File(getTopLevel().getAbsolutePath()+"/chrono.xml");
    }

    public File getCheckinFolder(){
        return new File(getTopLevel().getAbsolutePath()+"/.docascode/markdown");
    }

    public String getVersion(){
        ConfigCtrl configCtrl = new ConfigCtrl(this);
        return configCtrl.get(LOCAL,"docascode",null,"version");
    }

}