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.

ReflogResolveTest.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2011, GitHub Inc. 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.lib;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertNotNull;
  13. import static org.junit.Assert.assertNull;
  14. import static org.junit.Assert.fail;
  15. import org.eclipse.jgit.api.Git;
  16. import org.eclipse.jgit.errors.RevisionSyntaxException;
  17. import org.eclipse.jgit.junit.RepositoryTestCase;
  18. import org.eclipse.jgit.revwalk.RevCommit;
  19. import org.junit.Test;
  20. /**
  21. * Unit tests for resolving reflog-based revisions
  22. */
  23. public class ReflogResolveTest extends RepositoryTestCase {
  24. @Test
  25. public void resolveMasterCommits() throws Exception {
  26. try (Git git = new Git(db)) {
  27. writeTrashFile("file.txt", "content");
  28. git.add().addFilepattern("file.txt").call();
  29. RevCommit c1 = git.commit().setMessage("create file").call();
  30. writeTrashFile("file.txt", "content2");
  31. git.add().addFilepattern("file.txt").call();
  32. RevCommit c2 = git.commit().setMessage("edit file").call();
  33. assertEquals(c2, db.resolve("master@{0}"));
  34. assertEquals(c1, db.resolve("master@{1}"));
  35. }
  36. }
  37. @Test
  38. public void resolveUnnamedCurrentBranchCommits() throws Exception {
  39. try (Git git = new Git(db)) {
  40. writeTrashFile("file.txt", "content");
  41. git.add().addFilepattern("file.txt").call();
  42. RevCommit c1 = git.commit().setMessage("create file").call();
  43. writeTrashFile("file.txt", "content2");
  44. git.add().addFilepattern("file.txt").call();
  45. RevCommit c2 = git.commit().setMessage("edit file").call();
  46. assertEquals(c2, db.resolve("master@{0}"));
  47. assertEquals(c1, db.resolve("master@{1}"));
  48. git.checkout().setCreateBranch(true).setName("newbranch")
  49. .setStartPoint(c1).call();
  50. // same as current branch, e.g. master
  51. assertEquals(c1, db.resolve("@{0}"));
  52. try {
  53. assertEquals(c1, db.resolve("@{1}"));
  54. fail(); // Looking at wrong ref, e.g HEAD
  55. } catch (RevisionSyntaxException e) {
  56. assertNotNull(e);
  57. }
  58. // detached head, read HEAD reflog
  59. git.checkout().setName(c2.getName()).call();
  60. assertEquals(c2, db.resolve("@{0}"));
  61. assertEquals(c1, db.resolve("@{1}"));
  62. assertEquals(c2, db.resolve("@{2}"));
  63. }
  64. }
  65. @Test
  66. public void resolveReflogParent() throws Exception {
  67. try (Git git = new Git(db)) {
  68. writeTrashFile("file.txt", "content");
  69. git.add().addFilepattern("file.txt").call();
  70. RevCommit c1 = git.commit().setMessage("create file").call();
  71. writeTrashFile("file.txt", "content2");
  72. git.add().addFilepattern("file.txt").call();
  73. git.commit().setMessage("edit file").call();
  74. assertEquals(c1, db.resolve("master@{0}~1"));
  75. }
  76. }
  77. @Test
  78. public void resolveNonExistingBranch() throws Exception {
  79. try (Git git = new Git(db)) {
  80. writeTrashFile("file.txt", "content");
  81. git.add().addFilepattern("file.txt").call();
  82. git.commit().setMessage("create file").call();
  83. assertNull(db.resolve("notabranch@{7}"));
  84. }
  85. }
  86. @Test
  87. public void resolvePreviousBranch() throws Exception {
  88. try (Git git = new Git(db)) {
  89. writeTrashFile("file.txt", "content");
  90. git.add().addFilepattern("file.txt").call();
  91. RevCommit c1 = git.commit().setMessage("create file").call();
  92. writeTrashFile("file.txt", "content2");
  93. git.add().addFilepattern("file.txt").call();
  94. RevCommit c2 = git.commit().setMessage("edit file").call();
  95. git.checkout().setCreateBranch(true).setName("newbranch")
  96. .setStartPoint(c1).call();
  97. git.checkout().setName(c1.getName()).call();
  98. git.checkout().setName("master").call();
  99. assertEquals(c1.getName(), db.simplify("@{-1}"));
  100. assertEquals("newbranch", db.simplify("@{-2}"));
  101. assertEquals("master", db.simplify("@{-3}"));
  102. // chained expression
  103. try {
  104. // Cannot refer to reflog of detached head
  105. db.resolve("@{-1}@{0}");
  106. fail();
  107. } catch (RevisionSyntaxException e) {
  108. // good
  109. }
  110. assertEquals(c1.getName(), db.resolve("@{-2}@{0}").getName());
  111. assertEquals(c2.getName(), db.resolve("@{-3}@{0}").getName());
  112. }
  113. }
  114. @Test
  115. public void resolveDate() throws Exception {
  116. try (Git git = new Git(db)) {
  117. writeTrashFile("file.txt", "content");
  118. git.add().addFilepattern("file.txt").call();
  119. git.commit().setMessage("create file").call();
  120. try {
  121. db.resolve("master@{yesterday}");
  122. fail("Exception not thrown");
  123. } catch (RevisionSyntaxException e) {
  124. assertNotNull(e);
  125. }
  126. }
  127. }
  128. }