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.assertFalse;
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 assertTrue(cfg.get(CoreConfig.KEY).isLogAllRefUpdates());
82
83
84 commit("A Commit\n", commitTime, tz);
85 commitTime += 60 * 1000;
86 assertTrue(
87 "Reflog for HEAD should contain one entry",
88 db.getReflogReader(Constants.HEAD).getReverseEntries().size() == 1);
89
90
91 cfg.setBoolean("core", null, "logallrefupdates", false);
92 cfg.save();
93 assertFalse(cfg.get(CoreConfig.KEY).isLogAllRefUpdates());
94
95
96 commit("A Commit\n", commitTime, tz);
97 assertTrue(
98 "Reflog for HEAD should contain two entries",
99 db.getReflogReader(Constants.HEAD).getReverseEntries().size() == 2);
100 }
101
102 private void commit(String commitMsg, long commitTime, int tz)
103 throws IOException {
104 final CommitBuilder commit = new CommitBuilder();
105 commit.setAuthor(new PersonIdent(author, commitTime, tz));
106 commit.setCommitter(new PersonIdent(committer, commitTime, tz));
107 commit.setMessage(commitMsg);
108 ObjectId id;
109 try (ObjectInserter inserter = db.newObjectInserter()) {
110 commit.setTreeId(inserter.insert(new TreeFormatter()));
111 id = inserter.insert(commit);
112 inserter.flush();
113 }
114
115 int nl = commitMsg.indexOf('\n');
116 final RefUpdate ru = db.updateRef(Constants.HEAD);
117 ru.setNewObjectId(id);
118 ru.setRefLogMessage("commit : "
119 + ((nl == -1) ? commitMsg : commitMsg.substring(0, nl)), false);
120 ru.forceUpdate();
121 }
122 }