List of usage examples for org.eclipse.jgit.lib RefUpdate setExpectedOldObjectId
public void setExpectedOldObjectId(AnyObjectId id)
From source file:com.google.appraise.eclipse.core.client.git.JgitUtils.java
License:Open Source License
/** * Updates the given ref to the new commit. *//*from w w w . j a va2 s. c om*/ public static RefUpdate updateRef(Repository repo, ObjectId newObjectId, ObjectId expectedOldObjectId, String refName) throws IOException { RefUpdate refUpdate = repo.updateRef(refName); refUpdate.setNewObjectId(newObjectId); if (expectedOldObjectId == null) { refUpdate.setExpectedOldObjectId(ObjectId.zeroId()); } else { refUpdate.setExpectedOldObjectId(expectedOldObjectId); } return refUpdate; }
From source file:com.google.gerrit.acceptance.git.RefAdvertisementIT.java
License:Apache License
private void setUpChanges() throws Exception { gApi.projects().name(project.get()).branch("branch").create(new BranchInput()); // First 2 changes are merged, which means the tags pointing to them are // visible.//from ww w . j a va2s . c om allow(Permission.SUBMIT, admins, "refs/for/refs/heads/*"); PushOneCommit.Result mr = pushFactory.create(db, admin.getIdent(), testRepo).to("refs/for/master%submit"); mr.assertOkStatus(); c1 = mr.getChange(); r1 = changeRefPrefix(c1.getId()); PushOneCommit.Result br = pushFactory.create(db, admin.getIdent(), testRepo).to("refs/for/branch%submit"); br.assertOkStatus(); c2 = br.getChange(); r2 = changeRefPrefix(c2.getId()); // Second 2 changes are unmerged. mr = pushFactory.create(db, admin.getIdent(), testRepo).to("refs/for/master"); mr.assertOkStatus(); c3 = mr.getChange(); r3 = changeRefPrefix(c3.getId()); br = pushFactory.create(db, admin.getIdent(), testRepo).to("refs/for/branch"); br.assertOkStatus(); c4 = br.getChange(); r4 = changeRefPrefix(c4.getId()); try (Repository repo = repoManager.openRepository(project)) { // master-tag -> master RefUpdate mtu = repo.updateRef("refs/tags/master-tag"); mtu.setExpectedOldObjectId(ObjectId.zeroId()); mtu.setNewObjectId(repo.exactRef("refs/heads/master").getObjectId()); assertThat(mtu.update()).isEqualTo(RefUpdate.Result.NEW); // branch-tag -> branch RefUpdate btu = repo.updateRef("refs/tags/branch-tag"); btu.setExpectedOldObjectId(ObjectId.zeroId()); btu.setNewObjectId(repo.exactRef("refs/heads/branch").getObjectId()); assertThat(btu.update()).isEqualTo(RefUpdate.Result.NEW); } }
From source file:com.google.gerrit.acceptance.git.VisibleRefFilterIT.java
License:Apache License
private void setUpChanges() throws Exception { gApi.projects().name(project.get()).branch("branch").create(new BranchInput()); allow(Permission.SUBMIT, admins, "refs/for/refs/heads/*"); PushOneCommit.Result mr = pushFactory.create(db, admin.getIdent(), testRepo).to("refs/for/master%submit"); mr.assertOkStatus();/* w w w . ja v a2 s .co m*/ c1 = mr.getChange().getId(); r1 = changeRefPrefix(c1); PushOneCommit.Result br = pushFactory.create(db, admin.getIdent(), testRepo).to("refs/for/branch%submit"); br.assertOkStatus(); c2 = br.getChange().getId(); r2 = changeRefPrefix(c2); try (Repository repo = repoManager.openRepository(project)) { // master-tag -> master RefUpdate mtu = repo.updateRef("refs/tags/master-tag"); mtu.setExpectedOldObjectId(ObjectId.zeroId()); mtu.setNewObjectId(repo.getRef("refs/heads/master").getObjectId()); assertThat(mtu.update()).isEqualTo(RefUpdate.Result.NEW); // branch-tag -> branch RefUpdate btu = repo.updateRef("refs/tags/branch-tag"); btu.setExpectedOldObjectId(ObjectId.zeroId()); btu.setNewObjectId(repo.getRef("refs/heads/branch").getObjectId()); assertThat(btu.update()).isEqualTo(RefUpdate.Result.NEW); } }
From source file:com.google.gerrit.gpg.PublicKeyStore.java
License:Apache License
/** * Save pending keys to the store./*w w w . j a v a 2 s . com*/ * <p> * One commit is created and the ref updated. The pending list is cleared if * and only if the ref update succeeds, which allows for easy retries in case * of lock failure. * * @param cb commit builder with at least author and identity populated; tree * and parent are ignored. * @return result of the ref update. */ public RefUpdate.Result save(CommitBuilder cb) throws PGPException, IOException { if (toAdd.isEmpty() && toRemove.isEmpty()) { return RefUpdate.Result.NO_CHANGE; } if (reader == null) { load(); } if (notes == null) { notes = NoteMap.newEmptyMap(); } ObjectId newTip; try (ObjectInserter ins = repo.newObjectInserter()) { for (PGPPublicKeyRing keyRing : toAdd.values()) { saveToNotes(ins, keyRing); } for (Fingerprint fp : toRemove) { deleteFromNotes(ins, fp); } cb.setTreeId(notes.writeTree(ins)); if (cb.getTreeId().equals(tip != null ? tip.getTree() : EMPTY_TREE)) { return RefUpdate.Result.NO_CHANGE; } if (tip != null) { cb.setParentId(tip); } if (cb.getMessage() == null) { int n = toAdd.size() + toRemove.size(); cb.setMessage(String.format("Update %d public key%s", n, n != 1 ? "s" : "")); } newTip = ins.insert(cb); ins.flush(); } RefUpdate ru = repo.updateRef(PublicKeyStore.REFS_GPG_KEYS); ru.setExpectedOldObjectId(tip); ru.setNewObjectId(newTip); ru.setRefLogIdent(cb.getCommitter()); ru.setRefLogMessage("Store public keys", true); RefUpdate.Result result = ru.update(); reset(); switch (result) { case FAST_FORWARD: case NEW: case NO_CHANGE: toAdd.clear(); toRemove.clear(); break; default: break; } return result; }
From source file:com.google.gerrit.httpd.rpc.project.AddBranch.java
License:Apache License
@Override public ListBranchesResult call() throws NoSuchProjectException, InvalidNameException, InvalidRevisionException, IOException, BranchCreationNotAllowedException { final ProjectControl projectControl = projectControlFactory.controlFor(projectName); String refname = branchName;/*from w w w .j ava 2s .c o m*/ while (refname.startsWith("/")) { refname = refname.substring(1); } if (!refname.startsWith(Constants.R_REFS)) { refname = Constants.R_HEADS + refname; } if (!Repository.isValidRefName(refname)) { throw new InvalidNameException(); } if (refname.startsWith(ReceiveCommits.NEW_CHANGE)) { throw new BranchCreationNotAllowedException(ReceiveCommits.NEW_CHANGE); } final Branch.NameKey name = new Branch.NameKey(projectName, refname); final RefControl refControl = projectControl.controlForRef(name); final Repository repo = repoManager.openRepository(projectName); try { final ObjectId revid = parseStartingRevision(repo); final RevWalk rw = verifyConnected(repo, revid); RevObject object = rw.parseAny(revid); if (refname.startsWith(Constants.R_HEADS)) { // Ensure that what we start the branch from is a commit. If we // were given a tag, deference to the commit instead. // try { object = rw.parseCommit(object); } catch (IncorrectObjectTypeException notCommit) { throw new IllegalStateException(startingRevision + " not a commit"); } } if (!refControl.canCreate(rw, object)) { throw new IllegalStateException("Cannot create " + refname); } try { final RefUpdate u = repo.updateRef(refname); u.setExpectedOldObjectId(ObjectId.zeroId()); u.setNewObjectId(object.copy()); u.setRefLogIdent(identifiedUser.newRefLogIdent()); u.setRefLogMessage("created via web from " + startingRevision, false); final RefUpdate.Result result = u.update(rw); switch (result) { case FAST_FORWARD: case NEW: case NO_CHANGE: replication.scheduleUpdate(name.getParentKey(), refname); hooks.doRefUpdatedHook(name, u, identifiedUser.getAccount()); break; default: { throw new IOException(result.name()); } } } catch (IOException err) { log.error("Cannot create branch " + name, err); throw err; } } finally { repo.close(); } return listBranchesFactory.create(projectName).call(); }
From source file:com.google.gerrit.pgm.init.AllProjectsConfig.java
License:Apache License
private void updateRef(Repository repo, PersonIdent ident, ObjectId newRevision, String refLogMsg) throws IOException { RefUpdate ru = repo.updateRef(getRefName()); ru.setRefLogIdent(ident);//from w ww . j a v a2 s. co m ru.setNewObjectId(newRevision); ru.setExpectedOldObjectId(revision); ru.setRefLogMessage(refLogMsg, false); RefUpdate.Result r = ru.update(); switch (r) { case FAST_FORWARD: case NEW: case NO_CHANGE: break; default: throw new IOException("Failed to update " + getRefName() + " of " + project + ": " + r.name()); } }
From source file:com.google.gerrit.pgm.init.api.VersionedMetaDataOnInit.java
License:Apache License
private void updateRef(Repository repo, PersonIdent ident, ObjectId newRevision, String refLogMsg) throws IOException { RefUpdate ru = repo.updateRef(getRefName()); ru.setRefLogIdent(ident);/*from w ww . j av a2 s.c o m*/ ru.setNewObjectId(newRevision); ru.setExpectedOldObjectId(revision); ru.setRefLogMessage(refLogMsg, false); RefUpdate.Result r = ru.update(); switch (r) { case FAST_FORWARD: case NEW: case NO_CHANGE: break; case FORCED: case IO_FAILURE: case LOCK_FAILURE: case NOT_ATTEMPTED: case REJECTED: case REJECTED_CURRENT_BRANCH: case RENAMED: default: throw new IOException("Failed to update " + getRefName() + " of " + project + ": " + r.name()); } }
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);/*from ww w . j a va 2 s . c om*/ 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;//w w w . j av a 2s. co 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.account.externalids.ExternalIdsUpdate.java
License:Apache License
/** Commits updates to the external IDs. */ public static ObjectId commit(Repository repo, RevWalk rw, ObjectInserter ins, ObjectId rev, NoteMap noteMap, String commitMessage, PersonIdent committerIdent, PersonIdent authorIdent) throws IOException { CommitBuilder cb = new CommitBuilder(); cb.setMessage(commitMessage);/* w w w . jav a 2 s.c o m*/ cb.setTreeId(noteMap.writeTree(ins)); cb.setAuthor(authorIdent); cb.setCommitter(committerIdent); if (!rev.equals(ObjectId.zeroId())) { cb.setParentId(rev); } else { cb.setParentIds(); // Ref is currently nonexistent, commit has no parents. } if (cb.getTreeId() == null) { if (rev.equals(ObjectId.zeroId())) { cb.setTreeId(emptyTree(ins)); // No parent, assume empty tree. } else { RevCommit p = rw.parseCommit(rev); cb.setTreeId(p.getTree()); // Copy tree from parent. } } ObjectId commitId = ins.insert(cb); ins.flush(); RefUpdate u = repo.updateRef(RefNames.REFS_EXTERNAL_IDS); u.setRefLogIdent(committerIdent); u.setRefLogMessage("Update external IDs", false); u.setExpectedOldObjectId(rev); u.setNewObjectId(commitId); RefUpdate.Result res = u.update(); switch (res) { case NEW: case FAST_FORWARD: case NO_CHANGE: case RENAMED: case FORCED: break; case LOCK_FAILURE: throw new LockFailureException("Updating external IDs failed with " + res); case IO_FAILURE: case NOT_ATTEMPTED: case REJECTED: case REJECTED_CURRENT_BRANCH: default: throw new IOException("Updating external IDs failed with " + res); } return rw.parseCommit(commitId); }