commit | author | age
|
f13c4c
|
1 |
/*
|
JM |
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 |
*/
|
608ece
|
16 |
package com.gitblit.utils;
|
JM |
17 |
|
|
18 |
import java.io.IOException;
|
|
19 |
import java.io.OutputStream;
|
|
20 |
import java.text.MessageFormat;
|
|
21 |
import java.text.SimpleDateFormat;
|
|
22 |
import java.util.Date;
|
|
23 |
import java.util.HashMap;
|
|
24 |
import java.util.Map;
|
|
25 |
|
|
26 |
import org.eclipse.jgit.diff.DiffEntry;
|
|
27 |
import org.eclipse.jgit.diff.DiffFormatter;
|
|
28 |
import org.eclipse.jgit.diff.RawText;
|
|
29 |
import org.eclipse.jgit.revwalk.RevCommit;
|
|
30 |
|
|
31 |
import com.gitblit.Constants;
|
|
32 |
|
d9f687
|
33 |
/**
|
JM |
34 |
* A diff formatter that outputs standard patch content.
|
|
35 |
*
|
|
36 |
* @author James Moger
|
|
37 |
*
|
|
38 |
*/
|
608ece
|
39 |
public class PatchFormatter extends DiffFormatter {
|
JM |
40 |
|
|
41 |
private final OutputStream os;
|
|
42 |
|
2a7306
|
43 |
private Map<String, PatchTouple> changes = new HashMap<String, PatchTouple>();
|
608ece
|
44 |
|
2a7306
|
45 |
private PatchTouple currentTouple;
|
608ece
|
46 |
|
JM |
47 |
public PatchFormatter(OutputStream os) {
|
|
48 |
super(os);
|
|
49 |
this.os = os;
|
|
50 |
}
|
|
51 |
|
|
52 |
public void format(DiffEntry entry) throws IOException {
|
|
53 |
currentTouple = new PatchTouple();
|
|
54 |
changes.put(entry.getNewPath(), currentTouple);
|
|
55 |
super.format(entry);
|
|
56 |
}
|
|
57 |
|
|
58 |
@Override
|
2a7306
|
59 |
protected void writeLine(final char prefix, final RawText text, final int cur)
|
JM |
60 |
throws IOException {
|
608ece
|
61 |
switch (prefix) {
|
JM |
62 |
case '+':
|
|
63 |
currentTouple.insertions++;
|
|
64 |
break;
|
|
65 |
case '-':
|
|
66 |
currentTouple.deletions++;
|
|
67 |
break;
|
|
68 |
}
|
|
69 |
super.writeLine(prefix, text, cur);
|
|
70 |
}
|
|
71 |
|
|
72 |
public String getPatch(RevCommit commit) {
|
|
73 |
StringBuilder patch = new StringBuilder();
|
|
74 |
// hard-code the mon sep 17 2001 date string.
|
|
75 |
// I have no idea why that is there. it seems to be a constant.
|
|
76 |
patch.append("From " + commit.getName() + " Mon Sep 17 00:00:00 2001" + "\n");
|
|
77 |
patch.append("From: " + JGitUtils.getDisplayName(commit.getAuthorIdent()) + "\n");
|
2a7306
|
78 |
patch.append("Date: "
|
JM |
79 |
+ (new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z").format(new Date(commit
|
|
80 |
.getCommitTime() * 1000L))) + "\n");
|
608ece
|
81 |
patch.append("Subject: [PATCH] " + commit.getShortMessage() + "\n");
|
2a7306
|
82 |
patch.append('\n');
|
608ece
|
83 |
patch.append("---");
|
JM |
84 |
int maxPathLen = 0;
|
|
85 |
int files = 0;
|
|
86 |
int insertions = 0;
|
|
87 |
int deletions = 0;
|
|
88 |
for (String path : changes.keySet()) {
|
|
89 |
if (path.length() > maxPathLen) {
|
|
90 |
maxPathLen = path.length();
|
|
91 |
}
|
|
92 |
PatchTouple touple = changes.get(path);
|
|
93 |
files++;
|
|
94 |
insertions += touple.insertions;
|
|
95 |
deletions += touple.deletions;
|
|
96 |
}
|
|
97 |
int columns = 60;
|
|
98 |
int total = insertions + deletions;
|
|
99 |
int unit = total / columns + (total % columns > 0 ? 1 : 0);
|
|
100 |
if (unit == 0) {
|
|
101 |
unit = 1;
|
|
102 |
}
|
|
103 |
for (String path : changes.keySet()) {
|
|
104 |
PatchTouple touple = changes.get(path);
|
2a7306
|
105 |
patch.append("\n " + StringUtils.rightPad(path, maxPathLen, ' ') + " | "
|
JM |
106 |
+ StringUtils.leftPad("" + touple.total(), 4, ' ') + " "
|
|
107 |
+ touple.relativeScale(unit));
|
608ece
|
108 |
}
|
2a7306
|
109 |
patch.append(MessageFormat.format(
|
JM |
110 |
"\n {0} files changed, {1} insertions(+), {2} deletions(-)\n\n", files, insertions,
|
|
111 |
deletions));
|
608ece
|
112 |
patch.append(os.toString());
|
JM |
113 |
patch.append("\n--\n");
|
2a7306
|
114 |
patch.append(Constants.getGitBlitVersion());
|
608ece
|
115 |
return patch.toString();
|
JM |
116 |
}
|
|
117 |
|
2a7306
|
118 |
private static class PatchTouple {
|
JM |
119 |
int insertions;
|
|
120 |
int deletions;
|
608ece
|
121 |
|
JM |
122 |
int total() {
|
|
123 |
return insertions + deletions;
|
|
124 |
}
|
|
125 |
|
|
126 |
String relativeScale(int unit) {
|
2a7306
|
127 |
int plus = insertions / unit;
|
JM |
128 |
int minus = deletions / unit;
|
608ece
|
129 |
StringBuilder sb = new StringBuilder();
|
JM |
130 |
for (int i = 0; i < plus; i++) {
|
|
131 |
sb.append('+');
|
|
132 |
}
|
|
133 |
for (int i = 0; i < minus; i++) {
|
|
134 |
sb.append('-');
|
|
135 |
}
|
|
136 |
return sb.toString();
|
|
137 |
}
|
|
138 |
}
|
|
139 |
}
|