use of org.eclipse.jgit.lib.ObjectReader in project fuse-karaf by jboss-fuse.
the class GitPatchRepositoryImpl method diff.
@Override
public List<DiffEntry> diff(Git git, RevCommit commit1, RevCommit commit2, boolean showNameAndStatusOnly) throws GitAPIException, IOException {
ObjectReader reader = git.getRepository().newObjectReader();
CanonicalTreeParser ctp1 = new CanonicalTreeParser();
CanonicalTreeParser ctp2 = new CanonicalTreeParser();
if (commit1.getTree() == null) {
commit1 = new RevWalk(git.getRepository()).parseCommit(commit1);
}
if (commit2.getTree() == null) {
commit2 = new RevWalk(git.getRepository()).parseCommit(commit2);
}
ctp1.reset(reader, commit1.getTree());
ctp2.reset(reader, commit2.getTree());
return git.diff().setShowNameAndStatusOnly(showNameAndStatusOnly).setOldTree(ctp1).setNewTree(ctp2).call();
}
use of org.eclipse.jgit.lib.ObjectReader in project fuse-karaf by jboss-fuse.
the class DiffUtils method generateDiffReport.
/**
* <p>Having four commits, generate single, HTML report about all modified files</p>
* <p>Please excuse inline html code.</p>
* @param patch
* @param git
* @param conflicts
* @param base
* @param ours
* @param theirs
* @param resolved
* @param result
*/
public static void generateDiffReport(Patch patch, PatchResult patchResult, Git git, Set<String> conflicts, RevCommit base, RevCommit ours, RevCommit theirs, RevCommit resolved, Writer result) throws IOException {
ObjectReader reader = git.getRepository().newObjectReader();
CanonicalTreeParser ctpBase = new CanonicalTreeParser();
CanonicalTreeParser ctpOurs = new CanonicalTreeParser();
CanonicalTreeParser ctpTheirs = new CanonicalTreeParser();
CanonicalTreeParser ctpResolved = new CanonicalTreeParser();
ctpBase.reset(reader, base.getTree());
ctpOurs.reset(reader, ours.getTree());
ctpTheirs.reset(reader, theirs.getTree());
ctpResolved.reset(reader, resolved.getTree());
// this map will contain 3 diffs for each file/path:
// 0 - diff between base and "ours" ("ours" depends on patch kind and it's really "ours" in P-Patch,
// because patch change is cherry-picked on top of custom change. In R-Patch, custom changes come after
// patch, so they're called "theirs" in diff/git terminology)
// 1 - diff between base and "theirs" (see above)
// 2 - diff between base and resolved, effective and final state of history
Map<String, DiffEntry[]> report = new LinkedHashMap<>();
// 1. base -> ours
TreeWalk walk = new TreeWalk(reader);
walk.addTree(ctpBase);
walk.addTree(ctpOurs);
walk.setRecursive(true);
List<DiffEntry> diffs = DiffEntry.scan(walk);
diffs.forEach(de -> collect(report, de, 0));
// 2. base -> theirs
walk.reset();
ctpBase.reset(reader, base.getTree());
walk.addTree(ctpBase);
walk.addTree(ctpTheirs);
walk.setRecursive(true);
diffs = DiffEntry.scan(walk);
diffs.forEach(de -> collect(report, de, 1));
// 3. base -> resolved
walk.reset();
ctpBase.reset(reader, base.getTree());
walk.addTree(ctpBase);
walk.addTree(ctpResolved);
walk.setRecursive(true);
diffs = DiffEntry.scan(walk);
diffs.forEach(de -> collect(report, de, 2));
// report generation
PatchData pd = patchResult.getPatchData();
result.write(reportHeader.replace("@PATCH_ID@", pd.getId()));
PatchReport pr = patchResult.getReport();
result.write("<table class=\"summary\">\n" + " <tr>\n" + " <td class=\"f\">Patch ID:</td><td>" + pr.getId() + "</td>\n" + " </tr>\n" + " <tr>\n" + " <td class=\"f\">Patch type:</td><td>" + (pr.isRollup() ? "rollup" : "non-rollup") + "</td>\n" + " </tr>\n" + " <tr>\n" + " <td class=\"f\">Installation date:</td><td>" + DATE.format(pr.getTimestamp()) + "</td>\n" + " </tr>\n" + " <tr>\n" + " <td class=\"f\">Bundles updated</td><td>" + pr.getUpdatedBundles() + "</td>\n" + " </tr>\n" + " <tr>\n" + " <td class=\"f\">Features updated</td><td>" + pr.getUpdatedFeatures() + "</td>\n" + " </tr>\n" + " <tr>\n" + " <td class=\"f\">Features overriden</td><td>" + pr.getOverridenFeatures() + "</td>\n" + " </tr>\n" + " <tr>\n" + " <td class=\"f\">File conflicts</td><td>" + conflicts.size() + "</td>\n" + " </tr>\n" + " </table>\n" + "</div>\n");
if (conflicts.size() > 0) {
result.write("<h1 class=\"header\">\n" + " <div>Conflicting files</div>\n" + "</h1>\n");
}
for (Map.Entry<String, DiffEntry[]> e : report.entrySet()) {
if (!conflicts.contains(e.getKey())) {
// we don't care about diffs that aren't really conflicts
continue;
}
result.write(fileHeader1);
result.write(e.getKey());
result.write(fileHeader2);
// we have max 3 entries (not all entries may be present
result.write("<td class=\"side\">\n" + " <div class=\"header\">Custom version</div>\n" + " <div class=\"content" + (e.getValue()[0] != null ? "" : " empty") + "\">");
if (e.getValue()[0] != null) {
// custom change
diff(git, reader, e.getValue()[0], result);
} else {
result.write("No change");
}
result.write("</div>\n" + " </td>");
result.write("<td class=\"side\">\n" + " <div class=\"header\">Patch</div>\n" + " <div class=\"content" + (e.getValue()[1] != null ? "" : " empty") + "\">");
if (e.getValue()[1] != null) {
// patch change
diff(git, reader, e.getValue()[1], result);
} else {
result.write("No change");
}
result.write("</div>\n" + " </td>");
result.write("<td class=\"side\">\n" + " <div class=\"header\">Final version</div>\n" + " <div class=\"content" + (e.getValue()[2] != null ? "" : " empty") + "\">");
if (e.getValue()[2] != null) {
// effective change - should always be available
// or maybe not when both patch and user removed the file?
diff(git, reader, e.getValue()[2], result);
} else {
result.write("No change");
}
result.write("</div>\n" + " </td>");
result.write(fileFooter);
}
result.write(reportFooter);
}
use of org.eclipse.jgit.lib.ObjectReader in project gerrit by GerritCodeReview.
the class VersionedMetaDataOnInit method save.
protected void save(PersonIdent ident, String msg) throws IOException, ConfigInvalidException {
File path = getPath();
if (path == null) {
throw new IOException(project + " does not exist.");
}
try (Repository repo = new FileRepository(path);
ObjectInserter i = repo.newObjectInserter();
ObjectReader r = repo.newObjectReader();
RevWalk rw = new RevWalk(r)) {
inserter = i;
reader = r;
RevTree srcTree = revision != null ? rw.parseTree(revision) : null;
newTree = readTree(srcTree);
CommitBuilder commit = new CommitBuilder();
commit.setAuthor(ident);
commit.setCommitter(ident);
commit.setMessage(msg);
onSave(commit);
ObjectId res = newTree.writeTree(inserter);
if (res.equals(srcTree)) {
return;
}
commit.setTreeId(res);
if (revision != null) {
commit.addParentId(revision);
}
ObjectId newRevision = inserter.insert(commit);
updateRef(repo, ident, newRevision, "commit: " + msg);
revision = rw.parseCommit(newRevision);
} finally {
inserter = null;
reader = null;
}
}
use of org.eclipse.jgit.lib.ObjectReader in project gerrit by GerritCodeReview.
the class GroupNameNotes method loadAllGroups.
/**
* Loads the {@code GroupReference}s (name/UUID pairs) for all groups.
*
* <p>Even though group UUIDs should be unique, this class doesn't enforce it. For this reason,
* it's technically possible that two of the {@code GroupReference}s have a duplicate UUID but a
* different name. In practice, this shouldn't occur unless we introduce a bug in the future.
*
* @param repository the repository which holds the commits of the notes
* @return the {@code GroupReference}s of all existing groups/notes
* @throws IOException if the repository can't be accessed for some reason
* @throws ConfigInvalidException if one of the notes is in an invalid state
*/
public static ImmutableList<GroupReference> loadAllGroups(Repository repository) throws IOException, ConfigInvalidException {
Ref ref = repository.exactRef(RefNames.REFS_GROUPNAMES);
if (ref == null) {
return ImmutableList.of();
}
try (TraceTimer ignored = TraceContext.newTimer("Loading all groups", Metadata.builder().noteDbRefName(RefNames.REFS_GROUPNAMES).build());
RevWalk revWalk = new RevWalk(repository);
ObjectReader reader = revWalk.getObjectReader()) {
RevCommit notesCommit = revWalk.parseCommit(ref.getObjectId());
NoteMap noteMap = NoteMap.read(reader, notesCommit);
Multiset<GroupReference> groupReferences = HashMultiset.create();
for (Note note : noteMap) {
GroupReference groupReference = getGroupReference(reader, note.getData());
int numOfOccurrences = groupReferences.add(groupReference, 1);
if (numOfOccurrences > 1) {
GroupsNoteDbConsistencyChecker.logConsistencyProblemAsWarning("The UUID of group %s (%s) is duplicate in group name notes", groupReference.getName(), groupReference.getUUID());
}
}
return ImmutableList.copyOf(groupReferences);
}
}
use of org.eclipse.jgit.lib.ObjectReader in project gerrit by GerritCodeReview.
the class GroupNameNotes method loadGroup.
/**
* Loads the {@code GroupReference} (name/UUID pair) for the group with the specified name.
*
* @param repository the repository which holds the commits of the notes
* @param groupName the name of the group
* @return the corresponding {@code GroupReference} if a group/note with the given name exists
* @throws IOException if the repository can't be accessed for some reason
* @throws ConfigInvalidException if the note for the specified group is in an invalid state
*/
public static Optional<GroupReference> loadGroup(Repository repository, AccountGroup.NameKey groupName) throws IOException, ConfigInvalidException {
Ref ref = repository.exactRef(RefNames.REFS_GROUPNAMES);
if (ref == null) {
return Optional.empty();
}
try (RevWalk revWalk = new RevWalk(repository);
ObjectReader reader = revWalk.getObjectReader()) {
RevCommit notesCommit = revWalk.parseCommit(ref.getObjectId());
NoteMap noteMap = NoteMap.read(reader, notesCommit);
ObjectId noteDataBlobId = noteMap.get(getNoteKey(groupName));
if (noteDataBlobId == null) {
return Optional.empty();
}
return Optional.of(getGroupReference(reader, noteDataBlobId));
}
}
Aggregations