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
45
46 package org.eclipse.jgit.lib;
47
48 import static org.junit.Assert.assertEquals;
49 import static org.junit.Assert.assertFalse;
50 import static org.junit.Assert.assertTrue;
51
52 import java.io.IOException;
53
54 import org.eclipse.jgit.junit.RepositoryTestCase;
55 import org.eclipse.jgit.storage.file.FileBasedConfig;
56 import org.junit.Test;
57
58 public class ReflogConfigTest extends RepositoryTestCase {
59 @Test
60 public void testlogAllRefUpdates() throws Exception {
61 long commitTime = 1154236443000L;
62 int tz = -4 * 60;
63
64
65
66 assertEquals(0, db.getReflogReader(Constants.HEAD).getReverseEntries().size());
67 final FileBasedConfig cfg = db.getConfig();
68 cfg.setBoolean("core", null, "logallrefupdates", false);
69 cfg.save();
70
71
72
73 commit("A Commit\n", commitTime, tz);
74 commitTime += 60 * 1000;
75 assertTrue(
76 "Reflog for HEAD still contain no entry",
77 db.getReflogReader(Constants.HEAD).getReverseEntries().size() == 0);
78
79
80 cfg.setBoolean("core", null, "logallrefupdates", true);
81 cfg.save();
82 assertTrue(cfg.get(CoreConfig.KEY).isLogAllRefUpdates());
83
84
85 commit("A Commit\n", commitTime, tz);
86 commitTime += 60 * 1000;
87 assertTrue(
88 "Reflog for HEAD should contain one entry",
89 db.getReflogReader(Constants.HEAD).getReverseEntries().size() == 1);
90
91
92 cfg.setBoolean("core", null, "logallrefupdates", false);
93 cfg.save();
94 assertFalse(cfg.get(CoreConfig.KEY).isLogAllRefUpdates());
95
96
97 commit("A Commit\n", commitTime, tz);
98 assertTrue(
99 "Reflog for HEAD should contain two entries",
100 db.getReflogReader(Constants.HEAD).getReverseEntries().size() == 2);
101 }
102
103 private void commit(String commitMsg, long commitTime, int tz)
104 throws IOException {
105 final CommitBuilder commit = new CommitBuilder();
106 commit.setAuthor(new PersonIdent(author, commitTime, tz));
107 commit.setCommitter(new PersonIdent(committer, commitTime, tz));
108 commit.setMessage(commitMsg);
109 ObjectId id;
110 try (ObjectInserter inserter = db.newObjectInserter()) {
111 commit.setTreeId(inserter.insert(new TreeFormatter()));
112 id = inserter.insert(commit);
113 inserter.flush();
114 }
115
116 int nl = commitMsg.indexOf('\n');
117 final RefUpdate ru = db.updateRef(Constants.HEAD);
118 ru.setNewObjectId(id);
119 ru.setRefLogMessage("commit : "
120 + ((nl == -1) ? commitMsg : commitMsg.substring(0, nl)), false);
121 ru.forceUpdate();
122 }
123 }