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

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

Introduction

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

Prototype

public Result delete() throws IOException 

Source Link

Document

Delete the ref.

Usage

From source file:com.genuitec.eclipse.gerrit.tools.internal.fbranches.commands.DeleteFeatureBranchCommand.java

License:Open Source License

private void deleteBranchRemotely(final Repository repository, final String branch) throws Exception {
    PushOperationSpecification spec = BranchingUtils.setupPush(repository, ":" + branch); //$NON-NLS-1$

    PushOperationUI op = new PushOperationUI(repository, spec, false) {

        @Override//from  w  w  w.ja  v a 2  s. c  o m
        public PushOperationResult execute(IProgressMonitor monitor) throws CoreException {
            PushOperationResult result = super.execute(monitor);
            for (URIish uri : result.getURIs()) {
                if (result.getErrorMessage(uri) != null) {
                    return result;
                }
            }
            try {
                //delete reference from local repo
                RefUpdate updateRef = repository
                        .updateRef("refs/remotes/origin/" + branch.substring("refs/heads/".length())); //$NON-NLS-1$ //$NON-NLS-2$
                updateRef.setForceUpdate(true);
                updateRef.delete();
            } catch (Exception e) {
                RepositoryUtils.handleException(e);
            }
            return result;
        }

    };
    op.setCredentialsProvider(new EGitCredentialsProvider());
    op.start();
}

From source file:com.gitblit.plugin.smartticketbranches.SmartTicketBranchesHook.java

License:Apache License

@Override
public void onUpdateTicket(TicketModel ticket, Change change) {
    if (!ticket.hasPatchsets()) {
        // ticket has no patchsets, nothing to do
        return;//from ww  w . j a v  a 2 s . c o m
    }

    if (!change.isStatusChange()) {
        // not a status change, nothing to do
        return;
    }

    final Patchset ps = ticket.getCurrentPatchset();
    final String branch = PatchsetCommand.getTicketBranch(ticket.number);
    final IRepositoryManager repositoryManager = GitblitContext.getManager(IRepositoryManager.class);
    final Repository repo = repositoryManager.getRepository(ticket.repository);
    try {
        switch (change.getStatus()) {
        case New:
            // NOOP, new proposal
            log.debug("new proposal, skipping");
            break;
        case Open:
            /*
             *  Open or Re-open: create branch, if not exists
             */
            if (null == repo.getRef(branch)) {
                log.debug("ticket re-opened, trying to create '{}'", branch);
                RefUpdate ru = repo.updateRef(branch);
                ru.setExpectedOldObjectId(ObjectId.zeroId());
                ru.setNewObjectId(ObjectId.fromString(ps.tip));

                RevWalk rw = null;
                try {
                    rw = new RevWalk(repo);
                    RefUpdate.Result result = ru.update(rw);
                    switch (result) {
                    case NEW:
                        log.info(String.format("%s ticket RE-OPENED, created %s:%s", name, ticket.repository,
                                branch));
                        break;
                    default:
                        log.error(String.format("%s failed to re-create %s:%s (%s)", name, ticket.repository,
                                branch, result));
                        break;
                    }
                } finally {
                    if (rw != null) {
                        rw.release();
                    }
                }
            }
            break;
        default:
            /*
             * Ticket closed: delete branch, if exists
             */
            log.debug("ticket closed, trying to remove '{}'", branch);
            RefUpdate ru = repo.updateRef(branch);
            ru.setExpectedOldObjectId(ObjectId.fromString(ps.tip));
            ru.setNewObjectId(ObjectId.zeroId());
            ru.setForceUpdate(true);

            RefUpdate.Result result = ru.delete();
            switch (result) {
            case FORCED:
                log.info(String.format("%s ticket %s, removed %s:%s", name, change.getStatus(),
                        ticket.repository, branch));
                break;
            default:
                log.error(String.format("%s failed to remove %s:%s (%s)", name, ticket.repository, branch,
                        result));
                break;
            }
        }
    } catch (IOException e) {
        log.error(null, e);
    } finally {
        if (repo != null) {
            repo.close();
        }
    }
}

From source file:com.gitblit.utils.JGitUtils.java

License:Apache License

/**
 * Deletes the specified branch ref.//  w  w w  .  j ava  2 s . co m
 *
 * @param repository
 * @param branch
 * @return true if successful
 */
public static boolean deleteBranchRef(Repository repository, String branch) {

    try {
        RefUpdate refUpdate = repository.updateRef(branch, false);
        refUpdate.setForceUpdate(true);
        RefUpdate.Result result = refUpdate.delete();
        switch (result) {
        case NEW:
        case FORCED:
        case NO_CHANGE:
        case FAST_FORWARD:
            return true;
        default:
            LOGGER.error(MessageFormat.format("{0} failed to delete to {1} returned result {2}",
                    repository.getDirectory().getAbsolutePath(), branch, result));
        }
    } catch (Throwable t) {
        error(t, repository, "{0} failed to delete {1}", branch);
    }
    return false;
}

From source file:com.google.gerrit.acceptance.api.accounts.AccountIT.java

License:Apache License

@After
public void clearPublicKeyStore() throws Exception {
    try (Repository repo = repoManager.openRepository(allUsers)) {
        Ref ref = repo.getRef(REFS_GPG_KEYS);
        if (ref != null) {
            RefUpdate ru = repo.updateRef(REFS_GPG_KEYS);
            ru.setForceUpdate(true);// w w w . j av  a 2s  . c  o  m
            assertThat(ru.delete()).isEqualTo(RefUpdate.Result.FORCED);
        }
    }
}

