1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.internal.storage.file;
45
46 import static org.junit.Assert.assertEquals;
47
48 import java.io.File;
49 import java.util.Collections;
50
51 import org.eclipse.jgit.api.Git;
52 import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
53 import org.eclipse.jgit.lib.Constants;
54 import org.eclipse.jgit.lib.ObjectId;
55 import org.eclipse.jgit.lib.RefUpdate;
56 import org.eclipse.jgit.revwalk.RevCommit;
57 import org.eclipse.jgit.util.FileUtils;
58 import org.junit.Test;
59
60 public class GcReflogTest extends GcTestCase {
61 @Test
62 public void testPruneNone() throws Exception {
63 BranchBuilder bb = tr.branch("refs/heads/master");
64 bb.commit().add("A", "A").add("B", "B").create();
65 bb.commit().add("A", "A2").add("B", "B2").create();
66 new File(repo.getDirectory(), Constants.LOGS + "/refs/heads/master")
67 .delete();
68 stats = gc.getStatistics();
69 assertEquals(8, stats.numberOfLooseObjects);
70 gc.setExpireAgeMillis(0);
71 fsTick();
72 gc.prune(Collections.<ObjectId> emptySet());
73 stats = gc.getStatistics();
74 assertEquals(8, stats.numberOfLooseObjects);
75 tr.blob("x");
76 stats = gc.getStatistics();
77 assertEquals(9, stats.numberOfLooseObjects);
78 fsTick();
79 gc.prune(Collections.<ObjectId> emptySet());
80 stats = gc.getStatistics();
81 assertEquals(8, stats.numberOfLooseObjects);
82 }
83
84 @Test
85 public void testPackRepoWithCorruptReflog() throws Exception {
86
87
88
89 RefUpdate ru = repo.updateRef(Constants.HEAD);
90 ru.link("refs/to/garbage");
91 tr.branch("refs/heads/master").commit().add("A", "A").add("B", "B")
92 .create();
93
94 Git.wrap(repo).checkout().setName("refs/heads/master").call();
95 gc.gc();
96 }
97
98 @Test
99 public void testPackCommitsAndLooseOneNoReflog() throws Exception {
100 BranchBuilder bb = tr.branch("refs/heads/master");
101 RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
102 bb.commit().add("A", "A2").add("B", "B2").create();
103 tr.update("refs/heads/master", first);
104
105 stats = gc.getStatistics();
106 assertEquals(8, stats.numberOfLooseObjects);
107 assertEquals(0, stats.numberOfPackedObjects);
108
109 FileUtils.delete(new File(repo.getDirectory(), "logs/HEAD"),
110 FileUtils.RETRY | FileUtils.SKIP_MISSING);
111 FileUtils.delete(
112 new File(repo.getDirectory(), "logs/refs/heads/master"),
113 FileUtils.RETRY | FileUtils.SKIP_MISSING);
114 gc.gc();
115
116 stats = gc.getStatistics();
117 assertEquals(4, stats.numberOfLooseObjects);
118 assertEquals(4, stats.numberOfPackedObjects);
119 assertEquals(1, stats.numberOfPackFiles);
120 }
121
122 @Test
123 public void testPackCommitsAndLooseOneWithPruneNowNoReflog()
124 throws Exception {
125 BranchBuilder bb = tr.branch("refs/heads/master");
126 RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
127 bb.commit().add("A", "A2").add("B", "B2").create();
128 tr.update("refs/heads/master", first);
129
130 stats = gc.getStatistics();
131 assertEquals(8, stats.numberOfLooseObjects);
132 assertEquals(0, stats.numberOfPackedObjects);
133
134 FileUtils.delete(new File(repo.getDirectory(), "logs/HEAD"),
135 FileUtils.RETRY | FileUtils.SKIP_MISSING);
136 FileUtils.delete(
137 new File(repo.getDirectory(), "logs/refs/heads/master"),
138 FileUtils.RETRY | FileUtils.SKIP_MISSING);
139 gc.setExpireAgeMillis(0);
140 gc.gc();
141
142 stats = gc.getStatistics();
143 assertEquals(0, stats.numberOfLooseObjects);
144 assertEquals(4, stats.numberOfPackedObjects);
145 assertEquals(1, stats.numberOfPackFiles);
146 }
147 }