forked from ModeShape/modeshape
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GitCommitDetails.java
192 lines (169 loc) · 8.09 KB
/
GitCommitDetails.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* ModeShape (http://www.modeshape.org)
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
* See the AUTHORS.txt file in the distribution for a full listing of
* individual contributors.
*
* ModeShape is free software. Unless otherwise indicated, all code in ModeShape
* is licensed to you under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* ModeShape is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.modeshape.connector.git;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.infinispan.schematic.document.Document;
import org.modeshape.jcr.federation.spi.DocumentWriter;
import org.modeshape.jcr.federation.spi.PageKey;
import org.modeshape.jcr.federation.spi.PageWriter;
/**
* A {@link GitFunction} that returns the information about a particular commit. The structure of this area of the repository is
* as follows:
*
* <pre>
* /commit/{branchOrTagNameOrObjectId}
* </pre>
*/
public class GitCommitDetails extends GitFunction implements PageableGitFunction {
/**
* The name of the character set that is used when building the patch difference for a commit.
*/
public static final String DIFF_CHARSET_NAME = "UTF-8";
protected static final String NAME = "commit";
protected static final String ID = "/commit";
protected static Object referenceToCommit( ObjectId id,
Values values ) {
return values.referenceTo(ID + DELIMITER + id.getName());
}
protected static Object[] referencesToCommits( ObjectId[] ids,
Values values ) {
int size = ids.length;
Object[] results = new Object[size];
for (int i = 0; i != size; ++i) {
results[i] = referenceToCommit(ids[i], values);
}
return results;
}
public GitCommitDetails( GitConnector connector ) {
super(NAME, connector);
}
@Override
public Document execute( Repository repository,
Git git,
CallSpecification spec,
DocumentWriter writer,
Values values ) throws GitAPIException, IOException {
if (spec.parameterCount() == 0) {
// This is the top-level "/commit" node
writer.setPrimaryType(GitLexicon.DETAILS);
// Generate the child references to the branches, tags, and commits in the history ...
addBranchesAsChildren(git, spec, writer);
addTagsAsChildren(git, spec, writer);
addCommitsAsChildren(git, spec, writer, pageSize);
} else if (spec.parameterCount() == 1) {
// This is the top-level "/commit/{branchOrTagNameOrObjectId}" node
writer.setPrimaryType(GitLexicon.DETAILED_COMMIT);
// Add the properties describing this commit ...
RevWalk walker = new RevWalk(repository);
walker.setRetainBody(true);
try {
String branchOrTagOrCommitId = spec.parameter(0);
ObjectId objId = resolveBranchOrTagOrCommitId(repository, branchOrTagOrCommitId);
RevCommit commit = walker.parseCommit(objId);
writer.addProperty(GitLexicon.OBJECT_ID, objId.name());
writer.addProperty(GitLexicon.AUTHOR, authorName(commit));
writer.addProperty(GitLexicon.COMMITTER, commiterName(commit));
writer.addProperty(GitLexicon.COMMITTED, values.dateFrom(commit.getCommitTime()));
writer.addProperty(GitLexicon.TITLE, commit.getShortMessage());
writer.addProperty(GitLexicon.MESSAGE, commit.getFullMessage().trim());// removes trailing whitespace
writer.addProperty(GitLexicon.PARENTS, GitCommitDetails.referencesToCommits(commit.getParents(), values));
writer.addProperty(GitLexicon.TREE, GitTree.referenceToTree(objId, objId.name(), values));
// Compute the difference between the commit and it's parent(s), and generate the diff/patch file ...
List<DiffEntry> differences = computeDifferences(commit, walker, repository);
String patchFile = computePatch(differences, repository);
writer.addProperty(GitLexicon.DIFF, patchFile);
} finally {
walker.dispose();
}
} else {
return null;
}
return writer.document();
}
@Override
public boolean isPaged() {
return true;
}
@Override
public Document execute( Repository repository,
Git git,
CallSpecification spec,
PageWriter writer,
Values values,
PageKey pageKey ) throws GitAPIException, IOException {
if (spec.parameterCount() != 0) return null;
addCommitsAsPageOfChildren(git, repository, spec, writer, pageKey);
return writer.document();
}
protected List<DiffEntry> computeDifferences( RevCommit commit,
RevWalk walker,
Repository repository )
throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException {
// Set up the tree walk to obtain the difference between the commit and it's parent(s) ...
TreeWalk tw = new TreeWalk(repository);
tw.setRecursive(true);
tw.addTree(commit.getTree());
RevCommit[] parents = commit.getParents();
for (RevCommit parent : parents) {
RevCommit parentCommit = walker.parseCommit(parent);
tw.addTree(parentCommit.getTree());
//if there are multiple parents, we can't really have a multiple-way diff so we'll only look at the first parent
if (parents.length > 1) {
connector.getLogger().warn(GitI18n.commitWithMultipleParents, commit.getName(), parentCommit.getName());
break;
}
}
if (tw.getTreeCount() == 1) {
connector.getLogger().warn(GitI18n.commitWithSingleParent, commit.getName(), tw.getObjectId(0).name());
return Collections.emptyList();
}
// Now process the diff of each file ...
return DiffEntry.scan(tw);
}
protected String computePatch( Iterable<DiffEntry> entries,
Repository repository ) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
DiffFormatter formatter = new DiffFormatter(output);
formatter.setRepository(repository);
for (DiffEntry entry : entries) {
formatter.format(entry);
}
return output.toString(DIFF_CHARSET_NAME);
}
}