From 376d1f96d3c17f495114371311152ad274cda713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sun, 1 Sep 2024 11:07:05 +0200 Subject: [PATCH] Add support for System Property in JGitBuildTimestampProvider and add a fallback option. --- .../jgit/JGitBuildTimestampProvider.java | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/tycho-buildtimestamp-jgit/src/main/java/org/eclipse/tycho/extras/buildtimestamp/jgit/JGitBuildTimestampProvider.java b/tycho-buildtimestamp-jgit/src/main/java/org/eclipse/tycho/extras/buildtimestamp/jgit/JGitBuildTimestampProvider.java index 3b0310fe8c..adf9f44cf5 100644 --- a/tycho-buildtimestamp-jgit/src/main/java/org/eclipse/tycho/extras/buildtimestamp/jgit/JGitBuildTimestampProvider.java +++ b/tycho-buildtimestamp-jgit/src/main/java/org/eclipse/tycho/extras/buildtimestamp/jgit/JGitBuildTimestampProvider.java @@ -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 @@ -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)); @@ -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) { @@ -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));