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

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

Introduction

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

Prototype

public void setForceUpdate(boolean b) 

Source Link

Document

Set if this update wants to forcefully change the ref.

Usage

From source file:org.commonjava.gitwrap.BareGitRepository.java

License:Open Source License

public BareGitRepository createTag(final String tagSource, final String tagName, final String message,
        final boolean force) throws GitWrapException {
    try {//from   ww w  .j  a v a 2  s.c o  m
        final ObjectId src = repository.resolve(tagSource);
        if (src == null) {
            throw new GitWrapException("Cannot resolve tag-source: %s", tagSource);
        }

        if (!force && repository.resolve(tagName) != null) {
            throw new GitWrapException("Tag: %s already exists!", tagName);
        }

        String dest = tagName;
        if (!dest.startsWith(Constants.R_TAGS)) {
            dest = Constants.R_TAGS + tagName;
        }

        final String tagShort = dest.substring(Constants.R_TAGS.length());

        final ObjectLoader sourceLoader = repository.open(src);
        final ObjectInserter inserter = repository.newObjectInserter();

        final TagBuilder tag = new TagBuilder();
        tag.setTag(tagShort);
        tag.setTagger(new PersonIdent(repository));
        tag.setObjectId(src, sourceLoader.getType());
        tag.setMessage(message);

        final ObjectId tagId = inserter.insert(tag);
        tag.setTagId(tagId);

        final String refName = Constants.R_TAGS + tag.getTag();

        final RefUpdate tagRef = repository.updateRef(refName);
        tagRef.setNewObjectId(tag.getTagId());
        tagRef.setForceUpdate(force);
        tagRef.setRefLogMessage("Tagging source: " + src.name() + " as " + tagName, false);

        final Result updateResult = tagRef.update();

        switch (updateResult) {
        case NEW:
        case FAST_FORWARD:
        case FORCED: {
            break;
        }
        case REJECTED: {
            throw new GitWrapException("Tag already exists: %s", tagName);
        }
        default: {
            throw new GitWrapException("Cannot lock tag: %s", tagName);
        }
        }
    } catch (final IOException e) {
        throw new GitWrapException("Failed to add tag: %s", e, e.getMessage());
    }

    return this;
}

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

License:Open Source License

