-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recipe to print out git provenance (#2640)
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
rewrite-core/src/main/java/org/openrewrite/ShowGitProvenance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
}); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
rewrite-core/src/test/java/org/openrewrite/ShowGitProvenanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
) | ||
); | ||
} | ||
} |