public class GitScmProvider extends ScmProvider {
private static final Logger LOG = Loggers.get(GitScmProvider.class);
-
+ private static final String COULD_NOT_FIND_REF = "Could not find ref '%s' in refs/heads, refs/remotes, refs/remotes/upstream or refs/remotes/origin";
private final JGitBlameCommand jgitBlameCommand;
private final AnalysisWarnings analysisWarnings;
private final GitIgnoreCommand gitIgnoreCommand;
try (Repository repo = buildRepo(rootBaseDir)) {
Ref targetRef = resolveTargetRef(targetBranchName, repo);
if (targetRef == null) {
- analysisWarnings.addUnique(String.format("Could not find ref '%s' in refs/heads, refs/remotes/upstream or refs/remotes/origin. "
- + "You may see unexpected issues and changes. "
+ analysisWarnings.addUnique(String.format(COULD_NOT_FIND_REF
+ + ". You may see unexpected issues and changes. "
+ "Please make sure to fetch this ref before pull request analysis.", targetBranchName));
return null;
}
try (Repository repo = buildRepo(projectBaseDir)) {
Ref targetRef = resolveTargetRef(targetBranchName, repo);
if (targetRef == null) {
- analysisWarnings.addUnique(String.format("Could not find ref '%s' in refs/heads, refs/remotes/upstream or refs/remotes/origin. "
- + "You may see unexpected issues and changes. "
+ analysisWarnings.addUnique(String.format(COULD_NOT_FIND_REF
+ + ". You may see unexpected issues and changes. "
+ "Please make sure to fetch this ref before pull request analysis.", targetBranchName));
return null;
}
@CheckForNull
private Ref resolveTargetRef(String targetBranchName, Repository repo) throws IOException {
String localRef = "refs/heads/" + targetBranchName;
- String remoteRef = "refs/remotes/origin/" + targetBranchName;
+ String remotesRef = "refs/remotes/" + targetBranchName;
+ String originRef = "refs/remotes/origin/" + targetBranchName;
String upstreamRef = "refs/remotes/upstream/" + targetBranchName;
Ref targetRef;
// Because circle ci destroys the local reference to master, try to load remote ref first.
// https://discuss.circleci.com/t/git-checkout-of-a-branch-destroys-local-reference-to-master/23781
if (runningOnCircleCI()) {
- targetRef = getFirstExistingRef(repo, remoteRef, localRef, upstreamRef);
+ targetRef = getFirstExistingRef(repo, originRef, localRef, upstreamRef, remotesRef);
} else {
- targetRef = getFirstExistingRef(repo, localRef, remoteRef, upstreamRef);
+ targetRef = getFirstExistingRef(repo, localRef, originRef, upstreamRef, remotesRef);
}
-
+
if (targetRef == null) {
- LOG.warn("Could not find ref: {} in refs/heads, refs/remotes/upstream or refs/remotes/origin", targetBranchName);
+ LOG.warn(COULD_NOT_FIND_REF, targetBranchName);
}
return targetRef;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static org.sonar.scm.git.Utils.javaUnzip;
assertThat(newScmProvider().branchChangedFiles("master", worktree2))
.containsOnly(worktree2.resolve("file-b1"));
- verifyZeroInteractions(analysisWarnings);
+ verifyNoInteractions(analysisWarnings);
}
@Test
assertThat(newScmProvider().branchChangedFiles("master", worktree2))
.containsOnly(worktree2.resolve("file-b1"));
- verifyZeroInteractions(analysisWarnings);
+ verifyNoInteractions(analysisWarnings);
}
@Test
assertThat(newScmProvider().branchChangedFiles("local-only", worktree2))
.containsOnly(worktree2.resolve("file-b1"));
- verifyZeroInteractions(analysisWarnings);
+ verifyNoInteractions(analysisWarnings);
}
@Test
assertThat(newScmProvider().branchChangedFiles("master", worktree2))
.containsOnly(worktree2.resolve("file-b1"));
- verifyZeroInteractions(analysisWarnings);
+ verifyNoInteractions(analysisWarnings);
}
+ @Test
+ public void branchChangedFiles_finds_branch_in_specific_origin() throws IOException, GitAPIException {
+ git.branchCreate().setName("b1").call();
+ git.checkout().setName("b1").call();
+ createAndCommitFile("file-b1");
+
+ Path worktree2 = temp.newFolder().toPath();
+ Git.cloneRepository()
+ .setURI(worktree.toString())
+ .setRemote("upstream")
+ .setDirectory(worktree2.toFile())
+ .call();
+
+ assertThat(newScmProvider().branchChangedFiles("upstream/master", worktree2))
+ .containsOnly(worktree2.resolve("file-b1"));
+ verifyNoInteractions(analysisWarnings);
+ }
+
@Test
public void branchChangedFiles_should_return_null_when_branch_nonexistent() {
assertThat(newScmProvider().branchChangedFiles("nonexistent", worktree)).isNull();
}
};
assertThat(provider.branchChangedFiles("branch", worktree)).isNull();
- verifyZeroInteractions(analysisWarnings);
+ verifyNoInteractions(analysisWarnings);
}
@Test
};
assertThat(provider.branchChangedFiles("branch", worktree)).isNull();
- String warning = "Could not find ref 'branch' in refs/heads, refs/remotes/upstream or refs/remotes/origin."
+ String warning = "Could not find ref 'branch' in refs/heads, refs/remotes, refs/remotes/upstream or refs/remotes/origin."
+ " You may see unexpected issues and changes. Please make sure to fetch this ref before pull request analysis.";
verify(analysisWarnings).addUnique(warning);
}