use of org.eclipse.jgit.lib.ObjectReader 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 org.eclipse.jgit.lib.ObjectReader in project gerrit by GerritCodeReview.
the class AbstractQueryChangesTest method newPatchSet.
protected Change newPatchSet(TestRepository<Repo> repo, Change c, CurrentUser user) throws Exception {
// Add a new file so the patch set is not a trivial rebase, to avoid default
// Code-Review label copying.
int n = c.currentPatchSetId().get() + 1;
RevCommit commit = repo.parseBody(repo.commit().message("message").add("file" + n, "contents " + n).create());
PatchSetInserter inserter = patchSetFactory.create(changeNotesFactory.createChecked(c), PatchSet.id(c.getId(), n), commit).setFireRevisionCreated(false).setValidate(false);
try (BatchUpdate bu = updateFactory.create(c.getProject(), user, TimeUtil.now());
ObjectInserter oi = repo.getRepository().newObjectInserter();
ObjectReader reader = oi.newReader();
RevWalk rw = new RevWalk(reader)) {
bu.setRepository(repo.getRepository(), rw, oi);
bu.setNotify(NotifyResolver.Result.none());
bu.addOp(c.getId(), inserter);
bu.execute();
}
return inserter.getChange();
}
use of org.eclipse.jgit.lib.ObjectReader in project sonarqube by SonarSource.
the class GitScmProvider method prepareNewTree.
private static AbstractTreeIterator prepareNewTree(Repository repo) throws IOException {
CanonicalTreeParser treeParser = new CanonicalTreeParser();
try (ObjectReader objectReader = repo.newObjectReader()) {
Ref head = getHead(repo);
if (head == null) {
throw new IOException("HEAD reference not found");
}
treeParser.reset(objectReader, repo.parseCommit(head.getObjectId()).getTree());
}
return treeParser;
}
use of org.eclipse.jgit.lib.ObjectReader in project curiostack by curioswitch.
the class CurioGenericCiPlugin method computeAffectedFilesForMaster.
// Assume all tested changes are in a single commit, which works when commits are always squashed.
private static Set<String> computeAffectedFilesForMaster(Git git, Project rootProject) throws IOException {
ObjectId oldTreeId = git.getRepository().resolve("HEAD^{tree}");
ObjectId newTreeId = git.getRepository().resolve("HEAD^^{tree}");
final CanonicalTreeParser oldTreeParser;
final CanonicalTreeParser newTreeParser;
try (ObjectReader reader = git.getRepository().newObjectReader()) {
oldTreeParser = parser(reader, oldTreeId);
newTreeParser = parser(reader, newTreeId);
}
return computeAffectedFiles(git, oldTreeParser, newTreeParser, rootProject);
}
use of org.eclipse.jgit.lib.ObjectReader in project che by eclipse.
the class JGitDiffPage method commitToIndex.
/**
* Show changes between specified revision and index. If
* <code>commitId == null</code> then view changes between HEAD and index.
*
* @param commitId
* id of commit, pass <code>null</code> is the same as pass HEAD
* @param formatter
* diff formatter
* @return list of diff entries
* @throws IOException
* if any i/o errors occurs
*/
private List<DiffEntry> commitToIndex(String commitId, DiffFormatter formatter) throws IOException {
if (commitId == null) {
commitId = Constants.HEAD;
}
ObjectId commitA = repository.resolve(commitId);
if (commitA == null) {
throw new IllegalArgumentException("Invalid commit id " + commitId);
}
RevTree treeA;
try (RevWalk revWalkA = new RevWalk(repository)) {
treeA = revWalkA.parseTree(commitA);
}
DirCache dirCache = null;
List<DiffEntry> diff;
try (ObjectReader reader = repository.newObjectReader()) {
dirCache = repository.lockDirCache();
CanonicalTreeParser iterA = new CanonicalTreeParser();
iterA.reset(reader, treeA);
DirCacheIterator iterB = new DirCacheIterator(dirCache);
if (!params.isNoRenames()) {
// Use embedded RenameDetector it works well with index and
// revision history.
formatter.setDetectRenames(true);
int renameLimit = params.getRenameLimit();
if (renameLimit > 0) {
formatter.getRenameDetector().setRenameLimit(renameLimit);
}
}
diff = formatter.scan(iterA, iterB);
} finally {
if (dirCache != null) {
dirCache.unlock();
}
}
return diff;
}
Aggregations