3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.component;
22 import java.util.Optional;
23 import javax.annotation.Nullable;
24 import org.sonar.api.utils.MessageException;
25 import org.sonar.core.component.ComponentKeys;
26 import org.sonar.db.component.BranchType;
27 import org.sonar.scanner.protocol.output.ScannerReport;
28 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
30 import static java.lang.String.format;
31 import static org.apache.commons.lang.StringUtils.isEmpty;
32 import static org.apache.commons.lang.StringUtils.trimToNull;
35 * The default (and legacy) implementation of {@link Branch}. It is used
36 * when scanner is configured with parameter "sonar.branch".
37 * A legacy branch is implemented as a fork of the project, so any branch
38 * is considered as "main".
40 public class MainBranchImpl implements Branch {
43 private final String branchName;
45 public MainBranchImpl(@Nullable String name) {
46 this.branchName = name;
47 if (name != null && !ComponentKeys.isValidBranch(name)) {
48 throw MessageException.of(format("\"%s\" is not a valid branch name. "
49 + "Allowed characters are alphanumeric, '-', '_', '.' and '/'.", name));
54 public BranchType getType() {
55 return BranchType.LONG;
59 public boolean isMain() {
64 public Optional<String> getName() {
65 return Optional.ofNullable(branchName);
69 public boolean supportsCrossProjectCpd() {
70 // only on regular project, not on branches
71 return branchName == null;
75 public String generateKey(ScannerReport.Component module, @Nullable ScannerReport.Component fileOrDir) {
76 String moduleWithBranch = ComponentKeys.createKey(module.getKey(), branchName);
77 if (fileOrDir == null || isEmpty(fileOrDir.getPath())) {
78 return moduleWithBranch;
80 return ComponentKeys.createEffectiveKey(moduleWithBranch, trimToNull(fileOrDir.getPath()));