Skip to content

Commit db7dcf9

Browse files
committed
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
1 parent 9c8ba9e commit db7dcf9

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.OutputStream;
2828
import java.io.PrintWriter;
2929
import java.nio.file.Files;
30+
import java.util.List;
3031
import java.util.Set;
3132
import java.util.concurrent.TimeUnit;
3233

@@ -109,6 +110,46 @@ public void testAddExistingSingleFile() throws IOException, GitAPIException {
109110
}
110111
}
111112

113+
@Test
114+
public void testAddExistingMultipleFiles()
115+
throws IOException, GitAPIException {
116+
File file = new File(db.getWorkTree(), "a.txt");
117+
FileUtils.createNewFile(file);
118+
file = new File(db.getWorkTree(), "b.txt");
119+
FileUtils.createNewFile(file);
120+
try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
121+
writer.print("content");
122+
}
123+
124+
try (Git git = new Git(db)) {
125+
git.add().addFilepatterns("a.txt", "b.txt").call();
126+
127+
assertEquals(
128+
"[a.txt, mode:100644, content:][b.txt, mode:100644, content:content]",
129+
indexState(CONTENT));
130+
}
131+
}
132+
133+
@Test
134+
public void testAddExistingMultipleFilesCollection()
135+
throws IOException, GitAPIException {
136+
File file = new File(db.getWorkTree(), "a.txt");
137+
FileUtils.createNewFile(file);
138+
file = new File(db.getWorkTree(), "b.txt");
139+
FileUtils.createNewFile(file);
140+
try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
141+
writer.print("content");
142+
}
143+
144+
try (Git git = new Git(db)) {
145+
git.add().addFilepatterns(List.of("a.txt", "b.txt")).call();
146+
147+
assertEquals(
148+
"[a.txt, mode:100644, content:][b.txt, mode:100644, content:content]",
149+
indexState(CONTENT));
150+
}
151+
}
152+
112153
@Test
113154
public void testAddLink() throws IOException, GitAPIException {
114155
assumeTrue(db.getFS().supportsSymlinks());

org.eclipse.jgit/src/org/eclipse/jgit/api/AddCommand.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import java.text.MessageFormat;
2121
import java.time.Instant;
2222
import java.util.ArrayList;
23+
import java.util.Collection;
2324
import java.util.List;
25+
import java.util.Objects;
2426

2527
import org.eclipse.jgit.api.errors.FilterFailedException;
2628
import org.eclipse.jgit.api.errors.GitAPIException;
@@ -113,6 +115,36 @@ public AddCommand addFilepattern(String filepattern) {
113115
return this;
114116
}
115117

118+
/**
119+
* Add paths to a file/directory whose content should be added.
120+
*
121+
* @param patterns
122+
* repository-relative paths of file/directory to add (with
123+
* <code>/</code> as separator)
124+
* @return {@code this}
125+
* @since 7.6
126+
*/
127+
public AddCommand addFilepatterns(String... patterns) {
128+
List.of(Objects.requireNonNull(patterns)).forEach(this::addFilepattern);
129+
return this;
130+
}
131+
132+
/**
133+
* Add paths to a file/directory whose content should be added.
134+
*
135+
* @param patterns
136+
* repository-relative paths of file/directory to add (with
137+
* <code>/</code> as separator)
138+
* @return {@code this}
139+
* @since 7.6
140+
*/
141+
public AddCommand addFilepatterns(Collection<String> patterns) {
142+
if (patterns != null) {
143+
patterns.forEach(this::addFilepattern);
144+
}
145+
return this;
146+
}
147+
116148
/**
117149
* Allow clients to provide their own implementation of a FileTreeIterator
118150
*

0 commit comments

Comments
 (0)