Search in sources :

Example 11 with CheckoutCommand

use of org.eclipse.jgit.api.CheckoutCommand in project egit by eclipse.

the class DiscardChangesOperation method discardChanges.

private void discardChanges(Repository repository, Collection<String> paths) throws GitAPIException {
    ResourceUtil.saveLocalHistory(repository);
    try (Git git = new Git(repository)) {
        CheckoutCommand checkoutCommand = git.checkout();
        if (revision != null) {
            checkoutCommand.setStartPoint(revision);
        }
        if (stage != null) {
            checkoutCommand.setStage(stage.checkoutStage);
        }
        if (paths.isEmpty() || paths.contains("")) {
            // $NON-NLS-1$
            checkoutCommand.setAllPaths(true);
        } else {
            for (String path : paths) {
                checkoutCommand.addPath(path);
            }
        }
        checkoutCommand.call();
    }
}
Also used : CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) Git(org.eclipse.jgit.api.Git)

Example 12 with CheckoutCommand

use of org.eclipse.jgit.api.CheckoutCommand in project bndtools by bndtools.

the class GitCloneTemplate method generateOutputs.

@Override
public ResourceMap generateOutputs(Map<String, List<Object>> parameters, IProgressMonitor monitor) throws Exception {
    File workingDir = null;
    File gitDir = null;
    // Get existing checkout if available
    synchronized (this) {
        if (checkedOut != null) {
            workingDir = checkedOut.getWorkTree();
            gitDir = new File(workingDir, ".git");
        }
    }
    if (workingDir == null) {
        // Need to do a new checkout
        workingDir = Files.createTempDirectory("checkout").toFile();
        gitDir = new File(workingDir, ".git");
        try {
            CloneCommand cloneCmd = Git.cloneRepository().setURI(params.cloneUrl).setDirectory(workingDir).setNoCheckout(true);
            cloneCmd.setProgressMonitor(new EclipseGitProgressTransformer(monitor));
            Git git = cloneCmd.call();
            CheckoutCommand checkout = git.checkout().setCreateBranch(true).setName("_tmp");
            if (params.branch == null) {
                checkout.setStartPoint(GitCloneTemplateParams.DEFAULT_BRANCH);
            } else {
                String startPoint = null;
                if (params.branch.startsWith(Constants.DEFAULT_REMOTE_NAME + "/")) {
                    startPoint = params.branch;
                } else {
                    // Check for a matching tag
                    for (Ref ref : git.tagList().call()) {
                        if (ref.getName().endsWith("/" + params.branch)) {
                            startPoint = params.branch;
                            break;
                        }
                    }
                    if (startPoint == null) {
                        // Check remote branches
                        for (Ref ref : git.branchList().setListMode(ListMode.REMOTE).call()) {
                            if (ref.getName().endsWith("/" + params.branch)) {
                                startPoint = Constants.DEFAULT_REMOTE_NAME + "/" + params.branch;
                                break;
                            }
                        }
                        if (startPoint == null) {
                            if (SHA1_PATTERN.matcher(params.branch).matches()) {
                                startPoint = params.branch;
                                if (startPoint == null) {
                                    throw new Exception("Unable to find requested ref \"" + params.branch + "\"");
                                }
                            }
                        }
                    }
                }
                checkout.setStartPoint(startPoint);
            }
            checkout.call();
            checkedOut = git.getRepository();
        } catch (JGitInternalException e) {
            Throwable cause = e.getCause();
            if (cause instanceof Exception)
                throw (Exception) cause;
            throw e;
        }
    }
    final File exclude = gitDir;
    FileFilter filter = new FileFilter() {

        @Override
        public boolean accept(File path) {
            return !path.equals(exclude);
        }
    };
    return toResourceMap(workingDir, filter);
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) FileFilter(java.io.FileFilter) File(java.io.File) IOException(java.io.IOException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException)

Example 13 with CheckoutCommand

use of org.eclipse.jgit.api.CheckoutCommand in project bndtools by bndtools.

the class GitUtils method getRepository.

