GitScmProvider extends ScmProvider { @Override @CheckForNull public String revisionId(Path path) { RepositoryBuilder builder = getVerifiedRepositoryBuilder(path); try { Ref head = getHead(builder.build()); if (head == null || head.getObjectId() == null) { return null; } return head.getObjectId().getName(); } catch (IOException e) { throw new IllegalStateException("I/O error while getting revision ID for path: " + path, e); } } GitScmProvider(JGitBlameCommand jgitBlameCommand, AnalysisWarnings analysisWarnings, GitIgnoreCommand gitIgnoreCommand, System2 system2); @Override GitIgnoreCommand ignoreCommand(); @Override String key(); @Override boolean supports(File baseDir); @Override BlameCommand blameCommand(); @CheckForNull @Override Set<Path> branchChangedFiles(String targetBranchName, Path rootBaseDir); @CheckForNull @Override Map<Path, Set<Integer>> branchChangedLines(String targetBranchName, Path projectBaseDir, Set<Path> changedFiles); @CheckForNull Instant forkDate(String referenceBranchName, Path projectBaseDir); @Override Path relativePathFromScmRoot(Path path); @Override @CheckForNull String revisionId(Path path);  }
@Test public void revisionId_should_return_different_sha1_after_commit() throws IOException, GitAPIException { Path projectDir = worktree.resolve("project"); Files.createDirectory(projectDir); GitScmProvider provider = newGitScmProvider(); String sha1before = provider.revisionId(projectDir); assertThat(sha1before).hasSize(40); createAndCommitFile("project/file1"); String sha1after = provider.revisionId(projectDir); assertThat(sha1after).hasSize(40); assertThat(sha1after).isNotEqualTo(sha1before); assertThat(provider.revisionId(projectDir)).isEqualTo(sha1after); }
@Test public void revisionId_should_return_null_in_empty_repo() throws IOException { worktree = temp.newFolder().toPath(); Repository repo = FileRepositoryBuilder.create(worktree.resolve(".git").toFile()); repo.create(); git = new Git(repo); Path projectDir = worktree.resolve("project"); Files.createDirectory(projectDir); GitScmProvider provider = newGitScmProvider(); assertThat(provider.revisionId(projectDir)).isNull(); }