@Override
public void tagDelete(TagDeleteRequest request) throws GitException {
    try {/*from ww w  .  j  a v a  2 s  . c  om*/
        String tagName = request.getName();
        Ref tagRef = repository.findRef(tagName);
        if (tagRef == null) {
            throw new IllegalArgumentException("Tag " + tagName + " not found. ");
        }

        RefUpdate updateRef = repository.updateRef(tagRef.getName());
        updateRef.setRefLogMessage("tag deleted", false);
        updateRef.setForceUpdate(true);
        Result deleteResult = updateRef.delete();
        if (deleteResult != Result.FORCED && deleteResult != Result.FAST_FORWARD) {
            throw new GitException(String.format(ERROR_TAG_DELETE, tagName, deleteResult));
        }
    } catch (IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}

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

License:Open Source License

private void updateRepo(ObjectId tagId) throws TeamException {
    String refName = Constants.R_TAGS + tag.getTag();

    try {//from w  ww . j ava2 s  . c  o  m
        RefUpdate tagRef = repo.updateRef(refName);
        tagRef.setNewObjectId(tagId);

        tagRef.setForceUpdate(shouldMoveTag);
        Result updateResult = tagRef.update();

        if (updateResult != Result.NEW && updateResult != Result.FORCED)
            throw new TeamException(NLS.bind(CoreText.TagOperation_taggingFailure, tag.getTag(), updateResult));
    } catch (IOException e) {
        throw new TeamException(NLS.bind(CoreText.TagOperation_taggingFailure, tag.getTag(), e.getMessage()),
                e);
    }
}

From source file:org.eclipse.egit.ui.common.LocalRepositoryTestCase.java

License:Open Source License

protected static File createRemoteRepository(File repositoryDir) throws Exception {
    FileRepository myRepository = lookupRepository(repositoryDir);
    File gitDir = new File(testDirectory, REPO2);
    Repository myRemoteRepository = new FileRepository(gitDir);
    myRemoteRepository.create();//from   w  w  w.j a  v  a  2  s. c  o  m
    // double-check that this is bare
    assertTrue(myRemoteRepository.isBare());

    createStableBranch(myRepository);

    // now we configure a pure push destination
    myRepository.getConfig().setString("remote", "push", "pushurl",
            "file:///" + myRemoteRepository.getDirectory().getPath());
    myRepository.getConfig().setString("remote", "push", "push", "+refs/heads/*:refs/heads/*");

    // and a pure fetch destination
    myRepository.getConfig().setString("remote", "fetch", "url",
            "file:///" + myRemoteRepository.getDirectory().getPath());
    myRepository.getConfig().setString("remote", "fetch", "fetch", "+refs/heads/*:refs/heads/*");

    // a destination with both fetch and push urls and specs
    myRepository.getConfig().setString("remote", "both", "pushurl",
            "file:///" + myRemoteRepository.getDirectory().getPath());
    myRepository.getConfig().setString("remote", "both", "push", "+refs/heads/*:refs/heads/*");
    myRepository.getConfig().setString("remote", "both", "url",
            "file:///" + myRemoteRepository.getDirectory().getPath());
    myRepository.getConfig().setString("remote", "both", "fetch", "+refs/heads/*:refs/heads/*");

    // a destination with only a fetch url and push and fetch specs
    myRepository.getConfig().setString("remote", "mixed", "push", "+refs/heads/*:refs/heads/*");
    myRepository.getConfig().setString("remote", "mixed", "url",
            "file:///" + myRemoteRepository.getDirectory().getPath());
    myRepository.getConfig().setString("remote", "mixed", "fetch", "+refs/heads/*:refs/heads/*");

    myRepository.getConfig().save();
    // and push
    PushConfiguredRemoteAction pa = new PushConfiguredRemoteAction(myRepository, "push");

    pa.run(null, false);

    try {
        // delete the stable branch again
        RefUpdate op = myRepository.updateRef("refs/heads/stable");
        op.setRefLogMessage("branch deleted", //$NON-NLS-1$
                false);
        // we set the force update in order
        // to avoid having this rejected
        // due to minor issues
        op.setForceUpdate(true);
        op.delete();
    } catch (IOException ioe) {
        throw new InvocationTargetException(ioe);
    }
    return myRemoteRepository.getDirectory();
}

From source file:org.eclipse.egit.ui.view.repositories.GitRepositoriesViewTagHandlingTest.java

License:Open Source License

@Before
public void before() throws Exception {
    repository = lookupRepository(repositoryFile);
    for (String ref : repository.getTags().keySet()) {
        RefUpdate op = repository.updateRef(ref, true);
        op.setRefLogMessage("tag deleted", //$NON-NLS-1$
                false);//from  w w  w .  j av  a 2 s.  c  om
        // we set the force update in order
        // to avoid having this rejected
        // due to minor issues
        op.setForceUpdate(true);
        op.delete();
    }
    revWalk = new RevWalk(repository);
}

From source file:org.eclipse.egit.ui.view.repositories.GitRepositoriesViewTestBase.java

License:Open Source License

protected static File createRemoteRepository(File repositoryDir) throws Exception {
    Repository myRepository = org.eclipse.egit.core.Activator.getDefault().getRepositoryCache()
            .lookupRepository(repositoryDir);
    File gitDir = new File(getTestDirectory(), REPO2);
    Repository myRemoteRepository = lookupRepository(gitDir);
    myRemoteRepository.create();//from  www . j a va2 s . c  om

    createStableBranch(myRepository);

    // now we configure the push destination
    myRepository.getConfig().setString("remote", "push", "pushurl",
            "file:///" + myRemoteRepository.getDirectory().getPath());
    myRepository.getConfig().setString("remote", "push", "push", "+refs/heads/*:refs/heads/*");
    // TODO Bug: for some reason, this seems to be required
    myRepository.getConfig().setString(ConfigConstants.CONFIG_CORE_SECTION, null,
            ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "0");

    myRepository.getConfig().save();
    // and push
    PushConfiguredRemoteAction pa = new PushConfiguredRemoteAction(myRepository, "push");

    pa.run(null, false);
    TestUtil.joinJobs(JobFamilies.PUSH);
    try {
        // delete the stable branch again
        RefUpdate op = myRepository.updateRef("refs/heads/stable");
        op.setRefLogMessage("branch deleted", //$NON-NLS-1$
                false);
        // we set the force update in order
        // to avoid having this rejected
        // due to minor issues
        op.setForceUpdate(true);
        op.delete();
    } catch (IOException ioe) {
        throw new InvocationTargetException(ioe);
    }
    return myRemoteRepository.getDirectory();
}

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

License:Eclipse Distribution License

/**
 * Executes the {@code tag} command with all the options and parameters collected by the setter methods of this
 * class. Each instance of this class should only be used for one invocation of the command (means: one call to
 * {@link #call()})//  ww w  .  ja  v  a  2 s .c o m
 *
 * @return a {@link RevTag} object representing the successful tag
 * @throws NoHeadException       when called on a git repo without a HEAD reference
 * @throws JGitInternalException a low-level exception of JGit has occurred. The original exception can be retrieved
 *                               by calling {@link Exception#getCause()}. Expect only {@code IOException's} to be
 *                               wrapped.
 */
@Override
public RefUpdate.Result call()
        throws JGitInternalException, ConcurrentRefUpdateException, InvalidTagNameException, NoHeadException {
    checkCallable();

    RepositoryState state = repo.getRepositoryState();
    processOptions(state);
    Map<String, Ref> tags = repo.getTags();
    for (Map.Entry<String, Ref> entry : tags.entrySet()) {
        if (entry.getKey().equals(getName())) {
            Ref value = entry.getValue();
            RefUpdate update;
            try {
                update = repo.updateRef(value.getName());
                update.setForceUpdate(true);
                RefUpdate.Result delete = update.delete();
                return delete;
            } catch (IOException e) {
                throw new JGitInternalException(JGitText.get().exceptionCaughtDuringExecutionOfTagCommand, e);
            }
        }
    }
    throw new JGitInternalException(JGitText.get().exceptionCaughtDuringExecutionOfTagCommand);
}

From source file:org.kuali.student.git.importer.ConvertOldBranchesToTagsMain.java

License:Educational Community License

/**
 * @param args/*from  w  w  w. j  a  v a 2 s  . co m*/
 */
public static void main(String[] args) {

    if (args.length != 3 && args.length != 4) {
        System.err.println("USAGE: <git repository> <mode> <bare> [<ref prefix>]");
        System.err.println("\t<mode> : tag or delete");
        System.err.println("\t<bare> : 0 (false) or 1 (true)");
        System.err.println("\t<ref prefix> : refs/heads (default) or say refs/remotes/origin (test clone)");
        System.exit(-1);
    }

    boolean bare = false;

    if (args[2].trim().equals("1")) {
        bare = true;
    }

    boolean tagMode = false;

    boolean deleteMode = false;

    if (args[1].equals("tag"))
        tagMode = true;
    else if (args[1].equals("delete"))
        deleteMode = true;

    String refPrefix = Constants.R_HEADS;

    if (args.length == 4)
        refPrefix = args[3].trim();

    try {

        Repository repo = GitRepositoryUtils.buildFileRepository(new File(args[0]).getAbsoluteFile(), false,
                bare);

        Collection<Ref> repositoryHeads = repo.getRefDatabase().getRefs(refPrefix).values();

        RevWalk rw = new RevWalk(repo);

        Git git = new Git(repo);

        ObjectInserter objectInserter = repo.newObjectInserter();

        List<String> branchesToDelete = new ArrayList<>();

        for (Ref ref : repositoryHeads) {

            if (!ref.getName().contains("@"))
                continue; // we only want those with @ in the name

            if (deleteMode)
                branchesToDelete.add(ref.getName());

            if (!tagMode)
                continue;

            // else tag mode

            String simpleTagName = ref.getName().replaceFirst(refPrefix, "");

            RevCommit commit = rw.parseCommit(ref.getObjectId());

            ObjectId tagId = null;

            // tag this commit
            tagId = GitRefUtils.insertTag(simpleTagName, commit, objectInserter);

            if (tagId != null) {

                // update the tag reference
                // copied from JGit's TagCommand
                Result updateResult = GitRefUtils.createTagReference(repo, simpleTagName, tagId);

                if (updateResult != Result.NEW) {
                    log.warn("problem creating tag reference for " + simpleTagName + " result = "
                            + updateResult);
                }
            }

        }

        if (deleteMode) {

            for (String branch : branchesToDelete) {

                RefUpdate update = repo.updateRef(branch);

                update.setForceUpdate(true);

                Result result = update.delete(rw);

                if (result != Result.FORCED) {

                    log.warn("failed to delete the branch ref = " + branch);
                }

            }
        }

        rw.release();

        objectInserter.flush();
        objectInserter.release();

    } catch (Exception e) {

        log.error("unexpected Exception ", e);
    }
}

From source file:org.kuali.student.git.importer.FixImportRepo.java

License:Educational Community License

private static void updateRef(Repository repo, String branchName, String revision, AnyObjectId branchId)
        throws IOException {

    RefUpdate u = repo.updateRef(branchName, true);

    u.setForceUpdate(true);

    String resultMessage = "resetting branch " + branchName + " to revision " + revision + " at "
            + branchId.name();/*from ww  w . j  av  a  2s.c o m*/

    u.setNewObjectId(branchId);

    u.setRefLogMessage(resultMessage, true);

    Result result = u.update();

    log.info(resultMessage + " result = " + result);

}

From source file:org.kuali.student.git.importer.FixImportRepo.java

License:Educational Community License

private static void deleteRef(Repository repo, String branchName, String revision) throws IOException {

    RefUpdate u = repo.updateRef(branchName, true);

    u.setForceUpdate(true);

    String resultMessage = "deleting branch " + branchName + " at revision " + revision;

    u.setRefLogMessage(resultMessage, true);

    Result result = u.delete();//from   ww w . j  a va 2  s. co  m

    log.info(resultMessage + " result = " + result);

}