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:org.gitective.tests.GitTestCase.java

License:Open Source License

/**
 * Move file in test repository/*from w  ww.ja va2s  . c  om*/
 *
 * @param repo
 * @param from
 * @param to
 * @param message
 * @return commit
 * @throws Exception
 */
protected RevCommit mv(File repo, String from, String to, String message) throws Exception {
    File file = new File(repo.getParentFile(), from);
    file.renameTo(new File(repo.getParentFile(), to));
    Git git = Git.open(repo);
    git.rm().addFilepattern(from);
    git.add().addFilepattern(to).call();
    RevCommit commit = git.commit().setAll(true).setMessage(message).setAuthor(author).setCommitter(committer)
            .call();
    assertNotNull(commit);
    return commit;
}

From source file:org.gitective.tests.GitTestCase.java

License:Open Source License

/**
 * Add files to test repository/*from w  w  w . jav a2  s . c  o  m*/
 *
 * @param repo
 * @param paths
 * @param contents
 * @param message
 * @return commit
 * @throws Exception
 */
protected RevCommit add(File repo, List<String> paths, List<String> contents, String message) throws Exception {
    Git git = Git.open(repo);
    for (int i = 0; i < paths.size(); i++) {
        String path = paths.get(i);
        String content = contents.get(i);
        File file = new File(repo.getParentFile(), path);
        if (!file.getParentFile().exists())
            assertTrue(file.getParentFile().mkdirs());
        if (!file.exists())
            assertTrue(file.createNewFile());
        PrintWriter writer = new PrintWriter(file);
        if (content == null)
            content = "";
        try {
            writer.print(content);
        } finally {
            writer.close();
        }
        git.add().addFilepattern(path).call();
    }
    RevCommit commit = git.commit().setMessage(message).setAuthor(author).setCommitter(committer).call();
    assertNotNull(commit);
    return commit;
}

From source file:org.gitective.tests.GitTestCase.java

License:Open Source License

/**
 * Merge ref into current branch// w  w w  .ja va 2 s.  c o  m
 *
 * @param ref
 * @return result
 * @throws Exception
 */
protected MergeResult merge(String ref) throws Exception {
    Git git = Git.open(testRepo);
    return git.merge().setStrategy(MergeStrategy.RESOLVE)
            .include(CommitUtils.getCommit(git.getRepository(), ref)).call();
}

From source file:org.gitective.tests.GitTestCase.java

License:Open Source License

/**
 * Add note to latest commit with given content
 *
 * @param content//from   w w w .  ja  v a  2s  .  c  o  m
 * @param ref
 * @return note
 * @throws Exception
 */
protected Note note(String content, String ref) throws Exception {
    Git git = Git.open(testRepo);
    Note note = git.notesAdd().setMessage(content).setNotesRef(Constants.R_NOTES + ref)
            .setObjectId(CommitUtils.getHead(git.getRepository())).call();
    assertNotNull(note);
    return note;
}

From source file:org.gitective.tests.GitTestCase.java

License:Open Source License

/**
 * Delete and commit file at path/*from w ww .j  a va2 s .c  o m*/
 *
 * @param path
 * @return commit
 * @throws Exception
 */
protected RevCommit delete(String path) throws Exception {
    String message = MessageFormat.format("Committing {0} at {1}", path, new Date());
    Git git = Git.open(testRepo);
    git.rm().addFilepattern(path).call();
    RevCommit commit = git.commit().setOnly(path).setMessage(message).setAuthor(author).setCommitter(committer)
            .call();
    assertNotNull(commit);
    return commit;
}

From source file:org.gradle.vcs.git.internal.GitVersionControlSystem.java

License:Apache License

private static void resetRepo(File workingDir, GitVersionControlSpec gitSpec, VersionRef ref) {
    Git git = null;//from w  ww  .j ava 2s  .c  om
    try {
        git = Git.open(workingDir);
        git.reset().setMode(ResetCommand.ResetType.HARD).setRef(ref.getCanonicalId()).call();
        updateSubModules(git);
    } catch (IOException e) {
        throw wrapGitCommandException("reset", gitSpec.getUrl(), workingDir, e);
    } catch (GitAPIException e) {
        throw wrapGitCommandException("reset", gitSpec.getUrl(), workingDir, e);
    } catch (JGitInternalException e) {
        throw wrapGitCommandException("reset", gitSpec.getUrl(), workingDir, e);
    } finally {
        if (git != null) {
            git.close();
        }
    }
}

From source file:org.jboss.maven.plugins.qstools.ArchetypeSyncMojo.java

License:Apache License

/**
 * Clone the informed git repository and checkout the informed branch
 * /* w  w w .ja  va  2 s  .  c o m*/
 * @throws IOException
 * @throws InvalidRemoteException
 * @throws TransportException
 * @throws GitAPIException
 */
private void cloneOriginProject()
        throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    File gitLocalRepo = new File(outputPath + File.separator + "git");
    if (!gitLocalRepo.exists()) {
        getLog().info("Cloning " + projectGitRepo + " to " + gitLocalRepo);
        CloneCommand clone = Git.cloneRepository();
        clone.setBare(false);
        clone.setCloneAllBranches(true);
        clone.setDirectory(gitLocalRepo).setURI(projectGitRepo);
        clone.call();
    }
    getLog().info("Checking out " + branch + " branch");
    Git.open(gitLocalRepo).checkout().setName(branch).call();
}

