Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PatchFormatter.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.gitblit.utils;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import java.text.MessageFormat;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import org.eclipse.jgit.diff.DiffEntry;
  10. import org.eclipse.jgit.diff.DiffFormatter;
  11. import org.eclipse.jgit.diff.RawText;
  12. import org.eclipse.jgit.revwalk.RevCommit;
  13. import com.gitblit.Constants;
  14. public class PatchFormatter extends DiffFormatter {
  15. private final OutputStream os;
  16. private PatchTouple currentTouple = null;
  17. Map<String, PatchTouple> changes = new HashMap<String, PatchTouple>();
  18. public PatchFormatter(OutputStream os) {
  19. super(os);
  20. this.os = os;
  21. }
  22. public void format(DiffEntry entry) throws IOException {
  23. currentTouple = new PatchTouple();
  24. changes.put(entry.getNewPath(), currentTouple);
  25. super.format(entry);
  26. }
  27. @Override
  28. protected void writeLine(final char prefix, final RawText text, final int cur) throws IOException {
  29. switch (prefix) {
  30. case '+':
  31. currentTouple.insertions++;
  32. break;
  33. case '-':
  34. currentTouple.deletions++;
  35. break;
  36. }
  37. super.writeLine(prefix, text, cur);
  38. }
  39. public String getPatch(RevCommit commit) {
  40. StringBuilder patch = new StringBuilder();
  41. // hard-code the mon sep 17 2001 date string.
  42. // I have no idea why that is there. it seems to be a constant.
  43. patch.append("From " + commit.getName() + " Mon Sep 17 00:00:00 2001" + "\n");
  44. patch.append("From: " + JGitUtils.getDisplayName(commit.getAuthorIdent()) + "\n");
  45. patch.append("Date: " + (new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z").format(new Date(commit.getCommitTime() * 1000l))) + "\n");
  46. patch.append("Subject: [PATCH] " + commit.getShortMessage() + "\n");
  47. patch.append("\n");
  48. patch.append("---");
  49. int maxPathLen = 0;
  50. int files = 0;
  51. int insertions = 0;
  52. int deletions = 0;
  53. for (String path : changes.keySet()) {
  54. if (path.length() > maxPathLen) {
  55. maxPathLen = path.length();
  56. }
  57. PatchTouple touple = changes.get(path);
  58. files++;
  59. insertions += touple.insertions;
  60. deletions += touple.deletions;
  61. }
  62. int columns = 60;
  63. int total = insertions + deletions;
  64. int unit = total / columns + (total % columns > 0 ? 1 : 0);
  65. if (unit == 0) {
  66. unit = 1;
  67. }
  68. for (String path : changes.keySet()) {
  69. PatchTouple touple = changes.get(path);
  70. patch.append("\n " + StringUtils.rightPad(path, maxPathLen, ' ') + " | " + StringUtils.leftPad("" + touple.total(), 4, ' ') + " " + touple.relativeScale(unit));
  71. }
  72. patch.append(MessageFormat.format("\n {0} files changed, {1} insertions(+), {2} deletions(-)\n\n", files, insertions, deletions));
  73. patch.append(os.toString());
  74. patch.append("\n--\n");
  75. patch.append(Constants.getRunningVersion());
  76. return patch.toString();
  77. }
  78. private class PatchTouple {
  79. int insertions = 0;
  80. int deletions = 0;
  81. int total() {
  82. return insertions + deletions;
  83. }
  84. String relativeScale(int unit) {
  85. int plus = (insertions / unit);
  86. int minus = (deletions / unit);
  87. StringBuilder sb = new StringBuilder();
  88. for (int i = 0; i < plus; i++) {
  89. sb.append('+');
  90. }
  91. for (int i = 0; i < minus; i++) {
  92. sb.append('-');
  93. }
  94. return sb.toString();
  95. }
  96. }
  97. }