This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch fix-gpg-detection
in repository https://gitbox.apache.org/repos/asf/maven-gpg-plugin.git

commit b5baa51bb334bd442089f3d690c2cdd0be1c8736
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Tue Jun 30 01:38:49 2026 +0200

    Fix #322: Add environment-aware GPG path handling
    
    Instead of unconditionally converting Windows paths to MinGW format,
    now detect whether GPG is a MinGW/Cygwin build from version output.
    
    - Extended GpgVersionParser to detect MinGW/Cygwin environments by
      examining version output for 'mingw', 'cygwin', or 'msys' keywords
    - Added isMinGWorCygwin() method to expose detection result
    - Created convertPathForGpg() that only applies D:/path → /d/path
      conversion when GPG is detected as MinGW/Cygwin
    - Applies conversion conditionally to homedir, output, and input files
    - Preserves native Windows GPG behavior for non-MinGW builds
    
    This approach is more robust as it:
    - Doesn't break native Windows GPG installations
    - Automatically adapts to MinGW/Cygwin environments (Git Bash, MSYS2)
    - No user configuration required
    - Based on actual GPG runtime detection
---
 .../org/apache/maven/plugins/gpg/GpgSigner.java    | 25 +++++++++++++++++++---
 .../apache/maven/plugins/gpg/GpgVersionParser.java | 16 ++++++++++++++
 2 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/gpg/GpgSigner.java 
b/src/main/java/org/apache/maven/plugins/gpg/GpgSigner.java
index d3955ec..02a0ebd 100644
--- a/src/main/java/org/apache/maven/plugins/gpg/GpgSigner.java
+++ b/src/main/java/org/apache/maven/plugins/gpg/GpgSigner.java
@@ -50,6 +50,20 @@ public class GpgSigner extends AbstractGpgSigner {
         return keyname != null ? keyname : "default";
     }
 
+    /**
+     * Converts a Windows path to MinGW format only if GPG is running in a 
MinGW/Cygwin environment.
+     * For example, converts "D:/a/b/c" to "/d/a/b/c".
+     * This prevents MSYS2/GPG from incorrectly prepending the current working 
directory.
+     */
+    private String convertPathForGpg(String path, boolean isMinGWorCygwin) {
+        String forwardPath = path.replace('\\', '/');
+        if (isMinGWorCygwin && forwardPath.length() >= 2 && 
forwardPath.charAt(1) == ':') {
+            char driveLetter = Character.toLowerCase(forwardPath.charAt(0));
+            return "/" + driveLetter + forwardPath.substring(2);
+        }
+        return forwardPath;
+    }
+
     /**
      * {@inheritDoc}
      */
@@ -76,6 +90,11 @@ public class GpgSigner extends AbstractGpgSigner {
             throw new MojoExecutionException("Could not determine gpg 
version");
         }
 
+        boolean isMinGWorCygwin = versionParser.isMinGWorCygwin();
+        if (isMinGWorCygwin) {
+            getLog().debug("GPG detected as MinGW/Cygwin build - will use 
MinGW path format");
+        }
+
         getLog().debug("GPG Version: " + gpgVersion);
 
         if (args != null) {
@@ -86,7 +105,7 @@ public class GpgSigner extends AbstractGpgSigner {
 
         if (homeDir != null) {
             cmd.createArg().setValue("--homedir");
-            cmd.createArg().setValue(homeDir.getAbsolutePath().replace('\\', 
'/'));
+            
cmd.createArg().setValue(convertPathForGpg(homeDir.getAbsolutePath(), 
isMinGWorCygwin));
         }
 
         if (gpgVersion.isBefore(GpgVersion.parse("2.1"))) {
@@ -178,9 +197,9 @@ public class GpgSigner extends AbstractGpgSigner {
         }
 
         cmd.createArg().setValue("--output");
-        cmd.createArg().setValue(signature.getAbsolutePath().replace('\\', 
'/'));
+        
cmd.createArg().setValue(convertPathForGpg(signature.getAbsolutePath(), 
isMinGWorCygwin));
 
-        cmd.createArg().setValue(file.getAbsolutePath().replace('\\', '/'));
+        cmd.createArg().setValue(convertPathForGpg(file.getAbsolutePath(), 
isMinGWorCygwin));
 
         // 
----------------------------------------------------------------------------
         // Execute the command line
diff --git a/src/main/java/org/apache/maven/plugins/gpg/GpgVersionParser.java 
b/src/main/java/org/apache/maven/plugins/gpg/GpgVersionParser.java
index 8b38188..f5f6e9f 100644
--- a/src/main/java/org/apache/maven/plugins/gpg/GpgVersionParser.java
+++ b/src/main/java/org/apache/maven/plugins/gpg/GpgVersionParser.java
@@ -79,6 +79,10 @@ public class GpgVersionParser {
         return consumer.getGpgVersion();
     }
 
+    public boolean isMinGWorCygwin() {
+        return consumer.isMinGWorCygwin();
+    }
+
     /**
      * Consumes the output of {@code gpg --version}
      *
@@ -89,6 +93,7 @@ public class GpgVersionParser {
         private final Pattern gpgVersionPattern = Pattern.compile("gpg 
\\([^)]+\\) .+");
 
         private GpgVersion gpgVersion;
+        private boolean isMinGWorCygwin = false;
 
         @Override
         public void consumeLine(String line) throws IOException {
@@ -96,10 +101,21 @@ public class GpgVersionParser {
             if (m.matches()) {
                 gpgVersion = GpgVersion.parse(m.group());
             }
+
+            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
+                String lowerLine = line.toLowerCase();
+                if (lowerLine.contains("cygwin") || 
lowerLine.contains("mingw") || lowerLine.contains("msys")) {
+                    isMinGWorCygwin = true;
+                }
+            }
         }
 
         public GpgVersion getGpgVersion() {
             return gpgVersion;
         }
+
+        public boolean isMinGWorCygwin() {
+            return isMinGWorCygwin;
+        }
     }
 }

Reply via email to