Example usage for org.eclipse.jgit.api ResetCommand setRef

List of usage examples for org.eclipse.jgit.api ResetCommand setRef

Introduction

In this page you can find the example usage for org.eclipse.jgit.api ResetCommand setRef.

Prototype

public ResetCommand setRef(String ref) 

Source Link

Document

Set the name of the Ref to reset to

Usage

From source file:com.genuitec.org.eclipse.egit.core.op.ResetOperation.java

License:Open Source License

private void reset(IProgressMonitor monitor) throws CoreException {
    monitor.beginTask(NLS.bind(CoreText.ResetOperation_performingReset, type.toString().toLowerCase(), refName),
            2);/* w w w.  j a  v a  2s . com*/

    IProject[] validProjects = null;
    if (type == ResetType.HARD)
        validProjects = ProjectUtil.getValidOpenProjects(repository);

    ResetCommand reset = Git.wrap(repository).reset();
    reset.setMode(type);
    reset.setRef(refName);
    try {
        reset.call();
    } catch (GitAPIException e) {
        throw new TeamException(e.getLocalizedMessage(), e.getCause());
    }
    monitor.worked(1);

    // only refresh if working tree changes
    if (type == ResetType.HARD)
        ProjectUtil.refreshValidProjects(validProjects, !closeRemovedProjects,
                new SubProgressMonitor(monitor, 1));

    monitor.done();
}

From source file:com.mpdeimos.ct_tests.looper.GitRevisionLooper.java

License:Apache License

/** {@inheritDoc} */
@Override//from  w  w  w.  java2s . c  o  m
public RevIterator iterator() {

    return new RevIterator() {
        int index = commits.size();

        @Override
        public void remove() {
            throw new IllegalStateException();
        }

        @Override
        public RevisionInfo next() {
            final Commit commit = commits.get(--index);

            return new RevisionInfo(commits.size() - index - 1, commit.getId(), commit, root) {
                /** {@inheritDoc} 
                 * @throws ConQATException */
                @Override
                public File getPath() throws ConQATException {
                    CheckoutCommand checkout = new CheckoutCommand(repository) {
                        /**/};
                    checkout.setName(commit.getId());
                    //checkout.setForce(true);
                    try {
                        checkout.call();
                    } catch (Exception e) {
                        getLogger().error(e);
                        ResetCommand reset = new ResetCommand(repository) {
                            /**/};
                        reset.setMode(ResetType.HARD);
                        reset.setRef(commit.getId());
                        try {
                            reset.call();
                            CheckoutCommand checkout2 = new CheckoutCommand(repository) {
                                /**/};
                            checkout2.setName(commit.getId());
                            //                        checkout2.setForce(true);
                            checkout2.call();
                        } catch (Exception e1) {
                            getLogger().error(e1);
                            throw new ConQATException(e1);
                        }
                    }
                    return super.getPath();
                }
            };
        }

        @Override
        public boolean hasNext() {
            return index > 0;
        }

        @Override
        public Commit peekCommit() {
            if (!hasNext())
                throw new IllegalStateException();

            return commits.get(index - 1);
        }
    };
}

From source file:com.photon.phresco.framework.impl.SCMManagerImpl.java

License:Apache License

private void resetLocalCommit(File appDir, boolean gitExists, Exception e) throws PhrescoException {
    try {//from   w w  w.jav  a 2  s .c o  m
        if (gitExists == true && e.getLocalizedMessage().contains("not authorized")) {
            FileRepositoryBuilder builder = new FileRepositoryBuilder();
            Repository repository = builder.setGitDir(appDir).readEnvironment().findGitDir().build();
            Git git = new Git(repository);

            InitCommand initCommand = Git.init();
            initCommand.setDirectory(appDir);
            git = initCommand.call();

            ResetCommand reset = git.reset();
            ResetType mode = ResetType.SOFT;
            reset.setRef("HEAD~1").setMode(mode);
            reset.call();

            git.getRepository().close();
        }
    } catch (Exception pe) {
        new PhrescoException(pe);
    }
}

From source file:com.verigreen.jgit.JGitOperator.java

License:Apache License

public void reset(String refToResetTo) {

    ResetCommand command = _git.reset();
    command.setRef(refToResetTo);
    command.setMode(ResetType.HARD);//from   w w w . jav a  2 s. c o  m
    try {
        command.call();
    } catch (Throwable e) {
        throw new RuntimeException(String.format("Failed to reset to [%s]", refToResetTo), e);
    }
}

From source file:fr.treeptik.cloudunit.utils.GitUtils.java

License:Open Source License

/**
 * this method is associate with listGitTagsOfApplication() method
 * which list all tags with index, this is this index which must pass as parammeter of this method
 *
 * @param application//from w w w  .j  a  v  a 2 s  . c om
 * @param indexChosen
 * @param dockerManagerAddress
 * @param containerGitAddress
 * @return
 * @throws InvalidRemoteException
 * @throws TransportException
 * @throws GitAPIException
 * @throws IOException
 */
