use of com.google.gerrit.extensions.common.BlameInfo in project gerrit by GerritCodeReview.
the class GetBlame method apply.
@Override
public Response<List<BlameInfo>> apply(FileResource resource) throws RestApiException, IOException, InvalidChangeOperationException {
Project.NameKey project = resource.getRevision().getChange().getProject();
try (Repository repository = repoManager.openRepository(project);
InMemoryInserter ins = new InMemoryInserter(repository);
ObjectReader reader = ins.newReader();
RevWalk revWalk = new RevWalk(reader)) {
String refName = resource.getRevision().getEdit().isPresent() ? resource.getRevision().getEdit().get().getRefName() : resource.getRevision().getPatchSet().refName();
Ref ref = repository.findRef(refName);
if (ref == null) {
throw new ResourceNotFoundException("unknown ref " + refName);
}
ObjectId objectId = ref.getObjectId();
RevCommit revCommit = revWalk.parseCommit(objectId);
RevCommit[] parents = revCommit.getParents();
String path = resource.getPatchKey().fileName();
List<BlameInfo> result;
if (!base) {
result = blame(revCommit, path, repository, revWalk);
} else if (parents.length == 0) {
throw new ResourceNotFoundException("Initial commit doesn't have base");
} else if (parents.length == 1) {
result = blame(parents[0], path, repository, revWalk);
} else if (parents.length == 2) {
ObjectId automerge = autoMerger.lookupFromGitOrMergeInMemory(repository, revWalk, ins, revCommit, mergeStrategy);
result = blame(automerge, path, repository, revWalk);
} else {
throw new ResourceNotFoundException("Cannot generate blame for merge commit with more than 2 parents");
}
Response<List<BlameInfo>> r = Response.ok(result);
if (resource.isCacheable()) {
r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
}
return r;
}
}
use of com.google.gerrit.extensions.common.BlameInfo in project gerrit by GerritCodeReview.
the class GetBlame method toBlameInfo.
private static BlameInfo toBlameInfo(RevCommit commit, PersonIdent sourceAuthor) {
BlameInfo blameInfo = new BlameInfo();
blameInfo.author = sourceAuthor.getName();
blameInfo.id = commit.getName();
blameInfo.commitMsg = commit.getFullMessage();
blameInfo.time = commit.getCommitTime();
return blameInfo;
}
use of com.google.gerrit.extensions.common.BlameInfo in project gerrit by GerritCodeReview.
the class GetBlame method toBlameInfo.
private static BlameInfo toBlameInfo(RevCommit commit, PersonIdent sourceAuthor) {
BlameInfo blameInfo = new BlameInfo();
blameInfo.author = sourceAuthor.getName();
blameInfo.id = commit.getName();
blameInfo.commitMsg = commit.getFullMessage();
blameInfo.time = commit.getCommitTime();
return blameInfo;
}
use of com.google.gerrit.extensions.common.BlameInfo in project gerrit by GerritCodeReview.
the class GetBlame method apply.
@Override
public Response<List<BlameInfo>> apply(FileResource resource) throws RestApiException, OrmException, IOException, InvalidChangeOperationException {
if (!allowBlame) {
throw new BadRequestException("blame is disabled");
}
Project.NameKey project = resource.getRevision().getChange().getProject();
try (Repository repository = repoManager.openRepository(project);
ObjectInserter ins = repository.newObjectInserter();
ObjectReader reader = ins.newReader();
RevWalk revWalk = new RevWalk(reader)) {
String refName = resource.getRevision().getEdit().isPresent() ? resource.getRevision().getEdit().get().getRefName() : resource.getRevision().getPatchSet().getRefName();
Ref ref = repository.findRef(refName);
if (ref == null) {
throw new ResourceNotFoundException("unknown ref " + refName);
}
ObjectId objectId = ref.getObjectId();
RevCommit revCommit = revWalk.parseCommit(objectId);
RevCommit[] parents = revCommit.getParents();
String path = resource.getPatchKey().getFileName();
List<BlameInfo> result;
if (!base) {
result = blame(revCommit, path, repository, revWalk);
} else if (parents.length == 0) {
throw new ResourceNotFoundException("Initial commit doesn't have base");
} else if (parents.length == 1) {
result = blame(parents[0], path, repository, revWalk);
} else if (parents.length == 2) {
ObjectId automerge = autoMerger.merge(repository, revWalk, ins, revCommit, mergeStrategy);
result = blame(automerge, path, repository, revWalk);
} else {
throw new ResourceNotFoundException("Cannot generate blame for merge commit with more than 2 parents");
}
Response<List<BlameInfo>> r = Response.ok(result);
if (resource.isCacheable()) {
r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
}
return r;
}
}
use of com.google.gerrit.extensions.common.BlameInfo in project gerrit by GerritCodeReview.
the class GetBlame method blame.
private List<BlameInfo> blame(ObjectId id, String path, Repository repository, RevWalk revWalk) throws IOException {
ListMultimap<BlameInfo, RangeInfo> ranges = MultimapBuilder.hashKeys().arrayListValues().build();
List<BlameInfo> result = new ArrayList<>();
if (blameCache.findLastCommit(repository, id, path) == null) {
return result;
}
List<Region> blameRegions = blameCache.get(repository, id, path);
int from = 1;
for (Region region : blameRegions) {
RevCommit commit = revWalk.parseCommit(region.getSourceCommit());
BlameInfo blameInfo = toBlameInfo(commit, region.getSourceAuthor());
ranges.put(blameInfo, new RangeInfo(from, from + region.getCount() - 1));
from += region.getCount();
}
for (BlameInfo key : ranges.keySet()) {
key.ranges = ranges.get(key);
result.add(key);
}
return result;
}
Aggregations