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>
This commit is contained in:
parent
84dbc2d431
commit
b79882586d
|
@ -21,7 +21,6 @@
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -728,8 +727,7 @@ void convertToReftable(boolean writeLogs, boolean backup)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
File reftableDir = new File(getDirectory(), Constants.REFTABLE);
|
File reftableDir = new File(getDirectory(), Constants.REFTABLE);
|
||||||
File headFile = new File(getDirectory(), Constants.HEAD);
|
File headFile = new File(getDirectory(), Constants.HEAD);
|
||||||
if (reftableDir.exists()
|
if (reftableDir.exists() && FileUtils.hasFiles(reftableDir.toPath())) {
|
||||||
&& Files.list(reftableDir.toPath()).findAny().isPresent()) {
|
|
||||||
throw new IOException(JGitText.get().reftableDirExists);
|
throw new IOException(JGitText.get().reftableDirExists);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,6 @@
|
||||||
import java.nio.CharBuffer;
|
import java.nio.CharBuffer;
|
||||||
import java.nio.charset.CharacterCodingException;
|
import java.nio.charset.CharacterCodingException;
|
||||||
import java.nio.charset.CharsetEncoder;
|
import java.nio.charset.CharsetEncoder;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
@ -64,6 +63,7 @@
|
||||||
import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
|
import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
|
||||||
import org.eclipse.jgit.util.FS;
|
import org.eclipse.jgit.util.FS;
|
||||||
import org.eclipse.jgit.util.FS.ExecutionResult;
|
import org.eclipse.jgit.util.FS.ExecutionResult;
|
||||||
|
import org.eclipse.jgit.util.FileUtils;
|
||||||
import org.eclipse.jgit.util.Holder;
|
import org.eclipse.jgit.util.Holder;
|
||||||
import org.eclipse.jgit.util.IO;
|
import org.eclipse.jgit.util.IO;
|
||||||
import org.eclipse.jgit.util.Paths;
|
import org.eclipse.jgit.util.Paths;
|
||||||
|
@ -986,7 +986,7 @@ public boolean isModified(DirCacheEntry entry, boolean forceContentCheck,
|
||||||
idOffset) == 0) {
|
idOffset) == 0) {
|
||||||
Path p = repository.getWorkTree().toPath()
|
Path p = repository.getWorkTree().toPath()
|
||||||
.resolve(entry.getPathString());
|
.resolve(entry.getPathString());
|
||||||
return Files.list(p).findAny().isPresent();
|
return FileUtils.hasFiles(p);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
} else if (mode == FileMode.SYMLINK.getBits())
|
} else if (mode == FileMode.SYMLINK.getBits())
|
||||||
|
|
|
@ -43,6 +43,7 @@
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.eclipse.jgit.internal.JGitText;
|
import org.eclipse.jgit.internal.JGitText;
|
||||||
import org.eclipse.jgit.lib.Constants;
|
import org.eclipse.jgit.lib.Constants;
|
||||||
|
@ -801,6 +802,23 @@ static boolean isFile(File file) {
|
||||||
return Files.isRegularFile(file.toPath(), LinkOption.NOFOLLOW_LINKS);
|
return Files.isRegularFile(file.toPath(), LinkOption.NOFOLLOW_LINKS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Whether the given file can be executed.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue