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.

StatusCommandTest.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2011, Christian Halstrick <christian.halstrick@sap.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.assertFalse;
  13. import static org.junit.Assert.assertTrue;
  14. import static org.junit.Assume.assumeTrue;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import org.eclipse.jgit.api.errors.GitAPIException;
  18. import org.eclipse.jgit.api.errors.NoFilepatternException;
  19. import org.eclipse.jgit.errors.NoWorkTreeException;
  20. import org.eclipse.jgit.junit.RepositoryTestCase;
  21. import org.eclipse.jgit.lib.Sets;
  22. import org.eclipse.jgit.storage.file.FileBasedConfig;
  23. import org.eclipse.jgit.util.FS;
  24. import org.junit.Test;
  25. public class StatusCommandTest extends RepositoryTestCase {
  26. @Test
  27. public void testEmptyStatus() throws NoWorkTreeException,
  28. GitAPIException {
  29. try (Git git = new Git(db)) {
  30. Status stat = git.status().call();
  31. assertEquals(0, stat.getAdded().size());
  32. assertEquals(0, stat.getChanged().size());
  33. assertEquals(0, stat.getMissing().size());
  34. assertEquals(0, stat.getModified().size());
  35. assertEquals(0, stat.getRemoved().size());
  36. assertEquals(0, stat.getUntracked().size());
  37. }
  38. }
  39. @Test
  40. public void testDifferentStates() throws IOException,
  41. NoFilepatternException, GitAPIException {
  42. try (Git git = new Git(db)) {
  43. writeTrashFile("a", "content of a");
  44. writeTrashFile("b", "content of b");
  45. writeTrashFile("c", "content of c");
  46. git.add().addFilepattern("a").addFilepattern("b").call();
  47. Status stat = git.status().call();
  48. assertEquals(Sets.of("a", "b"), stat.getAdded());
  49. assertEquals(0, stat.getChanged().size());
  50. assertEquals(0, stat.getMissing().size());
  51. assertEquals(0, stat.getModified().size());
  52. assertEquals(0, stat.getRemoved().size());
  53. assertEquals(Sets.of("c"), stat.getUntracked());
  54. git.commit().setMessage("initial").call();
  55. writeTrashFile("a", "modified content of a");
  56. writeTrashFile("b", "modified content of b");
  57. writeTrashFile("d", "content of d");
  58. git.add().addFilepattern("a").addFilepattern("d").call();
  59. writeTrashFile("a", "again modified content of a");
  60. stat = git.status().call();
  61. assertEquals(Sets.of("d"), stat.getAdded());
  62. assertEquals(Sets.of("a"), stat.getChanged());
  63. assertEquals(0, stat.getMissing().size());
  64. assertEquals(Sets.of("b", "a"), stat.getModified());
  65. assertEquals(0, stat.getRemoved().size());
  66. assertEquals(Sets.of("c"), stat.getUntracked());
  67. git.add().addFilepattern(".").call();
  68. git.commit().setMessage("second").call();
  69. stat = git.status().call();
  70. assertEquals(0, stat.getAdded().size());
  71. assertEquals(0, stat.getChanged().size());
  72. assertEquals(0, stat.getMissing().size());
  73. assertEquals(0, stat.getModified().size());
  74. assertEquals(0, stat.getRemoved().size());
  75. assertEquals(0, stat.getUntracked().size());
  76. deleteTrashFile("a");
  77. assertFalse(new File(git.getRepository().getWorkTree(), "a").exists());
  78. git.add().addFilepattern("a").setUpdate(true).call();
  79. writeTrashFile("a", "recreated content of a");
  80. stat = git.status().call();
  81. assertEquals(0, stat.getAdded().size());
  82. assertEquals(0, stat.getChanged().size());
  83. assertEquals(0, stat.getMissing().size());
  84. assertEquals(0, stat.getModified().size());
  85. assertEquals(Sets.of("a"), stat.getRemoved());
  86. assertEquals(Sets.of("a"), stat.getUntracked());
  87. git.commit().setMessage("t").call();
  88. writeTrashFile("sub/a", "sub-file");
  89. stat = git.status().call();
  90. assertEquals(1, stat.getUntrackedFolders().size());
  91. assertTrue(stat.getUntrackedFolders().contains("sub"));
  92. }
  93. }
  94. @Test
  95. public void testDifferentStatesWithPaths() throws IOException,
  96. NoFilepatternException, GitAPIException {
  97. try (Git git = new Git(db)) {
  98. writeTrashFile("a", "content of a");
  99. writeTrashFile("D/b", "content of b");
  100. writeTrashFile("D/c", "content of c");
  101. writeTrashFile("D/D/d", "content of d");
  102. git.add().addFilepattern(".").call();
  103. writeTrashFile("a", "new content of a");
  104. writeTrashFile("D/b", "new content of b");
  105. writeTrashFile("D/D/d", "new content of d");
  106. // filter on an not existing path
  107. Status stat = git.status().addPath("x").call();
  108. assertEquals(0, stat.getModified().size());
  109. // filter on an existing file
  110. stat = git.status().addPath("a").call();
  111. assertEquals(Sets.of("a"), stat.getModified());
  112. // filter on an existing folder
  113. stat = git.status().addPath("D").call();
  114. assertEquals(Sets.of("D/b", "D/D/d"), stat.getModified());
  115. // filter on an existing folder and file
  116. stat = git.status().addPath("D/D").addPath("a").call();
  117. assertEquals(Sets.of("a", "D/D/d"), stat.getModified());
  118. // do not filter at all
  119. stat = git.status().call();
  120. assertEquals(Sets.of("a", "D/b", "D/D/d"), stat.getModified());
  121. }
  122. }
  123. @Test
  124. public void testExecutableWithNonNormalizedIndex() throws Exception {
  125. assumeTrue(FS.DETECTED.supportsExecute());
  126. try (Git git = new Git(db)) {
  127. // Commit a file with CR/LF into the index
  128. FileBasedConfig config = db.getConfig();
  129. config.setString("core", null, "autocrlf", "false");
  130. config.save();
  131. File testFile = writeTrashFile("file.txt", "line 1\r\nline 2\r\n");
  132. FS.DETECTED.setExecute(testFile, true);
  133. git.add().addFilepattern("file.txt").call();
  134. git.commit().setMessage("Initial").call();
  135. assertEquals(
  136. "[file.txt, mode:100755, content:line 1\r\nline 2\r\n]",
  137. indexState(CONTENT));
  138. config.setString("core", null, "autocrlf", "true");
  139. config.save();
  140. Status status = git.status().call();
  141. assertTrue("Expected no differences", status.isClean());
  142. }
  143. }
  144. @Test
  145. public void testFolderPrefix() throws Exception {
  146. // "audio" is a prefix of "audio-new" and "audio.new".
  147. try (Git git = new Git(db)) {
  148. // Order here is the git order, but that doesn't really matter.
  149. // They are processed by StatusCommand in this order even if written
  150. // in a different order. Bug 566799 would, when having processed
  151. // audio/foo, remove previously recorded untracked folders that have
  152. // "audio" as a prefix: audio-new and audio.new.
  153. writeTrashFile("audi", "foo", "foo");
  154. writeTrashFile("audio-new", "foo", "foo");
  155. writeTrashFile("audio.new", "foo", "foo");
  156. writeTrashFile("audio", "foo", "foo");
  157. writeTrashFile("audio_new", "foo", "foo");
  158. Status stat = git.status().call();
  159. assertEquals(Sets.of("audi", "audio-new", "audio.new", "audio",
  160. "audio_new"), stat.getUntrackedFolders());
  161. }
  162. }
  163. }