use of com.google.gerrit.git.LockFailureException 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 com.google.gerrit.git.LockFailureException in project gerrit by GerritCodeReview.
the class StarredChangesUtil method unstarAllForChangeDeletion.
/**
* Unstar the given change for all users.
*
* <p>Intended for use only when we're about to delete a change. For that reason, the change is
* not reindexed.
*
* @param changeId change ID.
* @throws IOException if an error occurred.
*/
public void unstarAllForChangeDeletion(Change.Id changeId) throws IOException {
try (Repository repo = repoManager.openRepository(allUsers);
RevWalk rw = new RevWalk(repo)) {
BatchRefUpdate batchUpdate = repo.getRefDatabase().newBatchUpdate();
batchUpdate.setAllowNonFastForwards(true);
batchUpdate.setRefLogIdent(serverIdent.get());
batchUpdate.setRefLogMessage("Unstar change " + changeId.get(), true);
for (Account.Id accountId : byChangeFromIndex(changeId).keySet()) {
String refName = RefNames.refsStarredChanges(changeId, accountId);
Ref ref = repo.getRefDatabase().exactRef(refName);
if (ref != null) {
batchUpdate.addCommand(new ReceiveCommand(ref.getObjectId(), ObjectId.zeroId(), refName));
}
}
batchUpdate.execute(rw, NullProgressMonitor.INSTANCE);
for (ReceiveCommand command : batchUpdate.getCommands()) {
if (command.getResult() != ReceiveCommand.Result.OK) {
String message = String.format("Unstar change %d failed, ref %s could not be deleted: %s", changeId.get(), command.getRefName(), command.getResult());
if (command.getResult() == ReceiveCommand.Result.LOCK_FAILURE) {
throw new LockFailureException(message, batchUpdate);
}
throw new GitUpdateFailureException(message, batchUpdate);
}
}
}
}
use of com.google.gerrit.git.LockFailureException 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()));
}
}
use of com.google.gerrit.git.LockFailureException in project gerrit by GerritCodeReview.
the class ProjectCreator method createEmptyCommits.
private void createEmptyCommits(Repository repo, Project.NameKey project, List<String> refs) throws IOException {
try (ObjectInserter oi = repo.newObjectInserter()) {
CommitBuilder cb = new CommitBuilder();
cb.setTreeId(oi.insert(Constants.OBJ_TREE, new byte[] {}));
cb.setAuthor(metaDataUpdateFactory.getUserPersonIdent());
cb.setCommitter(serverIdent.get());
cb.setMessage("Initial empty repository\n");
ObjectId id = oi.insert(cb);
oi.flush();
for (String ref : refs) {
RefUpdate ru = repo.updateRef(ref);
ru.setNewObjectId(id);
Result result = ru.update();
switch(result) {
case NEW:
referenceUpdated.fire(project, ru, ReceiveCommand.Type.CREATE, identifiedUser.get().state());
break;
case LOCK_FAILURE:
throw new LockFailureException(String.format("Failed to create ref \"%s\"", ref), ru);
case FAST_FORWARD:
case FORCED:
case IO_FAILURE:
case NOT_ATTEMPTED:
case NO_CHANGE:
case REJECTED:
case REJECTED_CURRENT_BRANCH:
case RENAMED:
case REJECTED_MISSING_OBJECT:
case REJECTED_OTHER_REASON:
default:
{
throw new IOException(String.format("Failed to create ref \"%s\": %s", ref, result.name()));
}
}
}
} catch (IOException e) {
logger.atSevere().withCause(e).log("Cannot create empty commit for %s", project.get());
throw e;
}
}
use of com.google.gerrit.git.LockFailureException in project gerrit by GerritCodeReview.
the class SetHead method apply.
@Override
public Response<String> apply(ProjectResource rsrc, HeadInput input) throws AuthException, ResourceNotFoundException, BadRequestException, UnprocessableEntityException, IOException, PermissionBackendException {
if (input == null || Strings.isNullOrEmpty(input.ref)) {
throw new BadRequestException("ref required");
}
String ref = RefNames.fullName(input.ref);
permissionBackend.user(rsrc.getUser()).project(rsrc.getNameKey()).ref(ref).check(RefPermission.SET_HEAD);
try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
Map<String, Ref> cur = repo.getRefDatabase().exactRef(Constants.HEAD, ref);
if (!cur.containsKey(ref)) {
throw new UnprocessableEntityException(String.format("Ref Not Found: %s", ref));
}
final String oldHead = cur.get(Constants.HEAD).getTarget().getName();
final String newHead = ref;
if (!oldHead.equals(newHead)) {
final RefUpdate u = repo.updateRef(Constants.HEAD, true);
u.setRefLogIdent(identifiedUser.get().newRefLogIdent());
RefUpdate.Result res = u.link(newHead);
switch(res) {
case NO_CHANGE:
case RENAMED:
case FORCED:
case NEW:
break;
case LOCK_FAILURE:
throw new LockFailureException("Setting HEAD failed", u);
case FAST_FORWARD:
case IO_FAILURE:
case NOT_ATTEMPTED:
case REJECTED:
case REJECTED_CURRENT_BRANCH:
case REJECTED_MISSING_OBJECT:
case REJECTED_OTHER_REASON:
default:
throw new IOException("Setting HEAD failed with " + res);
}
fire(rsrc.getNameKey(), oldHead, newHead);
}
return Response.ok(ref);
} catch (RepositoryNotFoundException e) {
throw new ResourceNotFoundException(rsrc.getName(), e);
}
}
Aggregations