summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTerry Parker <tparker@google.com>2021-01-25 18:46:25 -0800
committerTerry Parker <tparker@google.com>2021-01-26 16:04:13 -0800
commitb79882586de256060076324cd988f3a1cbd92b38 (patch)
tree5c8c1a4f548095ae0fd02910beb20bfcde845a18
parent84dbc2d43169cdd41c79d853b75483cfd76ce7d6 (diff)
downloadjgit-b79882586de256060076324cd988f3a1cbd92b38.tar.gz
jgit-b79882586de256060076324cd988f3a1cbd92b38.zip
Wrap the Files.list returned Stream in a try-with-resources block
Adds a new FileUtils.hasFiles(Path) helper method to correctly handle the Files.list returned Stream. These errors were found by compiling the code using JDK11's javac compiler. Change-Id: Ie8017fa54eb56afc2e939a2988d8b2c5032cd00f Signed-off-by: Terry Parker <tparker@google.com>
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java4
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java4
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java18
3 files changed, 21 insertions, 5 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java
index c8d3ffcd8f..b939d37ca2 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java
@@ -21,7 +21,6 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
-import java.nio.file.Files;
import java.text.MessageFormat;
import java.text.ParseException;
import java.util.ArrayList;
@@ -728,8 +727,7 @@ public class FileRepository extends Repository {
throws IOException {
File reftableDir = new File(getDirectory(), Constants.REFTABLE);
File headFile = new File(getDirectory(), Constants.HEAD);
- if (reftableDir.exists()
- && Files.list(reftableDir.toPath()).findAny().isPresent()) {
+ if (reftableDir.exists() && FileUtils.hasFiles(reftableDir.toPath())) {
throw new IOException(JGitText.get().reftableDirExists);
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java
index 1a9d473a78..6faf42bcf8 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java
@@ -25,7 +25,6 @@ import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetEncoder;
-import java.nio.file.Files;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.time.Instant;
@@ -64,6 +63,7 @@ import org.eclipse.jgit.submodule.SubmoduleWalk;
import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FS.ExecutionResult;
+import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.Holder;
import org.eclipse.jgit.util.IO;
import org.eclipse.jgit.util.Paths;
@@ -986,7 +986,7 @@ public abstract class WorkingTreeIterator extends AbstractTreeIterator {
idOffset) == 0) {
Path p = repository.getWorkTree().toPath()
.resolve(entry.getPathString());
- return Files.list(p).findAny().isPresent();
+ return FileUtils.hasFiles(p);
}
return false;
} else if (mode == FileMode.SYMLINK.getBits())
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java
index aa39a44642..b9dd9baa61 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java
@@ -43,6 +43,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.regex.Pattern;
+import java.util.stream.Stream;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Constants;
@@ -802,6 +803,23 @@ public class FileUtils {
}
/**
+ * Whether the path is a directory with files in it.
+ *
+ * @param dir
+ * directory path
+ * @return {@code true} if the given directory path contains files
+ * @throws IOException
+ * on any I/O errors accessing the path
+ *
+ * @since 5.11
+ */
+ public static boolean hasFiles(Path dir) throws IOException {
+ try (Stream<Path> stream = Files.list(dir)) {
+ return stream.findAny().isPresent();
+ }
+ }
+
+ /**
* Whether the given file can be executed.
*
* @param file