From: Simon Brandhof Date: Fri, 26 Sep 2014 08:22:13 +0000 (+0200) Subject: Revert "SONAR-5642 Initial work on Git provider" X-Git-Tag: 5.0-RC1~909 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c9fbb6a04bc2001bd9fb8519cb127c6c1ed56d29;p=sonarqube.git Revert "SONAR-5642 Initial work on Git provider" This reverts commit b375ce53532d11c75c9c920b36f0c4692d123c87. --- diff --git a/plugins/sonar-git-plugin/pom.xml b/plugins/sonar-git-plugin/pom.xml deleted file mode 100644 index ef707db2795..00000000000 --- a/plugins/sonar-git-plugin/pom.xml +++ /dev/null @@ -1,77 +0,0 @@ - - - 4.0.0 - - org.codehaus.sonar - sonar - 5.0-SNAPSHOT - ../.. - - org.codehaus.sonar.plugins - sonar-git-plugin - SonarQube :: Plugins :: Git - sonar-plugin - Git SCM Provider. - - - - com.google.code.findbugs - jsr305 - provided - - - org.codehaus.sonar - sonar-plugin-api - provided - - - - - org.codehaus.sonar - sonar-testing-harness - test - - - org.hamcrest - hamcrest-all - test - - - - org.mockito - mockito-core - test - - - org.codehaus.sonar - sonar-batch - ${project.version} - test - - - sonar-deprecated - org.codehaus.sonar - - - - - org.codehaus.sonar.plugins - sonar-xoo-plugin - test - - - - - - - org.codehaus.sonar - sonar-packaging-maven-plugin - - git - Git - org.sonar.plugins.scm.git.GitPlugin - - - - - diff --git a/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/GitPlugin.java b/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/GitPlugin.java deleted file mode 100644 index 033cef7b7de..00000000000 --- a/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/GitPlugin.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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.plugins.scm.git; - -import com.google.common.collect.ImmutableList; -import org.sonar.api.SonarPlugin; - -import java.util.List; - -public final class GitPlugin extends SonarPlugin { - - public List getExtensions() { - return ImmutableList.of( - GitScmProvider.class); - } - -} diff --git a/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/GitScmProvider.java b/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/GitScmProvider.java deleted file mode 100644 index 7fbfe516c20..00000000000 --- a/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/GitScmProvider.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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.plugins.scm.git; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.scm.ScmProvider; -import org.sonar.api.utils.command.Command; -import org.sonar.api.utils.command.CommandExecutor; -import org.sonar.api.utils.command.StreamConsumer; - -import java.io.File; - -public class GitScmProvider implements ScmProvider { - - private static final Logger LOG = LoggerFactory.getLogger(GitScmProvider.class); - - @Override - public String key() { - return "git"; - } - - @Override - public boolean supports(File baseDir) { - return new File(baseDir, ".git").exists(); - } - - @Override - public void blame(FileSystem fs, Iterable files, BlameResult result) { - for (InputFile inputFile : files) { - String filename = inputFile.relativePath(); - Command cl = createCommandLine(fs.baseDir(), filename); - SonarGitBlameConsumer consumer = new SonarGitBlameConsumer(LOG); - StringStreamConsumer stderr = new StringStreamConsumer(); - - int exitCode = execute(cl, consumer, stderr); - if (exitCode != 0) { - throw new IllegalStateException("The git blame command [" + cl.toString() + "] failed: " + stderr.getOutput()); - } - result.add(inputFile, consumer.getLines()); - } - } - - public static int execute(Command cl, StreamConsumer consumer, StreamConsumer stderr) { - LOG.info("Executing: " + cl); - LOG.info("Working directory: " + cl.getDirectory().getAbsolutePath()); - - return CommandExecutor.create().execute(cl, consumer, stderr, 10 * 1000); - } - - private static Command createCommandLine(File workingDirectory, String filename) { - Command cl = Command.create("git"); - cl.addArgument("blame"); - if (workingDirectory != null) { - cl.setDirectory(workingDirectory); - } - cl.addArgument("--porcelain"); - cl.addArgument(filename); - cl.addArgument("-w"); - return cl; - } - - private static class StringStreamConsumer implements StreamConsumer { - private StringBuffer string = new StringBuffer(); - - private String ls = System.getProperty("line.separator"); - - @Override - public void consumeLine(String line) { - string.append(line + ls); - } - - public String getOutput() { - return string.toString(); - } - } - -} diff --git a/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/SonarGitBlameConsumer.java b/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/SonarGitBlameConsumer.java deleted file mode 100644 index 3102b00f105..00000000000 --- a/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/SonarGitBlameConsumer.java +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Sonar SCM Activity Plugin - * Copyright (C) 2010 SonarSource - * dev@sonar.codehaus.org - * - * 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 02 - */ - -package org.sonar.plugins.scm.git; - -import com.google.common.annotations.VisibleForTesting; -import org.slf4j.Logger; -import org.sonar.api.batch.scm.BlameLine; -import org.sonar.api.utils.command.StreamConsumer; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Plain copy of package org.apache.maven.scm.provider.git.gitexe.command.blame.GitBlameConsumer - * Patched to allow user email retrieval when parsing Git blame results. - * - * @Todo: hack - to be submitted as an update in maven-scm-api for a future release - * - *

