Search in sources :

Example 31 with RefUpdate

use of org.eclipse.jgit.lib.RefUpdate in project gerrit by GerritCodeReview.

the class StarredChangesUtil method deleteRef.

private void deleteRef(Repository repo, String refName, ObjectId oldObjectId) throws IOException {
    if (ObjectId.zeroId().equals(oldObjectId)) {
        // ref doesn't exist
        return;
    }
    try (TraceTimer traceTimer = TraceContext.newTimer("Delete star labels", Metadata.builder().noteDbRefName(refName).build())) {
        RefUpdate u = repo.updateRef(refName);
        u.setForceUpdate(true);
        u.setExpectedOldObjectId(oldObjectId);
        u.setRefLogIdent(serverIdent.get());
        u.setRefLogMessage("Unstar change", true);
        RefUpdate.Result result = u.delete();
        switch(result) {
            case FORCED:
                gitRefUpdated.fire(allUsers, u, null);
                return;
            case LOCK_FAILURE:
                throw new LockFailureException(String.format("Delete star ref %s failed", refName), u);
            case NEW:
            case NO_CHANGE:
            case FAST_FORWARD:
            case IO_FAILURE:
            case NOT_ATTEMPTED:
            case REJECTED:
            case REJECTED_CURRENT_BRANCH:
            case RENAMED:
            case REJECTED_MISSING_OBJECT:
            case REJECTED_OTHER_REASON:
            default:
                throw new StorageException(String.format("Delete star ref %s failed: %s", refName, result.name()));
        }
    }
}
Also used : TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) StorageException(com.google.gerrit.exceptions.StorageException) LockFailureException(com.google.gerrit.git.LockFailureException) RefUpdate(org.eclipse.jgit.lib.RefUpdate) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate)

Example 32 with RefUpdate

use of org.eclipse.jgit.lib.RefUpdate in project gerrit by GerritCodeReview.

the class ProjectResetter method restoreRefs.

private void restoreRefs() throws IOException {
    for (Map.Entry<Project.NameKey, Collection<RefState>> e : savedRefStatesByProject.asMap().entrySet()) {
        try (Repository repo = repoManager.openRepository(e.getKey())) {
            for (RefState refState : e.getValue()) {
                if (refState.match(repo)) {
                    keptRefsByProject.put(e.getKey(), refState.ref());
                    continue;
                }
                Ref ref = repo.exactRef(refState.ref());
                RefUpdate updateRef = repo.updateRef(refState.ref());
                updateRef.setExpectedOldObjectId(ref != null ? ref.getObjectId() : ObjectId.zeroId());
                updateRef.setNewObjectId(refState.id());
                updateRef.setForceUpdate(true);
                RefUpdate.Result result = updateRef.update();
                checkState(result == RefUpdate.Result.FORCED || result == RefUpdate.Result.NEW, "resetting branch %s in %s failed", refState.ref(), e.getKey());
                restoredRefsByProject.put(e.getKey(), refState.ref());
            }
        }
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) RefState(com.google.gerrit.index.RefState) Collection(java.util.Collection) Map(java.util.Map) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 33 with RefUpdate

use of org.eclipse.jgit.lib.RefUpdate in project gerrit by GerritCodeReview.

the class ProjectResetter method deleteNewlyCreatedRefs.

private void deleteNewlyCreatedRefs() throws IOException {
    for (Map.Entry<Project.NameKey, Collection<String>> e : refsPatternByProject.asMap().entrySet()) {
        try (Repository repo = repoManager.openRepository(e.getKey())) {
            Collection<Ref> nonRestoredRefs = repo.getRefDatabase().getRefs().stream().filter(r -> !keptRefsByProject.containsEntry(e.getKey(), r.getName()) && !restoredRefsByProject.containsEntry(e.getKey(), r.getName())).collect(toSet());
            for (String refPattern : e.getValue()) {
                RefPatternMatcher matcher = RefPatternMatcher.getMatcher(refPattern);
                for (Ref ref : nonRestoredRefs) {
                    if (matcher.match(ref.getName(), null) && !deletedRefsByProject.containsEntry(e.getKey(), ref.getName())) {
                        RefUpdate updateRef = repo.updateRef(ref.getName());
                        updateRef.setExpectedOldObjectId(ref.getObjectId());
                        updateRef.setNewObjectId(ObjectId.zeroId());
                        updateRef.setForceUpdate(true);
                        RefUpdate.Result result = updateRef.delete();
                        checkState(result == RefUpdate.Result.FORCED, "deleting branch %s in %s failed", ref.getName(), e.getKey());
                        deletedRefsByProject.put(e.getKey(), ref.getName());
                    }
                }
            }
        }
    }
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) AccountCache(com.google.gerrit.server.account.AccountCache) Arrays(java.util.Arrays) ProjectCache(com.google.gerrit.server.project.ProjectCache) MultimapBuilder(com.google.common.collect.MultimapBuilder) Inject(com.google.inject.Inject) Multimap(com.google.common.collect.Multimap) RefPatternMatcher(com.google.gerrit.server.project.RefPatternMatcher) GroupIndexer(com.google.gerrit.server.index.group.GroupIndexer) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) RefNames(com.google.gerrit.entities.RefNames) Collectors.toSet(java.util.stream.Collectors.toSet) AccountGroup(com.google.gerrit.entities.AccountGroup) RefState(com.google.gerrit.index.RefState) Collection(java.util.Collection) Account(com.google.gerrit.entities.Account) Set(java.util.Set) RefUpdate(org.eclipse.jgit.lib.RefUpdate) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) GroupIncludeCache(com.google.gerrit.server.account.GroupIncludeCache) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ObjectId(org.eclipse.jgit.lib.ObjectId) Objects(java.util.Objects) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) Stream(java.util.stream.Stream) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) Ref(org.eclipse.jgit.lib.Ref) Project(com.google.gerrit.entities.Project) REFS_USERS(com.google.gerrit.entities.RefNames.REFS_USERS) GroupCache(com.google.gerrit.server.account.GroupCache) Repository(org.eclipse.jgit.lib.Repository) AccountIndexer(com.google.gerrit.server.index.account.AccountIndexer) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) RefPatternMatcher(com.google.gerrit.server.project.RefPatternMatcher) Collection(java.util.Collection) Map(java.util.Map) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 34 with RefUpdate

