From: Matthias Sohn Date: Sun, 22 Jan 2017 23:15:28 +0000 (+0100) Subject: [infer] Fix potential NPE in CloneCommand X-Git-Tag: v4.7.0.201704051617-r~114 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=423a583fcc343137b9369880b9b9a91fe03f6a59;p=jgit.git [infer] Fix potential NPE in CloneCommand Change-Id: Ie7eeba3ae719ff207c7535d535a9e0bd6c9e99e6 Signed-off-by: Matthias Sohn --- diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java index dd5da1582f..b065b9484b 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java @@ -151,23 +151,35 @@ public class CloneCommand extends TransportCommand { } } + private static boolean isNonEmptyDirectory(File dir) { + if (dir != null && dir.exists()) { + File[] files = dir.listFiles(); + return files != null && files.length != 0; + } + return false; + } + private Repository init(URIish u) throws GitAPIException { InitCommand command = Git.init(); command.setBare(bare); - if (directory == null && gitDir == null) + if (directory == null && gitDir == null) { directory = new File(u.getHumanishName(), Constants.DOT_GIT); + } validateDirs(directory, gitDir, bare); - if (directory != null && directory.exists() - && directory.listFiles().length != 0) + if (isNonEmptyDirectory(directory)) { throw new JGitInternalException(MessageFormat.format( JGitText.get().cloneNonEmptyDirectory, directory.getName())); - if (gitDir != null && gitDir.exists() && gitDir.listFiles().length != 0) + } + if (isNonEmptyDirectory(gitDir)) { throw new JGitInternalException(MessageFormat.format( JGitText.get().cloneNonEmptyDirectory, gitDir.getName())); - if (directory != null) + } + if (directory != null) { command.setDirectory(directory); - if (gitDir != null) + } + if (gitDir != null) { command.setGitDir(gitDir); + } return command.call().getRepository(); }