]> source.dussan.org Git - jgit.git/commitdiff
Fix file handle leak in FetchCommand#fetchSubmodules 16/114916/1
authorTim Hosey <timhoseydev@gmail.com>
Thu, 4 Jan 2018 01:58:05 +0000 (02:58 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Thu, 4 Jan 2018 01:58:05 +0000 (02:58 +0100)
The private fetchSubmodules method in the FetchCommand class creates a
Repository instance for each submodule being fetched, but never calls
closes on it.

This leads to the leaking of file handles.

Bug: 526494
Change-Id: I7070388b8b62063d9d5cd31afae3015a8388044f
Signed-off-by: Tim Hosey <timhoseydev@gmail.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit/src/org/eclipse/jgit/api/FetchCommand.java

index b2c28dab0c9fab4532484079f4e9c181658307d8..5d178bc13c93a90b034fd8c59d3ceffe6a3c9912 100644 (file)
@@ -171,38 +171,42 @@ public class FetchCommand extends TransportCommand<FetchCommand, FetchResult> {
                        }
                        walk.setTree(revWalk.parseTree(fetchHead));
                        while (walk.next()) {
-                               Repository submoduleRepo = walk.getRepository();
-
-                               // Skip submodules that don't exist locally (have not been
-                               // cloned), are not registered in the .gitmodules file, or
-                               // not registered in the parent repository's config.
-                               if (submoduleRepo == null || walk.getModulesPath() == null
-                                               || walk.getConfigUrl() == null) {
-                                       continue;
-                               }
+                               try (Repository submoduleRepo = walk.getRepository()) {
+
+                                       // Skip submodules that don't exist locally (have not been
+                                       // cloned), are not registered in the .gitmodules file, or
+                                       // not registered in the parent repository's config.
+                                       if (submoduleRepo == null || walk.getModulesPath() == null
+                                                       || walk.getConfigUrl() == null) {
+                                               continue;
+                                       }
 
-                               FetchRecurseSubmodulesMode recurseMode = getRecurseMode(
-                                               walk.getPath());
-
-                               // When the fetch mode is "yes" we always fetch. When the mode
-                               // is "on demand", we only fetch if the submodule's revision was
-                               // updated to an object that is not currently present in the
-                               // submodule.
-                               if ((recurseMode == FetchRecurseSubmodulesMode.ON_DEMAND
-                                               && !submoduleRepo.hasObject(walk.getObjectId()))
-                                               || recurseMode == FetchRecurseSubmodulesMode.YES) {
-                                       FetchCommand f = new FetchCommand(submoduleRepo)
-                                                       .setProgressMonitor(monitor).setTagOpt(tagOption)
-                                                       .setCheckFetchedObjects(checkFetchedObjects)
-                                                       .setRemoveDeletedRefs(isRemoveDeletedRefs())
-                                                       .setThin(thin).setRefSpecs(refSpecs)
-                                                       .setDryRun(dryRun)
-                                                       .setRecurseSubmodules(recurseMode);
-                                       configure(f);
-                                       if (callback != null) {
-                                               callback.fetchingSubmodule(walk.getPath());
+                                       FetchRecurseSubmodulesMode recurseMode = getRecurseMode(
+                                                       walk.getPath());
+
+                                       // When the fetch mode is "yes" we always fetch. When the
+                                       // mode
+                                       // is "on demand", we only fetch if the submodule's revision
+                                       // was
+                                       // updated to an object that is not currently present in the
+                                       // submodule.
+                                       if ((recurseMode == FetchRecurseSubmodulesMode.ON_DEMAND
+                                                       && !submoduleRepo.hasObject(walk.getObjectId()))
+                                                       || recurseMode == FetchRecurseSubmodulesMode.YES) {
+                                               FetchCommand f = new FetchCommand(submoduleRepo)
+                                                               .setProgressMonitor(monitor)
+                                                               .setTagOpt(tagOption)
+                                                               .setCheckFetchedObjects(checkFetchedObjects)
+                                                               .setRemoveDeletedRefs(isRemoveDeletedRefs())
+                                                               .setThin(thin).setRefSpecs(refSpecs)
+                                                               .setDryRun(dryRun)
+                                                               .setRecurseSubmodules(recurseMode);
+                                               configure(f);
+                                               if (callback != null) {
+                                                       callback.fetchingSubmodule(walk.getPath());
+                                               }
+                                               results.addSubmodule(walk.getPath(), f.call());
                                        }
-                                       results.addSubmodule(walk.getPath(), f.call());
                                }
                        }
                } catch (IOException e) {