Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipe to print out git provenance #2640

Merged
merged 1 commit into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
)
);
}
}