Search in sources :

Example 81 with ObjectReader

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();
}
Also used : ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser)

Example 82 with ObjectReader

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);
}
Also used : PatchData(org.jboss.fuse.patch.management.PatchData) PatchReport(org.jboss.fuse.patch.management.PatchReport) ObjectReader(org.eclipse.jgit.lib.ObjectReader) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) LinkedHashMap(java.util.LinkedHashMap) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 83 with ObjectReader

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;
    }
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) ObjectReader(org.eclipse.jgit.lib.ObjectReader) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) File(java.io.File) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 84 with ObjectReader

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);
    }
}
Also used : Ref(org.eclipse.jgit.lib.Ref) Note(org.eclipse.jgit.notes.Note) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) ObjectReader(org.eclipse.jgit.lib.ObjectReader) NoteMap(org.eclipse.jgit.notes.NoteMap) GroupReference(com.google.gerrit.entities.GroupReference) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 85 with ObjectReader

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));
    }
}
Also used : Ref(org.eclipse.jgit.lib.Ref) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) NoteMap(org.eclipse.jgit.notes.NoteMap) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

ObjectReader (org.eclipse.jgit.lib.ObjectReader)115 RevWalk (org.eclipse.jgit.revwalk.RevWalk)69 ObjectId (org.eclipse.jgit.lib.ObjectId)59 RevCommit (org.eclipse.jgit.revwalk.RevCommit)56 Repository (org.eclipse.jgit.lib.Repository)47 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)31 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)26 IOException (java.io.IOException)24 Test (org.junit.Test)23 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)20 RevTree (org.eclipse.jgit.revwalk.RevTree)19 DiffEntry (org.eclipse.jgit.diff.DiffEntry)18 Ref (org.eclipse.jgit.lib.Ref)17 PersonIdent (org.eclipse.jgit.lib.PersonIdent)14 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)13 TestRepository (org.eclipse.jgit.junit.TestRepository)12 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)11 Change (com.google.gerrit.reviewdb.client.Change)10 Map (java.util.Map)10 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)9