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.

StashListCommandTest.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.api;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertNotNull;
  13. import static org.junit.Assert.assertTrue;
  14. import java.util.Collection;
  15. import java.util.Iterator;
  16. import org.eclipse.jgit.junit.RepositoryTestCase;
  17. import org.eclipse.jgit.lib.Constants;
  18. import org.eclipse.jgit.lib.ObjectId;
  19. import org.eclipse.jgit.lib.RefUpdate;
  20. import org.eclipse.jgit.lib.RefUpdate.Result;
  21. import org.eclipse.jgit.revwalk.RevCommit;
  22. import org.junit.Test;
  23. /**
  24. * Unit tests of {@link StashListCommand}
  25. */
  26. public class StashListCommandTest extends RepositoryTestCase {
  27. @Test
  28. public void noStashRef() throws Exception {
  29. StashListCommand command = Git.wrap(db).stashList();
  30. Collection<RevCommit> stashed = command.call();
  31. assertNotNull(stashed);
  32. assertTrue(stashed.isEmpty());
  33. }
  34. @Test
  35. public void emptyStashReflog() throws Exception {
  36. Git git = Git.wrap(db);
  37. writeTrashFile("file.txt", "content");
  38. git.add().addFilepattern("file.txt").call();
  39. RevCommit commit = git.commit().setMessage("create file").call();
  40. RefUpdate update = db.updateRef(Constants.R_STASH);
  41. update.setNewObjectId(commit);
  42. update.disableRefLog();
  43. assertEquals(Result.NEW, update.update());
  44. StashListCommand command = Git.wrap(db).stashList();
  45. Collection<RevCommit> stashed = command.call();
  46. assertNotNull(stashed);
  47. assertTrue(stashed.isEmpty());
  48. }
  49. @Test
  50. public void singleStashedCommit() throws Exception {
  51. Git git = Git.wrap(db);
  52. writeTrashFile("file.txt", "content");
  53. git.add().addFilepattern("file.txt").call();
  54. RevCommit commit = git.commit().setMessage("create file").call();
  55. assertEquals(Result.NEW, newStashUpdate(commit).update());
  56. StashListCommand command = git.stashList();
  57. Collection<RevCommit> stashed = command.call();
  58. assertNotNull(stashed);
  59. assertEquals(1, stashed.size());
  60. assertEquals(commit, stashed.iterator().next());
  61. }
  62. @Test
  63. public void multipleStashedCommits() throws Exception {
  64. Git git = Git.wrap(db);
  65. writeTrashFile("file.txt", "content");
  66. git.add().addFilepattern("file.txt").call();
  67. RevCommit commit1 = git.commit().setMessage("create file").call();
  68. writeTrashFile("file.txt", "content2");
  69. git.add().addFilepattern("file.txt").call();
  70. RevCommit commit2 = git.commit().setMessage("edit file").call();
  71. assertEquals(Result.NEW, newStashUpdate(commit1).update());
  72. assertEquals(Result.FAST_FORWARD, newStashUpdate(commit2).update());
  73. StashListCommand command = git.stashList();
  74. Collection<RevCommit> stashed = command.call();
  75. assertNotNull(stashed);
  76. assertEquals(2, stashed.size());
  77. Iterator<RevCommit> iter = stashed.iterator();
  78. assertEquals(commit2, iter.next());
  79. assertEquals(commit1, iter.next());
  80. }
  81. private RefUpdate newStashUpdate(ObjectId newId) throws Exception {
  82. RefUpdate ru = db.updateRef(Constants.R_STASH);
  83. ru.setNewObjectId(newId);
  84. ru.setForceRefLog(true);
  85. return ru;
  86. }
  87. }