Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ReflogCommandTest.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2011, Chris Aniszczyk <caniszczyk@gmail.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNotNull;
  46. import java.util.Collection;
  47. import org.eclipse.jgit.junit.RepositoryTestCase;
  48. import org.eclipse.jgit.lib.Constants;
  49. import org.eclipse.jgit.lib.ObjectId;
  50. import org.eclipse.jgit.lib.ReflogEntry;
  51. import org.eclipse.jgit.revwalk.RevCommit;
  52. import org.junit.Before;
  53. import org.junit.Test;
  54. public class ReflogCommandTest extends RepositoryTestCase {
  55. private Git git;
  56. private RevCommit commit1, commit2;
  57. private static final String FILE = "test.txt";
  58. @Override
  59. @Before
  60. public void setUp() throws Exception {
  61. super.setUp();
  62. git = new Git(db);
  63. // commit something
  64. writeTrashFile(FILE, "Hello world");
  65. git.add().addFilepattern(FILE).call();
  66. commit1 = git.commit().setMessage("Initial commit").call();
  67. git.checkout().setCreateBranch(true).setName("b1").call();
  68. git.rm().addFilepattern(FILE).call();
  69. commit2 = git.commit().setMessage("Removed file").call();
  70. git.notesAdd().setObjectId(commit1).setMessage("data").call();
  71. }
  72. /**
  73. * Test getting the HEAD reflog
  74. *
  75. * @throws Exception
  76. */
  77. @Test
  78. public void testHeadReflog() throws Exception {
  79. Collection<ReflogEntry> reflog = git.reflog().call();
  80. assertNotNull(reflog);
  81. assertEquals(3, reflog.size());
  82. ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]);
  83. assertEquals(reflogs[2].getComment(),
  84. "commit (initial): Initial commit");
  85. assertEquals(reflogs[2].getNewId(), commit1.getId());
  86. assertEquals(reflogs[2].getOldId(), ObjectId.zeroId());
  87. assertEquals(reflogs[1].getComment(),
  88. "checkout: moving from master to b1");
  89. assertEquals(reflogs[1].getNewId(), commit1.getId());
  90. assertEquals(reflogs[1].getOldId(), commit1.getId());
  91. assertEquals(reflogs[0].getComment(), "commit: Removed file");
  92. assertEquals(reflogs[0].getNewId(), commit2.getId());
  93. assertEquals(reflogs[0].getOldId(), commit1.getId());
  94. }
  95. /**
  96. * Test getting the reflog for an explicit branch
  97. *
  98. * @throws Exception
  99. */
  100. @Test
  101. public void testBranchReflog() throws Exception {
  102. Collection<ReflogEntry> reflog = git.reflog()
  103. .setRef(Constants.R_HEADS + "b1").call();
  104. assertNotNull(reflog);
  105. assertEquals(2, reflog.size());
  106. ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]);
  107. assertEquals(reflogs[0].getComment(), "commit: Removed file");
  108. assertEquals(reflogs[0].getNewId(), commit2.getId());
  109. assertEquals(reflogs[0].getOldId(), commit1.getId());
  110. assertEquals(reflogs[1].getComment(),
  111. "branch: Created from commit Initial commit");
  112. assertEquals(reflogs[1].getNewId(), commit1.getId());
  113. assertEquals(reflogs[1].getOldId(), ObjectId.zeroId());
  114. }
  115. /**
  116. * Test getting the reflog for an amend commit
  117. *
  118. * @throws Exception
  119. */
  120. @Test
  121. public void testAmendReflog() throws Exception {
  122. RevCommit commit2a = git.commit().setAmend(true)
  123. .setMessage("Deleted file").call();
  124. Collection<ReflogEntry> reflog = git.reflog().call();
  125. assertNotNull(reflog);
  126. assertEquals(4, reflog.size());
  127. ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]);
  128. assertEquals(reflogs[3].getComment(),
  129. "commit (initial): Initial commit");
  130. assertEquals(reflogs[3].getNewId(), commit1.getId());
  131. assertEquals(reflogs[3].getOldId(), ObjectId.zeroId());
  132. assertEquals(reflogs[2].getComment(),
  133. "checkout: moving from master to b1");
  134. assertEquals(reflogs[2].getNewId(), commit1.getId());
  135. assertEquals(reflogs[2].getOldId(), commit1.getId());
  136. assertEquals(reflogs[1].getComment(), "commit: Removed file");
  137. assertEquals(reflogs[1].getNewId(), commit2.getId());
  138. assertEquals(reflogs[1].getOldId(), commit1.getId());
  139. assertEquals(reflogs[0].getComment(), "commit (amend): Deleted file");
  140. assertEquals(reflogs[0].getNewId(), commit2a.getId());
  141. assertEquals(reflogs[0].getOldId(), commit2.getId());
  142. }
  143. }