diff options
author | Luca Milanesio <luca.milanesio@gmail.com> | 2018-07-07 23:35:20 +0100 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2018-07-26 01:13:56 +0200 |
commit | 977726e5bb97e8aeb5d4ede69adbd8fc9801a14f (patch) | |
tree | ffb232bc029c4f7d29bdfe67298f598590c0aa05 /org.eclipse.jgit/src/org/eclipse | |
parent | eea9a7a0ba1e0e065c9a0fd01f0210bc2cc74068 (diff) | |
download | jgit-977726e5bb97e8aeb5d4ede69adbd8fc9801a14f.tar.gz jgit-977726e5bb97e8aeb5d4ede69adbd8fc9801a14f.zip |
Delete all loose refs empty directories
Remove completely the empty directories under refs/<namespace>
including the first level partition of the changes, when they are
completely empty.
Bug: 536777
Change-Id: I88304d34cc42435919c2d1480258684d993dfdca
Signed-off-by: Luca Milanesio <luca.milanesio@gmail.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java | 1 | ||||
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java | 28 |
2 files changed, 18 insertions, 11 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java index 56a24482be..4b7459c15b 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java @@ -104,6 +104,7 @@ public class JGitText extends TranslationBundle { /***/ public String buildingBitmaps; /***/ public String cachedPacksPreventsIndexCreation; /***/ public String cachedPacksPreventsListingObjects; + /***/ public String cannotAccessLastModifiedForSafeDeletion; /***/ public String cannotBeCombined; /***/ public String cannotBeRecursiveWhenTreesAreIncluded; /***/ public String cannotChangeActionOnComment; diff --git 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 index eb5c1db50e..d99a485249 100644 --- 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 @@ -885,16 +885,31 @@ public class GC { private void deleteEmptyRefsFolders() throws IOException { Path refs = repo.getDirectory().toPath().resolve(Constants.R_REFS); + // Avoid deleting a folder that was created after the threshold so that concurrent + // operations trying to create a reference are not impacted + Instant threshold = Instant.now().minus(30, ChronoUnit.SECONDS); try (Stream<Path> entries = Files.list(refs)) { Iterator<Path> iterator = entries.iterator(); while (iterator.hasNext()) { try (Stream<Path> s = Files.list(iterator.next())) { - s.forEach(this::deleteDir); + s.filter(path -> canBeSafelyDeleted(path, threshold)).forEach(this::deleteDir); } } } } + private boolean canBeSafelyDeleted(Path path, Instant threshold) { + try { + return Files.getLastModifiedTime(path).toInstant().isBefore(threshold); + } + catch (IOException e) { + LOG.warn(MessageFormat.format( + JGitText.get().cannotAccessLastModifiedForSafeDeletion, + path), e); + return false; + } + } + private void deleteDir(Path dir) { try (Stream<Path> dirs = Files.walk(dir)) { dirs.filter(this::isDirectory).sorted(Comparator.reverseOrder()) @@ -910,16 +925,7 @@ public class GC { private void delete(Path d) { try { - // Avoid deleting a folder that was just created so that concurrent - // operations trying to create a reference are not impacted - Instant threshold = Instant.now().minus(30, ChronoUnit.SECONDS); - Instant lastModified = Files.getLastModifiedTime(d).toInstant(); - if (lastModified.isBefore(threshold)) { - // If the folder is not empty, the delete operation will fail - // silently. This is a cheaper alternative to filtering the - // stream in the calling method. - Files.delete(d); - } + Files.delete(d); } catch (IOException e) { LOG.error(MessageFormat.format(JGitText.get().cannotDeleteFile, d), e); |