1
1
package io .jenkins .plugins .forensics .git .blame ;
2
2
3
- import java .io .IOException ;
4
- import java .util .Optional ;
5
- import java .util .stream .StreamSupport ;
6
-
7
3
import org .eclipse .jgit .api .BlameCommand ;
8
4
import org .eclipse .jgit .api .Git ;
9
5
import org .eclipse .jgit .api .errors .GitAPIException ;
10
6
import org .eclipse .jgit .api .errors .JGitInternalException ;
11
7
import org .eclipse .jgit .blame .BlameResult ;
12
8
import org .eclipse .jgit .lib .ObjectId ;
13
- import org .eclipse .jgit .lib .PersonIdent ;
14
9
import org .eclipse .jgit .lib .Repository ;
15
10
import org .eclipse .jgit .revwalk .RevCommit ;
16
11
19
14
import edu .umd .cs .findbugs .annotations .CheckForNull ;
20
15
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
21
16
17
+ import java .io .IOException ;
18
+ import java .io .Serial ;
19
+ import java .util .Optional ;
20
+ import java .util .stream .StreamSupport ;
21
+
22
22
import org .jenkinsci .plugins .gitclient .GitClient ;
23
23
import hudson .plugins .git .GitException ;
24
24
import hudson .remoting .VirtualChannel ;
45
45
// TODO: Blame needs only run for new warnings
46
46
@ SuppressFBWarnings (value = "SE" , justification = "GitClient implementation is Serializable" )
47
47
class GitBlamer extends Blamer {
48
+ @ Serial
48
49
private static final long serialVersionUID = -619059996626444900L ;
49
50
50
51
static final String NO_HEAD_ERROR = "Could not retrieve HEAD commit, aborting" ;
@@ -70,13 +71,13 @@ class GitBlamer extends Blamer {
70
71
71
72
@ Override
72
73
public Blames blame (final FileLocations locations , final FilteredLog log ) {
73
- Blames blames = new Blames ();
74
+ var blames = new Blames ();
74
75
try {
75
76
log .logInfo ("Invoking Git blamer to create author and commit information for %d affected files" ,
76
77
locations .size ());
77
78
log .logInfo ("-> GIT_COMMIT env = '%s'" , gitCommit );
78
79
79
- ObjectId headCommit = git .revParse (gitCommit );
80
+ var headCommit = git .revParse (gitCommit );
80
81
if (headCommit == null ) {
81
82
log .logError (NO_HEAD_ERROR );
82
83
return blames ;
@@ -106,6 +107,7 @@ public Blames blame(final FileLocations locations, final FilteredLog log) {
106
107
* Starts the blame commands.
107
108
*/
108
109
static class BlameCallback extends AbstractRepositoryCallback <RemoteResultWrapper <Blames >> {
110
+ @ Serial
109
111
private static final long serialVersionUID = 8794666938104738260L ;
110
112
private static final int WHOLE_FILE = 0 ;
111
113
@@ -129,15 +131,15 @@ public RemoteResultWrapper<Blames> invoke(final Repository repository, final Vir
129
131
log .logInfo ("-> Git commit ID = '%s'" , headCommit .getName ());
130
132
log .logInfo ("-> Git working tree = '%s'" , getWorkTree (repository ));
131
133
132
- BlameRunner blameRunner = new BlameRunner (repository , headCommit );
133
- LastCommitRunner lastCommitRunner = new LastCommitRunner (repository );
134
+ var blameRunner = new BlameRunner (repository , headCommit );
135
+ var lastCommitRunner = new LastCommitRunner (repository );
134
136
135
- FileBlameBuilder builder = new FileBlameBuilder ();
137
+ var builder = new FileBlameBuilder ();
136
138
for (String file : locations .getFiles ()) {
137
139
run (builder , file , blameRunner , lastCommitRunner , log );
138
140
139
141
if (Thread .interrupted ()) { // Cancel request by user
140
- String message = "Blaming has been interrupted while computing blame information" ;
142
+ var message = "Blaming has been interrupted while computing blame information" ;
141
143
log .logInfo (message );
142
144
143
145
throw new InterruptedException (message );
@@ -168,13 +170,13 @@ public RemoteResultWrapper<Blames> invoke(final Repository repository, final Vir
168
170
void run (final FileBlameBuilder builder , final String relativePath , final BlameRunner blameRunner ,
169
171
final LastCommitRunner lastCommitRunner , final FilteredLog log ) {
170
172
try {
171
- BlameResult blame = blameRunner .run (relativePath );
173
+ var blame = blameRunner .run (relativePath );
172
174
if (blame == null ) {
173
175
log .logError ("- no blame results for file '%s'" , relativePath );
174
176
}
175
177
else {
176
178
for (int line : locations .getLines (relativePath )) {
177
- FileBlame fileBlame = builder .build (relativePath );
179
+ var fileBlame = builder .build (relativePath );
178
180
if (line <= 0 ) {
179
181
fillWithLastCommit (relativePath , fileBlame , lastCommitRunner );
180
182
}
@@ -194,7 +196,7 @@ else if (line <= blame.getResultContents().size()) {
194
196
private void fillWithBlameResult (final String fileName , final FileBlame fileBlame , final BlameResult blame ,
195
197
final int line , final FilteredLog log ) {
196
198
int lineIndex = line - 1 ; // first line is index 0
197
- PersonIdent who = blame .getSourceAuthor (lineIndex );
199
+ var who = blame .getSourceAuthor (lineIndex );
198
200
if (who == null ) {
199
201
who = blame .getSourceCommitter (lineIndex );
200
202
}
@@ -206,7 +208,7 @@ private void fillWithBlameResult(final String fileName, final FileBlame fileBlam
206
208
fileBlame .setName (line , who .getName ());
207
209
fileBlame .setEmail (line , who .getEmailAddress ());
208
210
}
209
- RevCommit commit = blame .getSourceCommit (lineIndex );
211
+ var commit = blame .getSourceCommit (lineIndex );
210
212
if (commit == null ) {
211
213
log .logError ("- no commit ID and time found for line %d in file %s" , lineIndex , fileName );
212
214
}
@@ -220,9 +222,9 @@ private void fillWithLastCommit(final String relativePath, final FileBlame fileB
220
222
final LastCommitRunner lastCommitRunner ) throws GitAPIException {
221
223
Optional <RevCommit > commit = lastCommitRunner .run (relativePath );
222
224
if (commit .isPresent ()) {
223
- RevCommit revCommit = commit .get ();
225
+ var revCommit = commit .get ();
224
226
fileBlame .setCommit (WHOLE_FILE , revCommit .getName ());
225
- PersonIdent who = revCommit .getAuthorIdent ();
227
+ var who = revCommit .getAuthorIdent ();
226
228
if (who == null ) {
227
229
who = revCommit .getCommitterIdent ();
228
230
}
@@ -248,7 +250,7 @@ static class BlameRunner {
248
250
249
251
@ CheckForNull
250
252
BlameResult run (final String fileName ) throws GitAPIException {
251
- BlameCommand blame = new BlameCommand (repo );
253
+ var blame = new BlameCommand (repo );
252
254
blame .setFilePath (fileName );
253
255
blame .setStartCommit (headCommit );
254
256
return blame .call ();
@@ -266,7 +268,7 @@ static class LastCommitRunner {
266
268
}
267
269
268
270
Optional <RevCommit > run (final String fileName ) throws GitAPIException {
269
- try (Git git = new Git (repo )) {
271
+ try (var git = new Git (repo )) {
270
272
Iterable <RevCommit > commits = git .log ().addPath (fileName ).call ();
271
273
272
274
return StreamSupport .stream (commits .spliterator (), false ).findFirst ();
0 commit comments