public static synchronized Repository getRepository(File gitRoot, String branch, String gitUrl, String gitPushUrl) throws IOException, ConfigInvalidException, JGitInternalException, GitAPIException {
    File dotGit;
    if (gitRoot.getName().equals(Constants.DOT_GIT)) {
        dotGit = gitRoot;
    } else {
        dotGit = new File(gitRoot, Constants.DOT_GIT);
    }
    Repository repository = localRepos.get(dotGit);
    if (repository != null && dotGit.exists()) {
        return repository;
    }
    if (!dotGit.exists()) {
        Git.cloneRepository().setDirectory(gitRoot).setCloneAllBranches(true).setURI(gitUrl).call();
        FileBasedConfig config = new FileBasedConfig(new File(dotGit, "config"), FS.DETECTED);
        config.load();
        if (gitPushUrl != null) {
            config.setString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin", "pushurl", gitPushUrl);
        }
        config.save();
    }
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    repository = builder.setGitDir(dotGit).readEnvironment().findGitDir().build();
    localRepos.put(dotGit, repository);
    try {
        repository.incrementOpen();
        Git git = Git.wrap(repository);
        // Check branch
        boolean pull = true;
        String currentBranch = repository.getBranch();
        if (branch != null && !branch.equals(currentBranch)) {
            CheckoutCommand checkout = git.checkout();
            if (!branchExists(git, branch)) {
                checkout.setCreateBranch(true);
                pull = false;
            }
            checkout.setName(branch);
            checkout.call();
        }
        if (pull) {
            git.pull().call();
        } else {
            git.fetch().call();
        }
    } catch (Exception e) {
        if (!(e.getCause() instanceof TransportException)) {
            throw new RuntimeException(e);
        }
    } finally {
        if (repository != null) {
            repository.close();
        }
    }
    return repository;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) Git(org.eclipse.jgit.api.Git) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) TransportException(org.eclipse.jgit.errors.TransportException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) TransportException(org.eclipse.jgit.errors.TransportException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException)

Example 14 with CheckoutCommand

use of org.eclipse.jgit.api.CheckoutCommand in project blueocean-plugin by jenkinsci.

the class GitUtils method merge.

public static void merge(final Repository repo, final String localRef, final String remoteRef) {
    try (org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git(repo)) {
        CheckoutCommand checkoutCommand = git.checkout();
        checkoutCommand.setCreateBranch(false);
        checkoutCommand.setName(localRef);
        checkoutCommand.call();
        Ref mergeBranchRef = repo.exactRef(remoteRef);
        MergeResult merge = git.merge().include(mergeBranchRef).setFastForward(MergeCommand.FastForwardMode.FF_ONLY).call();
        if (merge.getConflicts() != null) {
            throw new RuntimeException("Merge has conflicts");
        }
    } catch (Exception e) {
        throw new ServiceException.UnexpectedErrorException("Unable to merge: " + remoteRef + " to: " + localRef, e);
    }
}
Also used : CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) MergeResult(org.eclipse.jgit.api.MergeResult) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(hudson.plugins.git.GitException) JSchException(com.jcraft.jsch.JSchException) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException) IOException(java.io.IOException) ServiceException(io.jenkins.blueocean.commons.ServiceException) TransportException(org.eclipse.jgit.api.errors.TransportException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) Ref(org.eclipse.jgit.lib.Ref) Git(org.jenkinsci.plugins.gitclient.Git) ServiceException(io.jenkins.blueocean.commons.ServiceException)

Aggregations

CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)14 Git (org.eclipse.jgit.api.Git)10 Ref (org.eclipse.jgit.lib.Ref)7 ArrayList (java.util.ArrayList)6 CloneCommand (org.eclipse.jgit.api.CloneCommand)5 FetchCommand (org.eclipse.jgit.api.FetchCommand)5 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)5 Repository (org.eclipse.jgit.lib.Repository)5 File (java.io.File)4 IOException (java.io.IOException)4 ListBranchCommand (org.eclipse.jgit.api.ListBranchCommand)4 MergeCommand (org.eclipse.jgit.api.MergeCommand)4 Status (org.eclipse.jgit.api.Status)4 StatusCommand (org.eclipse.jgit.api.StatusCommand)4 JGitInternalException (org.eclipse.jgit.api.errors.JGitInternalException)4 NotMergedException (org.eclipse.jgit.api.errors.NotMergedException)4 ObjectId (org.eclipse.jgit.lib.ObjectId)4 StoredConfig (org.eclipse.jgit.lib.StoredConfig)4 Test (org.junit.Test)4 FetchResult (org.eclipse.jgit.transport.FetchResult)3