public static List<String> resetOnChosenGitTag(Application application, int indexChosen,
        String dockerManagerAddress, String containerGitAddress)
        throws InvalidRemoteException, TransportException, GitAPIException, IOException {
    User user = application.getUser();
    String sshPort = application.getServers().get(0).getSshPort();
    String password = user.getPassword();
    String userNameGit = user.getLogin();
    String dockerManagerIP = dockerManagerAddress.substring(0, dockerManagerAddress.length() - 5);
    String remoteRepository = "ssh://" + userNameGit + "@" + dockerManagerIP + ":" + sshPort
            + containerGitAddress;
    File gitworkDir = Files.createTempDirectory("clone").toFile();
    CloneCommand clone = Git.cloneRepository();
    clone.setDirectory(gitworkDir);

    CredentialsProvider credentialsProvider = configCredentialsForGit(userNameGit, password);
    clone.setCredentialsProvider(credentialsProvider);
    clone.setURI(remoteRepository);
    Git git = clone.call();

    ListTagCommand listTagCommand = git.tagList();
    List<Ref> listRefs = listTagCommand.call();

    Ref ref = listRefs.get(indexChosen);

    ResetCommand resetCommand = git.reset();
    resetCommand.setMode(ResetType.HARD);
    resetCommand.setRef(ref.getName());
    resetCommand.call();

    PushCommand pushCommand = git.push();
    pushCommand.setCredentialsProvider(credentialsProvider);
    pushCommand.setForce(true);

    List<PushResult> listPushResults = (List<PushResult>) pushCommand.call();
    List<String> listPushResultsMessages = new ArrayList<>();
    for (PushResult pushResult : listPushResults) {
        listPushResultsMessages.add(pushResult.getMessages());
    }
    FilesUtils.deleteDirectory(gitworkDir);
    return listPushResultsMessages;
}

From source file:git_manager.tool.GitOperations.java

License:Open Source License

public void resetHard() {
    ResetCommand reset = git.reset();
    reset.setRef(Constants.HEAD);
    //      reset.addPath(".");
    reset.setMode(ResetType.HARD);//from ww  w . j a v  a  2  s .com
    try {
        // Though this should actually have GitManager.frame as the parent, I think 
        // having editor as the parent is far more convenient, since I observe that I generally 
        // tend to keep tool window at the corner and the processing IDE in the centre, 
        // and prefer the dialog box displaying at the center... I think...
        int x = Messages.showYesNoQuestion(editor, "Reset sketch to previous commit?",
                "Are you sure you want to reset the entire skecthbook to<br>the exact state it was in the previous commit?",
                "All changes made since then will be permanently lost.");
        if (x == JOptionPane.YES_OPTION) {
            x = Messages.showYesNoQuestion(editor, "Really reset sketch to previous commit?",
                    "Are you absolutely, positively sure you want to reset the entire<br>skecthbook to the exact state it was in the previous commit?",
                    "All changes made since then will be permanently lost, and even<br>git can't recover them.");
            if (x == JOptionPane.YES_OPTION) {
                reset.call();
                System.out
                        .println("Hard reset completed. Sketch is now exactly like the last snapshot/commit.");
            }
        }
    } catch (CheckoutConflictException e) {
        e.printStackTrace();
    } catch (GitAPIException e) {
        e.printStackTrace();
    }
}

From source file:org.ajoberstar.gradle.git.tasks.GitReset.java

License:Apache License

/**
 * Reset the changes as configured./*from   www .ja  v a  2  s. c  o m*/
 */
@TaskAction
public void reset() {
    ResetCommand cmd = getGit().reset();
    cmd.setRef(getRef());
    cmd.setMode(getResetType());

    List<String> pathsToReset = getPaths();
    for (String path : pathsToReset) {
        cmd.addPath(path);
    }

    try {
        cmd.call();
    } catch (CheckoutConflictException e) {
        throw new GradleException("The working tree changes conflict with the specified commit.", e);
    } catch (GitAPIException e) {
        throw new GradleException("Problem with reset.", e);
    }
    //TODO add progress monitor to log progress to Gradle status bar
}

From source file:org.archicontribs.modelrepository.grafico.MergeConflictHandler.java

License:Open Source License

private void resetToState(String ref) throws IOException, GitAPIException {
    // Reset HARD  which will lose all changes
    try (Git git = Git.open(fLocalGitFolder)) {
        ResetCommand resetCommand = git.reset();
        resetCommand.setRef(ref);
        resetCommand.setMode(ResetType.HARD);
        resetCommand.call();//  ww w  . j a  v  a  2  s .  co m
    }
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public void reset(ResetRequest request) throws GitException {
    try {//from  w  w w . j ava 2 s.com
        ResetCommand resetCommand = getGit().reset();
        resetCommand.setRef(request.getCommit());
        List<String> patterns = request.getFilePattern();
        patterns.stream().forEach(resetCommand::addPath);

        if (request.getType() != null && patterns.isEmpty()) {
            switch (request.getType()) {
            case HARD:
                resetCommand.setMode(ResetType.HARD);
                break;
            case KEEP:
                resetCommand.setMode(ResetType.KEEP);
                break;
            case MERGE:
                resetCommand.setMode(ResetType.MERGE);
                break;
            case MIXED:
                resetCommand.setMode(ResetType.MIXED);
                break;
            case SOFT:
                resetCommand.setMode(ResetType.SOFT);
                break;
            }
        }

        resetCommand.call();
    } catch (GitAPIException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}

From source file:org.eclipse.egit.core.op.RemoveFromIndexOperation.java

License:Open Source License

private static GitCommand<?> prepareCommand(Repository repository, Collection<String> paths) {
    Git git = new Git(repository);
    if (hasHead(repository)) {
        ResetCommand resetCommand = git.reset();
        resetCommand.setRef(HEAD);
        for (String path : paths)
            resetCommand.addPath(getCommandPath(path));
        return resetCommand;
    } else {//from w ww.ja v a 2 s  . com
        RmCommand rmCommand = git.rm();
        rmCommand.setCached(true);
        for (String path : paths)
            rmCommand.addFilepattern(getCommandPath(path));
        return rmCommand;
    }
}