use of org.eclipse.jgit.errors.MissingObjectException in project gerrit by GerritCodeReview.
the class CommentContextLoader method getContext.
/**
* Load the comment context for multiple contextInputs at once. This method will open the
* repository and read the source files for all necessary contextInputs' file paths.
*
* @param contextInputs a list of contextInputs.
* @return a Map where all entries consist of the input contextInputs and the values are their
* corresponding {@link CommentContext}.
*/
public Map<ContextInput, CommentContext> getContext(Collection<ContextInput> contextInputs) throws IOException {
ImmutableMap.Builder<ContextInput, CommentContext> result = ImmutableMap.builderWithExpectedSize(Iterables.size(contextInputs));
// Group contextInputs by commit ID so that each commit is parsed only once
Map<ObjectId, List<ContextInput>> commentsByCommitId = contextInputs.stream().collect(groupingBy(ContextInput::commitId));
try (Repository repo = repoManager.openRepository(project);
RevWalk rw = new RevWalk(repo)) {
for (ObjectId commitId : commentsByCommitId.keySet()) {
RevCommit commit;
try {
commit = rw.parseCommit(commitId);
} catch (IncorrectObjectTypeException | MissingObjectException e) {
logger.atWarning().log("Commit %s is missing or has an incorrect object type", commitId);
commentsByCommitId.get(commitId).forEach(contextInput -> result.put(contextInput, CommentContext.empty()));
continue;
}
for (ContextInput contextInput : commentsByCommitId.get(commitId)) {
Optional<Range> range = getStartAndEndLines(contextInput);
if (!range.isPresent()) {
result.put(contextInput, CommentContext.empty());
continue;
}
String filePath = contextInput.filePath();
switch(filePath) {
case COMMIT_MSG:
result.put(contextInput, getContextForCommitMessage(rw.getObjectReader(), commit, range.get(), contextInput.contextPadding()));
break;
case MERGE_LIST:
result.put(contextInput, getContextForMergeList(rw.getObjectReader(), commit, range.get(), contextInput.contextPadding()));
break;
default:
result.put(contextInput, getContextForFilePath(repo, rw, commit, filePath, range.get(), contextInput.contextPadding()));
}
}
}
return result.build();
}
}
use of org.eclipse.jgit.errors.MissingObjectException in project gerrit by GerritCodeReview.
the class Reachable method fromRefs.
boolean fromRefs(Project.NameKey project, Repository repo, RevCommit commit, List<Ref> refs, Optional<CurrentUser> optionalUserProvider) {
try (RevWalk rw = new RevWalk(repo)) {
Collection<Ref> filtered = optionalUserProvider.map(permissionBackend::user).orElse(permissionBackend.currentUser()).project(project).filter(refs, repo, RefFilterOptions.defaults());
Collection<RevCommit> visible = new ArrayList<>();
for (Ref r : filtered) {
try {
visible.add(rw.parseCommit(r.getObjectId()));
} catch (IncorrectObjectTypeException notCommit) {
// is common in the Linux kernel or git.git repository.
continue;
} catch (MissingObjectException notHere) {
// Log the problem with this branch, but keep processing.
logger.atWarning().log("Reference %s in %s points to dangling object %s", r.getName(), repo.getDirectory(), r.getObjectId());
continue;
}
}
// from the reachability check, do the trace here:
try (TraceTimer timer = TraceContext.newTimer("ReachabilityChecker.areAllReachable", Metadata.builder().projectName(project.get()).resourceCount(refs.size()).build())) {
ReachabilityChecker checker = rw.getObjectReader().createReachabilityChecker(rw);
Optional<RevCommit> unreachable = checker.areAllReachable(ImmutableList.of(rw.parseCommit(commit)), visible.stream());
return !unreachable.isPresent();
}
} catch (IOException | PermissionBackendException e) {
logger.atSevere().withCause(e).log("Cannot verify permissions to commit object %s in repository %s", commit.name(), project);
return false;
}
}
use of org.eclipse.jgit.errors.MissingObjectException in project gerrit by GerritCodeReview.
the class RefUtil method verifyConnected.
public static RevWalk verifyConnected(Repository repo, ObjectId revid) throws InvalidRevisionException {
try {
ObjectWalk rw = new ObjectWalk(repo);
try {
rw.markStart(rw.parseCommit(revid));
} catch (IncorrectObjectTypeException err) {
throw new InvalidRevisionException(revid.name(), err);
}
RefDatabase refDb = repo.getRefDatabase();
Iterable<Ref> refs = Iterables.concat(refDb.getRefsByPrefix(Constants.R_HEADS), refDb.getRefsByPrefix(Constants.R_TAGS));
Ref rc = refDb.exactRef(RefNames.REFS_CONFIG);
if (rc != null) {
refs = Iterables.concat(refs, Collections.singleton(rc));
}
for (Ref r : refs) {
try {
rw.markUninteresting(rw.parseAny(r.getObjectId()));
} catch (MissingObjectException err) {
continue;
}
}
rw.checkConnectivity();
return rw;
} catch (IncorrectObjectTypeException | MissingObjectException err) {
throw new InvalidRevisionException(revid.name(), err);
} catch (IOException err) {
logger.atSevere().withCause(err).log("Repository \"%s\" may be corrupt; suggest running git fsck", repo.getDirectory());
throw new InvalidRevisionException(revid.name());
}
}
use of org.eclipse.jgit.errors.MissingObjectException in project gerrit by GerritCodeReview.
the class BanCommit method ban.
/**
* Bans a list of commits from the given project.
*
* <p>The user must be specified, so it can be checked for the {@code BAN_COMMIT} permission.
*/
public BanCommitResult ban(Project.NameKey project, CurrentUser user, List<ObjectId> commitsToBan, String reason) throws AuthException, IOException, PermissionBackendException {
permissionBackend.user(user).project(project).check(ProjectPermission.BAN_COMMIT);
final BanCommitResult result = new BanCommitResult();
NoteMap banCommitNotes = NoteMap.newEmptyMap();
// Add a note for each banned commit to notes.
try (Repository repo = repoManager.openRepository(project);
RevWalk revWalk = new RevWalk(repo);
ObjectInserter inserter = repo.newObjectInserter()) {
ObjectId noteId = null;
for (ObjectId commitToBan : commitsToBan) {
try {
revWalk.parseCommit(commitToBan);
} catch (MissingObjectException e) {
// Ignore exception, non-existing commits can be banned.
} catch (IncorrectObjectTypeException e) {
result.notACommit(commitToBan);
continue;
}
if (noteId == null) {
noteId = createNoteContent(reason, inserter);
}
banCommitNotes.set(commitToBan, noteId);
}
NotesBranchUtil notesBranchUtil = notesBranchUtilFactory.create(project, repo, inserter);
NoteMap newlyCreated = notesBranchUtil.commitNewNotes(banCommitNotes, REFS_REJECT_COMMITS, createPersonIdent(), buildCommitMessage(commitsToBan, reason));
for (Note n : banCommitNotes) {
if (newlyCreated.contains(n)) {
result.commitBanned(n);
} else {
result.commitAlreadyBanned(n);
}
}
return result;
}
}
use of org.eclipse.jgit.errors.MissingObjectException 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;
}
Aggregations