use of org.eclipse.jgit.lib.RefUpdate in project gerrit by GerritCodeReview.

the class ConsistencyCheckerIT method deleteRef.

private void deleteRef(String refName) throws Exception {
    RefUpdate ru = serverSideTestRepo.getRepository().updateRef(refName, true);
    ru.setForceUpdate(true);
    assertThat(ru.delete()).isEqualTo(RefUpdate.Result.FORCED);
}
Also used : RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 35 with RefUpdate

use of org.eclipse.jgit.lib.RefUpdate in project gerrit by GerritCodeReview.

the class ConsistencyCheckerIT method missingDestRef.

@Test
public void missingDestRef() throws Exception {
    ChangeNotes notes = insertChange();
    String ref = "refs/heads/master";
    // Detach head so we're allowed to delete ref.
    serverSideTestRepo.reset(serverSideTestRepo.getRepository().exactRef(ref).getObjectId());
    RefUpdate ru = serverSideTestRepo.getRepository().updateRef(ref);
    ru.setForceUpdate(true);
    assertThat(ru.delete()).isEqualTo(RefUpdate.Result.FORCED);
    assertProblems(notes, null, problem("Destination ref not found (may be new branch): " + ref));
}
Also used : ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) RefUpdate(org.eclipse.jgit.lib.RefUpdate) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

RefUpdate (org.eclipse.jgit.lib.RefUpdate)110 Repository (org.eclipse.jgit.lib.Repository)51 ObjectId (org.eclipse.jgit.lib.ObjectId)45 IOException (java.io.IOException)34 Test (org.junit.Test)32 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)27 TestRepository (org.eclipse.jgit.junit.TestRepository)26 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)24 RevWalk (org.eclipse.jgit.revwalk.RevWalk)23 Result (org.eclipse.jgit.lib.RefUpdate.Result)22 RevCommit (org.eclipse.jgit.revwalk.RevCommit)20 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)18 Ref (org.eclipse.jgit.lib.Ref)17 RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)17 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)15 BatchRefUpdate (org.eclipse.jgit.lib.BatchRefUpdate)14 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)12 LockFailureException (com.google.gerrit.git.LockFailureException)10 PersonIdent (org.eclipse.jgit.lib.PersonIdent)9 DirCache (org.eclipse.jgit.dircache.DirCache)8