From 423a583fcc343137b9369880b9b9a91fe03f6a59 Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Mon, 23 Jan 2017 00:15:28 +0100 Subject: [PATCH] [infer] Fix potential NPE in CloneCommand Change-Id: Ie7eeba3ae719ff207c7535d535a9e0bd6c9e99e6 Signed-off-by: Matthias Sohn --- .../org/eclipse/jgit/api/CloneCommand.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) 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(); } -- 2.39.5