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.

StatusTest.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2012, François Rey <eclipse.org_@_francois_._rey_._name>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.pgm;
  44. import static org.junit.Assert.assertEquals;
  45. import org.eclipse.jgit.api.Git;
  46. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  47. import org.eclipse.jgit.lib.Constants;
  48. import org.eclipse.jgit.revwalk.RevCommit;
  49. import org.junit.Test;
  50. public class StatusTest extends CLIRepositoryTestCase {
  51. @Test
  52. public void testStatus() throws Exception {
  53. Git git = new Git(db);
  54. // Write all files
  55. writeTrashFile("tracked", "tracked");
  56. writeTrashFile("stagedNew", "stagedNew");
  57. writeTrashFile("stagedModified", "stagedModified");
  58. writeTrashFile("stagedDeleted", "stagedDeleted");
  59. writeTrashFile("trackedModified", "trackedModified");
  60. writeTrashFile("trackedDeleted", "trackedDeleted");
  61. writeTrashFile("untracked", "untracked");
  62. // Test untracked
  63. assertArrayOfLinesEquals(new String[] { // git status output
  64. "# On branch master", //
  65. "# Untracked files:", //
  66. "# ",//
  67. "# \tstagedDeleted", //
  68. "# \tstagedModified", //
  69. "# \tstagedNew", //
  70. "# \ttracked", //
  71. "# \ttrackedDeleted", //
  72. "# \ttrackedModified", //
  73. "# \tuntracked", //
  74. "" //
  75. }, execute("git status")); //
  76. // Add to index
  77. git.add().addFilepattern("tracked").call();
  78. git.add().addFilepattern("stagedModified").call();
  79. git.add().addFilepattern("stagedDeleted").call();
  80. git.add().addFilepattern("trackedModified").call();
  81. git.add().addFilepattern("trackedDeleted").call();
  82. // Test staged count
  83. assertArrayOfLinesEquals(new String[] { // git status output
  84. "# On branch master", //
  85. "# Changes to be committed:", //
  86. "# ", //
  87. "# \tnew file: stagedDeleted", //
  88. "# \tnew file: stagedModified", //
  89. "# \tnew file: tracked", //
  90. "# \tnew file: trackedDeleted", //
  91. "# \tnew file: trackedModified", //
  92. "# ", //
  93. "# Untracked files:", //
  94. "# ", //
  95. "# \tstagedNew", //
  96. "# \tuntracked", //
  97. "" //
  98. }, execute("git status")); //
  99. // Commit
  100. git.commit().setMessage("initial commit")
  101. .call();
  102. assertArrayOfLinesEquals(new String[] { // git status output
  103. "# On branch master", //
  104. "# Untracked files:", //
  105. "# ", //
  106. "# \tstagedNew", //
  107. "# \tuntracked", //
  108. "" //
  109. }, execute("git status")); //
  110. // Make some changes and stage them
  111. writeTrashFile("stagedModified", "stagedModified modified");
  112. deleteTrashFile("stagedDeleted");
  113. writeTrashFile("trackedModified", "trackedModified modified");
  114. deleteTrashFile("trackedDeleted");
  115. git.add().addFilepattern("stagedModified").call();
  116. git.rm().addFilepattern("stagedDeleted").call();
  117. git.add().addFilepattern("stagedNew").call();
  118. // Test staged/not-staged status
  119. assertArrayOfLinesEquals(new String[] { // git status output
  120. "# On branch master", //
  121. "# Changes to be committed:", //
  122. "# ", //
  123. "# \tdeleted: stagedDeleted", //
  124. "# \tmodified: stagedModified", //
  125. "# \tnew file: stagedNew", //
  126. "# ", //
  127. "# Changes not staged for commit:", //
  128. "# ", //
  129. "# \tdeleted: trackedDeleted", //
  130. "# \tmodified: trackedModified", //
  131. "# ", //
  132. "# Untracked files:", //
  133. "# ", //
  134. "# \tuntracked", //
  135. "" //
  136. }, execute("git status")); //
  137. // Create unmerged file
  138. writeTrashFile("unmerged", "unmerged");
  139. git.add().addFilepattern("unmerged").call();
  140. // Commit pending changes
  141. git.add().addFilepattern("trackedModified").call();
  142. git.rm().addFilepattern("trackedDeleted").call();
  143. git.commit().setMessage("commit before branching").call();
  144. assertArrayOfLinesEquals(new String[] { // git status output
  145. "# On branch master", //
  146. "# Untracked files:", //
  147. "# ", //
  148. "# \tuntracked", //
  149. "" //
  150. }, execute("git status")); //
  151. // Checkout new branch
  152. git.checkout().setCreateBranch(true).setName("test").call();
  153. // Test branch status
  154. assertArrayOfLinesEquals(new String[] { // git status output
  155. "# On branch test", //
  156. "# Untracked files:", //
  157. "# ", //
  158. "# \tuntracked", //
  159. "" //
  160. }, execute("git status")); //
  161. // Commit change and checkout master again
  162. writeTrashFile("unmerged", "changed in test branch");
  163. git.add().addFilepattern("unmerged").call();
  164. RevCommit testBranch = git.commit()
  165. .setMessage("changed unmerged in test branch").call();
  166. assertArrayOfLinesEquals(new String[] { // git status output
  167. "# On branch test", //
  168. "# Untracked files:", //
  169. "# ", //
  170. "# \tuntracked", //
  171. "" //
  172. }, execute("git status")); //
  173. git.checkout().setName("master").call();
  174. // Change the same file and commit
  175. writeTrashFile("unmerged", "changed in master branch");
  176. git.add().addFilepattern("unmerged").call();
  177. git.commit().setMessage("changed unmerged in master branch").call();
  178. assertArrayOfLinesEquals(new String[] { // git status output
  179. "# On branch master", //
  180. "# Untracked files:", //
  181. "# ", //
  182. "# \tuntracked", //
  183. "" //
  184. }, execute("git status")); //
  185. // Merge test branch into master
  186. git.merge().include(testBranch.getId()).call();
  187. // Test unmerged status
  188. assertArrayOfLinesEquals(new String[] { // git status output
  189. "# On branch master", //
  190. "# Unmerged paths:", //
  191. "# ", //
  192. "# \tunmerged", //
  193. "# ", //
  194. "# Untracked files:", //
  195. "# ", //
  196. "# \tuntracked", //
  197. "" //
  198. }, execute("git status")); //
  199. // Test detached head
  200. String commitId = db.getRef(Constants.MASTER).getObjectId().name();
  201. git.checkout().setName(commitId).call();
  202. assertArrayOfLinesEquals(new String[] { // git status output
  203. "# Not currently on any branch.", //
  204. "# Unmerged paths:", //
  205. "# ", //
  206. "# \tunmerged", //
  207. "# ", //
  208. "# Untracked files:", //
  209. "# ", //
  210. "# \tuntracked", //
  211. "" //
  212. }, execute("git status")); //
  213. }
  214. private void assertArrayOfLinesEquals(String[] expected, String[] actual) {
  215. assertEquals(toText(expected), toText(actual));
  216. }
  217. private String toText(String[] lines) {
  218. StringBuilder b = new StringBuilder();
  219. for (String s : lines) {
  220. b.append(s);
  221. b.append('\n');
  222. }
  223. return b.toString();
  224. }
  225. }