View Javadoc
1   /*
2    * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com>
3    * Copyright (C) 2009, Christian Halstrick, Matthias Sohn, SAP AG
4    * Copyright (C) 2009-2010, Google Inc. and others
5    *
6    * This program and the accompanying materials are made available under the
7    * terms of the Eclipse Distribution License v. 1.0 which is available at
8    * https://www.eclipse.org/org/documents/edl-v10.php.
9    *
10   * SPDX-License-Identifier: BSD-3-Clause
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  		// check that there are no entries in the reflog and turn off writing
31  		// reflogs
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  		// do one commit and check that reflog size is 0: no reflogs should be
39  		// written
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  		// set the logAllRefUpdates parameter to true and check it
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  		// do one commit and check that reflog size is increased to 1
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  		// set the logAllRefUpdates parameter to false and check it
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  		// do one commit and check that reflog size is 2
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  		// set the logAllRefUpdates parameter to false and check it
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  		// do one commit and check that reflog size is 3
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 }