Search in sources :

Example 36 with RefUpdate

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

the class RepoSequence method storeNew.

public void storeNew(int value) {
    counterLock.lock();
    try (Repository repo = repoManager.openRepository(projectName);
        RevWalk rw = new RevWalk(repo)) {
        Optional<IntBlob> blob = IntBlob.parse(repo, refName, rw);
        afterReadRef.run();
        ObjectId oldId;
        if (!blob.isPresent()) {
            oldId = ObjectId.zeroId();
        } else {
            oldId = blob.get().id();
        }
        RefUpdate refUpdate = IntBlob.tryStore(repo, rw, projectName, refName, oldId, value, gitRefUpdated);
        RefUpdateUtil.checkResult(refUpdate);
        counter = value;
        limit = counter + batchSize;
        acquireCount++;
    } catch (IOException e) {
        throw new StorageException(e);
    } finally {
        counterLock.unlock();
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) StorageException(com.google.gerrit.exceptions.StorageException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 37 with RefUpdate

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

the class BaseCommitUtil method updateRef.

private static RevCommit updateRef(Repository repo, RevWalk rw, String refName, ObjectId autoMergeId, RevCommit merge) throws IOException {
    RefUpdate ru = repo.updateRef(refName);
    ru.setNewObjectId(autoMergeId);
    ru.disableRefLog();
    switch(ru.update()) {
        case FAST_FORWARD:
        case FORCED:
        case NEW:
        case NO_CHANGE:
            return rw.parseCommit(autoMergeId);
        case LOCK_FAILURE:
            throw new LockFailureException(String.format("Failed to create auto-merge of %s", merge.name()), ru);
        case IO_FAILURE:
        case NOT_ATTEMPTED:
        case REJECTED:
        case REJECTED_CURRENT_BRANCH:
        case REJECTED_MISSING_OBJECT:
        case REJECTED_OTHER_REASON:
        case RENAMED:
        default:
            throw new IOException(String.format("Failed to create auto-merge of %s: Cannot write %s (%s)", merge.name(), refName, ru.getResult()));
    }
}
Also used : IOException(java.io.IOException) LockFailureException(com.google.gerrit.git.LockFailureException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 38 with RefUpdate

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

the class GroupsConsistencyIT method missingGroupRef.

@Test
public void missingGroupRef() throws Exception {
    try (Repository repo = repoManager.openRepository(allUsers)) {
        RefUpdate ru = repo.updateRef(RefNames.refsGroups(AccountGroup.uuid(g1.id)));
        ru.setForceUpdate(true);
        RefUpdate.Result result = ru.delete();
        assertThat(result).isEqualTo(Result.FORCED);
    }
    assertError("missing as group ref");
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Result(org.eclipse.jgit.lib.RefUpdate.Result) RefUpdate(org.eclipse.jgit.lib.RefUpdate) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 39 with RefUpdate

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

the class GroupsConsistencyIT method missingGroupNameRef.

@Test
public void missingGroupNameRef() throws Exception {
    try (Repository repo = repoManager.openRepository(allUsers)) {
        RefUpdate ru = repo.updateRef(RefNames.REFS_GROUPNAMES);
        ru.setForceUpdate(true);
        RefUpdate.Result result = ru.delete();
        assertThat(result).isEqualTo(Result.FORCED);
    }
    assertError("refs/meta/group-names does not exist");
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Result(org.eclipse.jgit.lib.RefUpdate.Result) RefUpdate(org.eclipse.jgit.lib.RefUpdate) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 40 with RefUpdate

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

the class CreateChangeIT method addNonCommitHead.

@Before
public void addNonCommitHead() throws Exception {
    try (Repository repo = repoManager.openRepository(project);
        ObjectInserter ins = repo.newObjectInserter()) {
        ObjectId answer = ins.insert(Constants.OBJ_BLOB, new byte[] { 42 });
        ins.flush();
        ins.close();
        RefUpdate update = repo.getRefDatabase().newUpdate("refs/heads/answer", false);
        update.setNewObjectId(answer);
        assertThat(update.forceUpdate()).isEqualTo(RefUpdate.Result.NEW);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) RefUpdate(org.eclipse.jgit.lib.RefUpdate) Before(org.junit.Before)

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