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

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

Introduction

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

Prototype

public FetchCommand fetch() 

Source Link

Document

Return a command object to execute a Fetch command

Usage

From source file:org.jboss.forge.addon.git.GitUtilsImpl.java

License:Open Source License

@Override
public FetchResult fetch(final Git git, final String remote, final String refSpec, final int timeout,
        final boolean fsck, final boolean dryRun, final boolean thin, final boolean prune)
        throws GitAPIException {
    FetchCommand fetch = git.fetch();
    fetch.setCheckFetchedObjects(fsck);//from   ww w  . j a va  2  s .  co  m
    fetch.setRemoveDeletedRefs(prune);
    if (refSpec != null)
        fetch.setRefSpecs(new RefSpec(refSpec));
    if (timeout >= 0)
        fetch.setTimeout(timeout);
    fetch.setDryRun(dryRun);
    fetch.setRemote(remote);
    fetch.setThin(thin);
    fetch.setProgressMonitor(new TextProgressMonitor());

    FetchResult result = fetch.call();
    return result;
}

From source file:org.jboss.forge.addon.git.GitUtilsImpl.java

License:Open Source License

@Override
public List<Ref> getRemoteBranches(final Git repo) throws GitAPIException {
    List<Ref> results = new ArrayList<>();
    try {/*w  w  w  .  ja v a  2  s .  com*/
        FetchResult fetch = repo.fetch().setRemote(GIT_REMOTE_ORIGIN).call();
        Collection<Ref> refs = fetch.getAdvertisedRefs();
        for (Ref ref : refs) {
            if (ref.getName().startsWith(GIT_REFS_HEADS)) {
                results.add(ref);
            }
        }
    } catch (InvalidRemoteException e) {
        e.printStackTrace();
    }

    return results;
}

From source file:org.jboss.forge.git.GitUtils.java

License:Open Source License

public static FetchResult fetch(final Git git, final String remote, final String refSpec, final int timeout,
        final boolean fsck, final boolean dryRun, final boolean thin, final boolean prune)
        throws GitAPIException {
    FetchCommand fetch = git.fetch();
    fetch.setCheckFetchedObjects(fsck);//  w w  w  . ja v a2s  .  c  o  m
    fetch.setRemoveDeletedRefs(prune);
    if (refSpec != null)
        fetch.setRefSpecs(new RefSpec(refSpec));
    if (timeout >= 0)
        fetch.setTimeout(timeout);
    fetch.setDryRun(dryRun);
    fetch.setRemote(remote);
    fetch.setThin(thin);
    fetch.setProgressMonitor(new TextProgressMonitor());

    FetchResult result = fetch.call();
    return result;
}

From source file:org.jboss.forge.git.GitUtils.java

License:Open Source License

public static List<Ref> getRemoteBranches(final Git repo) throws GitAPIException {
    List<Ref> results = new ArrayList<Ref>();
    try {/*from   w w  w.jav a 2  s  . c o  m*/
        FetchResult fetch = repo.fetch().setRemote("origin").call();
        Collection<Ref> refs = fetch.getAdvertisedRefs();
        for (Ref ref : refs) {
            if (ref.getName().startsWith("refs/heads")) {
                results.add(ref);
            }
        }
    } catch (InvalidRemoteException e) {
        e.printStackTrace();
    }

    return results;
}

From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java

License:Apache License

