Skip to content

Commit

Permalink
Add support for System Property in JGitBuildTimestampProvider
Browse files Browse the repository at this point in the history
and add a fallback option.

(cherry picked from commit 376d1f9)
  • Loading branch information
laeubi committed Sep 1, 2024
1 parent 178df57 commit ec10bed
Showing 1 changed file with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,31 +107,18 @@ public class JGitBuildTimestampProvider implements BuildTimestampProvider {

private enum DirtyBehavior {

ERROR, WARNING, IGNORE;
ERROR, WARNING, IGNORE, FALLBACK;

public static DirtyBehavior getDirtyWorkingTreeBehaviour(MojoExecution execution) {
final DirtyBehavior defaultBehaviour = ERROR;
Xpp3Dom pluginConfiguration = getDom(execution);
if (pluginConfiguration == null) {
return defaultBehaviour;
}
Xpp3Dom dirtyWorkingTreeDom = pluginConfiguration.getChild(PARAMETER_JGIT_DIRTY_WORKING_TREE);
if (dirtyWorkingTreeDom == null) {
return defaultBehaviour;
}
String value = dirtyWorkingTreeDom.getValue();
if (value == null) {
return defaultBehaviour;
}
value = value.trim();
if ("warning".equals(value)) {
return WARNING;
} else if ("ignore".equals(value)) {
return IGNORE;
public static DirtyBehavior getDirtyWorkingTreeBehaviour(String value) {
if (value != null && !value.isBlank()) {
for (DirtyBehavior behavior : DirtyBehavior.values()) {
if (behavior.name().equalsIgnoreCase(value)) {
return behavior;
}
}
}
return defaultBehaviour;
return ERROR;
}

}

@Override
Expand All @@ -158,7 +145,8 @@ public Date getTimestamp(MavenSession session, MavenProject project, MojoExecuti
}
return defaultTimestampProvider.getTimestamp(session, project, execution);
}
DirtyBehavior dirtyBehaviour = DirtyBehavior.getDirtyWorkingTreeBehaviour(execution);
DirtyBehavior dirtyBehaviour = DirtyBehavior
.getDirtyWorkingTreeBehaviour(getDirtyBehaviorValue(execution));
if (dirtyBehaviour != DirtyBehavior.IGNORE) {
// 1. check if 'git status' is clean for relPath
IndexDiff diff = new IndexDiff(repository, headId, new FileTreeIterator(repository));
Expand All @@ -171,6 +159,9 @@ public Date getTimestamp(MavenSession session, MavenProject project, MojoExecuti
diff.diff();
Status status = new Status(diff);
if (!status.isClean()) {
if (dirtyBehaviour == DirtyBehavior.FALLBACK) {
return defaultTimestampProvider.getTimestamp(session, project, execution);
}
String message = "Working tree is dirty.\ngit status " + (relPath != null ? relPath : "")
+ ":\n" + toGitStatusStyleOutput(diff);
if (dirtyBehaviour == DirtyBehavior.WARNING) {
Expand Down Expand Up @@ -210,6 +201,20 @@ public Date getTimestamp(MavenSession session, MavenProject project, MojoExecuti
}
}

private String getDirtyBehaviorValue(MojoExecution execution) {
Xpp3Dom pluginConfiguration = getDom(execution);
if (pluginConfiguration != null) {
Xpp3Dom dirtyWorkingTreeDom = pluginConfiguration.getChild(PARAMETER_JGIT_DIRTY_WORKING_TREE);
if (dirtyWorkingTreeDom != null) {
String value = dirtyWorkingTreeDom.getValue();
if (value != null) {
return value.trim();
}
}
}
return System.getProperty(PARAMETER_JGIT_DIRTY_WORKING_TREE);
}

private static TreeFilter createPathFilter(String relPath, MojoExecution execution) {
if (relPath != null && !relPath.isEmpty()) {
return new PathFilter(relPath, getIgnoreFilter(execution));
Expand Down

0 comments on commit ec10bed

Please sign in to comment.