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: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);
    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");
        }//w ww.ja va 2s .  c  o m
        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;
}

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

License:Apache License

private void set(String branch, ObjectId id) throws IOException {
    final RefUpdate u = realDb.updateRef(R_HEADS + branch);
    u.setForceUpdate(true);
    u.setNewObjectId(id);/*ww w  . j a  va  2s .  c  om*/
    switch (u.update()) {
    case NEW:
    case FAST_FORWARD:
    case FORCED:
        break;
    default:
        fail("unexpected update failure " + branch + " " + u.getResult());
    }
}

From source file:com.google.gerrit.server.account.AccountsUpdate.java

License:Apache License

public static void createUserBranch(Repository repo, ObjectInserter oi, PersonIdent committerIdent,
        PersonIdent authorIdent, Account account) throws IOException {
    ObjectId id = createInitialEmptyCommit(oi, committerIdent, authorIdent, account.getRegisteredOn());

    String refName = RefNames.refsUsers(account.getId());
    RefUpdate ru = repo.updateRef(refName);
    ru.setExpectedOldObjectId(ObjectId.zeroId());
    ru.setNewObjectId(id);/*w  ww.j a va 2s  . c o m*/
    ru.setForceUpdate(true);
    ru.setRefLogIdent(committerIdent);
    ru.setRefLogMessage("Create Account", true);
    Result result = ru.update();
    if (result != Result.NEW) {
        throw new IOException(String.format("Failed to update ref %s: %s", refName, result.name()));
    }
}

From source file:com.google.gerrit.server.account.AccountsUpdate.java

License:Apache License

public static void deleteUserBranch(Repository repo, PersonIdent refLogIdent, Account.Id accountId)
        throws IOException {
    String refName = RefNames.refsUsers(accountId);
    Ref ref = repo.exactRef(refName);
    if (ref == null) {
        return;//  www . j ava  2s .c o m
    }

    RefUpdate ru = repo.updateRef(refName);
    ru.setExpectedOldObjectId(ref.getObjectId());
    ru.setNewObjectId(ObjectId.zeroId());
    ru.setForceUpdate(true);
    ru.setRefLogIdent(refLogIdent);
    ru.setRefLogMessage("Delete Account", true);
    Result result = ru.delete();
    if (result != Result.FORCED) {
        throw new IOException(String.format("Failed to delete ref %s: %s", refName, result.name()));
    }
}

From source file:com.google.gerrit.server.change.ConsistencyChecker.java

License:Apache License

private void fixPatchSetRef(ProblemInfo p, PatchSet ps) {
    try {//from w ww.  j av  a 2  s .  c  o  m
        RefUpdate ru = repo.updateRef(ps.getId().toRefName());
        ru.setForceUpdate(true);
        ru.setNewObjectId(ObjectId.fromString(ps.getRevision().get()));
        ru.setRefLogIdent(newRefLogIdent());
        ru.setRefLogMessage("Repair patch set ref", true);
        RefUpdate.Result result = ru.update();
        switch (result) {
        case NEW:
        case FORCED:
        case FAST_FORWARD:
        case NO_CHANGE:
            p.status = Status.FIXED;
            p.outcome = "Repaired patch set ref";
            return;
        default:
            p.status = Status.FIX_FAILED;
            p.outcome = "Failed to update patch set ref: " + result;
            return;
        }
    } catch (IOException e) {
        String msg = "Error fixing patch set ref";
        log.warn(msg + ' ' + ps.getId().toRefName(), e);
        p.status = Status.FIX_FAILED;
        p.outcome = msg;
    }
}

From source file:com.google.gerrit.server.change.ConsistencyCheckerTest.java

License:Apache License

@Test
public void missingDestRef() throws Exception {
    RefUpdate ru = repo.getRepository().updateRef("refs/heads/master");
    ru.setForceUpdate(true);
    assertThat(ru.delete()).isEqualTo(RefUpdate.Result.FORCED);
    Change c = insertChange();/*from  ww w  . j a  va2  s  .  c o m*/
    RevCommit commit = repo.commit().create();
    PatchSet ps = newPatchSet(c.currentPatchSetId(), commit, userId);
    updatePatchSetRef(ps);
    db.patchSets().insert(singleton(ps));

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

From source file:com.google.gerrit.server.change.ConsistencyCheckerTest.java

License:Apache License

private void deleteRef(String refName) throws Exception {
    RefUpdate ru = repo.getRepository().updateRef(refName, true);
    ru.setForceUpdate(true);
    assertThat(ru.delete()).isEqualTo(RefUpdate.Result.FORCED);
}

From source file:com.google.gerrit.server.ChangeUtil.java

License:Apache License

public void deleteOnlyDraftPatchSet(PatchSet patch, Change change)
        throws NoSuchChangeException, OrmException, IOException {
    PatchSet.Id patchSetId = patch.getId();
    if (!patch.isDraft()) {
        throw new NoSuchChangeException(patchSetId.getParentKey());
    }//  w  w w  .ja  va  2  s  .  c  om

    try (Repository repo = gitManager.openRepository(change.getProject())) {
        RefUpdate update = repo.updateRef(patch.getRefName());
        update.setForceUpdate(true);
        update.disableRefLog();
        switch (update.delete()) {
        case NEW:
        case FAST_FORWARD:
        case FORCED:
        case NO_CHANGE:
            // Successful deletion.
            break;
        default:
            throw new IOException("Failed to delete ref " + patch.getRefName() + " in " + repo.getDirectory()
                    + ": " + update.getResult());
        }
        gitRefUpdated.fire(change.getProject(), update, ReceiveCommand.Type.DELETE);
    }

    deleteOnlyDraftPatchSetPreserveRef(this.db.get(), patch);
}

From source file:com.google.gerrit.server.edit.ChangeEditModifier.java

License:Apache License

private RefUpdate.Result update(Repository repo, IdentifiedUser me, String refName, RevWalk rw,
        ObjectId oldObjectId, ObjectId newEdit) throws IOException {
    RefUpdate ru = repo.updateRef(refName);
    ru.setExpectedOldObjectId(oldObjectId);
    ru.setNewObjectId(newEdit);/*ww w  . j ava2s  .  c om*/
    ru.setRefLogIdent(getRefLogIdent(me));
    ru.setRefLogMessage("inline edit (amend)", false);
    ru.setForceUpdate(true);
    RefUpdate.Result res = ru.update(rw);
    if (res != RefUpdate.Result.NEW && res != RefUpdate.Result.FORCED) {
        throw new IOException("update failed: " + ru);
    }
    return res;
}