Example usage for org.eclipse.jgit.api Git open

List of usage examples for org.eclipse.jgit.api Git open

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git open.

Prototype

public static Git open(File dir) throws IOException 

Source Link

Document

Open repository

Usage

From source file:ch.sbb.releasetrain.git.GitRepoImpl.java

License:Apache License

private Git gitOpen() throws IOException {
    if (this.git == null) {
        this.git = Git.open(new File(gitDir, ".git"));
    }//ww w .  ja v  a  2  s  .  c o  m
    return git;
}

From source file:ch.sourcepond.maven.release.scm.git.GitFactory.java

License:Apache License

public Git newGit() throws SCMException {
    final File gitDir = new File(".");
    try {/*  ww  w  . j a  v a 2s .  c  o  m*/
        return Git.open(gitDir);
    } catch (final RepositoryNotFoundException rnfe) {
        final String fullPathOfCurrentDir = pathOf(gitDir);
        final File gitRoot = getGitRootIfItExistsInOneOfTheParentDirectories(new File(fullPathOfCurrentDir));
        if (gitRoot == null) {
            throw new SCMException("Releases can only be performed from Git repositories.")
                    .add("%s is not a Git repository.", fullPathOfCurrentDir);
        }
        throw new SCMException("The release plugin can only be run from the root folder of your Git repository")
                .add("%s is not the root of a Gir repository", fullPathOfCurrentDir)
                .add("Try running the release plugin from %s", pathOf(gitRoot));
    } catch (final Exception e) {
        throw new SCMException("Could not open git repository. Is %s a git repository?", pathOf(gitDir))
                .add("Exception returned when accessing the git repo: %s", e.toString());
    }
}

From source file:co.bledo.gitmin.servlet.Review.java

License:Apache License

public Response index(Request request) throws Exception {

    log.entry(request);/*from w  w w.j a  va2s.c  o  m*/

    DateFormat dformat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");

    List<Repo> repos = GitminStorage.getRepos();
    Map<String, List<GitListItem>> lists = new HashMap<String, List<GitListItem>>();
    for (Repo repo : repos) {
        Git git = Git.open(new File(GitminConfig.getGitRepositoriesPath() + "/" + repo.name));
        Iterable<RevCommit> commits = git.log().setMaxCount(20).call();
        List<GitListItem> list = new ArrayList<GitListItem>();
        for (RevCommit commit : commits) {
            log.debug(commit);
            GitListItem item = new GitListItem();
            item.email = commit.getAuthorIdent().getEmailAddress();
            item.name = commit.getAuthorIdent().getName();
            item.subject = commit.getShortMessage();
            item.gravatar = "http://www.gravatar.com/avatar/" + Util.md5(Util.trim(item.email).toLowerCase())
                    + "?s=40";
            item.hash = commit.getName();
            item.date = dformat.format(new Date(commit.getCommitTime()));
            list.add(item);
        }

        lists.put(repo.name, list);
    }

    VelocityResponse resp = VelocityResponse.newInstance(request, this);
    resp.assign("lists", lists);

    return log.exit(resp);
}

From source file:co.bledo.gitmin.servlet.Review.java

License:Apache License

