]> source.dussan.org Git - jgit.git/commitdiff
[infer] Fix potential NPE in CloneCommand 85/89385/2
authorMatthias Sohn <matthias.sohn@sap.com>
Sun, 22 Jan 2017 23:15:28 +0000 (00:15 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Mon, 23 Jan 2017 18:55:12 +0000 (19:55 +0100)
Change-Id: Ie7eeba3ae719ff207c7535d535a9e0bd6c9e99e6
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java

index dd5da1582f7a422d098691a998f0737744abd3ef..b065b9484beb5ab352f274ae1b3ed3506ed6b911 100644 (file)
@@ -151,23 +151,35 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
                }
        }
 
+       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();
        }