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
|
/*
* SonarQube
* Copyright (C) 2009-2021 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program 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 program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.scm.svn;
import com.google.common.annotations.VisibleForTesting;
import java.util.List;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.scm.BlameCommand;
import org.sonar.api.batch.scm.BlameLine;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.tmatesoft.svn.core.SVNAuthenticationException;
import org.tmatesoft.svn.core.SVNErrorCode;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNDiffOptions;
import org.tmatesoft.svn.core.wc.SVNLogClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNStatus;
import org.tmatesoft.svn.core.wc.SVNStatusClient;
import org.tmatesoft.svn.core.wc.SVNStatusType;
import static org.sonar.scm.svn.SvnScmSupport.newSvnClientManager;
public class SvnBlameCommand extends BlameCommand {
private static final Logger LOG = Loggers.get(SvnBlameCommand.class);
private final SvnConfiguration configuration;
public SvnBlameCommand(SvnConfiguration configuration) {
this.configuration = configuration;
}
@Override
public void blame(final BlameInput input, final BlameOutput output) {
FileSystem fs = input.fileSystem();
LOG.debug("Working directory: " + fs.baseDir().getAbsolutePath());
SVNClientManager clientManager = null;
try {
clientManager = newSvnClientManager(configuration);
for (InputFile inputFile : input.filesToBlame()) {
blame(clientManager, inputFile, output);
}
} finally {
if (clientManager != null) {
try {
clientManager.dispose();
} catch (Exception e) {
LOG.warn("Unable to dispose SVN ClientManager", e);
}
}
}
}
@VisibleForTesting
void blame(SVNClientManager clientManager, InputFile inputFile, BlameOutput output) {
String filename = inputFile.relativePath();
LOG.debug("Process file {}", filename);
AnnotationHandler handler = new AnnotationHandler();
try {
if (!checkStatus(clientManager, inputFile)) {
return;
}
SVNLogClient logClient = clientManager.getLogClient();
logClient.setDiffOptions(new SVNDiffOptions(true, true, true));
logClient.doAnnotate(inputFile.file(), SVNRevision.UNDEFINED, SVNRevision.create(1), SVNRevision.BASE, true, true, handler, null);
} catch (SVNAuthenticationException e) {
if(configuration.isEmpty()) {
LOG.warn("Authentication to SVN server is required but no authentication data was passed to the scanner");
}
throw new IllegalStateException("Authentication error when executing blame for file " + filename, e);
} catch (SVNException e) {
throw new IllegalStateException("Error when executing blame for file " + filename, e);
}
List<BlameLine> lines = handler.getLines();
if (lines.size() == inputFile.lines() - 1) {
// SONARPLUGINS-3097 SVN do not report blame on last empty line
lines.add(lines.get(lines.size() - 1));
}
output.blameResult(inputFile, lines);
}
private static boolean checkStatus(SVNClientManager clientManager, InputFile inputFile) throws SVNException {
SVNStatusClient statusClient = clientManager.getStatusClient();
try {
SVNStatus status = statusClient.doStatus(inputFile.file(), false);
if (status == null) {
LOG.debug("File {} returns no svn state. Skipping it.", inputFile);
return false;
}
if (status.getContentsStatus() != SVNStatusType.STATUS_NORMAL) {
LOG.debug("File {} is not versionned or contains local modifications. Skipping it.", inputFile);
return false;
}
} catch (SVNException e) {
if (SVNErrorCode.WC_PATH_NOT_FOUND.equals(e.getErrorMessage().getErrorCode())
|| SVNErrorCode.WC_NOT_WORKING_COPY.equals(e.getErrorMessage().getErrorCode())) {
LOG.debug("File {} is not versionned. Skipping it.", inputFile);
return false;
}
throw e;
}
return true;
}
}
|