use of org.eclipse.jgit.errors.InvalidObjectIdException in project gerrit by GerritCodeReview.
the class PureRevert method get.
public boolean get(ChangeNotes notes, Optional<String> claimedOriginal) throws IOException, BadRequestException, ResourceConflictException {
PatchSet currentPatchSet = notes.getCurrentPatchSet();
if (currentPatchSet == null) {
throw new ResourceConflictException("current revision is missing");
}
if (!claimedOriginal.isPresent()) {
return pureRevertCache.isPureRevert(notes);
}
ObjectId claimedOriginalObjectId;
try {
claimedOriginalObjectId = ObjectId.fromString(claimedOriginal.get());
} catch (InvalidObjectIdException e) {
throw new BadRequestException("invalid object ID", e);
}
return pureRevertCache.isPureRevert(notes.getProjectName(), notes.getCurrentPatchSet().commitId(), claimedOriginalObjectId);
}
use of org.eclipse.jgit.errors.InvalidObjectIdException in project gerrit by GerritCodeReview.
the class GetMetaDiff method getOldMetaRevId.
private String getOldMetaRevId(ChangeResource resource) throws IOException, BadRequestException, PreconditionFailedException {
if (!oldMetaRevId.isEmpty()) {
return oldMetaRevId;
}
String newMetaRevId = getNewMetaRevId(resource);
try (Repository repo = repoManager.openRepository(resource.getProject());
RevWalk rw = new RevWalk(repo)) {
ObjectId resourceId = ObjectId.fromString(newMetaRevId);
RevCommit commit = rw.parseCommit(resourceId);
return commit.getParentCount() == 0 ? resourceId.getName() : commit.getParent(0).getId().getName();
} catch (InvalidObjectIdException e) {
throw new BadRequestException("invalid meta SHA1: " + newMetaRevId, e);
} catch (MissingObjectException e) {
throw new PreconditionFailedException(e.getMessage());
}
}
use of org.eclipse.jgit.errors.InvalidObjectIdException in project gerrit by GerritCodeReview.
the class PatchSetEvent method setRevision.
private void setRevision(ChangeUpdate update, PatchSet ps) throws IOException {
String rev = ps.getRevision().get();
String cert = ps.getPushCertificate();
ObjectId id;
try {
id = ObjectId.fromString(rev);
} catch (InvalidObjectIdException e) {
update.setRevisionForMissingCommit(rev, cert);
return;
}
try {
update.setCommit(rw, id, cert);
} catch (MissingObjectException e) {
update.setRevisionForMissingCommit(rev, cert);
return;
}
}
use of org.eclipse.jgit.errors.InvalidObjectIdException in project gerrit by GerritCodeReview.
the class CreateChange method getParentCommit.
@Nullable
private ObjectId getParentCommit(Repository repo, RevWalk revWalk, String inputBranch, @Nullable Boolean newBranch, @Nullable PatchSet basePatchSet, @Nullable String baseCommit, @Nullable MergeInput mergeInput) throws BadRequestException, IOException, UnprocessableEntityException, ResourceConflictException {
if (basePatchSet != null) {
return basePatchSet.commitId();
}
Ref destRef = repo.getRefDatabase().exactRef(inputBranch);
ObjectId parentCommit;
if (baseCommit != null) {
try {
parentCommit = ObjectId.fromString(baseCommit);
} catch (InvalidObjectIdException e) {
throw new UnprocessableEntityException(String.format("Base %s doesn't represent a valid SHA-1", baseCommit), e);
}
RevCommit parentRevCommit;
try {
parentRevCommit = revWalk.parseCommit(parentCommit);
} catch (MissingObjectException e) {
throw new UnprocessableEntityException(String.format("Base %s doesn't exist", baseCommit), e);
}
if (destRef == null) {
throw new BadRequestException("Destination branch does not exist");
}
RevCommit destRefRevCommit = revWalk.parseCommit(destRef.getObjectId());
if (!revWalk.isMergedInto(parentRevCommit, destRefRevCommit)) {
throw new BadRequestException(String.format("Commit %s doesn't exist on ref %s", baseCommit, inputBranch));
}
} else {
if (destRef != null) {
if (Boolean.TRUE.equals(newBranch)) {
throw new ResourceConflictException(String.format("Branch %s already exists.", inputBranch));
}
parentCommit = destRef.getObjectId();
} else {
if (Boolean.TRUE.equals(newBranch)) {
if (mergeInput != null) {
throw new BadRequestException("Cannot create merge: destination branch does not exist");
}
parentCommit = null;
} else {
throw new BadRequestException("Destination branch does not exist");
}
}
}
return parentCommit;
}
use of org.eclipse.jgit.errors.InvalidObjectIdException in project gerrit by GerritCodeReview.
the class CherryPickChange method getBaseCommit.
private RevCommit getBaseCommit(Ref destRef, String project, RevWalk revWalk, String base) throws RestApiException, IOException {
RevCommit destRefTip = revWalk.parseCommit(destRef.getObjectId());
// The tip commit of the destination ref is the default base for the newly created change.
if (Strings.isNullOrEmpty(base)) {
return destRefTip;
}
ObjectId baseObjectId;
try {
baseObjectId = ObjectId.fromString(base);
} catch (InvalidObjectIdException e) {
throw new BadRequestException(String.format("Base %s doesn't represent a valid SHA-1", base), e);
}
RevCommit baseCommit;
try {
baseCommit = revWalk.parseCommit(baseObjectId);
} catch (MissingObjectException e) {
throw new UnprocessableEntityException(String.format("Base %s doesn't exist", baseObjectId.name()), e);
}
InternalChangeQuery changeQuery = queryProvider.get();
changeQuery.enforceVisibility(true);
List<ChangeData> changeDatas = changeQuery.byBranchCommit(project, destRef.getName(), base);
if (changeDatas.isEmpty()) {
if (revWalk.isMergedInto(baseCommit, destRefTip)) {
// The base commit is a merged commit with no change associated.
return baseCommit;
}
throw new UnprocessableEntityException(String.format("Commit %s does not exist on branch %s", base, destRef.getName()));
} else if (changeDatas.size() != 1) {
throw new ResourceConflictException("Multiple changes found for commit " + base);
}
Change change = changeDatas.get(0).change();
if (!change.isAbandoned()) {
// The base commit is a valid change revision.
return baseCommit;
}
throw new ResourceConflictException(String.format("Change %s with commit %s is %s", change.getChangeId(), base, ChangeUtil.status(change)));
}
Aggregations