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 org.junit.Assert.assertEquals;
48  import static org.junit.Assert.assertNotNull;
49  import static org.junit.Assert.assertNull;
50  
51  import java.io.File;
52  import java.io.FileNotFoundException;
53  import java.io.FileOutputStream;
54  import java.io.IOException;
55  import java.text.SimpleDateFormat;
56  import java.util.List;
57  
58  import org.eclipse.jgit.lib.CheckoutEntry;
59  import org.eclipse.jgit.lib.Constants;
60  import org.eclipse.jgit.lib.ObjectId;
61  import org.eclipse.jgit.lib.PersonIdent;
62  import org.eclipse.jgit.lib.ReflogEntry;
63  import org.eclipse.jgit.lib.ReflogReader;
64  import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
65  import org.junit.Test;
66  
67  public class ReflogReaderTest extends SampleDataRepositoryTestCase {
68  
69  	static byte[] oneLine = "da85355dfc525c9f6f3927b876f379f46ccf826e 3e7549db262d1e836d9bf0af7e22355468f1717c A O Thor Too <authortoo@wri.tr> 1243028200 +0200\tcommit: Add a toString for debugging to RemoteRefUpdate\n"
70  			.getBytes();
71  
72  	static byte[] twoLine = ("0000000000000000000000000000000000000000 c6734895958052a9dbc396cff4459dc1a25029ab A U Thor <thor@committer.au> 1243028201 -0100\tbranch: Created from rr/renamebranchv4\n"
73  			+ "c6734895958052a9dbc396cff4459dc1a25029ab 54794942a18a237c57a80719afed44bb78172b10 Same A U Thor <same.author@example.com> 1243028202 +0100\trebase finished: refs/heads/rr/renamebranch5 onto c6e3b9fe2da0293f11eae202ec35fb343191a82d\n")
74  			.getBytes();
75  
76  	static byte[] twoLineWithAppendInProgress = ("0000000000000000000000000000000000000000 c6734895958052a9dbc396cff4459dc1a25029ab A U Thor <thor@committer.au> 1243028201 -0100\tbranch: Created from rr/renamebranchv4\n"
77  			+ "c6734895958052a9dbc396cff4459dc1a25029ab 54794942a18a237c57a80719afed44bb78172b10 Same A U Thor <same.author@example.com> 1243028202 +0100\trebase finished: refs/heads/rr/renamebranch5 onto c6e3b9fe2da0293f11eae202ec35fb343191a82d\n"
78  			+ "54794942a18a237c57a80719afed44bb78172b10 ")
79  			.getBytes();
80  
81  	static byte[] aLine = "1111111111111111111111111111111111111111 3e7549db262d1e836d9bf0af7e22355468f1717c A U Thor <thor@committer.au> 1243028201 -0100\tbranch: change to a\n"
82  			.getBytes();
83  
84  	static byte[] masterLine = "2222222222222222222222222222222222222222 3e7549db262d1e836d9bf0af7e22355468f1717c A U Thor <thor@committer.au> 1243028201 -0100\tbranch: change to master\n"
85  			.getBytes();
86  
87  	static byte[] headLine = "3333333333333333333333333333333333333333 3e7549db262d1e836d9bf0af7e22355468f1717c A U Thor <thor@committer.au> 1243028201 -0100\tbranch: change to HEAD\n"
88  			.getBytes();
89  
90  	static byte[] oneLineWithoutComment = "da85355dfc525c9f6f3927b876f379f46ccf826e 3e7549db262d1e836d9bf0af7e22355468f1717c A O Thor Too <authortoo@wri.tr> 1243028200 +0200\n"
91  			.getBytes();
92  
93  	static byte[] switchBranch = "0d43a6890a19fd657faad1c4cfbe3cb1b47851c3 4809df9c0d8bce5b00955563f77c5a9f25aa0d12 A O Thor Too <authortoo@wri.tr> 1315088009 +0200\tcheckout: moving from new/work to master\n"
94  			.getBytes();
95  
96  	@Test
97  	public void testReadOneLine() throws Exception {
98  		setupReflog("logs/refs/heads/master", oneLine);
99  
100 		ReflogReader reader = new ReflogReaderImpl(db, "refs/heads/master");
101 		ReflogEntry e = reader.getLastEntry();
102 		assertEquals(ObjectId
103 				.fromString("da85355dfc525c9f6f3927b876f379f46ccf826e"), e
104 				.getOldId());
105 		assertEquals(ObjectId
106 				.fromString("3e7549db262d1e836d9bf0af7e22355468f1717c"), e
107 				.getNewId());
108 		assertEquals("A O Thor Too", e.getWho().getName());
109 		assertEquals("authortoo@wri.tr", e.getWho().getEmailAddress());
110 		assertEquals(120, e.getWho().getTimeZoneOffset());
111 		assertEquals("2009-05-22T23:36:40", iso(e.getWho()));
112 		assertEquals("commit: Add a toString for debugging to RemoteRefUpdate",
113 				e.getComment());
114 	}
115 
116 	private static String iso(PersonIdent id) {
117 		final SimpleDateFormat fmt;
118 		fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
119 		fmt.setTimeZone(id.getTimeZone());
120 		return fmt.format(id.getWhen());
121 	}
122 
123 	@Test
124 	public void testReadTwoLine() throws Exception {
125 		setupReflog("logs/refs/heads/master", twoLine);
126 
127 		ReflogReader reader = new ReflogReaderImpl(db, "refs/heads/master");
128 		List<ReflogEntry> reverseEntries = reader.getReverseEntries();
129 		assertEquals(2, reverseEntries.size());
130 		ReflogEntry e = reverseEntries.get(0);
131 		assertEquals(ObjectId
132 				.fromString("c6734895958052a9dbc396cff4459dc1a25029ab"), e
133 				.getOldId());
134 		assertEquals(ObjectId
135 				.fromString("54794942a18a237c57a80719afed44bb78172b10"), e
136 				.getNewId());
137 		assertEquals("Same A U Thor", e.getWho().getName());
138 		assertEquals("same.author@example.com", e.getWho().getEmailAddress());
139 		assertEquals(60, e.getWho().getTimeZoneOffset());
140 		assertEquals("2009-05-22T22:36:42", iso(e.getWho()));
141 		assertEquals(
142 				"rebase finished: refs/heads/rr/renamebranch5 onto c6e3b9fe2da0293f11eae202ec35fb343191a82d",
143 				e.getComment());
144 
145 		e = reverseEntries.get(1);
146 		assertEquals(ObjectId
147 				.fromString("0000000000000000000000000000000000000000"), e
148 				.getOldId());
149 		assertEquals(ObjectId
150 				.fromString("c6734895958052a9dbc396cff4459dc1a25029ab"), e
151 				.getNewId());
152 		assertEquals("A U Thor", e.getWho().getName());
153 		assertEquals("thor@committer.au", e.getWho().getEmailAddress());
154 		assertEquals(-60, e.getWho().getTimeZoneOffset());
155 		assertEquals("2009-05-22T20:36:41", iso(e.getWho()));
156 		assertEquals("branch: Created from rr/renamebranchv4", e.getComment());
157 	}
158 
159 	@Test
160 	public void testReadWhileAppendIsInProgress() throws Exception {
161 		setupReflog("logs/refs/heads/master", twoLineWithAppendInProgress);
162 		ReflogReader reader = new ReflogReaderImpl(db, "refs/heads/master");
163 		List<ReflogEntry> reverseEntries = reader.getReverseEntries();
164 		assertEquals(2, reverseEntries.size());
165 		ReflogEntry e = reverseEntries.get(0);
166 		assertEquals(ObjectId
167 				.fromString("c6734895958052a9dbc396cff4459dc1a25029ab"), e
168 				.getOldId());
169 		assertEquals(ObjectId
170 				.fromString("54794942a18a237c57a80719afed44bb78172b10"), e
171 				.getNewId());
172 		assertEquals("Same A U Thor", e.getWho().getName());
173 		assertEquals("same.author@example.com", e.getWho().getEmailAddress());
174 		assertEquals(60, e.getWho().getTimeZoneOffset());
175 		assertEquals("2009-05-22T22:36:42", iso(e.getWho()));
176 		assertEquals(
177 				"rebase finished: refs/heads/rr/renamebranch5 onto c6e3b9fe2da0293f11eae202ec35fb343191a82d",
178 				e.getComment());
179 		// while similar to testReadTwoLine, we can assume that if we get the last entry
180 		// right, everything else is too
181 	}
182 
183 
184 	@Test
185 	public void testReadRightLog() throws Exception {
186 		setupReflog("logs/refs/heads/a", aLine);
187 		setupReflog("logs/refs/heads/master", masterLine);
188 		setupReflog("logs/HEAD", headLine);
189 		assertEquals("branch: change to master", db.getReflogReader("master")
190 				.getLastEntry().getComment());
191 		assertEquals("branch: change to a", db.getReflogReader("a")
192 				.getLastEntry().getComment());
193 		assertEquals("branch: change to HEAD", db.getReflogReader("HEAD")
194 				.getLastEntry().getComment());
195 	}
196 
197 	@Test
198 	public void testReadLineWithMissingComment() throws Exception {
199 		setupReflog("logs/refs/heads/master", oneLineWithoutComment);
200 		final ReflogReader reader = db.getReflogReader("master");
201 		ReflogEntry e = reader.getLastEntry();
202 		assertEquals(ObjectId
203 				.fromString("da85355dfc525c9f6f3927b876f379f46ccf826e"), e
204 				.getOldId());
205 		assertEquals(ObjectId
206 				.fromString("3e7549db262d1e836d9bf0af7e22355468f1717c"), e
207 				.getNewId());
208 		assertEquals("A O Thor Too", e.getWho().getName());
209 		assertEquals("authortoo@wri.tr", e.getWho().getEmailAddress());
210 		assertEquals(120, e.getWho().getTimeZoneOffset());
211 		assertEquals("2009-05-22T23:36:40", iso(e.getWho()));
212 		assertEquals("",
213 				e.getComment());
214 	}
215 
216 	@Test
217 	public void testNoLog() throws Exception {
218 		assertEquals(0, db.getReflogReader("master").getReverseEntries().size());
219 		assertNull(db.getReflogReader("master").getLastEntry());
220 	}
221 
222 	@Test
223 	public void testCheckout() throws Exception {
224 		setupReflog("logs/HEAD", switchBranch);
225 		List<ReflogEntry> entries = db.getReflogReader(Constants.HEAD)
226 				.getReverseEntries();
227 		assertEquals(1, entries.size());
228 		ReflogEntry entry = entries.get(0);
229 		CheckoutEntry checkout = entry.parseCheckout();
230 		assertNotNull(checkout);
231 		assertEquals("master", checkout.getToBranch());
232 		assertEquals("new/work", checkout.getFromBranch());
233 	}
234 
235 	@Test
236 	public void testSpecificEntryNumber() throws Exception {
237 		setupReflog("logs/refs/heads/master", twoLine);
238 
239 		ReflogReader reader = new ReflogReaderImpl(db, "refs/heads/master");
240 		ReflogEntry e = reader.getReverseEntry(0);
241 		assertEquals(
242 				ObjectId.fromString("c6734895958052a9dbc396cff4459dc1a25029ab"),
243 				e.getOldId());
244 		assertEquals(
245 				ObjectId.fromString("54794942a18a237c57a80719afed44bb78172b10"),
246 				e.getNewId());
247 		assertEquals("Same A U Thor", e.getWho().getName());
248 		assertEquals("same.author@example.com", e.getWho().getEmailAddress());
249 		assertEquals(60, e.getWho().getTimeZoneOffset());
250 		assertEquals("2009-05-22T22:36:42", iso(e.getWho()));
251 		assertEquals(
252 				"rebase finished: refs/heads/rr/renamebranch5 onto c6e3b9fe2da0293f11eae202ec35fb343191a82d",
253 				e.getComment());
254 
255 		e = reader.getReverseEntry(1);
256 		assertEquals(
257 				ObjectId.fromString("0000000000000000000000000000000000000000"),
258 				e.getOldId());
259 		assertEquals(
260 				ObjectId.fromString("c6734895958052a9dbc396cff4459dc1a25029ab"),
261 				e.getNewId());
262 		assertEquals("A U Thor", e.getWho().getName());
263 		assertEquals("thor@committer.au", e.getWho().getEmailAddress());
264 		assertEquals(-60, e.getWho().getTimeZoneOffset());
265 		assertEquals("2009-05-22T20:36:41", iso(e.getWho()));
266 		assertEquals("branch: Created from rr/renamebranchv4", e.getComment());
267 
268 		assertNull(reader.getReverseEntry(3));
269 	}
270 
271 	private void setupReflog(String logName, byte[] data)
272 			throws FileNotFoundException, IOException {
273 				File logfile = new File(db.getDirectory(), logName);
274 				if (!logfile.getParentFile().mkdirs()
275 						&& !logfile.getParentFile().isDirectory()) {
276 					throw new IOException(
277 							"oops, cannot create the directory for the test reflog file"
278 									+ logfile);
279 				}
280 				FileOutputStream fileOutputStream = new FileOutputStream(logfile);
281 				try {
282 					fileOutputStream.write(data);
283 				} finally {
284 					fileOutputStream.close();
285 				}
286 			}
287 
288 }