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

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

Introduction

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

Prototype

public ResetCommand addPath(String path) 

Source Link

Document

Repository relative path of file or directory to reset

Usage

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

License:Apache License

/**
 * Reset the changes as configured.//from  ww  w  . j  ava 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.apache.stratos.cartridge.agent.artifact.deployment.synchronizer.git.impl.GitBasedArtifactRepository.java

License:Apache License

private void resetToRemoteHead(RepositoryContext gitRepoCtx, List<String> paths) {

    ResetCommand resetCmd = gitRepoCtx.getGit().reset();

    // reset type is HARD, to remote master branch
    resetCmd.setMode(ResetCommand.ResetType.HARD).setRef(
            GitDeploymentSynchronizerConstants.ORIGIN + "/" + GitDeploymentSynchronizerConstants.MASTER);

    // add paths/*from  w w  w .  jav  a 2 s .c  o m*/
    for (String path : paths) {
        resetCmd.addPath(path);
        if (log.isDebugEnabled()) {
            log.debug("Added the file path " + path + " to reset");
        }
    }

    try {
        resetCmd.call();
        log.info("Reset the local branch to origin master successfully");

    } catch (GitAPIException e) {
        log.error("Reset to origin master failed", e);
    }

}

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);/*from  w w w  .j a  va 2 s. c o  m*/
        for (String path : paths)
            resetCommand.addPath(getCommandPath(path));
        return resetCommand;
    } else {
        RmCommand rmCommand = git.rm();
        rmCommand.setCached(true);
        for (String path : paths)
            rmCommand.addFilepattern(getCommandPath(path));
        return rmCommand;
    }
}

From source file:org.eclipse.orion.server.git.servlets.GitIndexHandlerV1.java

License:Open Source License

