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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.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.pgm;
  11. import static org.junit.Assert.assertArrayEquals;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. import static org.junit.Assert.assertTrue;
  15. import org.eclipse.jgit.api.Git;
  16. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  17. import org.eclipse.jgit.revwalk.RevCommit;
  18. import org.junit.Before;
  19. import org.junit.Ignore;
  20. import org.junit.Test;
  21. public class ResetTest extends CLIRepositoryTestCase {
  22. private Git git;
  23. @Override
  24. @Before
  25. public void setUp() throws Exception {
  26. super.setUp();
  27. git = new Git(db);
  28. }
  29. @Test
  30. public void testPathOptionHelp() throws Exception {
  31. String[] result = execute("git reset -h");
  32. assertTrue("Unexpected argument: " + result[1],
  33. result[1].endsWith("[-- path ...]"));
  34. }
  35. @Test
  36. public void testZombieArgument_Bug484951() throws Exception {
  37. String[] result = execute("git reset -h");
  38. assertFalse("Unexpected argument: " + result[0],
  39. result[0].contains("[VAL ...]"));
  40. }
  41. @Test
  42. public void testResetSelf() throws Exception {
  43. RevCommit commit = git.commit().setMessage("initial commit").call();
  44. assertStringArrayEquals("",
  45. execute("git reset --hard " + commit.getId().name()));
  46. assertEquals(commit.getId(),
  47. git.getRepository().exactRef("HEAD").getObjectId());
  48. }
  49. @Test
  50. public void testResetPrevious() throws Exception {
  51. RevCommit commit = git.commit().setMessage("initial commit").call();
  52. git.commit().setMessage("second commit").call();
  53. assertStringArrayEquals("",
  54. execute("git reset --hard " + commit.getId().name()));
  55. assertEquals(commit.getId(),
  56. git.getRepository().exactRef("HEAD").getObjectId());
  57. }
  58. @Test
  59. public void testResetEmptyPath() throws Exception {
  60. RevCommit commit = git.commit().setMessage("initial commit").call();
  61. assertStringArrayEquals("",
  62. execute("git reset --hard " + commit.getId().name() + " --"));
  63. assertEquals(commit.getId(),
  64. git.getRepository().exactRef("HEAD").getObjectId());
  65. }
  66. @Test
  67. public void testResetPathDoubleDash() throws Exception {
  68. resetPath(true, true);
  69. }
  70. @Test
  71. public void testResetPathNoDoubleDash() throws Exception {
  72. resetPath(false, true);
  73. }
  74. @Test
  75. public void testResetPathDoubleDashNoRef() throws Exception {
  76. resetPath(true, false);
  77. }
  78. @Ignore("Currently we cannote recognize if a name is a commit-ish or a path, "
  79. + "so 'git reset a' will not work if 'a' is not a branch name but a file path")
  80. @Test
  81. public void testResetPathNoDoubleDashNoRef() throws Exception {
  82. resetPath(false, false);
  83. }
  84. private void resetPath(boolean useDoubleDash, boolean supplyCommit)
  85. throws Exception {
  86. // create files a and b
  87. writeTrashFile("a", "Hello world a");
  88. writeTrashFile("b", "Hello world b");
  89. // stage the files
  90. git.add().addFilepattern(".").call();
  91. // commit the files
  92. RevCommit commit = git.commit().setMessage("files a & b").call();
  93. // change both files a and b
  94. writeTrashFile("a", "New Hello world a");
  95. writeTrashFile("b", "New Hello world b");
  96. // stage the files
  97. git.add().addFilepattern(".").call();
  98. // reset only file a
  99. String cmd = String.format("git reset %s%s a",
  100. supplyCommit ? commit.getId().name() : "",
  101. useDoubleDash ? " --" : "");
  102. assertStringArrayEquals("", execute(cmd));
  103. assertEquals(commit.getId(),
  104. git.getRepository().exactRef("HEAD").getObjectId());
  105. org.eclipse.jgit.api.Status status = git.status().call();
  106. // assert that file a is unstaged
  107. assertArrayEquals(new String[] { "a" },
  108. status.getModified().toArray());
  109. // assert that file b is still staged
  110. assertArrayEquals(new String[] { "b" },
  111. status.getChanged().toArray());
  112. }
  113. }