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

slawekjaranowski 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 c0a1f1a5a JGitStatusCommand: filter results by fileSet.getFileList() 
if not empty (#1370)
c0a1f1a5a is described below

commit c0a1f1a5af1578dac13e5701caf81250a4d4c6c0
Author: Pierre MarĂ©chal <[email protected]>
AuthorDate: Thu Nov 26 21:20:17 2026 +0100

    JGitStatusCommand: filter results by fileSet.getFileList() if not empty 
(#1370)
    
    * Fix JGitStatusCommand to respect fileSet if not empty
    
    * Apply Spotless format
    
    * Account for repo and fileSet base path difference.
    Compare using normalized Path instead of plain Strings
    
    * Support fileSet filtering for both includes and excludes list
    
    * Replace unnecessary use of `getCanonicalFile` to avoid performance hit 
introduced with Java 12
    
    * Filter JGit status by the explicit file list, with tests
    
    ScmFileSet only fills includes/excludes for the pattern constructors, so a 
file list was never applied.
    
    * Apply batched suggestions from code review
    
    Co-authored-by: Slawomir Jaranowski <[email protected]>
    
    ---------
    
    Co-authored-by: Sylwester Lachiewicz <[email protected]>
    Co-authored-by: Slawomir Jaranowski <[email protected]>
---
 .../git/jgit/command/status/JGitStatusCommand.java | 30 ++++++-
 .../jgit/command/status/JGitStatusCommandTest.java | 91 ++++++++++++++++++++++
 2 files changed, 120 insertions(+), 1 deletion(-)

diff --git 
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/status/JGitStatusCommand.java
 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/status/JGitStatusCommand.java
index 60ad85021..ae4ca9d11 100644
--- 
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/status/JGitStatusCommand.java
+++ 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/status/JGitStatusCommand.java
@@ -19,9 +19,13 @@
 package org.apache.maven.scm.provider.git.jgit.command.status;
 
 import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 import org.apache.maven.scm.ScmException;
 import org.apache.maven.scm.ScmFile;
@@ -51,7 +55,25 @@ protected StatusScmResult 
executeStatusCommand(ScmProviderRepository repo, ScmFi
             git = JGitUtils.openRepo(fileSet.getBasedir());
             Status status = git.status().call();
             List<ScmFile> changedFiles = getFileStati(status);
-
+            if (isFileSetFiltered(fileSet)) {
+                // account for difference in repo and fileSet base path
+                Path gitRoot = git.getRepository()
+                        .getWorkTree()
+                        .toPath()
+                        .toAbsolutePath()
+                        .normalize();
+                Path fileSetBase =
+                        
fileSet.getBasedir().toPath().toAbsolutePath().normalize();
+                Path relativeBase = gitRoot.relativize(fileSetBase);
+                Set<Path> fileSetPaths = fileSet.getFileList().stream()
+                        .map(f -> f.toPath())
+                        .map(p -> p.isAbsolute()
+                                ? gitRoot.relativize(p.normalize())
+                                : relativeBase.resolve(p).normalize())
+                        .collect(Collectors.toSet());
+                changedFiles.removeIf(scmFile ->
+                        
!fileSetPaths.contains(Paths.get(scmFile.getPath()).normalize()));
+            }
             return new StatusScmResult("JGit status", changedFiles);
         } catch (IOException | GitAPIException e) {
             throw new ScmException("JGit status failure!", e);
@@ -75,4 +97,10 @@ private void addAsScmFiles(Collection<ScmFile> all, 
Collection<String> files, Sc
             all.add(new ScmFile(f, status));
         }
     }
+
+    private static boolean isFileSetFiltered(ScmFileSet fileSet) {
+        return !fileSet.getFileList().isEmpty()
+                || (fileSet.getIncludes() != null && 
!fileSet.getIncludes().isEmpty())
+                || (fileSet.getExcludes() != null && 
!fileSet.getExcludes().isEmpty());
+    }
 }
diff --git 
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/test/java/org/apache/maven/scm/provider/git/jgit/command/status/JGitStatusCommandTest.java
 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/test/java/org/apache/maven/scm/provider/git/jgit/command/status/JGitStatusCommandTest.java
new file mode 100644
index 000000000..4b25d0c2e
--- /dev/null
+++ 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/test/java/org/apache/maven/scm/provider/git/jgit/command/status/JGitStatusCommandTest.java
@@ -0,0 +1,91 @@
+/*
+ * 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.jgit.command.status;
+
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.apache.maven.scm.ScmFileSet;
+import org.apache.maven.scm.command.status.StatusScmResult;
+import org.eclipse.jgit.api.Git;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Status filtering when the file set base directory is a subdirectory of the 
work tree.
+ */
+class JGitStatusCommandTest {
+
+    @TempDir
+    Path workTree;
+
+    private File basedir;
+
+    @BeforeEach
+    void setUp() throws Exception {
+        Path sub = Files.createDirectory(workTree.resolve("sub"));
+        Files.write(sub.resolve("a.txt"), 
"a".getBytes(StandardCharsets.UTF_8));
+        Files.write(sub.resolve("b.txt"), 
"b".getBytes(StandardCharsets.UTF_8));
+        try (Git git = Git.init().setDirectory(workTree.toFile()).call()) {
+            git.add().addFilepattern("sub").call();
+            git.commit()
+                    .setMessage("initial")
+                    .setAuthor("test", "[email protected]")
+                    .setCommitter("test", "[email protected]")
+                    .setSign(false)
+                    .call();
+        }
+        Files.write(sub.resolve("a.txt"), 
"a2".getBytes(StandardCharsets.UTF_8));
+        Files.write(sub.resolve("b.txt"), 
"b2".getBytes(StandardCharsets.UTF_8));
+        basedir = sub.toFile();
+    }
+
+    @Test
+    void explicitFileListFiltersStatus() throws Exception {
+        assertEquals(Collections.singletonList("sub/a.txt"), changedPaths(new 
ScmFileSet(basedir, new File("a.txt"))));
+        assertEquals(
+                Collections.singletonList("sub/b.txt"),
+                changedPaths(new ScmFileSet(basedir, 
Collections.singletonList(new File("b.txt")))));
+    }
+
+    @Test
+    void includesAndExcludesFilterStatus() throws Exception {
+        assertEquals(Collections.singletonList("sub/a.txt"), changedPaths(new 
ScmFileSet(basedir, "*.txt", "b.txt")));
+    }
+
+    @Test
+    void absoluteFileListFiltersStatus() throws Exception {
+        assertEquals(
+                Collections.singletonList("sub/a.txt"),
+                changedPaths(new ScmFileSet(basedir, new File(basedir, 
"a.txt"))));
+    }
+
+    private List<String> changedPaths(ScmFileSet fileSet) throws Exception {
+        StatusScmResult result = new 
JGitStatusCommand().executeStatusCommand(null, fileSet);
+        return result.getChangedFiles().stream().map(f -> 
f.getPath()).sorted().collect(Collectors.toList());
+    }
+}

Reply via email to