Example usage for org.eclipse.jgit.lib RefUpdate getName

List of usage examples for org.eclipse.jgit.lib RefUpdate getName

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib RefUpdate getName.

Prototype

public String getName() 

Source Link

Document

Get the name of the ref this update will operate on.

Usage

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

License:Open Source License

private void writeRef() throws TeamException {
    try {/*from ww  w  .  ja v a2  s .c o  m*/
        final RefUpdate ru = repository.updateRef(Constants.HEAD);
        ru.setNewObjectId(commit.getId());
        String name = refName;
        if (name.startsWith("refs/heads/")) //$NON-NLS-1$
            name = name.substring(11);
        if (name.startsWith("refs/remotes/")) //$NON-NLS-1$
            name = name.substring(13);
        String message = "reset --" //$NON-NLS-1$
                + type.toString().toLowerCase() + " " + name; //$NON-NLS-1$
        ru.setRefLogMessage(message, false);
        if (ru.forceUpdate() == RefUpdate.Result.LOCK_FAILURE)
            throw new TeamException(NLS.bind(CoreText.ResetOperation_cantUpdate, ru.getName()));
    } catch (IOException e) {
        throw new TeamException(NLS.bind(CoreText.ResetOperation_updatingFailed, Constants.HEAD), e);
    }
}

From source file:org.jfrog.bamboo.release.scm.git.ResetCommand.java

License:Eclipse Distribution License

/**
 * Executes the {@code Reset} command. Each instance of this class should only be used for one invocation of the
 * command. Don't call this method twice on an instance.
 *
 * @return the Ref after reset/*from  ww  w  .  j av a  2  s .  c o m*/
 */
@Override
public Ref call() throws GitAPIException {
    checkCallable();

    Ref r;
    RevCommit commit;

    try {
        boolean merging = false;
        if (repo.getRepositoryState().equals(RepositoryState.MERGING)
                || repo.getRepositoryState().equals(RepositoryState.MERGING_RESOLVED)) {
            merging = true;
        }

        // resolve the ref to a commit
        final ObjectId commitId;
        try {
            commitId = repo.resolve(ref);
        } catch (IOException e) {
            throw new JGitInternalException(MessageFormat.format(JGitText.get().cannotRead, ref), e);
        }
        RevWalk rw = new RevWalk(repo);
        try {
            commit = rw.parseCommit(commitId);
        } catch (IOException e) {
            throw new JGitInternalException(
                    MessageFormat.format(JGitText.get().cannotReadCommit, commitId.toString()), e);
        } finally {
            rw.release();
        }

        // write the ref
        final RefUpdate ru = repo.updateRef(Constants.HEAD);
        ru.setNewObjectId(commitId);

        String refName = Repository.shortenRefName(ref);
        String message = "reset --" //$NON-NLS-1$
                + mode.toString().toLowerCase() + " " + refName; //$NON-NLS-1$
        ru.setRefLogMessage(message, false);
        if (ru.forceUpdate() == RefUpdate.Result.LOCK_FAILURE) {
            throw new JGitInternalException(MessageFormat.format(JGitText.get().cannotLock, ru.getName()));
        }

        switch (mode) {
        case HARD:
            checkoutIndex(commit);
            break;
        case MIXED:
            resetIndex(commit);
            break;
        case SOFT: // do nothing, only the ref was changed
            break;
        }

        if (mode != ResetType.SOFT && merging) {
            resetMerge();
        }

        setCallable(false);
        r = ru.getRef();
    } catch (IOException e) {
        throw new JGitInternalException("Error while executing reset command");
    }

    return r;
}

From source file:org.nbgit.client.CommitBuilder.java

License:Open Source License

/**
 * Write the commit to the repository./*from  w  w  w.  ja v a 2 s.co m*/
 *
 * @throws IOException if creation of the commit fails.
 */
public void write() throws IOException {
    index.write();
    final RefUpdate ru = repository.updateRef(Constants.HEAD);
    ObjectId[] parentIds;
    if (ru.getOldObjectId() != null)
        parentIds = new ObjectId[] { ru.getOldObjectId() };
    else
        parentIds = new ObjectId[0];

    ObjectId id = writeCommit(index.writeTree(), parentIds);
    if (!updateRef(ru, id))
        logger.output("Failed to update " + ru.getName() + " to commit " + id + ".");
}

