From: David Pursehouse Date: Tue, 31 Jan 2017 00:30:32 +0000 (+0900) Subject: Merge branch 'stable-4.6' X-Git-Tag: v4.7.0.201704051617-r~92 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=62411453f1e821152cad7c8847728cc162a0b561;p=jgit.git Merge branch 'stable-4.6' * stable-4.6: Clean up orphan files in GC Change-Id: I4fb6b4cd03d032535a9c04ede784bea880b4536b Signed-off-by: David Pursehouse --- 62411453f1e821152cad7c8847728cc162a0b561 diff --cc org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java index e69bbe0a4d,e3e73e25f7..87a5ee5ae4 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java @@@ -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 + *

+ * 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. + *

+ */ + private void deleteOrphans() { + Path packDir = Paths.get(repo.getObjectsDirectory().getAbsolutePath(), + "pack"); //$NON-NLS-1$ + List fileNames = null; + try (Stream 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