private boolean handlePost(HttpServletRequest request, HttpServletResponse response, Repository db, IPath path)
        throws ServletException, NoFilepatternException, IOException, JSONException {
    JSONObject toReset = OrionServlet.readJSONRequest(request);
    String resetType = toReset.optString(GitConstants.KEY_RESET_TYPE, null);
    if (resetType != null) {
        JSONArray paths = toReset.optJSONArray(ProtocolConstants.KEY_PATH);
        if (paths != null) {
            String msg = NLS.bind("Mixing {0} and {1} parameters is not allowed.",
                    new Object[] { ProtocolConstants.KEY_PATH, GitConstants.KEY_RESET_TYPE });
            return statusHandler.handleRequest(request, response,
                    new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
        }/*from www.  jav  a 2  s . c  om*/
        String ref = toReset.optString(GitConstants.KEY_TAG_COMMIT, Constants.HEAD);
        try {
            ResetType type = ResetType.valueOf(resetType);
            switch (type) {
            case MIXED:
            case HARD:
                Git git = new Git(db);
                // "git reset --{type} HEAD ."
                git.reset().setMode(type).setRef(ref).call();
                return true;
            case KEEP:
            case MERGE:
            case SOFT:
                String msg = NLS.bind("The reset type is not yet supported: {0}.", resetType);
                return statusHandler.handleRequest(request, response,
                        new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_IMPLEMENTED, msg, null));
            }
        } catch (IllegalArgumentException e) {
            String msg = NLS.bind("Unknown or malformed reset type: {0}.", resetType);
            return statusHandler.handleRequest(request, response,
                    new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
        }
    } else {
        String commit = toReset.optString(GitConstants.KEY_TAG_COMMIT, null);
        if (commit != null) {
            String msg = NLS.bind("Mixing {0} and {1} parameters is not allowed.",
                    new Object[] { ProtocolConstants.KEY_PATH, GitConstants.KEY_TAG_COMMIT });
            return statusHandler.handleRequest(request, response,
                    new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
        }
        JSONArray paths = toReset.optJSONArray(ProtocolConstants.KEY_PATH);
        Git git = new Git(db);
        ResetCommand reset = git.reset().setRef(Constants.HEAD);
        if (paths != null) {
            for (int i = 0; i < paths.length(); i++) {
                reset.addPath(paths.getString(i));
            }
        } else {
            String p = path.removeFirstSegments(2).toString();
            if (p.isEmpty()) {
                String msg = "Path cannot be empty.";
                return statusHandler.handleRequest(request, response,
                        new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
            }
            reset.addPath(p);
        }
        reset.call();
        return true;
    }
    return false;
}

From source file:org.openflexo.hannah.IterativeFileGenerator.java

License:Open Source License

/**
 * <p>Ends the generation. It asks to resolve conflicts (if any). By 
 * default conflict are resolved using user modifications.</p>
 * @param callback callback to handle conflicts
 * @throws IOException/* w  ww. ja v a  2  s. c  om*/
 */
public void end(ConflictHandler callback) throws IOException, GitAPIException {
    final Status status = git.status().call();

    // checks if needs commit.
    if (status.isClean() == false) {

        // checks for files to add
        boolean execute = false;
        final AddCommand add = git.add();
        for (String filename : status.getModified()) {
            execute = true;
            add.addFilepattern(filename);
        }

        for (String filename : status.getUntracked()) {
            execute = true;
            add.addFilepattern(filename);
        }
        if (execute)
            add.call();

        // checks for files to remove
        execute = false;
        final RmCommand rm = git.rm();
        for (String filename : status.getMissing()) {
            execute = true;
            rm.addFilepattern(filename);
        }
        if (execute)
            rm.call();

        git.commit().setMessage("Generation").call();
    }

    // checks out master branch
    git.checkout().setName(MASTER).call();

    // merges generation branch with master (resolving conflict with USER).
    final Repository repo = git.getRepository();
    final Ref generationHead = repo.getRef(GENERATION);
    final MergeStatus mergeStatus = git.merge().include(generationHead).call().getMergeStatus();

    // in case of conflicts, uses the resolution mode to choose the outcome
    if (mergeStatus == MergeStatus.CONFLICTING) {
        // maps to stores confliting files
        final Map<String, DirCacheEntry> baseEntry = new LinkedHashMap<String, DirCacheEntry>();
        final Map<String, DirCacheEntry> userEntry = new LinkedHashMap<String, DirCacheEntry>();
        final Map<String, DirCacheEntry> generationEntry = new LinkedHashMap<String, DirCacheEntry>();

        // for all conflicting entry collects base, user and generation entries. 
        final DirCache cache = repo.lockDirCache();
        for (int i = 0; i < cache.getEntryCount(); i++) {
            final DirCacheEntry entry = cache.getEntry(i);
            switch (entry.getStage()) {
            case DirCacheEntry.STAGE_1:
                baseEntry.put(entry.getPathString(), entry);
                break;
            case DirCacheEntry.STAGE_2:
                userEntry.put(entry.getPathString(), entry);
                break;
            case DirCacheEntry.STAGE_3:
                generationEntry.put(entry.getPathString(), entry);
                break;
            }
        }

        // creates list of conflicting files
        final List<ConflictingFile> conflictingFiles = new ArrayList<ConflictingFile>();
        final MergeAlgorithm mergeAlgorithm = new MergeAlgorithm();
        for (final String path : baseEntry.keySet()) {
            final RawText baseText = getRawText(baseEntry.get(path));
            final RawText userText = getRawText(userEntry.get(path));
            final RawText generationText = getRawText(generationEntry.get(path));

            final MergeResult<RawText> result = mergeAlgorithm.merge(RawTextComparator.DEFAULT, baseText,
                    userText, generationText);
            conflictingFiles.add(new ConflictingFile(path, result));

        }
        // unlocks cache.
        cache.unlock();

        // calls the callback
        callback.conflicts(conflictingFiles);

        // prepares the reset command
        final ResetCommand reset = git.reset();

        // applies callback selections
        for (final ConflictingFile conflictingFile : conflictingFiles) {
            final File file = new File(repo.getWorkTree(), conflictingFile.getPath());
            FileUtil.writeFile(file, conflictingFile.getContents(), "UTF-8");

            reset.addPath(conflictingFile.getPath());
        }

        // resets repository state to allows commit.
        reset.call();

        // commit resolutions
        git.commit().setMessage("User/Generation merge conflicts resolutions.").call();
    }

    // renames git repository to hannah
    gitFolder.renameTo(hannahFolder);
}