From source file:com.google.gerrit.acceptance.api.accounts.AccountIT.java

License:Apache License

@After
public void deleteGpgKeys() throws Exception {
    String ref = REFS_GPG_KEYS;/*from   ww  w.  j a  v a 2s.com*/
    try (Repository repo = repoManager.openRepository(allUsers)) {
        if (repo.getRefDatabase().exactRef(ref) != null) {
            RefUpdate ru = repo.updateRef(ref);
            ru.setForceUpdate(true);
            assert_().withFailureMessage("Failed to delete " + ref).that(ru.delete())
                    .isEqualTo(RefUpdate.Result.FORCED);
        }
    }
}

From source file:com.google.gerrit.acceptance.api.accounts.GeneralPreferencesIT.java

License:Apache License

@After
public void cleanUp() throws Exception {
    gApi.accounts().id(user42.getId().toString()).setPreferences(GeneralPreferencesInfo.defaults());

    try (Repository git = repoManager.openRepository(allUsers)) {
        if (git.exactRef(RefNames.REFS_USERS_DEFAULT) != null) {
            RefUpdate u = git.updateRef(RefNames.REFS_USERS_DEFAULT);
            u.setForceUpdate(true);/*from  www. jav  a 2 s . c om*/
            assertThat(u.delete()).isEqualTo(RefUpdate.Result.FORCED);
        }
    }
    accountCache.evictAll();
}

From source file:com.google.gerrit.acceptance.api.config.GeneralPreferencesIT.java

License:Apache License

@After
public void cleanUp() throws Exception {
    try (Repository git = repoManager.openRepository(allUsers)) {
        if (git.exactRef(RefNames.REFS_USERS_DEFAULT) != null) {
            RefUpdate u = git.updateRef(RefNames.REFS_USERS_DEFAULT);
            u.setForceUpdate(true);/*from   www  .  j av a 2s. c  o  m*/
            assertThat(u.delete()).isEqualTo(RefUpdate.Result.FORCED);
        }
    }
    accountCache.evictAll();
}

From source file:com.google.gerrit.acceptance.server.change.ConsistencyCheckerIT.java

License:Apache License

@Test
public void missingDestRef() throws Exception {
    String ref = "refs/heads/master";
    // Detach head so we're allowed to delete ref.
    testRepo.reset(testRepo.getRepository().getRef(ref).getObjectId());
    RefUpdate ru = testRepo.getRepository().updateRef(ref);
    ru.setForceUpdate(true);//from w  w  w  .ja  v a  2  s . c  om
    assertThat(ru.delete()).isEqualTo(RefUpdate.Result.FORCED);
    Change c = insertChange();
    RevCommit commit = testRepo.commit().create();
    PatchSet ps = newPatchSet(c.currentPatchSetId(), commit, adminId);
    updatePatchSetRef(ps);
    db.patchSets().insert(singleton(ps));

    assertProblems(c, "Destination ref not found (may be new branch): " + ref);
}

From source file:com.google.gerrit.acceptance.server.change.ConsistencyCheckerIT.java

License:Apache License

private void deleteRef(String refName) throws Exception {
    RefUpdate ru = testRepo.getRepository().updateRef(refName, true);
    ru.setForceUpdate(true);// ww  w .j a  v a2  s. com
    assertThat(ru.delete()).isEqualTo(RefUpdate.Result.FORCED);
}

From source file:com.google.gerrit.httpd.rpc.project.DeleteBranches.java

License:Apache License

@Override
public Set<Branch.NameKey> call() throws NoSuchProjectException, RepositoryNotFoundException {
    final ProjectControl projectControl = projectControlFactory.controlFor(projectName);

    for (Branch.NameKey k : toRemove) {
        if (!projectName.equals(k.getParentKey())) {
            throw new IllegalArgumentException("All keys must be from same project");
        }/*from ww  w  .j a v a2  s  . com*/
        if (!projectControl.controlForRef(k).canDelete()) {
            throw new IllegalStateException("Cannot delete " + k.getShortName());
        }
    }

    final Set<Branch.NameKey> deleted = new HashSet<Branch.NameKey>();
    final Repository r = repoManager.openRepository(projectName);
    try {
        for (final Branch.NameKey branchKey : toRemove) {
            final String refname = branchKey.get();
            final RefUpdate.Result result;
            final RefUpdate u;
            try {
                u = r.updateRef(refname);
                u.setForceUpdate(true);
                result = u.delete();
            } catch (IOException e) {
                log.error("Cannot delete " + branchKey, e);
                continue;
            }

            switch (result) {
            case NEW:
            case NO_CHANGE:
            case FAST_FORWARD:
            case FORCED:
                deleted.add(branchKey);
                replication.scheduleUpdate(projectName, refname);
                hooks.doRefUpdatedHook(branchKey, u, identifiedUser.getAccount());
                break;

            case REJECTED_CURRENT_BRANCH:
                log.warn("Cannot delete " + branchKey + ": " + result.name());
                break;

            default:
                log.error("Cannot delete " + branchKey + ": " + result.name());
                break;
            }
        }
    } finally {
        r.close();
    }
    return deleted;
}