1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.internal.storage.file;
12
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.IOException;
17 import java.nio.file.Files;
18 import java.nio.file.Path;
19 import java.nio.file.Paths;
20 import java.nio.file.attribute.FileTime;
21 import java.time.Instant;
22
23 import org.junit.Before;
24 import org.junit.Test;
25
26 public class GcDeleteEmptyRefsFoldersTest extends GcTestCase {
27 private static final String REF_FOLDER_01 = "A/B/01";
28 private static final String REF_FOLDER_02 = "C/D/02";
29
30 private Path refsDir;
31 private Path heads;
32
33 @Override
34 @Before
35 public void setUp() throws Exception {
36 super.setUp();
37 refsDir = Paths.get(repo.getDirectory().getAbsolutePath())
38 .resolve("refs");
39 heads = refsDir.resolve("heads");
40 }
41
42 @Test
43 public void emptyRefFoldersAreDeleted() throws Exception {
44 FileTime fileTime = FileTime.from(Instant.now().minusSeconds(31));
45 Path refDir01 = Files.createDirectories(heads.resolve(REF_FOLDER_01));
46 Path refDir02 = Files.createDirectories(heads.resolve(REF_FOLDER_02));
47 setLastModifiedTime(fileTime, heads, REF_FOLDER_01);
48 setLastModifiedTime(fileTime, heads, REF_FOLDER_02);
49 assertTrue(refDir01.toFile().exists());
50 assertTrue(refDir02.toFile().exists());
51 gc.gc();
52
53 assertFalse(refDir01.toFile().exists());
54 assertFalse(refDir01.getParent().toFile().exists());
55 assertFalse(refDir01.getParent().getParent().toFile().exists());
56 assertFalse(refDir02.toFile().exists());
57 assertFalse(refDir02.getParent().toFile().exists());
58 assertFalse(refDir02.getParent().getParent().toFile().exists());
59 }
60
61 @Test
62 public void emptyRefFoldersSkipFiles() throws Exception {
63 FileTime fileTime = FileTime.from(Instant.now().minusSeconds(31));
64 Path refFile = Files.createFile(refsDir.resolve(".DS_Store"));
65 Path refDir01 = Files.createDirectories(heads.resolve(REF_FOLDER_01));
66 Path refDir02 = Files.createDirectories(heads.resolve(REF_FOLDER_02));
67 setLastModifiedTime(fileTime, heads, REF_FOLDER_01);
68 setLastModifiedTime(fileTime, heads, REF_FOLDER_02);
69 assertTrue(refDir01.toFile().exists());
70 assertTrue(refDir02.toFile().exists());
71 gc.gc();
72 assertTrue(Files.exists(refFile));
73 }
74
75 private void setLastModifiedTime(FileTime fileTime, Path path, String folder) throws IOException {
76 long numParents = folder.chars().filter(c -> c == '/').count();
77 Path folderPath = path.resolve(folder);
78 for(int folderLevel = 0; folderLevel <= numParents; folderLevel ++ ) {
79 Files.setLastModifiedTime(folderPath, fileTime);
80 folderPath = folderPath.getParent();
81 }
82 }
83
84 @Test
85 public void emptyRefFoldersAreKeptIfTheyAreTooRecent()
86 throws Exception {
87 Path refDir01 = Files.createDirectories(heads.resolve(REF_FOLDER_01));
88 Path refDir02 = Files.createDirectories(heads.resolve(REF_FOLDER_02));
89 assertTrue(refDir01.toFile().exists());
90 assertTrue(refDir02.toFile().exists());
91 gc.gc();
92
93 assertTrue(refDir01.toFile().exists());
94 assertTrue(refDir02.toFile().exists());
95 }
96
97 @Test
98 public void nonEmptyRefsFoldersAreKept() throws Exception {
99 Path refDir01 = Files.createDirectories(heads.resolve(REF_FOLDER_01));
100 Path refDir02 = Files.createDirectories(heads.resolve(REF_FOLDER_02));
101 Path ref01 = Files.createFile(refDir01.resolve("ref01"));
102 Path ref02 = Files.createFile(refDir01.resolve("ref02"));
103 assertTrue(refDir01.toFile().exists());
104 assertTrue(refDir02.toFile().exists());
105 assertTrue(ref01.toFile().exists());
106 assertTrue(ref02.toFile().exists());
107 gc.gc();
108 assertTrue(refDir01.toFile().exists());
109 assertTrue(refDir02.toFile().exists());
110 assertTrue(ref01.toFile().exists());
111 assertTrue(ref02.toFile().exists());
112 }
113 }