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.assertTrue;
50
51 import java.io.IOException;
52
53 import org.eclipse.jgit.junit.RepositoryTestCase;
54 import org.eclipse.jgit.storage.file.FileBasedConfig;
55 import org.junit.Test;
56
57 public class ReflogConfigTest extends RepositoryTestCase {
58 @Test
59 public void testlogAllRefUpdates() throws Exception {
60 long commitTime = 1154236443000L;
61 int tz = -4 * 60;
62
63
64
65 assertTrue(db.getReflogReader(Constants.HEAD).getReverseEntries()
66 .isEmpty());
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("Reflog for HEAD still contain no entry", db
76 .getReflogReader(Constants.HEAD).getReverseEntries().isEmpty());
77
78
79 cfg.setBoolean("core", null, "logallrefupdates", true);
80 cfg.save();
81 assertEquals(CoreConfig.LogRefUpdates.TRUE,
82 cfg.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
83 ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES,
84 CoreConfig.LogRefUpdates.FALSE));
85
86
87 commit("A Commit\n", commitTime, tz);
88 commitTime += 60 * 1000;
89 assertTrue(
90 "Reflog for HEAD should contain one entry",
91 db.getReflogReader(Constants.HEAD).getReverseEntries().size() == 1);
92
93
94 cfg.setBoolean("core", null, "logallrefupdates", false);
95 cfg.save();
96 assertEquals(CoreConfig.LogRefUpdates.FALSE,
97 cfg.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
98 ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES,
99 CoreConfig.LogRefUpdates.TRUE));
100
101
102 commit("A Commit\n", commitTime, tz);
103 commitTime += 60 * 1000;
104 assertTrue(
105 "Reflog for HEAD should contain two entries",
106 db.getReflogReader(Constants.HEAD).getReverseEntries().size() == 2);
107
108
109 cfg.setEnum("core", null, "logallrefupdates",
110 CoreConfig.LogRefUpdates.ALWAYS);
111 cfg.save();
112 assertEquals(CoreConfig.LogRefUpdates.ALWAYS,
113 cfg.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
114 ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES,
115 CoreConfig.LogRefUpdates.FALSE));
116
117
118 commit("A Commit\n", commitTime, tz);
119 assertTrue("Reflog for HEAD should contain three entries",
120 db.getReflogReader(Constants.HEAD).getReverseEntries()
121 .size() == 3);
122 }
123
124 private void commit(String commitMsg, long commitTime, int tz)
125 throws IOException {
126 final CommitBuilder commit = new CommitBuilder();
127 commit.setAuthor(new PersonIdent(author, commitTime, tz));
128 commit.setCommitter(new PersonIdent(committer, commitTime, tz));
129 commit.setMessage(commitMsg);
130 ObjectId id;
131 try (ObjectInserter inserter = db.newObjectInserter()) {
132 commit.setTreeId(inserter.insert(new TreeFormatter()));
133 id = inserter.insert(commit);
134 inserter.flush();
135 }
136
137 int nl = commitMsg.indexOf('\n');
138 final RefUpdate ru = db.updateRef(Constants.HEAD);
139 ru.setNewObjectId(id);
140 ru.setRefLogMessage("commit : "
141 + ((nl == -1) ? commitMsg : commitMsg.substring(0, nl)), false);
142 ru.forceUpdate();
143 }
144 }