Add AddCommand #addFilepatterns methods for varargs and collection arguments to facilitate adding multiple patterns in a single API call. Bug: jgit-247 Change-Id: I162eacea891d45a6e8feb7d0f6b66f8addfed78f
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java index 2266772..0d87109 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java
@@ -27,6 +27,7 @@ import java.io.OutputStream; import java.io.PrintWriter; import java.nio.file.Files; +import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -110,6 +111,46 @@ public void testAddExistingSingleFile() throws IOException, GitAPIException { } @Test + public void testAddExistingMultipleFiles() + throws IOException, GitAPIException { + File file = new File(db.getWorkTree(), "a.txt"); + FileUtils.createNewFile(file); + file = new File(db.getWorkTree(), "b.txt"); + FileUtils.createNewFile(file); + try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) { + writer.print("content"); + } + + try (Git git = new Git(db)) { + git.add().addFilepatterns("a.txt", "b.txt").call(); + + assertEquals( + "[a.txt, mode:100644, content:][b.txt, mode:100644, content:content]", + indexState(CONTENT)); + } + } + + @Test + public void testAddExistingMultipleFilesCollection() + throws IOException, GitAPIException { + File file = new File(db.getWorkTree(), "a.txt"); + FileUtils.createNewFile(file); + file = new File(db.getWorkTree(), "b.txt"); + FileUtils.createNewFile(file); + try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) { + writer.print("content"); + } + + try (Git git = new Git(db)) { + git.add().addFilepatterns(List.of("a.txt", "b.txt")).call(); + + assertEquals( + "[a.txt, mode:100644, content:][b.txt, mode:100644, content:content]", + indexState(CONTENT)); + } + } + + @Test public void testAddLink() throws IOException, GitAPIException { assumeTrue(db.getFS().supportsSymlinks()); try (Git git = new Git(db)) {
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/AddCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/AddCommand.java index b4d1cab..23bda6f 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/AddCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/AddCommand.java
@@ -20,7 +20,9 @@ import java.text.MessageFormat; import java.time.Instant; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Objects; import org.eclipse.jgit.api.errors.FilterFailedException; import org.eclipse.jgit.api.errors.GitAPIException; @@ -114,6 +116,36 @@ public AddCommand addFilepattern(String filepattern) { } /** + * Add paths to a file/directory whose content should be added. + * + * @param patterns + * repository-relative paths of file/directory to add (with + * <code>/</code> as separator) + * @return {@code this} + * @since 7.6 + */ + public AddCommand addFilepatterns(String... patterns) { + List.of(Objects.requireNonNull(patterns)).forEach(this::addFilepattern); + return this; + } + + /** + * Add paths to a file/directory whose content should be added. + * + * @param patterns + * repository-relative paths of file/directory to add (with + * <code>/</code> as separator) + * @return {@code this} + * @since 7.6 + */ + public AddCommand addFilepatterns(Collection<String> patterns) { + if (patterns != null) { + patterns.forEach(this::addFilepattern); + } + return this; + } + + /** * Allow clients to provide their own implementation of a FileTreeIterator * * @param f