You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PatchFormatter.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.utils;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. import java.text.MessageFormat;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Date;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import org.eclipse.jgit.diff.DiffEntry;
  25. import org.eclipse.jgit.diff.DiffFormatter;
  26. import org.eclipse.jgit.diff.RawText;
  27. import org.eclipse.jgit.revwalk.RevCommit;
  28. import com.gitblit.Constants;
  29. public class PatchFormatter extends DiffFormatter {
  30. private final OutputStream os;
  31. private PatchTouple currentTouple = null;
  32. Map<String, PatchTouple> changes = new HashMap<String, PatchTouple>();
  33. public PatchFormatter(OutputStream os) {
  34. super(os);
  35. this.os = os;
  36. }
  37. public void format(DiffEntry entry) throws IOException {
  38. currentTouple = new PatchTouple();
  39. changes.put(entry.getNewPath(), currentTouple);
  40. super.format(entry);
  41. }
  42. @Override
  43. protected void writeLine(final char prefix, final RawText text, final int cur) throws IOException {
  44. switch (prefix) {
  45. case '+':
  46. currentTouple.insertions++;
  47. break;
  48. case '-':
  49. currentTouple.deletions++;
  50. break;
  51. }
  52. super.writeLine(prefix, text, cur);
  53. }
  54. public String getPatch(RevCommit commit) {
  55. StringBuilder patch = new StringBuilder();
  56. // hard-code the mon sep 17 2001 date string.
  57. // I have no idea why that is there. it seems to be a constant.
  58. patch.append("From " + commit.getName() + " Mon Sep 17 00:00:00 2001" + "\n");
  59. patch.append("From: " + JGitUtils.getDisplayName(commit.getAuthorIdent()) + "\n");
  60. patch.append("Date: " + (new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z").format(new Date(commit.getCommitTime() * 1000l))) + "\n");
  61. patch.append("Subject: [PATCH] " + commit.getShortMessage() + "\n");
  62. patch.append("\n");
  63. patch.append("---");
  64. int maxPathLen = 0;
  65. int files = 0;
  66. int insertions = 0;
  67. int deletions = 0;
  68. for (String path : changes.keySet()) {
  69. if (path.length() > maxPathLen) {
  70. maxPathLen = path.length();
  71. }
  72. PatchTouple touple = changes.get(path);
  73. files++;
  74. insertions += touple.insertions;
  75. deletions += touple.deletions;
  76. }
  77. int columns = 60;
  78. int total = insertions + deletions;
  79. int unit = total / columns + (total % columns > 0 ? 1 : 0);
  80. if (unit == 0) {
  81. unit = 1;
  82. }
  83. for (String path : changes.keySet()) {
  84. PatchTouple touple = changes.get(path);
  85. patch.append("\n " + StringUtils.rightPad(path, maxPathLen, ' ') + " | " + StringUtils.leftPad("" + touple.total(), 4, ' ') + " " + touple.relativeScale(unit));
  86. }
  87. patch.append(MessageFormat.format("\n {0} files changed, {1} insertions(+), {2} deletions(-)\n\n", files, insertions, deletions));
  88. patch.append(os.toString());
  89. patch.append("\n--\n");
  90. patch.append(Constants.getRunningVersion());
  91. return patch.toString();
  92. }
  93. private class PatchTouple {
  94. int insertions = 0;
  95. int deletions = 0;
  96. int total() {
  97. return insertions + deletions;
  98. }
  99. String relativeScale(int unit) {
  100. int plus = (insertions / unit);
  101. int minus = (deletions / unit);
  102. StringBuilder sb = new StringBuilder();
  103. for (int i = 0; i < plus; i++) {
  104. sb.append('+');
  105. }
  106. for (int i = 0; i < minus; i++) {
  107. sb.append('-');
  108. }
  109. return sb.toString();
  110. }
  111. }
  112. }