Skip to content

Commit

Permalink
Recipe to print out git provenance (#2640)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmccarp authored and sambsnyd committed Jan 14, 2023
1 parent 079657e commit a9dda38
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
62 changes: 62 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/ShowGitProvenance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2022 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite;

import org.openrewrite.internal.ListUtils;
import org.openrewrite.marker.GitProvenance;
import org.openrewrite.marker.Markup;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ShowGitProvenance extends Recipe {

@Override
public String getDisplayName() {
return "Show Git source control metadata";
}

@Override
public String getDescription() {
return "List out the contents of each unique `GitProvenance` marker in the set of source files. " +
"When everything is working correctly, exactly one such marker should be printed as all source files are " +
"expected to come from the same repository / branch / commit ID.";
}

@Override
protected List<SourceFile> visit(List<SourceFile> before, ExecutionContext ctx) {
Set<GitProvenance> provenances = new HashSet<>();

return ListUtils.map(before, sourceFile -> {
GitProvenance provenance = sourceFile.getMarkers().findFirst(GitProvenance.class).orElse(null);
if (provenance == null || !provenances.add(provenance)) {
return sourceFile;
}
return Markup.info(sourceFile, String.format("GitProvenance:\n" +
" origin: %s\n" +
" branch: %s\n" +
" changeset: %s\n" +
" autocrlf: %s\n" +
" eol: %s",
provenance.getOrigin(),
provenance.getBranch(),
provenance.getChange(),
provenance.getAutocrlf() != null ? provenance.getAutocrlf().toString() : "null",
provenance.getEol() != null ? provenance.getEol().toString() : "null"));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2022 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite;

import org.junit.jupiter.api.Test;
import org.openrewrite.marker.GitProvenance;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.marker.GitProvenance.AutoCRLF.False;
import static org.openrewrite.marker.GitProvenance.EOL.Native;
import static org.openrewrite.test.SourceSpecs.text;

class ShowGitProvenanceTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new ShowGitProvenance());
}

@Test
void showGitProvenance() {
rewriteRun(
text("Hello, World!", """
~~(GitProvenance:
origin: https://github.com/moderneinc/moderne-scm
branch: main
changeset: 1234567
autocrlf: False
eol: Native)~~>Hello, World!""",
spec -> spec.markers(new GitProvenance(Tree.randomId(), "https://github.com/moderneinc/moderne-scm", "main", "1234567", False, Native))
)
);
}
}

0 comments on commit a9dda38

Please sign in to comment.