|
36 | 36 | import org.jspecify.annotations.Nullable; |
37 | 37 | import org.openrewrite.ExecutionContext; |
38 | 38 | import org.openrewrite.ParseExceptionResult; |
| 39 | +import org.openrewrite.PathUtils; |
39 | 40 | import org.openrewrite.SourceFile; |
40 | 41 | import org.openrewrite.internal.StringUtils; |
41 | 42 | import org.openrewrite.java.JavaParser; |
|
45 | 46 | import org.openrewrite.java.marker.JavaVersion; |
46 | 47 | import org.openrewrite.jgit.api.Git; |
47 | 48 | import org.openrewrite.jgit.lib.FileMode; |
| 49 | +import org.openrewrite.jgit.lib.ObjectId; |
| 50 | +import org.openrewrite.jgit.revwalk.RevCommit; |
| 51 | +import org.openrewrite.jgit.revwalk.RevWalk; |
48 | 52 | import org.openrewrite.jgit.treewalk.FileTreeIterator; |
49 | 53 | import org.openrewrite.jgit.treewalk.TreeWalk; |
50 | 54 | import org.openrewrite.jgit.treewalk.WorkingTreeIterator; |
| 55 | +import org.openrewrite.jgit.treewalk.filter.PathFilter; |
51 | 56 | import org.openrewrite.jgit.treewalk.filter.PathFilterGroup; |
52 | 57 | import org.openrewrite.kotlin.KotlinParser; |
53 | 58 | import org.openrewrite.marker.*; |
@@ -226,6 +231,7 @@ public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, Xml.@Nullab |
226 | 231 | sourceFiles = Stream.concat(sourceFiles, nonProjectResources); |
227 | 232 |
|
228 | 233 | return sourceFiles.map(addProvenance(projectProvenance)) |
| 234 | + .map(addGitTreeEntryInformation()) |
229 | 235 | .map(this::logParseErrors); |
230 | 236 | } |
231 | 237 |
|
@@ -830,6 +836,36 @@ private <T extends SourceFile> UnaryOperator<T> addProvenance(List<Marker> prove |
830 | 836 | }; |
831 | 837 | } |
832 | 838 |
|
| 839 | + private <T extends SourceFile> UnaryOperator<T> addGitTreeEntryInformation() { |
| 840 | + return s -> { |
| 841 | + if (repository == null) { |
| 842 | + return s; |
| 843 | + } |
| 844 | + |
| 845 | + try { |
| 846 | + ObjectId head = repository.resolve("HEAD"); |
| 847 | + if (head == null) { |
| 848 | + return s; |
| 849 | + } |
| 850 | + |
| 851 | + try (RevWalk revWalk = new RevWalk(repository); |
| 852 | + TreeWalk treeWalk = new TreeWalk(repository)) { |
| 853 | + RevCommit commit = revWalk.parseCommit(head); |
| 854 | + treeWalk.addTree(commit.getTree()); |
| 855 | + treeWalk.setRecursive(true); |
| 856 | + treeWalk.setFilter(PathFilter.create(PathUtils.separatorsToUnix(s.getSourcePath().toString()))); |
| 857 | + |
| 858 | + if (treeWalk.next()) { |
| 859 | + return s.withMarkers(s.getMarkers().add(new GitTreeEntry(randomId(), treeWalk.getObjectId(0).name(), treeWalk.getRawMode(0)))); |
| 860 | + } |
| 861 | + return s; |
| 862 | + } |
| 863 | + } catch (IOException e) { |
| 864 | + throw new UncheckedIOException(e); |
| 865 | + } |
| 866 | + }; |
| 867 | + } |
| 868 | + |
833 | 869 | private static Collection<Path> listJavaSources(MavenProject mavenProject, List<String> compileSourceRoots) throws MojoExecutionException { |
834 | 870 | Set<Path> javaSources = new LinkedHashSet<>(); |
835 | 871 | for (String compileSourceRoot : compileSourceRoots) { |
|
0 commit comments