public static void fetchRepository(final Git git, final CredentialsProvider credentialsProvider,
        final RefSpec... refSpecs) throws InvalidRemoteException {
    final List<RefSpec> specs = new ArrayList<RefSpec>();
    if (refSpecs == null || refSpecs.length == 0) {
        specs.add(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
        specs.add(new RefSpec("+refs/tags/*:refs/tags/*"));
        specs.add(new RefSpec("+refs/notes/*:refs/notes/*"));
    } else {//from   w  w  w. ja v  a  2 s .c o m
        specs.addAll(Arrays.asList(refSpecs));
    }

    try {
        git.fetch().setCredentialsProvider(credentialsProvider).setRefSpecs(specs).call();

    } catch (final InvalidRemoteException e) {
        throw e;
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java

License:Apache License

public static void syncRepository(final Git git, final CredentialsProvider credentialsProvider,
        final String origin, boolean force) throws InvalidRemoteException {

    if (origin == null || origin.isEmpty()) {
        fetchRepository(git, credentialsProvider);
    } else {/*from w w  w  . j ava2  s. c om*/
        try {
            final StoredConfig config = git.getRepository().getConfig();
            config.setString("remote", "upstream", "url", origin);
            config.save();
        } catch (final Exception ex) {
            throw new RuntimeException(ex);
        }

        final List<RefSpec> specs = new ArrayList<RefSpec>();
        specs.add(new RefSpec("+refs/heads/*:refs/remotes/upstream/*"));
        specs.add(new RefSpec("+refs/tags/*:refs/tags/*"));
        specs.add(new RefSpec("+refs/notes/*:refs/notes/*"));

        try {
            git.fetch().setCredentialsProvider(credentialsProvider).setRefSpecs(specs).setRemote(origin).call();

            git.branchCreate().setName("master")
                    .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM)
                    .setStartPoint("upstream/master").setForce(true).call();

        } catch (final InvalidRemoteException e) {
            throw e;
        } catch (final Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}

From source file:org.ms123.common.git.GitServiceImpl.java

License:Open Source License

private Boolean updateAvailable(Git git) throws Exception {
    FetchCommand fc = git.fetch();
    fc.setDryRun(true);/*from   w  ww . j av a  2 s. com*/
    fc.setRemote("origin");
    FetchResult fr = null;
    try {
        fr = fc.call();
    } catch (org.eclipse.jgit.api.errors.InvalidRemoteException e) {
        return false;
    }
    debug("getAdvertisedRefs:" + fr.getAdvertisedRefs());
    debug("getTrackingRefUpdates:" + fr.getTrackingRefUpdates());
    return !fr.getTrackingRefUpdates().isEmpty();
}

From source file:org.mule.module.git.GitConnector.java

License:Open Source License

/**
 * Fetch changes from another repository 
 *
 * {@sample.xml ../../../doc/mule-module-git.xml.sample git:fetch}
 *
 * @param overrideDirectory Name of the directory to use for git repository
 */// ww w  .ja  v  a  2s . c o m
@Processor
public void fetch(@Optional String overrideDirectory) {
    try {
        Git git = new Git(getGitRepo(overrideDirectory));
        FetchCommand fetch = git.fetch();
        fetch.call();
    } catch (Exception e) {
        throw new RuntimeException("Unable to fetch", e);
    }

}

From source file:org.ocpsoft.redoculous.model.impl.GitRepository.java

License:Open Source License

private void cloneRepository() {
    File refsDir = getRefsDir();//  w ww .j ava2 s.  c o m
    File cacheDir = getCacheDir();

    if (!getRepoDir().exists()) {
        RedoculousProgressMonitor monitor = new RedoculousProgressMonitor();
        try {
            getRepoDir().mkdirs();
            refsDir.mkdirs();
            cacheDir.mkdirs();

            Git git = Git.cloneRepository().setURI(getUrl()).setRemote("origin").setCloneAllBranches(true)
                    .setDirectory(getRepoDir()).setProgressMonitor(monitor).call();

            try {
                git.fetch().setRemote("origin").setTagOpt(TagOpt.FETCH_TAGS).setThin(false).setTimeout(10)
                        .setProgressMonitor(monitor).call();
            } finally {
                git.getRepository().close();
            }

        } catch (Exception e) {
            throw new RuntimeException("Could not clone repository [" + getUrl() + "] [" + getKey() + "]", e);
        }
    }
}

From source file:org.ocpsoft.redoculous.model.impl.GitRepository.java

License:Open Source License

@Override
public void update() {
    File repoDir = getRepoDir();//  ww  w  . ja va 2  s . c o m

    Git git = null;
    RedoculousProgressMonitor monitor = new RedoculousProgressMonitor();
    try {
        log.info("Handling update request for [" + getUrl() + "] [" + getKey() + "]");
        git = Git.open(repoDir);

        git.fetch().setTagOpt(TagOpt.FETCH_TAGS).setRemote("origin")
                .setRefSpecs(new RefSpec("+refs/heads/*:refs/remotes/origin/*")).setProgressMonitor(monitor)
                .call();

        git.fetch().setTagOpt(TagOpt.FETCH_TAGS).setRemote("origin")
                .setRefSpecs(new RefSpec("+refs/tags/*:refs/tags/*")).setProgressMonitor(monitor).call();

        git.reset().setMode(ResetType.HARD).setRef("refs/remotes/origin/" + git.getRepository().getBranch())
                .call();

        git.clean().setCleanDirectories(true).call();

        Files.delete(getRefsDir(), true);
        Files.delete(getCacheDir(), true);
    } catch (Exception e) {
        throw new RuntimeException("Could not update repository [" + getUrl() + "] [" + getKey() + "]", e);
    } finally {
        if (git != null)
            git.getRepository().close();
    }
}