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

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-scm.git


The following commit(s) were added to refs/heads/master by this push:
     new e76df2af2 [SCM-1027] Fix git diff --cached invocation with two 
revisions
e76df2af2 is described below

commit e76df2af237ff9df567f84b364812d5f9751f971
Author: Arturo Bernal <[email protected]>
AuthorDate: Mon Nov 2 10:02:27 2026 +0100

    [SCM-1027] Fix git diff --cached invocation with two revisions
    
    GitDiffCommand.executeDiffCommand() always ran a second "git diff
    --cached" after the plain diff, passing it both the start and end
    revision when both were given. git diff --cached accepts at most one
    revision, so the command failed with a usage error, breaking any diff
    between two fixed revisions (e.g. scm:diff -DstartScmVersion=...
    -DendScmVersion=...).
    
    When both revisions are given, the plain diff already compares the
    two full trees and the index has no part to play in it, so skip the
    cached diff in that case instead of passing git an invalid
    two-revision --cached invocation.
    
    Claude-Session: https://claude.ai/code/session_0153qyh22xBMN1vPfWGz3jSH
---
 .../git/gitexe/command/diff/GitDiffCommand.java    | 16 ++--
 .../gitexe/command/diff/GitDiffCommandTest.java    | 92 ++++++++++++++++++++++
 2 files changed, 103 insertions(+), 5 deletions(-)

diff --git 
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/diff/GitDiffCommand.java
 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/diff/GitDiffCommand.java
index 9401688ac..a4a8f9d5c 100644
--- 
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/diff/GitDiffCommand.java
+++ 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/diff/GitDiffCommand.java
@@ -55,11 +55,17 @@ protected DiffScmResult executeDiffCommand(
                     clDiff2Index.toString(), "The git-diff command failed.", 
stderr.getOutput(), false);
         }
 
-        Commandline clDiff2Head = createCommandLine(fileSet.getBasedir(), 
startVersion, endVersion, true);
-
-        exitCode = GitCommandLineUtils.execute(clDiff2Head, consumer, stderr);
-        if (exitCode != 0) {
-            return new DiffScmResult(clDiff2Head.toString(), "The git-diff 
command failed.", stderr.getOutput(), false);
+        // "git diff --cached" takes at most one revision; with both start and 
end given, the plain diff
+        // above already compares the two and the index plays no part in it, 
so skip the cached diff
+        // instead of passing git an invalid two-revision "--cached" 
invocation (SCM-1027).
+        if (startVersion == null || endVersion == null) {
+            Commandline clDiff2Head = createCommandLine(fileSet.getBasedir(), 
startVersion, endVersion, true);
+
+            exitCode = GitCommandLineUtils.execute(clDiff2Head, consumer, 
stderr);
+            if (exitCode != 0) {
+                return new DiffScmResult(
+                        clDiff2Head.toString(), "The git-diff command 
failed.", stderr.getOutput(), false);
+            }
         }
 
         return new DiffScmResult(
diff --git 
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/java/org/apache/maven/scm/provider/git/gitexe/command/diff/GitDiffCommandTest.java
 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/java/org/apache/maven/scm/provider/git/gitexe/command/diff/GitDiffCommandTest.java
new file mode 100644
index 000000000..826aebd02
--- /dev/null
+++ 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/java/org/apache/maven/scm/provider/git/gitexe/command/diff/GitDiffCommandTest.java
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.apache.maven.scm.provider.git.gitexe.command.diff;
+
+import java.io.File;
+
+import org.apache.maven.scm.CommandParameters;
+import org.apache.maven.scm.ScmFileSet;
+import org.apache.maven.scm.ScmRevision;
+import org.apache.maven.scm.ScmTestCase;
+import org.apache.maven.scm.command.checkin.CheckInScmResult;
+import org.apache.maven.scm.command.checkout.CheckOutScmResult;
+import org.apache.maven.scm.command.diff.DiffScmResult;
+import org.apache.maven.scm.command.info.InfoScmResult;
+import org.apache.maven.scm.provider.ScmProvider;
+import org.apache.maven.scm.provider.git.GitScmTestUtils;
+import org.apache.maven.scm.repository.ScmRepository;
+import org.codehaus.plexus.util.FileUtils;
+import org.junit.jupiter.api.Test;
+
+import static 
org.apache.maven.scm.provider.git.GitScmTestUtils.GIT_COMMAND_LINE;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Reproduces SCM-1027: {@code git diff --cached} rejects two revision 
arguments, so a diff requested with
+ * both a start and an end revision failed instead of comparing the two.
+ */
+public class GitDiffCommandTest extends ScmTestCase {
+
+    @Test
+    void testDiffAcceptsBothStartAndEndRevision() throws Exception {
+        checkSystemCmdPresence(GIT_COMMAND_LINE);
+
+        File repo = getRepositoryRoot();
+        File checkedOutRepo = getWorkingCopy();
+
+        GitScmTestUtils.initRepo("src/test/resources/repository/", repo, 
getWorkingDirectory());
+
+        String scmUrl = "scm:git:" + 
repo.toPath().toAbsolutePath().toUri().toASCIIString();
+        ScmRepository scmRepository = 
getScmManager().makeScmRepository(scmUrl);
+
+        FileUtils.deleteDirectory(checkedOutRepo);
+        assertTrue(checkedOutRepo.mkdirs());
+
+        CheckOutScmResult checkOutResult = 
getScmManager().checkOut(scmRepository, new ScmFileSet(checkedOutRepo));
+        assertResultIsSuccess(checkOutResult);
+
+        GitScmTestUtils.setDefaultGitConfig(checkedOutRepo);
+
+        ScmProvider provider = getScmManager().getProviderByUrl(scmUrl);
+        ScmFileSet checkedOutFileSet = new ScmFileSet(checkedOutRepo);
+
+        File file = new File(checkedOutRepo, "scm-1027.txt");
+
+        FileUtils.fileWrite(file.getAbsolutePath(), "first revision");
+        CheckInScmResult firstCheckin = getScmManager().checkIn(scmRepository, 
checkedOutFileSet, "first revision");
+        assertResultIsSuccess(firstCheckin);
+        ScmRevision firstRevision = new ScmRevision(currentRevision(provider, 
scmRepository, checkedOutFileSet));
+
+        FileUtils.fileWrite(file.getAbsolutePath(), "second revision");
+        CheckInScmResult secondCheckin = 
getScmManager().checkIn(scmRepository, checkedOutFileSet, "second revision");
+        assertResultIsSuccess(secondCheckin);
+        ScmRevision secondRevision = new ScmRevision(currentRevision(provider, 
scmRepository, checkedOutFileSet));
+
+        DiffScmResult result = getScmManager().diff(scmRepository, 
checkedOutFileSet, firstRevision, secondRevision);
+
+        assertResultIsSuccess(result);
+    }
+
+    private static String currentRevision(ScmProvider provider, ScmRepository 
repository, ScmFileSet fileSet)
+            throws Exception {
+        InfoScmResult result = 
provider.info(repository.getProviderRepository(), fileSet, new 
CommandParameters());
+        assertTrue(result.isSuccess(), "info command should succeed: " + 
result.getProviderMessage());
+        return result.getInfoItems().get(0).getRevision();
+    }
+}

Reply via email to