From source file:org.uberfire.java.nio.fs.jgit.util.commands.RefTreeUpdateCommand.java

License:Apache License

private void commit(final Repository repo, final RevCommit original, final BiFunction fun) throws IOException {
    try (final ObjectReader reader = repo.newObjectReader();
            final ObjectInserter inserter = repo.newObjectInserter();
            final RevWalk rw = new RevWalk(reader)) {

        final RefTreeDatabase refdb = (RefTreeDatabase) repo.getRefDatabase();
        final RefDatabase bootstrap = refdb.getBootstrap();
        final RefUpdate refUpdate = bootstrap.newUpdate(refdb.getTxnCommitted(), false);

        final CommitBuilder cb = new CommitBuilder();
        final Ref ref = bootstrap.exactRef(refdb.getTxnCommitted());
        final RefTree tree;
        if (ref != null && ref.getObjectId() != null) {
            tree = RefTree.read(reader, rw.parseTree(ref.getObjectId()));
            cb.setParentId(ref.getObjectId());
            refUpdate.setExpectedOldObjectId(ref.getObjectId());
        } else {// www  .  j a  v a 2 s  . com
            tree = RefTree.newEmptyTree();
            refUpdate.setExpectedOldObjectId(ObjectId.zeroId());
        }

        if (fun.apply(reader, tree)) {
            final Ref ref2 = bootstrap.exactRef(refdb.getTxnCommitted());
            if (ref2 == null || ref2.getObjectId().equals(ref != null ? ref.getObjectId() : null)) {
                cb.setTreeId(tree.writeTree(inserter));
                if (original != null) {
                    cb.setAuthor(original.getAuthorIdent());
                    cb.setCommitter(original.getAuthorIdent());
                } else {
                    final PersonIdent personIdent = new PersonIdent("user", "user@example.com");
                    cb.setAuthor(personIdent);
                    cb.setCommitter(personIdent);
                }
                refUpdate.setNewObjectId(inserter.insert(cb));
                inserter.flush();
                final RefUpdate.Result result = refUpdate.update(rw);
                switch (result) {
                case NEW:
                case FAST_FORWARD:
                    break;
                default:
                    throw new RuntimeException(
                            repo.getDirectory() + " -> " + result.toString() + " : " + refUpdate.getName());
                }
                final File commited = new File(repo.getDirectory(), refdb.getTxnCommitted());
                final File accepted = new File(repo.getDirectory(), refdb.getTxnNamespace() + "accepted");
                Files.copy(commited.toPath(), accepted.toPath(), StandardCopyOption.REPLACE_EXISTING);
            }
        }
    }
}

From source file:svnserver.repository.git.push.GitPushEmbedded.java

License:GNU General Public License

private void runReceiveHook(@NotNull Repository repository, @NotNull RefUpdate refUpdate, @NotNull String hook,
        @NotNull User userInfo) throws IOException, SVNException {
    runHook(repository, hook, userInfo, processBuilder -> {
        final Process process = processBuilder.start();
        try (Writer stdin = new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8)) {
            stdin.write(getObjectId(refUpdate.getOldObjectId()));
            stdin.write(' ');
            stdin.write(getObjectId(refUpdate.getNewObjectId()));
            stdin.write(' ');
            stdin.write(refUpdate.getName());
            stdin.write('\n');
        }/*from w ww  . jav a2  s  .c  o m*/
        return process;
    });
}

From source file:svnserver.repository.git.push.GitPushEmbedded.java

License:GNU General Public License

private void runUpdateHook(@NotNull Repository repository, @NotNull RefUpdate refUpdate, @Nullable String hook,
        @NotNull User userInfo) throws IOException, SVNException {
    runHook(repository, hook, userInfo, processBuilder -> {
        processBuilder.command().addAll(Arrays.asList(refUpdate.getName(),
                getObjectId(refUpdate.getOldObjectId()), getObjectId(refUpdate.getNewObjectId())));
        return processBuilder.start();
    });//www  . j a  va2  s  .co m
}