View Javadoc
1   /*
2    * Copyright (C) 2011, Chris Aniszczyk <caniszczyk@gmail.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.api;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertNotNull;
14  
15  import java.util.Collection;
16  
17  import org.eclipse.jgit.junit.RepositoryTestCase;
18  import org.eclipse.jgit.lib.Constants;
19  import org.eclipse.jgit.lib.ObjectId;
20  import org.eclipse.jgit.lib.ReflogEntry;
21  import org.eclipse.jgit.revwalk.RevCommit;
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  public class ReflogCommandTest extends RepositoryTestCase {
26  
27  	private Git git;
28  
29  	private RevCommit commit1, commit2;
30  
31  	private static final String FILE = "test.txt";
32  
33  	@Override
34  	@Before
35  	public void setUp() throws Exception {
36  		super.setUp();
37  
38  		git = new Git(db);
39  		// commit something
40  		writeTrashFile(FILE, "Hello world");
41  		git.add().addFilepattern(FILE).call();
42  		commit1 = git.commit().setMessage("Initial commit").call();
43  		git.checkout().setCreateBranch(true).setName("b1").call();
44  		git.rm().addFilepattern(FILE).call();
45  		commit2 = git.commit().setMessage("Removed file").call();
46  		git.notesAdd().setObjectId(commit1).setMessage("data").call();
47  	}
48  
49  	/**
50  	 * Test getting the HEAD reflog
51  	 *
52  	 * @throws Exception
53  	 */
54  	@Test
55  	public void testHeadReflog() throws Exception {
56  		Collection<ReflogEntry> reflog = git.reflog().call();
57  		assertNotNull(reflog);
58  		assertEquals(3, reflog.size());
59  		ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]);
60  		assertEquals(reflogs[2].getComment(),
61  				"commit (initial): Initial commit");
62  		assertEquals(reflogs[2].getNewId(), commit1.getId());
63  		assertEquals(reflogs[2].getOldId(), ObjectId.zeroId());
64  		assertEquals(reflogs[1].getComment(),
65  				"checkout: moving from master to b1");
66  		assertEquals(reflogs[1].getNewId(), commit1.getId());
67  		assertEquals(reflogs[1].getOldId(), commit1.getId());
68  		assertEquals(reflogs[0].getComment(), "commit: Removed file");
69  		assertEquals(reflogs[0].getNewId(), commit2.getId());
70  		assertEquals(reflogs[0].getOldId(), commit1.getId());
71  	}
72  
73  	/**
74  	 * Test getting the reflog for an explicit branch
75  	 *
76  	 * @throws Exception
77  	 */
78  	@Test
79  	public void testBranchReflog() throws Exception {
80  		Collection<ReflogEntry> reflog = git.reflog()
81  				.setRef(Constants.R_HEADS + "b1").call();
82  		assertNotNull(reflog);
83  		assertEquals(2, reflog.size());
84  		ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]);
85  		assertEquals(reflogs[0].getComment(), "commit: Removed file");
86  		assertEquals(reflogs[0].getNewId(), commit2.getId());
87  		assertEquals(reflogs[0].getOldId(), commit1.getId());
88  		assertEquals(reflogs[1].getComment(),
89  				"branch: Created from commit Initial commit");
90  		assertEquals(reflogs[1].getNewId(), commit1.getId());
91  		assertEquals(reflogs[1].getOldId(), ObjectId.zeroId());
92  	}
93  
94  	/**
95  	 * Test getting the reflog for an amend commit
96  	 *
97  	 * @throws Exception
98  	 */
99  	@Test
100 	public void testAmendReflog() throws Exception {
101 		RevCommit commit2a = git.commit().setAmend(true)
102 				.setMessage("Deleted file").call();
103 		Collection<ReflogEntry> reflog = git.reflog().call();
104 		assertNotNull(reflog);
105 		assertEquals(4, reflog.size());
106 		ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]);
107 		assertEquals(reflogs[3].getComment(),
108 				"commit (initial): Initial commit");
109 		assertEquals(reflogs[3].getNewId(), commit1.getId());
110 		assertEquals(reflogs[3].getOldId(), ObjectId.zeroId());
111 		assertEquals(reflogs[2].getComment(),
112 				"checkout: moving from master to b1");
113 		assertEquals(reflogs[2].getNewId(), commit1.getId());
114 		assertEquals(reflogs[2].getOldId(), commit1.getId());
115 		assertEquals(reflogs[1].getComment(), "commit: Removed file");
116 		assertEquals(reflogs[1].getNewId(), commit2.getId());
117 		assertEquals(reflogs[1].getOldId(), commit1.getId());
118 		assertEquals(reflogs[0].getComment(), "commit (amend): Deleted file");
119 		assertEquals(reflogs[0].getNewId(), commit2a.getId());
120 		assertEquals(reflogs[0].getOldId(), commit2.getId());
121 	}
122 }