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.

RepoCommandTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright (C) 2014, Google Inc.
  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.gitrepo;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import java.io.BufferedReader;
  48. import java.io.File;
  49. import java.io.FileReader;
  50. import org.eclipse.jgit.api.CloneCommand;
  51. import org.eclipse.jgit.api.Git;
  52. import org.eclipse.jgit.junit.JGitTestUtil;
  53. import org.eclipse.jgit.junit.RepositoryTestCase;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.junit.Test;
  58. public class RepoCommandTest extends RepositoryTestCase {
  59. private static final String BRANCH = "branch";
  60. private static final String TAG = "release";
  61. private Repository defaultDb;
  62. private Repository notDefaultDb;
  63. private Repository groupADb;
  64. private Repository groupBDb;
  65. private String rootUri;
  66. private String defaultUri;
  67. private String notDefaultUri;
  68. private String groupAUri;
  69. private String groupBUri;
  70. private ObjectId oldCommitId;
  71. public void setUp() throws Exception {
  72. super.setUp();
  73. defaultDb = createWorkRepository();
  74. Git git = new Git(defaultDb);
  75. JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "branch world");
  76. git.add().addFilepattern("hello.txt").call();
  77. oldCommitId = git.commit().setMessage("Initial commit").call().getId();
  78. git.checkout().setName(BRANCH).setCreateBranch(true).call();
  79. git.checkout().setName("master").call();
  80. git.tag().setName(TAG).call();
  81. JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "master world");
  82. git.add().addFilepattern("hello.txt").call();
  83. git.commit().setMessage("Second commit").call();
  84. notDefaultDb = createWorkRepository();
  85. git = new Git(notDefaultDb);
  86. JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello");
  87. git.add().addFilepattern("world.txt").call();
  88. git.commit().setMessage("Initial commit").call();
  89. groupADb = createWorkRepository();
  90. git = new Git(groupADb);
  91. JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world");
  92. git.add().addFilepattern("a.txt").call();
  93. git.commit().setMessage("Initial commit").call();
  94. groupBDb = createWorkRepository();
  95. git = new Git(groupBDb);
  96. JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world");
  97. git.add().addFilepattern("b.txt").call();
  98. git.commit().setMessage("Initial commit").call();
  99. resolveRelativeUris();
  100. }
  101. @Test
  102. public void testAddRepoManifest() throws Exception {
  103. StringBuilder xmlContent = new StringBuilder();
  104. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  105. .append("<manifest>")
  106. .append("<remote name=\"remote1\" fetch=\".\" />")
  107. .append("<default revision=\"master\" remote=\"remote1\" />")
  108. .append("<project path=\"foo\" name=\"")
  109. .append(defaultUri)
  110. .append("\" />")
  111. .append("</manifest>");
  112. writeTrashFile("manifest.xml", xmlContent.toString());
  113. RepoCommand command = new RepoCommand(db);
  114. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  115. .setURI(rootUri)
  116. .call();
  117. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  118. assertTrue("submodule was checked out", hello.exists());
  119. BufferedReader reader = new BufferedReader(new FileReader(hello));
  120. String content = reader.readLine();
  121. reader.close();
  122. assertEquals("submodule content is as expected.", "master world", content);
  123. }
  124. @Test
  125. public void testRepoManifestGroups() throws Exception {
  126. StringBuilder xmlContent = new StringBuilder();
  127. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  128. .append("<manifest>")
  129. .append("<remote name=\"remote1\" fetch=\".\" />")
  130. .append("<default revision=\"master\" remote=\"remote1\" />")
  131. .append("<project path=\"foo\" name=\"")
  132. .append(defaultUri)
  133. .append("\" groups=\"a,test\" />")
  134. .append("<project path=\"bar\" name=\"")
  135. .append(notDefaultUri)
  136. .append("\" groups=\"notdefault\" />")
  137. .append("<project path=\"a\" name=\"")
  138. .append(groupAUri)
  139. .append("\" groups=\"a\" />")
  140. .append("<project path=\"b\" name=\"")
  141. .append(groupBUri)
  142. .append("\" groups=\"b\" />")
  143. .append("</manifest>");
  144. // default should have foo, a & b
  145. Repository localDb = createWorkRepository();
  146. JGitTestUtil.writeTrashFile(localDb, "manifest.xml", xmlContent.toString());
  147. RepoCommand command = new RepoCommand(localDb);
  148. command.setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  149. .setURI(rootUri)
  150. .call();
  151. File file = new File(localDb.getWorkTree(), "foo/hello.txt");
  152. assertTrue("default has foo", file.exists());
  153. file = new File(localDb.getWorkTree(), "bar/world.txt");
  154. assertFalse("default doesn't have bar", file.exists());
  155. file = new File(localDb.getWorkTree(), "a/a.txt");
  156. assertTrue("default has a", file.exists());
  157. file = new File(localDb.getWorkTree(), "b/b.txt");
  158. assertTrue("default has b", file.exists());
  159. // all,-a should have bar & b
  160. localDb = createWorkRepository();
  161. JGitTestUtil.writeTrashFile(localDb, "manifest.xml", xmlContent.toString());
  162. command = new RepoCommand(localDb);
  163. command.setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  164. .setURI(rootUri)
  165. .setGroups("all,-a")
  166. .call();
  167. file = new File(localDb.getWorkTree(), "foo/hello.txt");
  168. assertFalse("\"all,-a\" doesn't have foo", file.exists());
  169. file = new File(localDb.getWorkTree(), "bar/world.txt");
  170. assertTrue("\"all,-a\" has bar", file.exists());
  171. file = new File(localDb.getWorkTree(), "a/a.txt");
  172. assertFalse("\"all,-a\" doesn't have a", file.exists());
  173. file = new File(localDb.getWorkTree(), "b/b.txt");
  174. assertTrue("\"all,-a\" has have b", file.exists());
  175. }
  176. @Test
  177. public void testRepoManifestCopyFile() throws Exception {
  178. Repository localDb = createWorkRepository();
  179. StringBuilder xmlContent = new StringBuilder();
  180. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  181. .append("<manifest>")
  182. .append("<remote name=\"remote1\" fetch=\".\" />")
  183. .append("<default revision=\"master\" remote=\"remote1\" />")
  184. .append("<project path=\"foo\" name=\"")
  185. .append(defaultUri)
  186. .append("\">")
  187. .append("<copyfile src=\"hello.txt\" dest=\"Hello\" />")
  188. .append("</project>")
  189. .append("</manifest>");
  190. JGitTestUtil.writeTrashFile(localDb, "manifest.xml", xmlContent.toString());
  191. RepoCommand command = new RepoCommand(localDb);
  192. command.setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  193. .setURI(rootUri)
  194. .call();
  195. // The original file should exist
  196. File hello = new File(localDb.getWorkTree(), "foo/hello.txt");
  197. assertTrue("The original file exists", hello.exists());
  198. BufferedReader reader = new BufferedReader(new FileReader(hello));
  199. String content = reader.readLine();
  200. reader.close();
  201. assertEquals("The original file has expected content", "master world", content);
  202. // The dest file should also exist
  203. hello = new File(localDb.getWorkTree(), "Hello");
  204. assertTrue("The destination file exists", hello.exists());
  205. reader = new BufferedReader(new FileReader(hello));
  206. content = reader.readLine();
  207. reader.close();
  208. assertEquals("The destination file has expected content", "master world", content);
  209. }
  210. @Test
  211. public void testBareRepo() throws Exception {
  212. Repository remoteDb = createBareRepository();
  213. Repository tempDb = createWorkRepository();
  214. StringBuilder xmlContent = new StringBuilder();
  215. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  216. .append("<manifest>")
  217. .append("<remote name=\"remote1\" fetch=\".\" />")
  218. .append("<default revision=\"master\" remote=\"remote1\" />")
  219. .append("<project path=\"foo\" name=\"")
  220. .append(defaultUri)
  221. .append("\" />")
  222. .append("</manifest>");
  223. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml", xmlContent.toString());
  224. RepoCommand command = new RepoCommand(remoteDb);
  225. command.setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  226. .setURI(rootUri)
  227. .call();
  228. // Clone it
  229. File directory = createTempDirectory("testBareRepo");
  230. CloneCommand clone = Git.cloneRepository();
  231. clone.setDirectory(directory);
  232. clone.setURI(remoteDb.getDirectory().toURI().toString());
  233. Repository localDb = clone.call().getRepository();
  234. // The .gitmodules file should exist
  235. File gitmodules = new File(localDb.getWorkTree(), ".gitmodules");
  236. assertTrue("The .gitmodules file exists", gitmodules.exists());
  237. // The first line of .gitmodules file should be expected
  238. BufferedReader reader = new BufferedReader(new FileReader(gitmodules));
  239. String content = reader.readLine();
  240. reader.close();
  241. assertEquals("The first line of .gitmodules file is as expected.",
  242. "[submodule \"foo\"]", content);
  243. // The gitlink should be the same as remote head sha1
  244. String gitlink = localDb.resolve(Constants.HEAD + ":foo").name();
  245. String remote = defaultDb.resolve(Constants.HEAD).name();
  246. assertEquals("The gitlink is same as remote head", remote, gitlink);
  247. }
  248. @Test
  249. public void testRevision() throws Exception {
  250. StringBuilder xmlContent = new StringBuilder();
  251. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  252. .append("<manifest>")
  253. .append("<remote name=\"remote1\" fetch=\".\" />")
  254. .append("<default revision=\"master\" remote=\"remote1\" />")
  255. .append("<project path=\"foo\" name=\"")
  256. .append(defaultUri)
  257. .append("\" revision=\"")
  258. .append(oldCommitId.name())
  259. .append("\" />")
  260. .append("</manifest>");
  261. writeTrashFile("manifest.xml", xmlContent.toString());
  262. RepoCommand command = new RepoCommand(db);
  263. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  264. .setURI(rootUri)
  265. .call();
  266. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  267. BufferedReader reader = new BufferedReader(new FileReader(hello));
  268. String content = reader.readLine();
  269. reader.close();
  270. assertEquals("submodule content is as expected.", "branch world", content);
  271. }
  272. @Test
  273. public void testRevisionBranch() throws Exception {
  274. StringBuilder xmlContent = new StringBuilder();
  275. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  276. .append("<manifest>")
  277. .append("<remote name=\"remote1\" fetch=\".\" />")
  278. .append("<default revision=\"")
  279. .append(BRANCH)
  280. .append("\" remote=\"remote1\" />")
  281. .append("<project path=\"foo\" name=\"")
  282. .append(defaultUri)
  283. .append("\" />")
  284. .append("</manifest>");
  285. writeTrashFile("manifest.xml", xmlContent.toString());
  286. RepoCommand command = new RepoCommand(db);
  287. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  288. .setURI(rootUri)
  289. .call();
  290. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  291. BufferedReader reader = new BufferedReader(new FileReader(hello));
  292. String content = reader.readLine();
  293. reader.close();
  294. assertEquals("submodule content is as expected.", "branch world", content);
  295. }
  296. @Test
  297. public void testRevisionTag() throws Exception {
  298. StringBuilder xmlContent = new StringBuilder();
  299. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  300. .append("<manifest>")
  301. .append("<remote name=\"remote1\" fetch=\".\" />")
  302. .append("<default revision=\"master\" remote=\"remote1\" />")
  303. .append("<project path=\"foo\" name=\"")
  304. .append(defaultUri)
  305. .append("\" revision=\"")
  306. .append(TAG)
  307. .append("\" />")
  308. .append("</manifest>");
  309. writeTrashFile("manifest.xml", xmlContent.toString());
  310. RepoCommand command = new RepoCommand(db);
  311. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  312. .setURI(rootUri)
  313. .call();
  314. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  315. BufferedReader reader = new BufferedReader(new FileReader(hello));
  316. String content = reader.readLine();
  317. reader.close();
  318. assertEquals("submodule content is as expected.", "branch world", content);
  319. }
  320. @Test
  321. public void testRevisionBare() throws Exception {
  322. Repository remoteDb = createBareRepository();
  323. Repository tempDb = createWorkRepository();
  324. StringBuilder xmlContent = new StringBuilder();
  325. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  326. .append("<manifest>")
  327. .append("<remote name=\"remote1\" fetch=\".\" />")
  328. .append("<default revision=\"")
  329. .append(BRANCH)
  330. .append("\" remote=\"remote1\" />")
  331. .append("<project path=\"foo\" name=\"")
  332. .append(defaultUri)
  333. .append("\" />")
  334. .append("</manifest>");
  335. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml", xmlContent.toString());
  336. RepoCommand command = new RepoCommand(remoteDb);
  337. command.setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  338. .setURI(rootUri)
  339. .call();
  340. // Clone it
  341. File directory = createTempDirectory("testRevisionBare");
  342. CloneCommand clone = Git.cloneRepository();
  343. clone.setDirectory(directory);
  344. clone.setURI(remoteDb.getDirectory().toURI().toString());
  345. Repository localDb = clone.call().getRepository();
  346. // The gitlink should be the same as oldCommitId
  347. String gitlink = localDb.resolve(Constants.HEAD + ":foo").name();
  348. assertEquals("The gitlink is same as remote head",
  349. oldCommitId.name(), gitlink);
  350. }
  351. @Test
  352. public void testCopyFileBare() throws Exception {
  353. Repository remoteDb = createBareRepository();
  354. Repository tempDb = createWorkRepository();
  355. StringBuilder xmlContent = new StringBuilder();
  356. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  357. .append("<manifest>")
  358. .append("<remote name=\"remote1\" fetch=\".\" />")
  359. .append("<default revision=\"master\" remote=\"remote1\" />")
  360. .append("<project path=\"foo\" name=\"")
  361. .append(defaultUri)
  362. .append("\" revision=\"")
  363. .append(BRANCH)
  364. .append("\" >")
  365. .append("<copyfile src=\"hello.txt\" dest=\"Hello\" />")
  366. .append("</project>")
  367. .append("</manifest>");
  368. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml", xmlContent.toString());
  369. RepoCommand command = new RepoCommand(remoteDb);
  370. command.setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  371. .setURI(rootUri)
  372. .call();
  373. // Clone it
  374. File directory = createTempDirectory("testCopyFileBare");
  375. CloneCommand clone = Git.cloneRepository();
  376. clone.setDirectory(directory);
  377. clone.setURI(remoteDb.getDirectory().toURI().toString());
  378. Repository localDb = clone.call().getRepository();
  379. // The Hello file should exist
  380. File hello = new File(localDb.getWorkTree(), "Hello");
  381. assertTrue("The Hello file exists", hello.exists());
  382. // The content of Hello file should be expected
  383. BufferedReader reader = new BufferedReader(new FileReader(hello));
  384. String content = reader.readLine();
  385. reader.close();
  386. assertEquals("The Hello file has expected content", "branch world", content);
  387. }
  388. private void resolveRelativeUris() {
  389. // Find the longest common prefix ends with "/" as rootUri.
  390. defaultUri = defaultDb.getDirectory().toURI().toString();
  391. notDefaultUri = notDefaultDb.getDirectory().toURI().toString();
  392. groupAUri = groupADb.getDirectory().toURI().toString();
  393. groupBUri = groupBDb.getDirectory().toURI().toString();
  394. int start = 0;
  395. while (start <= defaultUri.length()) {
  396. int newStart = defaultUri.indexOf('/', start + 1);
  397. String prefix = defaultUri.substring(0, newStart);
  398. if (!notDefaultUri.startsWith(prefix) ||
  399. !groupAUri.startsWith(prefix) ||
  400. !groupBUri.startsWith(prefix)) {
  401. start++;
  402. rootUri = defaultUri.substring(0, start);
  403. defaultUri = defaultUri.substring(start);
  404. notDefaultUri = notDefaultUri.substring(start);
  405. groupAUri = groupAUri.substring(start);
  406. groupBUri = groupBUri.substring(start);
  407. return;
  408. }
  409. start = newStart;
  410. }
  411. }
  412. }