]> source.dussan.org Git - jgit.git/commitdiff
Merge branch 'stable-4.6' 20/89920/2
authorDavid Pursehouse <david.pursehouse@gmail.com>
Tue, 31 Jan 2017 00:30:32 +0000 (09:30 +0900)
committerDavid Pursehouse <david.pursehouse@gmail.com>
Tue, 31 Jan 2017 00:31:10 +0000 (09:31 +0900)
* stable-4.6:
  Clean up orphan files in GC

Change-Id: I4fb6b4cd03d032535a9c04ede784bea880b4536b
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
1  2 
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java

index e69bbe0a4d6266693acaefa41dab265e0fdc6878,e3e73e25f7681d34a18f7620c723d99cd9c3976c..87a5ee5ae40a7eae3de26a7d45bf123936994594
@@@ -772,14 -671,48 +783,56 @@@ public class GC 
                return ret;
        }
  
 +      private static boolean isHead(Ref ref) {
 +              return ref.getName().startsWith(Constants.R_HEADS);
 +      }
 +
 +      private static boolean isTag(Ref ref) {
 +              return ref.getName().startsWith(Constants.R_TAGS);
 +      }
 +
+       /**
+        * Deletes orphans
+        * <p>
+        * A file is considered an orphan if it is either a "bitmap" or an index
+        * file, and its corresponding pack file is missing in the list.
+        * </p>
+        */
+       private void deleteOrphans() {
+               Path packDir = Paths.get(repo.getObjectsDirectory().getAbsolutePath(),
+                               "pack"); //$NON-NLS-1$
+               List<String> fileNames = null;
+               try (Stream<Path> files = Files.list(packDir)) {
+                       fileNames = files.map(path -> path.getFileName().toString())
+                                       .filter(name -> {
+                                               return (name.endsWith(PACK_EXT)
+                                                               || name.endsWith(BITMAP_EXT)
+                                                               || name.endsWith(INDEX_EXT));
+                                       }).sorted(Collections.reverseOrder())
+                                       .collect(Collectors.toList());
+               } catch (IOException e1) {
+                       // ignore
+               }
+               if (fileNames == null) {
+                       return;
+               }
+               String base = null;
+               for (String n : fileNames) {
+                       if (n.endsWith(PACK_EXT)) {
+                               base = n.substring(0, n.lastIndexOf('.'));
+                       } else {
+                               if (base == null || !n.startsWith(base)) {
+                                       try {
+                                               Files.delete(new File(packDir.toFile(), n).toPath());
+                                       } catch (IOException e) {
+                                               LOG.error(e.getMessage(), e);
+                                       }
+                               }
+                       }
+               }
+       }
        /**
         * @param ref
         *            the ref which log should be inspected