You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReflogCommandTest.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertNotNull;
  13. import java.util.Collection;
  14. import org.eclipse.jgit.junit.RepositoryTestCase;
  15. import org.eclipse.jgit.lib.Constants;
  16. import org.eclipse.jgit.lib.ObjectId;
  17. import org.eclipse.jgit.lib.ReflogEntry;
  18. import org.eclipse.jgit.revwalk.RevCommit;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. public class ReflogCommandTest extends RepositoryTestCase {
  22. private Git git;
  23. private RevCommit commit1, commit2;
  24. private static final String FILE = "test.txt";
  25. @Override
  26. @Before
  27. public void setUp() throws Exception {
  28. super.setUp();
  29. git = new Git(db);
  30. // commit something
  31. writeTrashFile(FILE, "Hello world");
  32. git.add().addFilepattern(FILE).call();
  33. commit1 = git.commit().setMessage("Initial commit").call();
  34. git.checkout().setCreateBranch(true).setName("b1").call();
  35. git.rm().addFilepattern(FILE).call();
  36. commit2 = git.commit().setMessage("Removed file").call();
  37. git.notesAdd().setObjectId(commit1).setMessage("data").call();
  38. }
  39. /**
  40. * Test getting the HEAD reflog
  41. *
  42. * @throws Exception
  43. */
  44. @Test
  45. public void testHeadReflog() throws Exception {
  46. Collection<ReflogEntry> reflog = git.reflog().call();
  47. assertNotNull(reflog);
  48. assertEquals(3, reflog.size());
  49. ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]);
  50. assertEquals(reflogs[2].getComment(),
  51. "commit (initial): Initial commit");
  52. assertEquals(reflogs[2].getNewId(), commit1.getId());
  53. assertEquals(reflogs[2].getOldId(), ObjectId.zeroId());
  54. assertEquals(reflogs[1].getComment(),
  55. "checkout: moving from master to b1");
  56. assertEquals(reflogs[1].getNewId(), commit1.getId());
  57. assertEquals(reflogs[1].getOldId(), commit1.getId());
  58. assertEquals(reflogs[0].getComment(), "commit: Removed file");
  59. assertEquals(reflogs[0].getNewId(), commit2.getId());
  60. assertEquals(reflogs[0].getOldId(), commit1.getId());
  61. }
  62. /**
  63. * Test getting the reflog for an explicit branch
  64. *
  65. * @throws Exception
  66. */
  67. @Test
  68. public void testBranchReflog() throws Exception {
  69. Collection<ReflogEntry> reflog = git.reflog()
  70. .setRef(Constants.R_HEADS + "b1").call();
  71. assertNotNull(reflog);
  72. assertEquals(2, reflog.size());
  73. ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]);
  74. assertEquals(reflogs[0].getComment(), "commit: Removed file");
  75. assertEquals(reflogs[0].getNewId(), commit2.getId());
  76. assertEquals(reflogs[0].getOldId(), commit1.getId());
  77. assertEquals(reflogs[1].getComment(),
  78. "branch: Created from commit Initial commit");
  79. assertEquals(reflogs[1].getNewId(), commit1.getId());
  80. assertEquals(reflogs[1].getOldId(), ObjectId.zeroId());
  81. }
  82. /**
  83. * Test getting the reflog for an amend commit
  84. *
  85. * @throws Exception
  86. */
  87. @Test
  88. public void testAmendReflog() throws Exception {
  89. RevCommit commit2a = git.commit().setAmend(true)
  90. .setMessage("Deleted file").call();
  91. Collection<ReflogEntry> reflog = git.reflog().call();
  92. assertNotNull(reflog);
  93. assertEquals(4, reflog.size());
  94. ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]);
  95. assertEquals(reflogs[3].getComment(),
  96. "commit (initial): Initial commit");
  97. assertEquals(reflogs[3].getNewId(), commit1.getId());
  98. assertEquals(reflogs[3].getOldId(), ObjectId.zeroId());
  99. assertEquals(reflogs[2].getComment(),
  100. "checkout: moving from master to b1");
  101. assertEquals(reflogs[2].getNewId(), commit1.getId());
  102. assertEquals(reflogs[2].getOldId(), commit1.getId());
  103. assertEquals(reflogs[1].getComment(), "commit: Removed file");
  104. assertEquals(reflogs[1].getNewId(), commit2.getId());
  105. assertEquals(reflogs[1].getOldId(), commit1.getId());
  106. assertEquals(reflogs[0].getComment(), "commit (amend): Deleted file");
  107. assertEquals(reflogs[0].getNewId(), commit2a.getId());
  108. assertEquals(reflogs[0].getOldId(), commit2.getId());
  109. }
  110. }