FileRepository: cleanup refs outside refs/ on reftable conversion

Change-Id: Iab7d3a08906e826e26572f534512a09d3a5876b0
Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
Han-Wen Nienhuys 2019-11-19 19:55:06 -08:00 committed by Matthias Sohn
parent a48b77d782
commit e0744891fe
2 changed files with 18 additions and 0 deletions

View File

@ -122,6 +122,11 @@ public void testRacyReload() throws Exception {
}
}
@Test
public void additionalRefsAreRemoved() {
assertFalse(new File(db.getDirectory(), Constants.HEAD).exists());
}
@Test
public void testCompactFully() throws Exception {
ObjectId c1 = db.resolve("master^^");

View File

@ -46,6 +46,8 @@
package org.eclipse.jgit.internal.storage.file;
import static java.util.stream.Collectors.toList;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@ -740,6 +742,10 @@ void convertToReftable(boolean writeLogs, boolean backup)
File packedRefs = new File(getDirectory(), Constants.PACKED_REFS);
File logsDir = new File(getDirectory(), Constants.LOGS);
List<String> additional = getRefDatabase().getAdditionalRefs().stream()
.map(Ref::getName).collect(toList());
additional.add(Constants.HEAD);
if (backup) {
FileUtils.rename(refsFile, new File(getDirectory(), "refs.old"));
if (packedRefs.exists()) {
@ -750,10 +756,17 @@ void convertToReftable(boolean writeLogs, boolean backup)
FileUtils.rename(logsDir,
new File(getDirectory(), Constants.LOGS + ".old"));
}
for (String r : additional) {
FileUtils.rename(new File(getDirectory(), r),
new File(getDirectory(), r + ".old"));
}
} else {
packedRefs.delete(); // ignore return value.
FileUtils.delete(logsDir, FileUtils.RECURSIVE);
FileUtils.delete(refsFile, FileUtils.RECURSIVE);
for (String r : additional) {
new File(getDirectory(), r).delete();
}
}
// Put new data.