use of org.eclipse.jgit.lib.RefUpdate in project gerrit by GerritCodeReview.
the class VersionedMetaDataOnInit method updateRef.
private void updateRef(Repository repo, PersonIdent ident, ObjectId newRevision, String refLogMsg) throws IOException {
RefUpdate ru = repo.updateRef(getRefName());
ru.setRefLogIdent(ident);
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:
case REJECTED_MISSING_OBJECT:
case REJECTED_OTHER_REASON:
default:
throw new IOException("Failed to update " + getRefName() + " of " + project + ": " + r.name());
}
}
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()));
}
}
}
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());
}
}
}
}
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());
}
}
}
}
}
}
use of org.eclipse.jgit.lib.RefUpdate in project gerrit by GerritCodeReview.
the class GeneralPreferencesIT method cleanUp.
@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);
assertThat(u.delete()).isEqualTo(RefUpdate.Result.FORCED);
}
}
accountCache.evictAll();
}
Aggregations