private Path path(final DiffEntry s) { final ChangeType changeType = s.getChangeType(); switch (changeType) { case DELETE: return Paths.get(s.getOldPath()); case ADD: return Paths.get(s.getNewPath()); case COPY: return Paths.get(s.getNewPath()); case MODIFY: return Paths.get(s.getNewPath()); case RENAME: return Paths.get(s.getNewPath()); default: throw new RuntimeException("Unrecognized change type: " + changeType); } }
@Override public void format(DiffEntry ent) throws IOException { currentPath = diffStat.addPath(ent); nofLinesCurrent = 0; isOff = false; entry = ent; if (!truncated) { totalNofLinesPrevious = totalNofLinesCurrent; if (globalDiffLimit > 0 && totalNofLinesPrevious > globalDiffLimit) { truncated = true; isOff = true; } truncateTo = os.size(); } else { isOff = true; } if (truncated) { skipped.add(ent); } else { // Produce a header here and now String path; String id; if (ChangeType.DELETE.equals(ent.getChangeType())) { path = ent.getOldPath(); id = ent.getOldId().name(); } else { path = ent.getNewPath(); id = ent.getNewId().name(); } StringBuilder sb = new StringBuilder( MessageFormat.format( "<div class='header'><div class=\"diffHeader\" id=\"n{0}\"><i class=\"icon-file\"></i> ", id)); sb.append(StringUtils.escapeForHtml(path, false)).append("</div></div>"); sb.append("<div class=\"diff\"><table cellpadding='0'><tbody>\n"); os.write(sb.toString().getBytes()); } // Keep formatting, but if off, don't produce anything anymore. We just keep on counting. super.format(ent); if (!truncated) { // Close the table os.write("</tbody></table></div>\n".getBytes()); } }
/** * Writes an initial table row containing information about added/removed/renamed/copied files. In * case of a deletion, we also suppress generating the diff; it's not interesting. (All lines * removed.) */ private void handleChange() { // XXX Would be nice if we could generate blob links for the cases handled here. Alas, we lack // the repo // name, and cannot reliably determine it here. We could get the .git directory of a Repository, // if we // passed in the repo, and then take the name of the parent directory, but that'd fail for repos // nested // in GitBlit projects. And we don't know if the repo is inside a project or is a top-level // repo. // // That's certainly solvable (just pass along more information), but would require a larger // rewrite than // I'm prepared to do now. String message; switch (entry.getChangeType()) { case ADD: message = getMsg("gb.diffNewFile", "New file"); break; case DELETE: message = getMsg("gb.diffDeletedFile", "File was deleted"); isOff = true; break; case RENAME: message = MessageFormat.format( getMsg("gb.diffRenamedFile", "File was renamed from {0}"), entry.getOldPath()); break; case COPY: message = MessageFormat.format( getMsg("gb.diffCopiedFile", "File was copied from {0}"), entry.getOldPath()); break; default: return; } writeFullWidthLine(message); }
/** * Workaround function for complex private methods in DiffFormatter. This sets the html for the * diff headers. * * @return */ public String getHtml() { String html = RawParseUtils.decode(os.toByteArray()); String[] lines = html.split("\n"); StringBuilder sb = new StringBuilder(); for (String line : lines) { if (line.startsWith("index")) { // skip index lines } else if (line.startsWith("new file") || line.startsWith("deleted file")) { // skip new file lines } else if (line.startsWith("\\ No newline")) { // skip no new line } else if (line.startsWith("---") || line.startsWith("+++")) { // skip --- +++ lines } else if (line.startsWith("diff")) { // skip diff lines } else { boolean gitLinkDiff = line.length() > 0 && line.substring(1).startsWith("Subproject commit"); if (gitLinkDiff) { sb.append("<tr><th class='diff-line'></th><th class='diff-line'></th>"); if (line.charAt(0) == '+') { sb.append("<th class='diff-state diff-state-add'></th><td class=\"diff-cell add2\">"); } else { sb.append( "<th class='diff-state diff-state-sub'></th><td class=\"diff-cell remove2\">"); } line = StringUtils.escapeForHtml(line.substring(1), false); } sb.append(line); if (gitLinkDiff) { sb.append("</td></tr>"); } sb.append('\n'); } } if (truncated) { sb.append( MessageFormat.format( "<div class='header'><div class='diffHeader'>{0}</div></div>", StringUtils.escapeForHtml( getMsg("gb.diffTruncated", "Diff truncated after the above file"), false))); // List all files not shown. We can be sure we do have at least one path in skipped. sb.append( "<div class='diff'><table cellpadding='0'><tbody><tr><td class='diff-cell' colspan='4'>"); String deletedSuffix = StringUtils.escapeForHtml(getMsg("gb.diffDeletedFileSkipped", "(deleted)"), false); boolean first = true; for (DiffEntry entry : skipped) { if (!first) { sb.append('\n'); } if (ChangeType.DELETE.equals(entry.getChangeType())) { sb.append( "<span id=\"n" + entry.getOldId().name() + "\">" + StringUtils.escapeForHtml(entry.getOldPath(), false) + ' ' + deletedSuffix + "</span>"); } else { sb.append( "<span id=\"n" + entry.getNewId().name() + "\">" + StringUtils.escapeForHtml(entry.getNewPath(), false) + "</span>"); } first = false; } skipped.clear(); sb.append("</td></tr></tbody></table></div>"); } return sb.toString(); }
public static List<PathChangeModel> getFilesInCommit(Repository repository, RevCommit commit) { List<PathChangeModel> list = new ArrayList<PathChangeModel>(); if (!hasCommits(repository)) { return list; } RevWalk rw = new RevWalk(repository); try { if (commit == null) { ObjectId object = getDefaultBranch(repository); commit = rw.parseCommit(object); } if (commit.getParentCount() == 0) { TreeWalk tw = new TreeWalk(repository); tw.reset(); tw.setRecursive(true); tw.addTree(commit.getTree()); while (tw.next()) { list.add( new PathChangeModel( tw.getPathString(), tw.getPathString(), 0, tw.getRawMode(0), commit.getId().getName(), ChangeType.ADD)); } tw.release(); } else { RevCommit parent = rw.parseCommit(commit.getParent(0).getId()); DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE); df.setRepository(repository); df.setDiffComparator(RawTextComparator.DEFAULT); df.setDetectRenames(true); List<DiffEntry> diffs = df.scan(parent.getTree(), commit.getTree()); for (DiffEntry diff : diffs) { if (diff.getChangeType().equals(ChangeType.DELETE)) { list.add( new PathChangeModel( diff.getOldPath(), diff.getOldPath(), 0, diff.getNewMode().getBits(), commit.getId().getName(), diff.getChangeType())); } else { list.add( new PathChangeModel( diff.getNewPath(), diff.getNewPath(), 0, diff.getNewMode().getBits(), commit.getId().getName(), diff.getChangeType())); } } } } catch (Throwable t) { // todo Logger.error(t, t.getMessage()); } finally { rw.dispose(); } return list; }
private static boolean isRename(DiffEntry ent) { return ent.getChangeType() == ChangeType.RENAME || ent.getChangeType() == ChangeType.COPY; }
public String getDiffText(String str) { int filenubs = 0; int lines = 0; FileRepositoryBuilder builder = new FileRepositoryBuilder(); File gitDir = new File("F:/work/demo-rio/.git"); Repository repository = null; try { if (git == null) { git = Git.open(gitDir); } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { String cid = str; repository = builder.setGitDir(gitDir).readEnvironment().findGitDir().build(); RevWalk walk = new RevWalk(repository); ObjectId objId = ObjectId.fromString(cid); RevCommit commit = walk.parseCommit(objId); System.out.println(commit.getFullMessage()); TreeWalk tw = new TreeWalk(repository); RevCommit[] parent_commits = commit.getParents(); if (parent_commits.length == 0) { throw new Exception("当前只有一个版本"); } ObjectId objId2 = parent_commits[0].toObjectId(); RevCommit paren_commit = walk.parseCommit(objId2); tw.addTree(paren_commit.getTree()); tw.addTree(commit.getTree()); tw.setRecursive(true); ByteArrayOutputStream out = new ByteArrayOutputStream(); DiffFormatter df = new DiffFormatter(out); df.setRepository(git.getRepository()); RenameDetector rd = new RenameDetector(repository); rd.addAll(DiffEntry.scan(tw)); List<DiffEntry> diffEntries = rd.compute(); if (diffEntries != null || diffEntries.size() != 0) { Iterator<DiffEntry> iterator = new ArrayList<DiffEntry>(diffEntries).iterator(); DiffEntry diffEntry = null; while (iterator.hasNext()) { diffEntry = iterator.next(); String changeType = diffEntry.getChangeType().toString(); String type = ""; if (changeType.equals("DELETE")) { type = ConfigUtil.getFileType(diffEntry.getOldPath()); filenubs++; System.out.println(diffEntry.getOldPath()); } else { type = ConfigUtil.getFileType(diffEntry.getNewPath()); filenubs++; System.out.println(diffEntry.getNewPath()); } // 检查文件的后缀 // System.out.println(type); if (fileTypes.contains(type)) { df.format(diffEntry); String diffText = out.toString("UTF-8"); lines += scanDiffText(diffText); } } } } catch (Exception e) { e.printStackTrace(); } return filenubs + "&" + lines; // System.out.println("the changed file nubs: "+ filenubs); // System.out.println("the changed linr nubs: "+ lines); }