public Response commit(Request req) throws IOException, GitAPIException {
    String repoName = req.getParam("repo", "");
    String hash = req.getParam("hash", "");

    Git git = Git.open(new File(GitminConfig.getGitRepositoriesPath() + "/" + repoName));

    /*/*  ww w .  j a v  a 2  s  . c om*/
    ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
    List<DiffEntry> diffTest = git.diff().setOutputStream(baos1)
    .setOldTree(getTreeIterator(git.getRepository(), hash))
    .setNewTree(getTreeIterator(git.getRepository(), hash + "^"))
    .call();
    System.out.println(baos1.toString());
    */

    RepositoryBuilder builder = new RepositoryBuilder();
    Repository repo = builder.setGitDir(new File(GitminConfig.getGitRepositoriesPath() + "/" + repoName))
            .readEnvironment().findGitDir().build();

    RevWalk rw = new RevWalk(repo);

    ObjectId hashOid = repo.resolve(hash);

    RevCommit commit = rw.parseCommit(hashOid);
    RevCommit parent = rw.parseCommit(commit.getParent(0).getId());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DiffFormatter df = new DiffFormatter(baos);
    df.setRepository(repo);
    df.setDiffComparator(RawTextComparator.DEFAULT);
    df.setDetectRenames(true);
    List<DiffEntry> diffs = df.scan(parent, commit);
    List<CommitInfo> commitInfos = new ArrayList<CommitInfo>();
    for (DiffEntry diff : diffs) {

        CommitInfo nfo = new CommitInfo();

        df.format(diff);

        nfo.diff = baos.toString();
        nfo.oldContents = getFileContent(repo, parent, diff.getOldPath());
        nfo.newContents = getFileContent(repo, parent, diff.getNewPath());
        nfo.newFile = diff.getNewPath();
        nfo.oldFile = diff.getOldPath();

        commitInfos.add(nfo);
    }

    VelocityResponse resp = VelocityResponse.newInstance(req, this);
    resp.assign("nfolist", commitInfos);
    return log.exit(resp);
}

From source file:com.barchart.jenkins.cascade.PluginScmGit.java

License:BSD License

/**
 * Short name of current branch./*from   w  w  w. ja  v  a2 s .  c o  m*/
 */
public static String branch(final File workspace) {
    try {
        final Git git = Git.open(workspace);
        return git.getRepository().getBranch();
    } catch (final Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:com.barchart.jenkins.cascade.PluginScmGit.java

License:BSD License

/**
 * Spec name of current branch.//  w ww.j a v  a  2 s  .c om
 */
public static String branchSpec(final File workspace) {
    try {
        final Git git = Git.open(workspace);
        return git.getRepository().getFullBranch();
    } catch (final Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:com.barchart.jenkins.cascade.PluginScmGit.java

License:BSD License

/**
 * See {@link Git#add()}/*from  w ww .ja v a 2  s  .  c o m*/
 */
public static DirCache doAdd(final File workspace, final String pattern) {
    try {
        final Git git = Git.open(workspace);
        return git.add().addFilepattern(pattern).call();
    } catch (final Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:com.barchart.jenkins.cascade.PluginScmGit.java

License:BSD License

/**
 * See {@link Git#checkout()}//from w  ww .j  ava 2s  .  c o  m
 */
public static CheckoutResult doCheckout(final File workspace, final String localBranch, final String remoteName,
        final String remoteBranch) {
    try {
        final Git git = Git.open(workspace);
        final CheckoutCommand command = git.checkout().setName(localBranch).setForce(true);
        if (findRef(workspace, localBranch) == null) {
            command.setCreateBranch(true).setUpstreamMode(SetupUpstreamMode.TRACK)
                    .setStartPoint(remote(remoteName, remoteBranch)).call();
        } else {
            command.call();
        }
        return command.getResult();
    } catch (final Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:com.barchart.jenkins.cascade.PluginScmGit.java

License:BSD License

/**
 * Remove untracked files./*  w w  w  .j a  v  a2s . c o  m*/
 */
public static Set<String> doClean(final File workspace) {
    try {
        final Git git = Git.open(workspace);
        return git.clean().call();
    } catch (final Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:com.barchart.jenkins.cascade.PluginScmGit.java

License:BSD License

/**
 * See {@link Git#commit()}//from ww  w  .j a  v  a  2  s .c  om
 */
public static RevCommit doCommit(final File workspace, final PersonIdent person, final String message) {
    try {
        final Git git = Git.open(workspace);
        final CommitCommand command = git.commit();
        if (person != null) {
            command.setAuthor(person).setCommitter(person);
        }
        return command.setMessage(message).call();
    } catch (final Throwable e) {
        throw new RuntimeException(e);
    }
}