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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. @Test
  214. public void testStatusPorcelain() throws Exception {
  215. Git git = new Git(db);
  216. // Write all files
  217. writeTrashFile("tracked", "tracked");
  218. writeTrashFile("stagedNew", "stagedNew");
  219. writeTrashFile("stagedModified", "stagedModified");
  220. writeTrashFile("stagedDeleted", "stagedDeleted");
  221. writeTrashFile("trackedModified", "trackedModified");
  222. writeTrashFile("trackedDeleted", "trackedDeleted");
  223. writeTrashFile("untracked", "untracked");
  224. // Test untracked
  225. assertArrayOfLinesEquals(new String[] { // git status output
  226. "?? stagedDeleted", //
  227. "?? stagedModified", //
  228. "?? stagedNew", //
  229. "?? tracked", //
  230. "?? trackedDeleted", //
  231. "?? trackedModified", //
  232. "?? untracked", //
  233. "" //
  234. }, execute("git status --porcelain")); //
  235. // Add to index
  236. git.add().addFilepattern("tracked").call();
  237. git.add().addFilepattern("stagedModified").call();
  238. git.add().addFilepattern("stagedDeleted").call();
  239. git.add().addFilepattern("trackedModified").call();
  240. git.add().addFilepattern("trackedDeleted").call();
  241. // Test staged count
  242. assertArrayOfLinesEquals(new String[] { // git status output
  243. "A stagedDeleted", //
  244. "A stagedModified", //
  245. "A tracked", //
  246. "A trackedDeleted", //
  247. "A trackedModified", //
  248. "?? stagedNew", //
  249. "?? untracked", //
  250. "" //
  251. }, execute("git status --porcelain")); //
  252. // Commit
  253. git.commit().setMessage("initial commit").call();
  254. assertArrayOfLinesEquals(new String[] { // git status output
  255. "?? stagedNew", //
  256. "?? untracked", //
  257. "" //
  258. }, execute("git status --porcelain")); //
  259. // Make some changes and stage them
  260. writeTrashFile("stagedModified", "stagedModified modified");
  261. deleteTrashFile("stagedDeleted");
  262. writeTrashFile("trackedModified", "trackedModified modified");
  263. deleteTrashFile("trackedDeleted");
  264. git.add().addFilepattern("stagedModified").call();
  265. git.rm().addFilepattern("stagedDeleted").call();
  266. git.add().addFilepattern("stagedNew").call();
  267. // Test staged/not-staged status
  268. assertArrayOfLinesEquals(new String[] { // git status output
  269. "D stagedDeleted", //
  270. "M stagedModified", //
  271. "A stagedNew", //
  272. " D trackedDeleted", //
  273. " M trackedModified", //
  274. "?? untracked", //
  275. "" //
  276. }, execute("git status --porcelain")); //
  277. // Create unmerged file
  278. writeTrashFile("unmerged", "unmerged");
  279. git.add().addFilepattern("unmerged").call();
  280. // Commit pending changes
  281. git.add().addFilepattern("trackedModified").call();
  282. git.rm().addFilepattern("trackedDeleted").call();
  283. git.commit().setMessage("commit before branching").call();
  284. assertArrayOfLinesEquals(new String[] { // git status output
  285. "?? untracked", //
  286. "" //
  287. }, execute("git status --porcelain")); //
  288. // Checkout new branch
  289. git.checkout().setCreateBranch(true).setName("test").call();
  290. // Test branch status
  291. assertArrayOfLinesEquals(new String[] { // git status output
  292. "?? untracked", //
  293. "" //
  294. }, execute("git status --porcelain")); //
  295. // Commit change and checkout master again
  296. writeTrashFile("unmerged", "changed in test branch");
  297. git.add().addFilepattern("unmerged").call();
  298. RevCommit testBranch = git.commit()
  299. .setMessage("changed unmerged in test branch").call();
  300. assertArrayOfLinesEquals(new String[] { // git status output
  301. "?? untracked", //
  302. "" //
  303. }, execute("git status --porcelain")); //
  304. git.checkout().setName("master").call();
  305. // Change the same file and commit
  306. writeTrashFile("unmerged", "changed in master branch");
  307. git.add().addFilepattern("unmerged").call();
  308. git.commit().setMessage("changed unmerged in master branch").call();
  309. assertArrayOfLinesEquals(new String[] { // git status output
  310. "?? untracked", //
  311. "" //
  312. }, execute("git status --porcelain")); //
  313. // Merge test branch into master
  314. git.merge().include(testBranch.getId()).call();
  315. // Test unmerged status
  316. assertArrayOfLinesEquals(new String[] { // git status output
  317. "UU unmerged", //
  318. "?? untracked", //
  319. "" //
  320. }, execute("git status --porcelain")); //
  321. // Test detached head
  322. String commitId = db.getRef(Constants.MASTER).getObjectId().name();
  323. git.checkout().setName(commitId).call();
  324. assertArrayOfLinesEquals(new String[] { // git status output
  325. "UU unmerged", //
  326. "?? untracked", //
  327. "" //
  328. }, execute("git status --porcelain")); //
  329. }
  330. }