org.gitistics.test.RepositoryWokerImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.gitistics.test.RepositoryWokerImpl.java

Source

/*******************************************************************************
 * Copyright (C) 2013 Steven Rowlands
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package org.gitistics.test;

import java.io.File;
import java.lang.reflect.Proxy;

import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.MergeResult;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;

public class RepositoryWokerImpl implements RepositoryWorker {

    private Repository repository;

    private File dir;

    private Git git;

    public RepositoryWokerImpl(File dir, Repository repository) throws Exception {
        this.dir = dir;
        this.repository = repository;
        this.git = Git.wrap(repository);
    }

    @SuppressWarnings("unchecked")
    private <T> RepositoryWorkerReturn<T> proxy(T object) {
        return (RepositoryWorkerReturn<T>) Proxy.newProxyInstance(this.getClass().getClassLoader(),
                new Class[] { RepositoryWorkerReturn.class }, new ReturnHolder(this, object));
    }

    /**
     * Short hand for common commits
     * @param message
     * @return
     */
    public RepositoryWorkerReturn<RevCommit> commit(String message) {
        try {
            return proxy(git.commit().setMessage(message).call());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public RepositoryWorker add(String file, String content) {
        try {
            File f = new File(dir, file);
            if (!f.exists()) {
                f.createNewFile();
            }
            FileUtils.writeStringToFile(f, content);
            Git.wrap(repository).add().addFilepattern(file).call();
            return this;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public RepositoryWokerImpl modify(String file, String newContent) throws Exception {
        File f = new File(dir, file);
        FileUtils.writeStringToFile(f, newContent);
        Git.wrap(repository).add().addFilepattern(file).call();
        return this;
    }

    public RepositoryWokerImpl remove(String file) throws Exception {
        File f = new File(dir, file);
        if (f.exists()) {
            f.delete();
        }
        Git.wrap(repository).add().setUpdate(true).addFilepattern(".").call();
        return this;
    }

    public RepositoryWokerImpl branch(String name) throws Exception {
        this.git.checkout().setCreateBranch(true).setName(name).call();
        return this;
    }

    public RepositoryWokerImpl checkout(String name) throws Exception {
        this.git.checkout().setName(name).call();
        return this;
    }

    public MergeResult merge(Ref r) throws Exception {
        return git.merge().include(r).call();
    }

    public RepositoryWorkerReturn<MergeResult> merge(RevCommit r) throws Exception {
        return proxy(git.merge().include(r).call());
    }
}