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 003f865bd [SCM-706] Stage deletions of files missing on disk on git
check-in
003f865bd is described below
commit 003f865bdfdac3ee6b084f7e3279fb097104926a
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Tue Nov 17 02:11:59 2026 +0100
[SCM-706] Stage deletions of files missing on disk on git check-in
git-add fails with a pathspec error for a file that was removed with
git-rm. gitexe now passes only the files that exist to git-add and stages
the deletion of the missing ones with git rm --cached --ignore-unmatch,
which covers both a file already removed with git-rm and a tracked file
deleted on disk only. jgit's AddCommand does not stage the deletion of a
file deleted on disk, so the jgit check-in removes such files from the
index the same way.
Fixes #913
---
.../gitexe/command/checkin/GitCheckInCommand.java | 34 ++++++++-
.../command/checkin/GitCheckInCommandTckTest.java | 82 ++++++++++++++++++++++
.../jgit/command/checkin/JGitCheckInCommand.java | 15 ++++
3 files changed, 128 insertions(+), 3 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/checkin/GitCheckInCommand.java
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/checkin/GitCheckInCommand.java
index 02be947e5..f52e643ad 100644
---
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/checkin/GitCheckInCommand.java
+++
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/checkin/GitCheckInCommand.java
@@ -93,7 +93,20 @@ protected CheckInScmResult executeCheckInCommand(
}
try {
- if (!fileSet.getFileList().isEmpty()) {
+ // files that no longer exist cannot be given to git-add if they
were removed with git-rm (SCM-706),
+ // their deletion is staged with git-rm --cached instead
+ List<File> filesToAdd = new ArrayList<>();
+ List<File> filesToRemove = new ArrayList<>();
+ for (File file : fileSet.getFileList()) {
+ File absoluteFile = file.isAbsolute() ? file : new
File(fileSet.getBasedir(), file.getPath());
+ if (absoluteFile.exists()) {
+ filesToAdd.add(file);
+ } else {
+ filesToRemove.add(file);
+ }
+ }
+
+ if (!filesToAdd.isEmpty()) {
// if specific fileSet is given, we have to git-add them first
// otherwise we will use 'git-commit -a' later
@@ -101,7 +114,7 @@ protected CheckInScmResult executeCheckInCommand(
// SCM-714: Workaround for the Windows terminal command limit
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
- for (File file : fileSet.getFileList()) {
+ for (File file : filesToAdd) {
clAdd =
GitAddCommand.createCommandLine(fileSet.getBasedir(),
Collections.singletonList(file));
exitCode = GitCommandLineUtils.execute(clAdd, stdout,
stderr);
@@ -110,7 +123,7 @@ protected CheckInScmResult executeCheckInCommand(
}
}
} else {
- clAdd =
GitAddCommand.createCommandLine(fileSet.getBasedir(), fileSet.getFileList());
+ clAdd =
GitAddCommand.createCommandLine(fileSet.getBasedir(), filesToAdd);
exitCode = GitCommandLineUtils.execute(clAdd, stdout,
stderr);
}
@@ -120,6 +133,21 @@ protected CheckInScmResult executeCheckInCommand(
}
}
+ if (!filesToRemove.isEmpty()) {
+ Commandline clRemove =
GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "rm");
+ clRemove.createArg().setValue("--cached");
+ clRemove.createArg().setValue("--ignore-unmatch");
+ clRemove.createArg().setValue("--quiet");
+ clRemove.createArg().setValue("--");
+ GitCommandLineUtils.addTarget(clRemove, filesToRemove);
+ exitCode = GitCommandLineUtils.execute(clRemove, stdout,
stderr);
+
+ if (exitCode != 0) {
+ return new CheckInScmResult(
+ clRemove.toString(), "The git-rm command failed.",
stderr.getOutput(), false);
+ }
+ }
+
// SCM-709: statusCommand uses repositoryRoot instead of
workingDirectory, adjust it with
// relativeRepositoryPath
URI relativeRepositoryPath =
GitStatusCommand.getRelativeCWD(logger, fileSet);
diff --git
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gittest/src/main/java/org/apache/maven/scm/provider/git/command/checkin/GitCheckInCommandTckTest.java
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gittest/src/main/java/org/apache/maven/scm/provider/git/command/checkin/GitCheckInCommandTckTest.java
index b4eae2c2d..7fb771d4d 100644
---
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gittest/src/main/java/org/apache/maven/scm/provider/git/command/checkin/GitCheckInCommandTckTest.java
+++
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gittest/src/main/java/org/apache/maven/scm/provider/git/command/checkin/GitCheckInCommandTckTest.java
@@ -18,12 +18,16 @@
*/
package org.apache.maven.scm.provider.git.command.checkin;
+import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
+import java.util.Arrays;
import org.apache.maven.scm.CommandParameter;
import org.apache.maven.scm.CommandParameters;
@@ -31,6 +35,7 @@
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.command.checkin.CheckInScmResult;
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
+import org.apache.maven.scm.command.remove.RemoveScmResult;
import org.apache.maven.scm.provider.git.GitScmTestUtils;
import org.apache.maven.scm.provider.git.util.GitUtil;
import org.apache.maven.scm.repository.ScmRepository;
@@ -39,6 +44,7 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author <a href="mailto:[email protected]">Mark Struberg</a>
@@ -144,6 +150,82 @@ void testCommitNoVerify() throws Exception {
}
}
+ /**
+ * SCM-706: a file removed from the index and from the disk must not break
the check-in of an explicit file set.
+ */
+ @Test
+ void testCheckInFileSetWithRemovedFile() throws Exception {
+ File workingCopy = getWorkingCopy();
+ GitScmTestUtils.setDefaultGitConfig(workingCopy);
+
+ RemoveScmResult removeResult =
+ getScmManager().remove(getScmRepository(), new
ScmFileSet(workingCopy, new File("readme.txt")), "");
+ assertResultIsSuccess(removeResult);
+ assertFalse(new File(workingCopy, "readme.txt").exists(), "readme.txt
should be removed from the disk");
+
+ FileUtils.fileWrite(new File(workingCopy,
"beer.xml").getAbsolutePath(), "1 litre");
+ assertResultIsSuccess(
+ getScmManager().add(getScmRepository(), new
ScmFileSet(workingCopy, new File("beer.xml")), "beer.xml"));
+
+ CommandParameters commandParameters = new CommandParameters();
+ commandParameters.setString(CommandParameter.MESSAGE, "Remove one file
and add another");
+
+ CheckInScmResult result = getScmManager()
+ .checkIn(
+ getScmRepository(),
+ new ScmFileSet(workingCopy, Arrays.asList(new
File("readme.txt"), new File("beer.xml"))),
+ commandParameters);
+ assertResultIsSuccess(result);
+
+ String lastCommit = git(workingCopy, "show", "--name-status",
"--format=", "HEAD");
+ assertTrue(lastCommit.contains("D\treadme.txt"), lastCommit);
+ assertTrue(lastCommit.contains("A\tbeer.xml"), lastCommit);
+ }
+
+ /**
+ * SCM-706: a tracked file deleted from the disk only (not with git-rm) is
checked in as a deletion.
+ */
+ @Test
+ void testCheckInFileSetWithFileDeletedOnDisk() throws Exception {
+ File workingCopy = getWorkingCopy();
+ GitScmTestUtils.setDefaultGitConfig(workingCopy);
+
+ assertTrue(new File(workingCopy, "readme.txt").delete(), "readme.txt
should be deleted from the disk");
+
+ CommandParameters commandParameters = new CommandParameters();
+ commandParameters.setString(CommandParameter.MESSAGE, "Delete one
file");
+
+ CheckInScmResult result = getScmManager()
+ .checkIn(
+ getScmRepository(),
+ new ScmFileSet(workingCopy, Arrays.asList(new
File("readme.txt"))),
+ commandParameters);
+ assertResultIsSuccess(result);
+
+ String lastCommit = git(workingCopy, "show", "--name-status",
"--format=", "HEAD");
+ assertTrue(lastCommit.contains("D\treadme.txt"), lastCommit);
+ }
+
+ private static String git(File workingDirectory, String... arguments)
throws Exception {
+ String[] command = new String[arguments.length + 1];
+ command[0] = "git";
+ System.arraycopy(arguments, 0, command, 1, arguments.length);
+ Process process = new ProcessBuilder(command)
+ .directory(workingDirectory)
+ .redirectErrorStream(true)
+ .start();
+ try (BufferedReader reader =
+ new BufferedReader(new
InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
+ StringBuilder output = new StringBuilder();
+ String line;
+ while ((line = reader.readLine()) != null) {
+ output.append(line).append('\n');
+ }
+ process.waitFor();
+ return output.toString();
+ }
+ }
+
private CheckOutScmResult checkoutRepoInto(File workingCopy, ScmRepository
scmRepository) throws Exception {
FileUtils.deleteDirectory(workingCopy);
workingCopy.mkdir();
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/checkin/JGitCheckInCommand.java
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommand.java
index bbbfecfae..d8faf42a9 100644
---
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommand.java
+++
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommand.java
@@ -50,6 +50,7 @@
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.RmCommand;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.api.errors.GitAPIException;
@@ -116,6 +117,20 @@ public CheckInScmResult
executeCommand(ScmProviderRepository repo, ScmFileSet fi
if (!fileSet.getFileList().isEmpty()) {
// add files first
doCommit = JGitUtils.addAllFiles(git, fileSet).size() > 0;
+
+ // AddCommand does not stage the deletion of a file that is
missing on the disk (SCM-706)
+ RmCommand rm = git.rm().setCached(true);
+ boolean anyMissing = false;
+ for (File file : JGitUtils.getWorkingCopyRelativePaths(
+ git.getRepository().getWorkTree(), fileSet)) {
+ if (!new File(git.getRepository().getWorkTree(),
file.getPath()).exists()) {
+
rm.addFilepattern(JGitUtils.toNormalizedFilePath(file));
+ anyMissing = true;
+ }
+ }
+ if (anyMissing) {
+ rm.call();
+ }
if (!doCommit) {
Status status = git.status().call();
doCommit = status.getAdded().size() > 0