您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

StatusTest.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2012, 2013 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 org.eclipse.jgit.api.Git;
  45. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  46. import org.eclipse.jgit.lib.Constants;
  47. import org.eclipse.jgit.revwalk.RevCommit;
  48. import org.junit.Test;
  49. public class StatusTest extends CLIRepositoryTestCase {
  50. @Test
  51. public void testStatus() throws Exception {
  52. Git git = new Git(db);
  53. // Write all files
  54. writeTrashFile("tracked", "tracked");
  55. writeTrashFile("stagedNew", "stagedNew");
  56. writeTrashFile("stagedModified", "stagedModified");
  57. writeTrashFile("stagedDeleted", "stagedDeleted");
  58. writeTrashFile("trackedModified", "trackedModified");
  59. writeTrashFile("trackedDeleted", "trackedDeleted");
  60. writeTrashFile("untracked", "untracked");
  61. // Test untracked
  62. assertArrayOfLinesEquals(new String[] { // git status output
  63. "# On branch master", //
  64. "# Untracked files:", //
  65. "# ",//
  66. "# \tstagedDeleted", //
  67. "# \tstagedModified", //
  68. "# \tstagedNew", //
  69. "# \ttracked", //
  70. "# \ttrackedDeleted", //
  71. "# \ttrackedModified", //
  72. "# \tuntracked", //
  73. "" //
  74. }, execute("git status")); //
  75. // Add to index
  76. git.add().addFilepattern("tracked").call();
  77. git.add().addFilepattern("stagedModified").call();
  78. git.add().addFilepattern("stagedDeleted").call();
  79. git.add().addFilepattern("trackedModified").call();
  80. git.add().addFilepattern("trackedDeleted").call();
  81. // Test staged count
  82. assertArrayOfLinesEquals(new String[] { // git status output
  83. "# On branch master", //
  84. "# Changes to be committed:", //
  85. "# ", //
  86. "# \tnew file: stagedDeleted", //
  87. "# \tnew file: stagedModified", //
  88. "# \tnew file: tracked", //
  89. "# \tnew file: trackedDeleted", //
  90. "# \tnew file: trackedModified", //
  91. "# ", //
  92. "# Untracked files:", //
  93. "# ", //
  94. "# \tstagedNew", //
  95. "# \tuntracked", //
  96. "" //
  97. }, execute("git status")); //
  98. // Commit
  99. git.commit().setMessage("initial commit")
  100. .call();
  101. assertArrayOfLinesEquals(new String[] { // git status output
  102. "# On branch master", //
  103. "# Untracked files:", //
  104. "# ", //
  105. "# \tstagedNew", //
  106. "# \tuntracked", //
  107. "" //
  108. }, execute("git status")); //
  109. // Make some changes and stage them
  110. writeTrashFile("stagedModified", "stagedModified modified");
  111. deleteTrashFile("stagedDeleted");
  112. writeTrashFile("trackedModified", "trackedModified modified");
  113. deleteTrashFile("trackedDeleted");
  114. git.add().addFilepattern("stagedModified").call();
  115. git.rm().addFilepattern("stagedDeleted").call();
  116. git.add().addFilepattern("stagedNew").call();
  117. // Test staged/not-staged status
  118. assertArrayOfLinesEquals(new String[] { // git status output
  119. "# On branch master", //
  120. "# Changes to be committed:", //
  121. "# ", //
  122. "# \tdeleted: stagedDeleted", //
  123. "# \tmodified: stagedModified", //
  124. "# \tnew file: stagedNew", //
  125. "# ", //
  126. "# Changes not staged for commit:", //
  127. "# ", //
  128. "# \tdeleted: trackedDeleted", //
  129. "# \tmodified: trackedModified", //
  130. "# ", //
  131. "# Untracked files:", //
  132. "# ", //
  133. "# \tuntracked", //
  134. "" //
  135. }, execute("git status")); //
  136. // Create unmerged file
  137. writeTrashFile("unmerged", "unmerged");
  138. git.add().addFilepattern("unmerged").call();
  139. // Commit pending changes
  140. git.add().addFilepattern("trackedModified").call();
  141. git.rm().addFilepattern("trackedDeleted").call();
  142. git.commit().setMessage("commit before branching").call();
  143. assertArrayOfLinesEquals(new String[] { // git status output
  144. "# On branch master", //
  145. "# Untracked files:", //
  146. "# ", //
  147. "# \tuntracked", //
  148. "" //
  149. }, execute("git status")); //
  150. // Checkout new branch
  151. git.checkout().setCreateBranch(true).setName("test").call();
  152. // Test branch status
  153. assertArrayOfLinesEquals(new String[] { // git status output
  154. "# On branch test", //
  155. "# Untracked files:", //
  156. "# ", //
  157. "# \tuntracked", //
  158. "" //
  159. }, execute("git status")); //
  160. // Commit change and checkout master again
  161. writeTrashFile("unmerged", "changed in test branch");
  162. git.add().addFilepattern("unmerged").call();
  163. RevCommit testBranch = git.commit()
  164. .setMessage("changed unmerged in test branch").call();
  165. assertArrayOfLinesEquals(new String[] { // git status output
  166. "# On branch test", //
  167. "# Untracked files:", //
  168. "# ", //
  169. "# \tuntracked", //
  170. "" //
  171. }, execute("git status")); //
  172. git.checkout().setName("master").call();
  173. // Change the same file and commit
  174. writeTrashFile("unmerged", "changed in master branch");
  175. git.add().addFilepattern("unmerged").call();
  176. git.commit().setMessage("changed unmerged in master branch").call();
  177. assertArrayOfLinesEquals(new String[] { // git status output
  178. "# On branch master", //
  179. "# Untracked files:", //
  180. "# ", //
  181. "# \tuntracked", //
  182. "" //
  183. }, execute("git status")); //
  184. // Merge test branch into master
  185. git.merge().include(testBranch.getId()).call();
  186. // Test unmerged status
  187. assertArrayOfLinesEquals(new String[] { // git status output
  188. "# On branch master", //
  189. "# Unmerged paths:", //
  190. "# ", //
  191. "# \tboth modified: unmerged", //
  192. "# ", //
  193. "# Untracked files:", //
  194. "# ", //
  195. "# \tuntracked", //
  196. "" //
  197. }, execute("git status")); //
  198. // Test detached head
  199. String commitId = db.getRef(Constants.MASTER).getObjectId().name();
  200. git.checkout().setName(commitId).call();
  201. assertArrayOfLinesEquals(new String[] { // git status output
  202. "# Not currently on any branch.", //
  203. "# Unmerged paths:", //
  204. "# ", //
  205. "# \tboth modified: unmerged", //
  206. "# ", //
  207. "# Untracked files:", //
  208. "# ", //
  209. "# \tuntracked", //
  210. "" //
  211. }, execute("git status")); //
  212. }
  213. }