View Javadoc
1   /*
2    * Copyright (C) 2009, Robin Rosenberg
3    * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.internal.storage.file;
46  
47  import static java.nio.charset.StandardCharsets.UTF_8;
48  import static org.junit.Assert.assertEquals;
49  import static org.junit.Assert.assertNotNull;
50  import static org.junit.Assert.assertNull;
51  
52  import java.io.File;
53  import java.io.FileNotFoundException;
54  import java.io.FileOutputStream;
55  import java.io.IOException;
56  import java.text.SimpleDateFormat;
57  import java.util.List;
58  
59  import org.eclipse.jgit.lib.CheckoutEntry;
60  import org.eclipse.jgit.lib.Constants;
61  import org.eclipse.jgit.lib.ObjectId;
62  import org.eclipse.jgit.lib.PersonIdent;
63  import org.eclipse.jgit.lib.ReflogEntry;
64  import org.eclipse.jgit.lib.ReflogReader;
65  import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
66  import org.junit.Test;
67  
68  public class ReflogReaderTest extends SampleDataRepositoryTestCase {
69  
70  	static byte[] oneLine = "da85355dfc525c9f6f3927b876f379f46ccf826e 3e7549db262d1e836d9bf0af7e22355468f1717c A O Thor Too <authortoo@wri.tr> 1243028200 +0200\tcommit: Add a toString for debugging to RemoteRefUpdate\n"
71  			.getBytes(UTF_8);
72  
73  	static byte[] twoLine = ("0000000000000000000000000000000000000000 c6734895958052a9dbc396cff4459dc1a25029ab A U Thor <thor@committer.au> 1243028201 -0100\tbranch: Created from rr/renamebranchv4\n"
74  			+ "c6734895958052a9dbc396cff4459dc1a25029ab 54794942a18a237c57a80719afed44bb78172b10 Same A U Thor <same.author@example.com> 1243028202 +0100\trebase finished: refs/heads/rr/renamebranch5 onto c6e3b9fe2da0293f11eae202ec35fb343191a82d\n")
75  					.getBytes(UTF_8);
76  
77  	static byte[] twoLineWithAppendInProgress = ("0000000000000000000000000000000000000000 c6734895958052a9dbc396cff4459dc1a25029ab A U Thor <thor@committer.au> 1243028201 -0100\tbranch: Created from rr/renamebranchv4\n"
78  			+ "c6734895958052a9dbc396cff4459dc1a25029ab 54794942a18a237c57a80719afed44bb78172b10 Same A U Thor <same.author@example.com> 1243028202 +0100\trebase finished: refs/heads/rr/renamebranch5 onto c6e3b9fe2da0293f11eae202ec35fb343191a82d\n"
79  			+ "54794942a18a237c57a80719afed44bb78172b10 ")
80  					.getBytes(UTF_8);
81  
82  	static byte[] aLine = "1111111111111111111111111111111111111111 3e7549db262d1e836d9bf0af7e22355468f1717c A U Thor <thor@committer.au> 1243028201 -0100\tbranch: change to a\n"
83  			.getBytes(UTF_8);
84  
85  	static byte[] masterLine = "2222222222222222222222222222222222222222 3e7549db262d1e836d9bf0af7e22355468f1717c A U Thor <thor@committer.au> 1243028201 -0100\tbranch: change to master\n"
86  			.getBytes(UTF_8);
87  
88  	static byte[] headLine = "3333333333333333333333333333333333333333 3e7549db262d1e836d9bf0af7e22355468f1717c A U Thor <thor@committer.au> 1243028201 -0100\tbranch: change to HEAD\n"
89  			.getBytes(UTF_8);
90  
91  	static byte[] oneLineWithoutComment = "da85355dfc525c9f6f3927b876f379f46ccf826e 3e7549db262d1e836d9bf0af7e22355468f1717c A O Thor Too <authortoo@wri.tr> 1243028200 +0200\n"
92  			.getBytes(UTF_8);
93  
94  	static byte[] switchBranch = "0d43a6890a19fd657faad1c4cfbe3cb1b47851c3 4809df9c0d8bce5b00955563f77c5a9f25aa0d12 A O Thor Too <authortoo@wri.tr> 1315088009 +0200\tcheckout: moving from new/work to master\n"
95  			.getBytes(UTF_8);
96  
97  	@Test
98  	public void testReadOneLine() throws Exception {
99  		setupReflog("logs/refs/heads/master", oneLine);
100 
101 		ReflogReader reader = new ReflogReaderImpl(db, "refs/heads/master");
102 		ReflogEntry e = reader.getLastEntry();
103 		assertEquals(ObjectId
104 				.fromString("da85355dfc525c9f6f3927b876f379f46ccf826e"), e
105 				.getOldId());
106 		assertEquals(ObjectId
107 				.fromString("3e7549db262d1e836d9bf0af7e22355468f1717c"), e
108 				.getNewId());
109 		assertEquals("A O Thor Too", e.getWho().getName());
110 		assertEquals("authortoo@wri.tr", e.getWho().getEmailAddress());
111 		assertEquals(120, e.getWho().getTimeZoneOffset());
112 		assertEquals("2009-05-22T23:36:40", iso(e.getWho()));
113 		assertEquals("commit: Add a toString for debugging to RemoteRefUpdate",
114 				e.getComment());
115 	}
116 
117 	private static String iso(PersonIdent id) {
118 		final SimpleDateFormat fmt;
119 		fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
120 		fmt.setTimeZone(id.getTimeZone());
121 		return fmt.format(id.getWhen());
122 	}
123 
124 	@Test
125 	public void testReadTwoLine() throws Exception {
126 		setupReflog("logs/refs/heads/master", twoLine);
127 
128 		ReflogReader reader = new ReflogReaderImpl(db, "refs/heads/master");
129 		List<ReflogEntry> reverseEntries = reader.getReverseEntries();
130 		assertEquals(2, reverseEntries.size());
131 		ReflogEntry e = reverseEntries.get(0);
132 		assertEquals(ObjectId
133 				.fromString("c6734895958052a9dbc396cff4459dc1a25029ab"), e
134 				.getOldId());
135 		assertEquals(ObjectId
136 				.fromString("54794942a18a237c57a80719afed44bb78172b10"), e
137 				.getNewId());
138 		assertEquals("Same A U Thor", e.getWho().getName());
139 		assertEquals("same.author@example.com", e.getWho().getEmailAddress());
140 		assertEquals(60, e.getWho().getTimeZoneOffset());
141 		assertEquals("2009-05-22T22:36:42", iso(e.getWho()));
142 		assertEquals(
143 				"rebase finished: refs/heads/rr/renamebranch5 onto c6e3b9fe2da0293f11eae202ec35fb343191a82d",
144 				e.getComment());
145 
146 		e = reverseEntries.get(1);
147 		assertEquals(ObjectId
148 				.fromString("0000000000000000000000000000000000000000"), e
149 				.getOldId());
150 		assertEquals(ObjectId
151 				.fromString("c6734895958052a9dbc396cff4459dc1a25029ab"), e
152 				.getNewId());
153 		assertEquals("A U Thor", e.getWho().getName());
154 		assertEquals("thor@committer.au", e.getWho().getEmailAddress());
155 		assertEquals(-60, e.getWho().getTimeZoneOffset());
156 		assertEquals("2009-05-22T20:36:41", iso(e.getWho()));
157 		assertEquals("branch: Created from rr/renamebranchv4", e.getComment());
158 	}
159 
160 	@Test
161 	public void testReadWhileAppendIsInProgress() throws Exception {
162 		setupReflog("logs/refs/heads/master", twoLineWithAppendInProgress);
163 		ReflogReader reader = new ReflogReaderImpl(db, "refs/heads/master");
164 		List<ReflogEntry> reverseEntries = reader.getReverseEntries();
165 		assertEquals(2, reverseEntries.size());
166 		ReflogEntry e = reverseEntries.get(0);
167 		assertEquals(ObjectId
168 				.fromString("c6734895958052a9dbc396cff4459dc1a25029ab"), e
169 				.getOldId());
170 		assertEquals(ObjectId
171 				.fromString("54794942a18a237c57a80719afed44bb78172b10"), e
172 				.getNewId());
173 		assertEquals("Same A U Thor", e.getWho().getName());
174 		assertEquals("same.author@example.com", e.getWho().getEmailAddress());
175 		assertEquals(60, e.getWho().getTimeZoneOffset());
176 		assertEquals("2009-05-22T22:36:42", iso(e.getWho()));
177 		assertEquals(
178 				"rebase finished: refs/heads/rr/renamebranch5 onto c6e3b9fe2da0293f11eae202ec35fb343191a82d",
179 				e.getComment());
180 		// while similar to testReadTwoLine, we can assume that if we get the last entry
181 		// right, everything else is too
182 	}
183 
184 
185 	@Test
186 	public void testReadRightLog() throws Exception {
187 		setupReflog("logs/refs/heads/a", aLine);
188 		setupReflog("logs/refs/heads/master", masterLine);
189 		setupReflog("logs/HEAD", headLine);
190 		assertEquals("branch: change to master", db.getReflogReader("master")
191 				.getLastEntry().getComment());
192 		assertEquals("branch: change to a", db.getReflogReader("a")
193 				.getLastEntry().getComment());
194 		assertEquals("branch: change to HEAD", db.getReflogReader("HEAD")
195 				.getLastEntry().getComment());
196 	}
197 
198 	@Test
199 	public void testReadLineWithMissingComment() throws Exception {
200 		setupReflog("logs/refs/heads/master", oneLineWithoutComment);
201 		final ReflogReader reader = db.getReflogReader("master");
202 		ReflogEntry e = reader.getLastEntry();
203 		assertEquals(ObjectId
204 				.fromString("da85355dfc525c9f6f3927b876f379f46ccf826e"), e
205 				.getOldId());
206 		assertEquals(ObjectId
207 				.fromString("3e7549db262d1e836d9bf0af7e22355468f1717c"), e
208 				.getNewId());
209 		assertEquals("A O Thor Too", e.getWho().getName());
210 		assertEquals("authortoo@wri.tr", e.getWho().getEmailAddress());
211 		assertEquals(120, e.getWho().getTimeZoneOffset());
212 		assertEquals("2009-05-22T23:36:40", iso(e.getWho()));
213 		assertEquals("",
214 				e.getComment());
215 	}
216 
217 	@Test
218 	public void testNoLog() throws Exception {
219 		assertEquals(0, db.getReflogReader("master").getReverseEntries().size());
220 		assertNull(db.getReflogReader("master").getLastEntry());
221 	}
222 
223 	@Test
224 	public void testCheckout() throws Exception {
225 		setupReflog("logs/HEAD", switchBranch);
226 		List<ReflogEntry> entries = db.getReflogReader(Constants.HEAD)
227 				.getReverseEntries();
228 		assertEquals(1, entries.size());
229 		ReflogEntry entry = entries.get(0);
230 		CheckoutEntry checkout = entry.parseCheckout();
231 		assertNotNull(checkout);
232 		assertEquals("master", checkout.getToBranch());
233 		assertEquals("new/work", checkout.getFromBranch());
234 	}
235 
236 	@Test
237 	public void testSpecificEntryNumber() throws Exception {
238 		setupReflog("logs/refs/heads/master", twoLine);
239 
240 		ReflogReader reader = new ReflogReaderImpl(db, "refs/heads/master");
241 		ReflogEntry e = reader.getReverseEntry(0);
242 		assertEquals(
243 				ObjectId.fromString("c6734895958052a9dbc396cff4459dc1a25029ab"),
244 				e.getOldId());
245 		assertEquals(
246 				ObjectId.fromString("54794942a18a237c57a80719afed44bb78172b10"),
247 				e.getNewId());
248 		assertEquals("Same A U Thor", e.getWho().getName());
249 		assertEquals("same.author@example.com", e.getWho().getEmailAddress());
250 		assertEquals(60, e.getWho().getTimeZoneOffset());
251 		assertEquals("2009-05-22T22:36:42", iso(e.getWho()));
252 		assertEquals(
253 				"rebase finished: refs/heads/rr/renamebranch5 onto c6e3b9fe2da0293f11eae202ec35fb343191a82d",
254 				e.getComment());
255 
256 		e = reader.getReverseEntry(1);
257 		assertEquals(
258 				ObjectId.fromString("0000000000000000000000000000000000000000"),
259 				e.getOldId());
260 		assertEquals(
261 				ObjectId.fromString("c6734895958052a9dbc396cff4459dc1a25029ab"),
262 				e.getNewId());
263 		assertEquals("A U Thor", e.getWho().getName());
264 		assertEquals("thor@committer.au", e.getWho().getEmailAddress());
265 		assertEquals(-60, e.getWho().getTimeZoneOffset());
266 		assertEquals("2009-05-22T20:36:41", iso(e.getWho()));
267 		assertEquals("branch: Created from rr/renamebranchv4", e.getComment());
268 
269 		assertNull(reader.getReverseEntry(3));
270 	}
271 
272 	private void setupReflog(String logName, byte[] data)
273 			throws FileNotFoundException, IOException {
274 		File logfile = new File(db.getDirectory(), logName);
275 		if (!logfile.getParentFile().mkdirs()
276 				&& !logfile.getParentFile().isDirectory()) {
277 			throw new IOException(
278 					"oops, cannot create the directory for the test reflog file"
279 							+ logfile);
280 		}
281 		try (FileOutputStream fileOutputStream = new FileOutputStream(logfile)) {
282 			fileOutputStream.write(data);
283 		}
284 	}
285 
286 }