From source file:org.jboss.tools.openshift.reddeer.utils.TestUtils.java

License:Open Source License

public static void closeGitRepository(File repoDir) {
    try {/*from w ww. j a  va  2s  .c  o  m*/
        Git git = Git.open(repoDir);
        git.getRepository().close();
        git.close();
    } catch (IOException ex) {
        // DO NOTHING
    }
}

From source file:org.jdamico.jgitbkp.components.ManagerComponent.java

License:Open Source License

public void cloneRepos(List<RepoImpl> reposLst, Config config) throws JGitBackupException {

    for (int i = 0; i < reposLst.size(); i++) {

        File f = new File(reposLst.get(i).getClonedPath());

        if (f.mkdir()) {

            try {
                Git.cloneRepository().setCloneAllBranches(true).setCloneSubmodules(true).setNoCheckout(false)
                        .setURI(config.getProtocol() + "://" + config.getHostPath() + "/"
                                + reposLst.get(i).getName())
                        .setCredentialsProvider(
                                new UsernamePasswordCredentialsProvider(config.getUser(), config.getPasswd()))
                        .setDirectory(new File(reposLst.get(i).getClonedPath()))
                        .setProgressMonitor(new TextProgressMonitor()).call();

            } catch (InvalidRemoteException e) {
                LoggerManager.getInstance().logAtExceptionTime(this.getClass().getName(), e.getMessage());
                throw new JGitBackupException(e);
            } catch (TransportException e) {
                LoggerManager.getInstance().logAtExceptionTime(this.getClass().getName(), e.getMessage());
                throw new JGitBackupException(e);
            } catch (GitAPIException e) {
                LoggerManager.getInstance().logAtExceptionTime(this.getClass().getName(), e.getMessage());
                throw new JGitBackupException(e);
            }//  w  w  w .  jav  a2s .  c o  m

        } else {
            try {
                Git git = Git.open(f);
                git.pull();
                LoggerManager.getInstance().logAtDebugTime(this.getClass().getName(),
                        "Updating data from repository.");
            } catch (IOException e) {
                LoggerManager.getInstance().logAtExceptionTime(this.getClass().getName(), e.getMessage());
                throw new JGitBackupException(e);
            }
            String err = "Unable to create directory for clone [" + reposLst.get(i).getName()
                    + "]. Check the path and permissions.";
            if (!f.exists()) {
                if (!Starter.silent)
                    System.out.println(err);
                LoggerManager.getInstance().logAtExceptionTime(this.getClass().getName(), err);
            }

        }
    }

}

From source file:org.jdamico.jgitbkp.components.ManagerComponent.java

License:Open Source License

public List<BundleStatus> generateBundles(List<RepoImpl> reposLst, Config config) throws JGitBackupException {

    List<BundleStatus> bundleStatusLst = new ArrayList<BundleStatus>();

    boolean backupStatus = false;
    StringBuffer backupMessage = new StringBuffer();

    for (int i = 0; i < reposLst.size(); i++) {
        String bundlePath = config.getBundlePath() + "/" + reposLst.get(i).getName() + ".bundle";

        File gitWorkDir = new File(reposLst.get(i).getClonedPath());
        Git git = null;/*from  ww  w  . j  a v a2  s.co m*/
        Repository localRepoGit = null;
        BundleWriter bundleWriter = null;
        ProgressMonitor pMonitor = null;
        OutputStream oStream = null;
        try {
            git = Git.open(gitWorkDir);
            git.pull();
            if (!Starter.silent)
                System.out.println("Updated data for [" + reposLst.get(i).getName() + "]");
            localRepoGit = git.getRepository();
            bundleWriter = new BundleWriter(localRepoGit);
            pMonitor = new TextProgressMonitor();
            oStream = new FileOutputStream(bundlePath);
            Map<String, Ref> allRefs = localRepoGit.getAllRefs();
            Collection<Ref> values = allRefs.values();
            Iterator<Ref> iter = values.iterator();
            while (iter.hasNext()) {
                try {
                    Ref element = iter.next();
                    bundleWriter.include(element);
                } catch (IllegalArgumentException e) {

                    if (!e.getMessage().equalsIgnoreCase("Invalid ref name: HEAD")) {
                        String err = reposLst.get(i).getName() + ": " + e.getMessage();
                        backupMessage.append(err);
                        LoggerManager.getInstance().logAtExceptionTime(this.getClass().getName(), err);
                    }
                }

            }
            if (!Starter.silent)
                bundleWriter.writeBundle(pMonitor, oStream);
            else
                bundleWriter.writeBundle(null, oStream);

        } catch (IOException e) {
            String err = reposLst.get(i).getName() + ": " + e.getMessage();
            backupMessage.append(err);
            LoggerManager.getInstance().logAtExceptionTime(this.getClass().getName(), err);
            throw new JGitBackupException(e);
        }

        File f = new File(bundlePath);
        if (f.exists() && f.length() > 100) {
            backupStatus = f.exists();
        } else
            backupStatus = false;

        bundleStatusLst.add(new BundleStatus(reposLst.get(i).getName(), backupStatus, backupMessage.toString(),
                Utils.getInstance().getCurrentDateTimeFormated(Constants.DATE_FORMAT), bundlePath));
        if (!Starter.silent)
            System.out.println("Bundle created: " + bundlePath + " - " + backupStatus);
    }

    return bundleStatusLst;
}