aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
diff options
context:
space:
mode:
authorLuca Milanesio <luca.milanesio@gmail.com>2018-07-07 23:35:20 +0100
committerMatthias Sohn <matthias.sohn@sap.com>2018-07-26 01:13:56 +0200
commit977726e5bb97e8aeb5d4ede69adbd8fc9801a14f (patch)
treeffb232bc029c4f7d29bdfe67298f598590c0aa05 /org.eclipse.jgit
parenteea9a7a0ba1e0e065c9a0fd01f0210bc2cc74068 (diff)
downloadjgit-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')
-rw-r--r--org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties1
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java1
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java28
3 files changed, 19 insertions, 11 deletions
diff --git a/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties b/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties
index 68bb68e426..16f184babc 100644
--- a/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties
+++ b/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties
@@ -45,6 +45,7 @@ branchNameInvalid=Branch name {0} is not allowed
buildingBitmaps=Building bitmaps
cachedPacksPreventsIndexCreation=Using cached packs prevents index creation
cachedPacksPreventsListingObjects=Using cached packs prevents listing objects
+cannotAccessLastModifiedForSafeDeletion=Unable to access lastModifiedTime of file {0}, skip deletion since we cannot safely avoid race condition
cannotBeCombined=Cannot be combined.
cannotBeRecursiveWhenTreesAreIncluded=TreeWalk shouldn't be recursive when tree objects are included.
cannotChangeActionOnComment=Cannot change action on comment line in git-rebase-todo file, old action: {0}, new action: {1}.
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);