1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jgit.lib;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertTrue;
17
18 import java.io.IOException;
19
20 import org.eclipse.jgit.junit.RepositoryTestCase;
21 import org.eclipse.jgit.storage.file.FileBasedConfig;
22 import org.junit.Test;
23
24 public class ReflogConfigTest extends RepositoryTestCase {
25 @Test
26 public void testlogAllRefUpdates() throws Exception {
27 long commitTime = 1154236443000L;
28 int tz = -4 * 60;
29
30
31
32 assertTrue(db.getReflogReader(Constants.HEAD).getReverseEntries()
33 .isEmpty());
34 final FileBasedConfig cfg = db.getConfig();
35 cfg.setBoolean("core", null, "logallrefupdates", false);
36 cfg.save();
37
38
39
40 commit("A Commit\n", commitTime, tz);
41 commitTime += 60 * 1000;
42 assertTrue("Reflog for HEAD still contain no entry", db
43 .getReflogReader(Constants.HEAD).getReverseEntries().isEmpty());
44
45
46 cfg.setBoolean("core", null, "logallrefupdates", true);
47 cfg.save();
48 assertEquals(CoreConfig.LogRefUpdates.TRUE,
49 cfg.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
50 ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES,
51 CoreConfig.LogRefUpdates.FALSE));
52
53
54 commit("A Commit\n", commitTime, tz);
55 commitTime += 60 * 1000;
56 assertTrue(
57 "Reflog for HEAD should contain one entry",
58 db.getReflogReader(Constants.HEAD).getReverseEntries().size() == 1);
59
60
61 cfg.setBoolean("core", null, "logallrefupdates", false);
62 cfg.save();
63 assertEquals(CoreConfig.LogRefUpdates.FALSE,
64 cfg.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
65 ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES,
66 CoreConfig.LogRefUpdates.TRUE));
67
68
69 commit("A Commit\n", commitTime, tz);
70 commitTime += 60 * 1000;
71 assertTrue(
72 "Reflog for HEAD should contain two entries",
73 db.getReflogReader(Constants.HEAD).getReverseEntries().size() == 2);
74
75
76 cfg.setEnum("core", null, "logallrefupdates",
77 CoreConfig.LogRefUpdates.ALWAYS);
78 cfg.save();
79 assertEquals(CoreConfig.LogRefUpdates.ALWAYS,
80 cfg.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
81 ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES,
82 CoreConfig.LogRefUpdates.FALSE));
83
84
85 commit("A Commit\n", commitTime, tz);
86 assertTrue("Reflog for HEAD should contain three entries",
87 db.getReflogReader(Constants.HEAD).getReverseEntries()
88 .size() == 3);
89 }
90
91 private void commit(String commitMsg, long commitTime, int tz)
92 throws IOException {
93 final CommitBuilder commit = new CommitBuilder();
94 commit.setAuthor(new PersonIdent(author, commitTime, tz));
95 commit.setCommitter(new PersonIdent(committer, commitTime, tz));
96 commit.setMessage(commitMsg);
97 ObjectId id;
98 try (ObjectInserter inserter = db.newObjectInserter()) {
99 commit.setTreeId(inserter.insert(new TreeFormatter()));
100 id = inserter.insert(commit);
101 inserter.flush();
102 }
103
104 int nl = commitMsg.indexOf('\n');
105 final RefUpdate ru = db.updateRef(Constants.HEAD);
106 ru.setNewObjectId(id);
107 ru.setRefLogMessage("commit : "
108 + ((nl == -1) ? commitMsg : commitMsg.substring(0, nl)), false);
109 ru.forceUpdate();
110 }
111 }