use of org.eclipse.jgit.revwalk.RevWalk in project gerrit by GerritCodeReview.
the class GetArchive method apply.
@Override
public BinaryResult apply(RevisionResource rsrc) throws BadRequestException, IOException, MethodNotAllowedException {
if (Strings.isNullOrEmpty(format)) {
throw new BadRequestException("format is not specified");
}
final ArchiveFormat f = allowedFormats.extensions.get("." + format);
if (f == null) {
throw new BadRequestException("unknown archive format");
}
if (f == ArchiveFormat.ZIP) {
throw new MethodNotAllowedException("zip format is disabled");
}
boolean close = true;
final Repository repo = repoManager.openRepository(rsrc.getControl().getProject().getNameKey());
try {
final RevCommit commit;
String name;
try (RevWalk rw = new RevWalk(repo)) {
commit = rw.parseCommit(ObjectId.fromString(rsrc.getPatchSet().getRevision().get()));
name = name(f, rw, commit);
}
BinaryResult bin = new BinaryResult() {
@Override
public void writeTo(OutputStream out) throws IOException {
try {
new ArchiveCommand(repo).setFormat(f.name()).setTree(commit.getTree()).setOutputStream(out).call();
} catch (GitAPIException e) {
throw new IOException(e);
}
}
@Override
public void close() throws IOException {
repo.close();
}
};
bin.disableGzip().setContentType(f.getMimeType()).setAttachmentName(name);
close = false;
return bin;
} finally {
if (close) {
repo.close();
}
}
}
use of org.eclipse.jgit.revwalk.RevWalk in project gerrit by GerritCodeReview.
the class GetContent method getMessage.
private String getMessage(ChangeNotes notes) throws OrmException, IOException {
Change.Id changeId = notes.getChangeId();
PatchSet ps = psUtil.current(db.get(), notes);
if (ps == null) {
throw new NoSuchChangeException(changeId);
}
try (Repository git = gitManager.openRepository(notes.getProjectName());
RevWalk revWalk = new RevWalk(git)) {
RevCommit commit = revWalk.parseCommit(ObjectId.fromString(ps.getRevision().get()));
return commit.getFullMessage();
} catch (RepositoryNotFoundException e) {
throw new NoSuchChangeException(changeId, e);
}
}
use of org.eclipse.jgit.revwalk.RevWalk in project gerrit by GerritCodeReview.
the class GetMergeList method apply.
@Override
public Response<List<CommitInfo>> apply(RevisionResource rsrc) throws BadRequestException, IOException {
Project.NameKey p = rsrc.getChange().getProject();
try (Repository repo = repoManager.openRepository(p);
RevWalk rw = new RevWalk(repo)) {
String rev = rsrc.getPatchSet().getRevision().get();
RevCommit commit = rw.parseCommit(ObjectId.fromString(rev));
rw.parseBody(commit);
if (uninterestingParent < 1 || uninterestingParent > commit.getParentCount()) {
throw new BadRequestException("No such parent: " + uninterestingParent);
}
if (commit.getParentCount() < 2) {
return createResponse(rsrc, ImmutableList.<CommitInfo>of());
}
List<RevCommit> commits = MergeListBuilder.build(rw, commit, uninterestingParent);
List<CommitInfo> result = new ArrayList<>(commits.size());
ChangeJson changeJson = json.noOptions();
for (RevCommit c : commits) {
result.add(changeJson.toCommit(rsrc.getControl(), rw, c, addLinks, true));
}
return createResponse(rsrc, result);
}
}
use of org.eclipse.jgit.revwalk.RevWalk in project gerrit by GerritCodeReview.
the class ConflictsPredicate method listFiles.
public static List<String> listFiles(Change c, Arguments args, ChangeDataCache changeDataCache) throws OrmException {
try (Repository repo = args.repoManager.openRepository(c.getProject());
RevWalk rw = new RevWalk(repo)) {
RevCommit ps = rw.parseCommit(changeDataCache.getTestAgainst());
if (ps.getParentCount() > 1) {
String dest = c.getDest().get();
Ref destBranch = repo.getRefDatabase().getRef(dest);
destBranch.getObjectId();
rw.setRevFilter(RevFilter.MERGE_BASE);
rw.markStart(rw.parseCommit(destBranch.getObjectId()));
rw.markStart(ps);
RevCommit base = rw.next();
// TODO(zivkov): handle the case with multiple merge bases
List<String> files = new ArrayList<>();
try (TreeWalk tw = new TreeWalk(repo)) {
if (base != null) {
tw.setFilter(TreeFilter.ANY_DIFF);
tw.addTree(base.getTree());
}
tw.addTree(ps.getTree());
tw.setRecursive(true);
while (tw.next()) {
files.add(tw.getPathString());
}
}
return files;
}
return args.changeDataFactory.create(args.db.get(), c).currentFilePaths();
} catch (IOException e) {
throw new OrmException(e);
}
}
use of org.eclipse.jgit.revwalk.RevWalk in project gerrit by GerritCodeReview.
the class OutputStreamQuery method buildChangeAttribute.
private ChangeAttribute buildChangeAttribute(ChangeData d, Map<Project.NameKey, Repository> repos, Map<Project.NameKey, RevWalk> revWalks) throws OrmException, IOException {
ChangeControl cc = d.changeControl().forUser(user);
LabelTypes labelTypes = cc.getLabelTypes();
ChangeAttribute c = eventFactory.asChangeAttribute(db, d.change());
eventFactory.extend(c, d.change());
if (!trackingFooters.isEmpty()) {
eventFactory.addTrackingIds(c, trackingFooters.extract(d.commitFooters()));
}
if (includeAllReviewers) {
eventFactory.addAllReviewers(db, c, d.notes());
}
if (includeSubmitRecords) {
eventFactory.addSubmitRecords(c, new SubmitRuleEvaluator(d).setAllowClosed(true).setAllowDraft(true).evaluate());
}
if (includeCommitMessage) {
eventFactory.addCommitMessage(c, d.commitMessage());
}
RevWalk rw = null;
if (includePatchSets || includeCurrentPatchSet || includeDependencies) {
Project.NameKey p = d.change().getProject();
rw = revWalks.get(p);
// Cache and reuse repos and revwalks.
if (rw == null) {
Repository repo = repoManager.openRepository(p);
checkState(repos.put(p, repo) == null);
rw = new RevWalk(repo);
revWalks.put(p, rw);
}
}
if (includePatchSets) {
eventFactory.addPatchSets(db, rw, c, d.visiblePatchSets(), includeApprovals ? d.approvals().asMap() : null, includeFiles, d.change(), labelTypes);
}
if (includeCurrentPatchSet) {
PatchSet current = d.currentPatchSet();
if (current != null && cc.isPatchVisible(current, d.db())) {
c.currentPatchSet = eventFactory.asPatchSetAttribute(db, rw, d.change(), current);
eventFactory.addApprovals(c.currentPatchSet, d.currentApprovals(), labelTypes);
if (includeFiles) {
eventFactory.addPatchSetFileNames(c.currentPatchSet, d.change(), d.currentPatchSet());
}
if (includeComments) {
eventFactory.addPatchSetComments(c.currentPatchSet, d.publishedComments());
}
}
}
if (includeComments) {
eventFactory.addComments(c, d.messages());
if (includePatchSets) {
eventFactory.addPatchSets(db, rw, c, d.visiblePatchSets(), includeApprovals ? d.approvals().asMap() : null, includeFiles, d.change(), labelTypes);
for (PatchSetAttribute attribute : c.patchSets) {
eventFactory.addPatchSetComments(attribute, d.publishedComments());
}
}
}
if (includeDependencies) {
eventFactory.addDependencies(rw, c, d.change(), d.currentPatchSet());
}
c.plugins = queryProcessor.create(d);
return c;
}
Aggregations