- * For more information, see: - * DEVACT-103 - * - * @since 1.5.1 - */ -public class SonarGitBlameConsumer implements StreamConsumer { - - private static final String GIT_COMMITTER_PREFIX = "committer"; - private static final String GIT_COMMITTER_TIME = GIT_COMMITTER_PREFIX + "-time "; - private static final String GIT_AUTHOR_EMAIL = "author-mail "; - private static final String GIT_COMMITTER_EMAIL = GIT_COMMITTER_PREFIX + "-mail "; - private static final String OPENING_EMAIL_FIELD = "<"; - private static final String CLOSING_EMAIL_FIELD = ">"; - - private List lines = new ArrayList(); - - /** - * Since the porcelain format only contains the commit information - * the first time a specific sha-1 commit appears, we need to store - * this information somwehere. - *

- * key: the sha-1 of the commit - * value: the {@link BlameLine} containing the full committer/author info - */ - private Map commitInfo = new HashMap(); - - private boolean expectRevisionLine = true; - - private String revision = null; - private String author = null; - private String committer = null; - private Date time = null; - private Logger logger; - - public Logger getLogger() { - return logger; - } - - public SonarGitBlameConsumer(Logger logger) { - this.logger = logger; - } - - public void consumeLine(String line) { - if (line == null) { - return; - } - - if (expectRevisionLine) { - // this is the revision line - consumeRevisionLine(line); - } else { - - if (extractCommitInfoFromLine(line)) { - return; - } - - if (line.startsWith("\t")) { - // this is the content line. - // we actually don't need the content, but this is the right time to add the blame line - consumeContentLine(); - } - } - } - - @VisibleForTesting - protected boolean extractCommitInfoFromLine(String line) { - if (line.startsWith(GIT_AUTHOR_EMAIL)) { - author = extractEmail(line); - return true; - } - - if (line.startsWith(GIT_COMMITTER_EMAIL)) { - committer = extractEmail(line); - return true; - } - - if (line.startsWith(GIT_COMMITTER_TIME)) { - String timeStr = line.substring(GIT_COMMITTER_TIME.length()); - time = new Date(Long.parseLong(timeStr) * 1000L); - return true; - } - return false; - } - - @VisibleForTesting - protected String getAuthor() { - return author; - } - - @VisibleForTesting - protected String getCommitter() { - return committer; - } - - @VisibleForTesting - protected Date getTime() { - return time; - } - - private String extractEmail(String line) { - - int emailStartIndex = line.indexOf(OPENING_EMAIL_FIELD); - int emailEndIndex = line.indexOf(CLOSING_EMAIL_FIELD); - - if (emailStartIndex == -1 || emailEndIndex == -1 || emailEndIndex <= emailStartIndex) { - return null; - } - return line.substring(emailStartIndex + 1, emailEndIndex); - } - - private void consumeContentLine() { - BlameLine blameLine = new BlameLine(time, revision, author, committer); - getLines().add(blameLine); - - // keep commitinfo for this sha-1 - commitInfo.put(revision, blameLine); - - if (getLogger().isDebugEnabled()) { - DateFormat df = SimpleDateFormat.getDateTimeInstance(); - getLogger().debug(author + " " + df.format(time)); - } - - expectRevisionLine = true; - } - - private void consumeRevisionLine(String line) { - String[] parts = line.split("\\s", 4); - - if (parts.length >= 1) { - revision = parts[0]; - - BlameLine oldLine = commitInfo.get(revision); - - if (oldLine != null) { - // restore the commit info - author = oldLine.getAuthor(); - committer = oldLine.getCommitter(); - time = oldLine.getDate(); - } - - expectRevisionLine = false; - } - } - - public List getLines() { - return lines; - } -} diff --git a/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/package-info.java b/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/package-info.java deleted file mode 100644 index 748474dc689..00000000000 --- a/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/package-info.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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. - */ - -@ParametersAreNonnullByDefault -package org.sonar.plugins.scm.git; - -import javax.annotation.ParametersAreNonnullByDefault; - diff --git a/plugins/sonar-git-plugin/src/test/java/org/sonar/plugins/scm/git/GitPluginTest.java b/plugins/sonar-git-plugin/src/test/java/org/sonar/plugins/scm/git/GitPluginTest.java deleted file mode 100644 index d65e0eb5844..00000000000 --- a/plugins/sonar-git-plugin/src/test/java/org/sonar/plugins/scm/git/GitPluginTest.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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.plugins.scm.git; - -import org.junit.Test; - -import static org.fest.assertions.Assertions.assertThat; - -public class GitPluginTest { - - @Test - public void getExtensions() { - assertThat(new GitPlugin().getExtensions()).hasSize(1); - } -} diff --git a/plugins/sonar-git-plugin/src/test/java/org/sonar/plugins/scm/git/medium/GitMediumTest.java b/plugins/sonar-git-plugin/src/test/java/org/sonar/plugins/scm/git/medium/GitMediumTest.java deleted file mode 100644 index a1f34593f1b..00000000000 --- a/plugins/sonar-git-plugin/src/test/java/org/sonar/plugins/scm/git/medium/GitMediumTest.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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.plugins.scm.git.medium; - -import com.google.common.collect.ImmutableMap; -import org.apache.commons.io.FileUtils; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.sensor.duplication.DuplicationGroup; -import org.sonar.batch.mediumtest.BatchMediumTester; -import org.sonar.batch.mediumtest.BatchMediumTester.TaskResult; -import org.sonar.plugins.scm.git.GitPlugin; -import org.sonar.xoo.XooPlugin; - -import java.io.File; -import java.io.IOException; -import java.util.List; - -import static org.fest.assertions.Assertions.assertThat; - -public class GitMediumTest { - - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - public BatchMediumTester tester = BatchMediumTester.builder() - .registerPlugin("xoo", new XooPlugin()) - .registerPlugin("git", new GitPlugin()) - .addDefaultQProfile("xoo", "Sonar Way") - .bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor")) - .build(); - - private File baseDir; - - private ImmutableMap.Builder builder; - - @Before - public void prepare() throws IOException { - tester.start(); - - baseDir = temp.newFolder(); - - builder = ImmutableMap.builder() - .put("sonar.task", "scan") - .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) - .put("sonar.projectKey", "com.foo.project") - .put("sonar.projectName", "Foo Project") - .put("sonar.projectVersion", "1.0-SNAPSHOT") - .put("sonar.projectDescription", "Description of Foo Project"); - } - - @After - public void stop() { - tester.stop(); - } - - @Test - public void testDuplications() throws IOException { - File srcDir = new File(baseDir, "src"); - srcDir.mkdir(); - - String duplicatedStuff = "Sample xoo\ncontent\nfoo\nbar\ntoto\ntiti\nfoo\nbar\ntoto\ntiti\nbar\ntoto\ntiti\nfoo\nbar\ntoto\ntiti"; - - File xooFile1 = new File(srcDir, "sample1.xoo"); - FileUtils.write(xooFile1, duplicatedStuff); - - File xooFile2 = new File(srcDir, "sample2.xoo"); - FileUtils.write(xooFile2, duplicatedStuff); - - TaskResult result = tester.newTask() - .properties(builder - .put("sonar.sources", "src") - .put("sonar.cpd.xoo.minimumTokens", "10") - .put("sonar.verbose", "true") - .build()) - .start(); - - assertThat(result.inputFiles()).hasSize(2); - - // 4 measures per file - assertThat(result.measures()).hasSize(8); - - InputFile inputFile = result.inputFiles().get(0); - // One clone group - List duplicationGroups = result.duplicationsFor(inputFile); - assertThat(duplicationGroups).hasSize(1); - - DuplicationGroup cloneGroup = duplicationGroups.get(0); - assertThat(cloneGroup.duplicates()).hasSize(1); - assertThat(cloneGroup.originBlock().startLine()).isEqualTo(1); - assertThat(cloneGroup.originBlock().length()).isEqualTo(17); - } - -} diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/XooScmProvider.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/XooScmProvider.java index 895d47dfbbe..71582efc3ef 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/XooScmProvider.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/XooScmProvider.java @@ -60,14 +60,14 @@ public class XooScmProvider implements ScmProvider { } @Override - public void blame(Iterable files, BlameResult handler) { + public void blame(Iterable files, BlameResultHandler handler) { for (InputFile inputFile : files) { processFile(inputFile, handler); } } @VisibleForTesting - protected void processFile(InputFile inputFile, BlameResult handler) { + protected void processFile(InputFile inputFile, BlameResultHandler handler) { File ioFile = inputFile.file(); File scmDataFile = new java.io.File(ioFile.getParentFile(), ioFile.getName() + SCM_EXTENSION); if (!scmDataFile.exists()) { @@ -94,7 +94,7 @@ public class XooScmProvider implements ScmProvider { blame.add(new BlameLine(date, revision, author)); } } - handler.add(inputFile, blame); + handler.handle(inputFile, blame); } catch (IOException e) { throw new IllegalStateException(e); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scm/ScmActivitySensor.java b/sonar-batch/src/main/java/org/sonar/batch/scm/ScmActivitySensor.java index 3a2cd265817..4bdf5163431 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scm/ScmActivitySensor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scm/ScmActivitySensor.java @@ -25,7 +25,7 @@ import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputFile.Status; import org.sonar.api.batch.scm.BlameLine; -import org.sonar.api.batch.scm.ScmProvider.BlameResult; +import org.sonar.api.batch.scm.ScmProvider.BlameResultHandler; import org.sonar.api.batch.sensor.Sensor; import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.SensorDescriptor; @@ -92,10 +92,10 @@ public final class ScmActivitySensor implements Sensor { filesToBlame.add(f); } } - configuration.provider().blame(fs, filesToBlame, new BlameResult() { + configuration.provider().blame(filesToBlame, new BlameResultHandler() { @Override - public void add(InputFile file, List lines) { + public void handle(InputFile file, List lines) { PropertiesBuilder authors = propertiesBuilder(CoreMetrics.SCM_AUTHORS_BY_LINE); PropertiesBuilder dates = propertiesBuilder(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/scm/ScmProvider.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/scm/ScmProvider.java index 2630f95b5e6..1fd3327d7c3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/scm/ScmProvider.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/scm/ScmProvider.java @@ -21,7 +21,6 @@ package org.sonar.api.batch.scm; import org.sonar.api.BatchExtension; import org.sonar.api.batch.InstantiationStrategy; -import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; import java.io.File; @@ -48,14 +47,14 @@ public interface ScmProvider extends BatchExtension { * Compute blame of the provided files. Computation can be done in parallel. * If there is an error that prevent to blame a file then an exception should be raised. */ - void blame(FileSystem fs, Iterable files, BlameResult result); + void blame(Iterable files, BlameResultHandler handler); /** - * Callback for the provider to save results of blame per file. + * Callback for the provider to return results of blame per file. */ - public static interface BlameResult { + public static interface BlameResultHandler { - void add(InputFile file, List lines); + void handle(InputFile file, List lines); }