1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.api;
11
12 import java.io.IOException;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Collections;
16
17 import org.eclipse.jgit.api.errors.GitAPIException;
18 import org.eclipse.jgit.api.errors.JGitInternalException;
19 import org.eclipse.jgit.blame.BlameGenerator;
20 import org.eclipse.jgit.blame.BlameResult;
21 import org.eclipse.jgit.diff.DiffAlgorithm;
22 import org.eclipse.jgit.diff.RawTextComparator;
23 import org.eclipse.jgit.lib.AnyObjectId;
24 import org.eclipse.jgit.lib.ObjectId;
25 import org.eclipse.jgit.lib.Repository;
26
27
28
29
30
31 public class BlameCommand extends GitCommand<BlameResult> {
32
33 private String path;
34
35 private DiffAlgorithm diffAlgorithm;
36
37 private RawTextComparator textComparator;
38
39 private ObjectId startCommit;
40
41 private Collection<ObjectId> reverseEndCommits;
42
43 private Boolean followFileRenames;
44
45
46
47
48
49
50
51 public BlameCommand(Repository repo) {
52 super(repo);
53 }
54
55
56
57
58
59
60
61
62 public BlameCommand setFilePath(String filePath) {
63 this.path = filePath;
64 return this;
65 }
66
67
68
69
70
71
72
73
74 public BlameCommand setDiffAlgorithm(DiffAlgorithm diffAlgorithm) {
75 this.diffAlgorithm = diffAlgorithm;
76 return this;
77 }
78
79
80
81
82
83
84
85
86 public BlameCommand setTextComparator(RawTextComparator textComparator) {
87 this.textComparator = textComparator;
88 return this;
89 }
90
91
92
93
94
95
96
97
98 public BlameCommand setStartCommit(AnyObjectId commit) {
99 this.startCommit = commit.toObjectId();
100 return this;
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115 public BlameCommand setFollowFileRenames(boolean follow) {
116 followFileRenames = Boolean.valueOf(follow);
117 return this;
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public BlameCommand reverse(AnyObjectId start, AnyObjectId end)
134 throws IOException {
135 return reverse(start, Collections.singleton(end.toObjectId()));
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 public BlameCommand reverse(AnyObjectId start, Collection<ObjectId> end)
152 throws IOException {
153 startCommit = start.toObjectId();
154 reverseEndCommits = new ArrayList<>(end);
155 return this;
156 }
157
158
159
160
161
162
163
164 @Override
165 public BlameResult call() throws GitAPIException {
166 checkCallable();
167 try (BlameGenerator gen = new BlameGenerator(repo, path)) {
168 if (diffAlgorithm != null)
169 gen.setDiffAlgorithm(diffAlgorithm);
170 if (textComparator != null)
171 gen.setTextComparator(textComparator);
172 if (followFileRenames != null)
173 gen.setFollowFileRenames(followFileRenames.booleanValue());
174
175 if (reverseEndCommits != null)
176 gen.reverse(startCommit, reverseEndCommits);
177 else if (startCommit != null)
178 gen.push(null, startCommit);
179 else {
180 gen.prepareHead();
181 }
182 return gen.computeBlameResult();
183 } catch (IOException e) {
184 throw new JGitInternalException(e.getMessage(), e);
185 }
186 }
187 }