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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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 java.nio.charset.StandardCharsets.UTF_8;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertTrue;
  49. import static org.junit.Assert.fail;
  50. import java.io.BufferedReader;
  51. import java.io.ByteArrayInputStream;
  52. import java.io.File;
  53. import java.io.FileReader;
  54. import java.io.IOException;
  55. import java.net.URI;
  56. import java.util.HashMap;
  57. import java.util.Map;
  58. import org.eclipse.jgit.api.Git;
  59. import org.eclipse.jgit.api.errors.GitAPIException;
  60. import org.eclipse.jgit.api.errors.InvalidRemoteException;
  61. import org.eclipse.jgit.api.errors.RefNotFoundException;
  62. import org.eclipse.jgit.junit.JGitTestUtil;
  63. import org.eclipse.jgit.junit.RepositoryTestCase;
  64. import org.eclipse.jgit.lib.BlobBasedConfig;
  65. import org.eclipse.jgit.lib.Config;
  66. import org.eclipse.jgit.lib.Constants;
  67. import org.eclipse.jgit.lib.ObjectId;
  68. import org.eclipse.jgit.lib.ObjectReader;
  69. import org.eclipse.jgit.lib.Ref;
  70. import org.eclipse.jgit.lib.Repository;
  71. import org.eclipse.jgit.revwalk.RevCommit;
  72. import org.eclipse.jgit.storage.file.FileBasedConfig;
  73. import org.eclipse.jgit.util.FS;
  74. import org.junit.Test;
  75. public class RepoCommandTest extends RepositoryTestCase {
  76. private static final String BRANCH = "branch";
  77. private static final String TAG = "release";
  78. private Repository defaultDb;
  79. private Repository notDefaultDb;
  80. private Repository groupADb;
  81. private Repository groupBDb;
  82. private String rootUri;
  83. private String defaultUri;
  84. private String notDefaultUri;
  85. private String groupAUri;
  86. private String groupBUri;
  87. private ObjectId oldCommitId;
  88. @Override
  89. public void setUp() throws Exception {
  90. super.setUp();
  91. defaultDb = createWorkRepository();
  92. try (Git git = new Git(defaultDb)) {
  93. JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "branch world");
  94. git.add().addFilepattern("hello.txt").call();
  95. oldCommitId = git.commit().setMessage("Initial commit").call().getId();
  96. git.checkout().setName(BRANCH).setCreateBranch(true).call();
  97. git.checkout().setName("master").call();
  98. git.tag().setName(TAG).call();
  99. JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "master world");
  100. git.add().addFilepattern("hello.txt").call();
  101. git.commit().setMessage("Second commit").call();
  102. addRepoToClose(defaultDb);
  103. }
  104. notDefaultDb = createWorkRepository();
  105. try (Git git = new Git(notDefaultDb)) {
  106. JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello");
  107. git.add().addFilepattern("world.txt").call();
  108. git.commit().setMessage("Initial commit").call();
  109. addRepoToClose(notDefaultDb);
  110. }
  111. groupADb = createWorkRepository();
  112. try (Git git = new Git(groupADb)) {
  113. JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world");
  114. git.add().addFilepattern("a.txt").call();
  115. git.commit().setMessage("Initial commit").call();
  116. addRepoToClose(groupADb);
  117. }
  118. groupBDb = createWorkRepository();
  119. try (Git git = new Git(groupBDb)) {
  120. JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world");
  121. git.add().addFilepattern("b.txt").call();
  122. git.commit().setMessage("Initial commit").call();
  123. addRepoToClose(groupBDb);
  124. }
  125. resolveRelativeUris();
  126. }
  127. class IndexedRepos implements RepoCommand.RemoteReader {
  128. Map<String, Repository> uriRepoMap;
  129. IndexedRepos() {
  130. uriRepoMap = new HashMap<>();
  131. }
  132. void put(String u, Repository r) {
  133. uriRepoMap.put(u, r);
  134. }
  135. @Override
  136. public ObjectId sha1(String uri, String refname) throws GitAPIException {
  137. if (!uriRepoMap.containsKey(uri)) {
  138. return null;
  139. }
  140. Repository r = uriRepoMap.get(uri);
  141. try {
  142. Ref ref = r.findRef(refname);
  143. if (ref == null) return null;
  144. ref = r.peel(ref);
  145. ObjectId id = ref.getObjectId();
  146. return id;
  147. } catch (IOException e) {
  148. throw new InvalidRemoteException("", e);
  149. }
  150. }
  151. @Override
  152. public byte[] readFile(String uri, String refName, String path)
  153. throws GitAPIException, IOException {
  154. Repository repo = uriRepoMap.get(uri);
  155. String idStr = refName + ":" + path;
  156. ObjectId id = repo.resolve(idStr);
  157. if (id == null) {
  158. throw new RefNotFoundException(
  159. String.format("repo %s does not have %s", repo.toString(), idStr));
  160. }
  161. try (ObjectReader reader = repo.newObjectReader()) {
  162. return reader.open(id).getCachedBytes(Integer.MAX_VALUE);
  163. }
  164. }
  165. }
  166. @Test
  167. public void androidSetup() throws Exception {
  168. Repository child = Git.cloneRepository()
  169. .setURI(groupADb.getDirectory().toURI().toString())
  170. .setDirectory(createUniqueTestGitDir(true)).setBare(true).call()
  171. .getRepository();
  172. Repository dest = Git.cloneRepository()
  173. .setURI(db.getDirectory().toURI().toString())
  174. .setDirectory(createUniqueTestGitDir(true)).setBare(true).call()
  175. .getRepository();
  176. assertTrue(dest.isBare());
  177. assertTrue(child.isBare());
  178. StringBuilder xmlContent = new StringBuilder();
  179. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  180. .append("<manifest>")
  181. .append("<remote name=\"remote1\" fetch=\"..\" />")
  182. .append("<default revision=\"master\" remote=\"remote1\" />")
  183. .append("<project path=\"base\" name=\"platform/base\" />")
  184. .append("</manifest>");
  185. RepoCommand cmd = new RepoCommand(dest);
  186. IndexedRepos repos = new IndexedRepos();
  187. repos.put("platform/base", child);
  188. RevCommit commit = cmd
  189. .setInputStream(new ByteArrayInputStream(
  190. xmlContent.toString().getBytes(UTF_8)))
  191. .setRemoteReader(repos)
  192. .setURI("platform/")
  193. .setTargetURI("platform/superproject")
  194. .setRecordRemoteBranch(true)
  195. .setRecordSubmoduleLabels(true)
  196. .call();
  197. String idStr = commit.getId().name() + ":" + ".gitmodules";
  198. ObjectId modId = dest.resolve(idStr);
  199. try (ObjectReader reader = dest.newObjectReader()) {
  200. byte[] bytes = reader.open(modId).getCachedBytes(Integer.MAX_VALUE);
  201. Config base = new Config();
  202. BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
  203. String subUrl = cfg.getString("submodule", "base", "url");
  204. assertEquals(subUrl, "../base");
  205. }
  206. child.close();
  207. dest.close();
  208. }
  209. @Test
  210. public void gerritSetup() throws Exception {
  211. Repository child =
  212. Git.cloneRepository().setURI(groupADb.getDirectory().toURI().toString())
  213. .setDirectory(createUniqueTestGitDir(true))
  214. .setBare(true).call().getRepository();
  215. Repository dest = Git.cloneRepository()
  216. .setURI(db.getDirectory().toURI().toString()).setDirectory(createUniqueTestGitDir(true))
  217. .setBare(true).call().getRepository();
  218. assertTrue(dest.isBare());
  219. assertTrue(child.isBare());
  220. StringBuilder xmlContent = new StringBuilder();
  221. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  222. .append("<manifest>")
  223. .append("<remote name=\"remote1\" fetch=\".\" />")
  224. .append("<default revision=\"master\" remote=\"remote1\" />")
  225. .append("<project path=\"plugins/cookbook\" name=\"plugins/cookbook\" />")
  226. .append("</manifest>");
  227. RepoCommand cmd = new RepoCommand(dest);
  228. IndexedRepos repos = new IndexedRepos();
  229. repos.put("plugins/cookbook", child);
  230. RevCommit commit = cmd
  231. .setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(UTF_8)))
  232. .setRemoteReader(repos)
  233. .setURI("")
  234. .setTargetURI("gerrit")
  235. .setRecordRemoteBranch(true)
  236. .setRecordSubmoduleLabels(true)
  237. .call();
  238. String idStr = commit.getId().name() + ":" + ".gitmodules";
  239. ObjectId modId = dest.resolve(idStr);
  240. try (ObjectReader reader = dest.newObjectReader()) {
  241. byte[] bytes = reader.open(modId).getCachedBytes(Integer.MAX_VALUE);
  242. Config base = new Config();
  243. BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
  244. String subUrl = cfg.getString("submodule", "plugins/cookbook", "url");
  245. assertEquals(subUrl, "../plugins/cookbook");
  246. }
  247. child.close();
  248. dest.close();
  249. }
  250. @Test
  251. public void absoluteRemoteURL() throws Exception {
  252. Repository child =
  253. Git.cloneRepository().setURI(groupADb.getDirectory().toURI().toString())
  254. .setDirectory(createUniqueTestGitDir(true))
  255. .setBare(true).call().getRepository();
  256. Repository dest = Git.cloneRepository()
  257. .setURI(db.getDirectory().toURI().toString()).setDirectory(createUniqueTestGitDir(true))
  258. .setBare(true).call().getRepository();
  259. String abs = "https://chromium.googlesource.com";
  260. String repoUrl = "https://chromium.googlesource.com/chromium/src";
  261. boolean fetchSlash = false;
  262. boolean baseSlash = false;
  263. do {
  264. do {
  265. String fetchUrl = fetchSlash ? abs + "/" : abs;
  266. String baseUrl = baseSlash ? abs + "/" : abs;
  267. StringBuilder xmlContent = new StringBuilder();
  268. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  269. .append("<manifest>")
  270. .append("<remote name=\"origin\" fetch=\"" + fetchUrl + "\" />")
  271. .append("<default revision=\"master\" remote=\"origin\" />")
  272. .append("<project path=\"src\" name=\"chromium/src\" />")
  273. .append("</manifest>");
  274. RepoCommand cmd = new RepoCommand(dest);
  275. IndexedRepos repos = new IndexedRepos();
  276. repos.put(repoUrl, child);
  277. RevCommit commit = cmd
  278. .setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(UTF_8)))
  279. .setRemoteReader(repos)
  280. .setURI(baseUrl)
  281. .setTargetURI("gerrit")
  282. .setRecordRemoteBranch(true)
  283. .setRecordSubmoduleLabels(true)
  284. .call();
  285. String idStr = commit.getId().name() + ":" + ".gitmodules";
  286. ObjectId modId = dest.resolve(idStr);
  287. try (ObjectReader reader = dest.newObjectReader()) {
  288. byte[] bytes = reader.open(modId).getCachedBytes(Integer.MAX_VALUE);
  289. Config base = new Config();
  290. BlobBasedConfig cfg = new BlobBasedConfig(base, bytes);
  291. String subUrl = cfg.getString("submodule", "src", "url");
  292. assertEquals("https://chromium.googlesource.com/chromium/src", subUrl);
  293. }
  294. fetchSlash = !fetchSlash;
  295. } while (fetchSlash);
  296. baseSlash = !baseSlash;
  297. } while (baseSlash);
  298. child.close();
  299. dest.close();
  300. }
  301. @Test
  302. public void testAddRepoManifest() throws Exception {
  303. StringBuilder xmlContent = new StringBuilder();
  304. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  305. .append("<manifest>")
  306. .append("<remote name=\"remote1\" fetch=\".\" />")
  307. .append("<default revision=\"master\" remote=\"remote1\" />")
  308. .append("<project path=\"foo\" name=\"")
  309. .append(defaultUri)
  310. .append("\" />")
  311. .append("</manifest>");
  312. writeTrashFile("manifest.xml", xmlContent.toString());
  313. RepoCommand command = new RepoCommand(db);
  314. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  315. .setURI(rootUri)
  316. .call();
  317. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  318. assertTrue("submodule should be checked out", hello.exists());
  319. BufferedReader reader = new BufferedReader(new FileReader(hello));
  320. String content = reader.readLine();
  321. reader.close();
  322. assertEquals("submodule content should be as expected",
  323. "master world", content);
  324. }
  325. @Test
  326. public void testRepoManifestGroups() throws Exception {
  327. StringBuilder xmlContent = new StringBuilder();
  328. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  329. .append("<manifest>")
  330. .append("<remote name=\"remote1\" fetch=\".\" />")
  331. .append("<default revision=\"master\" remote=\"remote1\" />")
  332. .append("<project path=\"foo\" name=\"")
  333. .append(defaultUri)
  334. .append("\" groups=\"a,test\" />")
  335. .append("<project path=\"bar\" name=\"")
  336. .append(notDefaultUri)
  337. .append("\" groups=\"notdefault\" />")
  338. .append("<project path=\"a\" name=\"")
  339. .append(groupAUri)
  340. .append("\" groups=\"a\" />")
  341. .append("<project path=\"b\" name=\"")
  342. .append(groupBUri)
  343. .append("\" groups=\"b\" />")
  344. .append("</manifest>");
  345. // default should have foo, a & b
  346. Repository localDb = createWorkRepository();
  347. JGitTestUtil.writeTrashFile(
  348. localDb, "manifest.xml", xmlContent.toString());
  349. RepoCommand command = new RepoCommand(localDb);
  350. command
  351. .setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  352. .setURI(rootUri)
  353. .call();
  354. File file = new File(localDb.getWorkTree(), "foo/hello.txt");
  355. assertTrue("default should have foo", file.exists());
  356. file = new File(localDb.getWorkTree(), "bar/world.txt");
  357. assertFalse("default shouldn't have bar", file.exists());
  358. file = new File(localDb.getWorkTree(), "a/a.txt");
  359. assertTrue("default should have a", file.exists());
  360. file = new File(localDb.getWorkTree(), "b/b.txt");
  361. assertTrue("default should have b", file.exists());
  362. // all,-a should have bar & b
  363. localDb = createWorkRepository();
  364. JGitTestUtil.writeTrashFile(
  365. localDb, "manifest.xml", xmlContent.toString());
  366. command = new RepoCommand(localDb);
  367. command
  368. .setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  369. .setURI(rootUri)
  370. .setGroups("all,-a")
  371. .call();
  372. file = new File(localDb.getWorkTree(), "foo/hello.txt");
  373. assertFalse("\"all,-a\" shouldn't have foo", file.exists());
  374. file = new File(localDb.getWorkTree(), "bar/world.txt");
  375. assertTrue("\"all,-a\" should have bar", file.exists());
  376. file = new File(localDb.getWorkTree(), "a/a.txt");
  377. assertFalse("\"all,-a\" shuoldn't have a", file.exists());
  378. file = new File(localDb.getWorkTree(), "b/b.txt");
  379. assertTrue("\"all,-a\" should have b", file.exists());
  380. }
  381. @Test
  382. public void testRepoManifestCopyFile() throws Exception {
  383. Repository localDb = createWorkRepository();
  384. StringBuilder xmlContent = new StringBuilder();
  385. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  386. .append("<manifest>")
  387. .append("<remote name=\"remote1\" fetch=\".\" />")
  388. .append("<default revision=\"master\" remote=\"remote1\" />")
  389. .append("<project path=\"foo\" name=\"")
  390. .append(defaultUri)
  391. .append("\">")
  392. .append("<copyfile src=\"hello.txt\" dest=\"Hello\" />")
  393. .append("</project>")
  394. .append("</manifest>");
  395. JGitTestUtil.writeTrashFile(
  396. localDb, "manifest.xml", xmlContent.toString());
  397. RepoCommand command = new RepoCommand(localDb);
  398. command
  399. .setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  400. .setURI(rootUri)
  401. .call();
  402. // The original file should exist
  403. File hello = new File(localDb.getWorkTree(), "foo/hello.txt");
  404. assertTrue("The original file should exist", hello.exists());
  405. BufferedReader reader = new BufferedReader(new FileReader(hello));
  406. String content = reader.readLine();
  407. reader.close();
  408. assertEquals("The original file should have expected content",
  409. "master world", content);
  410. // The dest file should also exist
  411. hello = new File(localDb.getWorkTree(), "Hello");
  412. assertTrue("The destination file should exist", hello.exists());
  413. reader = new BufferedReader(new FileReader(hello));
  414. content = reader.readLine();
  415. reader.close();
  416. assertEquals("The destination file should have expected content",
  417. "master world", content);
  418. }
  419. @Test
  420. public void testBareRepo() throws Exception {
  421. Repository remoteDb = createBareRepository();
  422. Repository tempDb = createWorkRepository();
  423. StringBuilder xmlContent = new StringBuilder();
  424. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  425. .append("<manifest>")
  426. .append("<remote name=\"remote1\" fetch=\".\" />")
  427. .append("<default revision=\"master\" remote=\"remote1\" />")
  428. .append("<project path=\"foo\" name=\"").append(defaultUri)
  429. .append("\" />").append("</manifest>");
  430. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  431. xmlContent.toString());
  432. RepoCommand command = new RepoCommand(remoteDb);
  433. command.setPath(
  434. tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  435. .setURI(rootUri).call();
  436. // Clone it
  437. File directory = createTempDirectory("testBareRepo");
  438. Repository localDb = Git.cloneRepository().setDirectory(directory)
  439. .setURI(remoteDb.getDirectory().toURI().toString()).call()
  440. .getRepository();
  441. // The .gitmodules file should exist
  442. File gitmodules = new File(localDb.getWorkTree(), ".gitmodules");
  443. assertTrue("The .gitmodules file should exist", gitmodules.exists());
  444. // The first line of .gitmodules file should be expected
  445. BufferedReader reader = new BufferedReader(new FileReader(gitmodules));
  446. String content = reader.readLine();
  447. reader.close();
  448. assertEquals("The first line of .gitmodules file should be as expected",
  449. "[submodule \"foo\"]", content);
  450. // The gitlink should be the same as remote head sha1
  451. String gitlink = localDb.resolve(Constants.HEAD + ":foo").name();
  452. localDb.close();
  453. String remote = defaultDb.resolve(Constants.HEAD).name();
  454. assertEquals("The gitlink should be the same as remote head", remote,
  455. gitlink);
  456. }
  457. @Test
  458. public void testRevision() throws Exception {
  459. StringBuilder xmlContent = new StringBuilder();
  460. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  461. .append("<manifest>")
  462. .append("<remote name=\"remote1\" fetch=\".\" />")
  463. .append("<default revision=\"master\" remote=\"remote1\" />")
  464. .append("<project path=\"foo\" name=\"")
  465. .append(defaultUri)
  466. .append("\" revision=\"")
  467. .append(oldCommitId.name())
  468. .append("\" />")
  469. .append("</manifest>");
  470. writeTrashFile("manifest.xml", xmlContent.toString());
  471. RepoCommand command = new RepoCommand(db);
  472. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  473. .setURI(rootUri)
  474. .call();
  475. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  476. BufferedReader reader = new BufferedReader(new FileReader(hello));
  477. String content = reader.readLine();
  478. reader.close();
  479. assertEquals("submodule content should be as expected",
  480. "branch world", content);
  481. }
  482. @Test
  483. public void testRevisionBranch() throws Exception {
  484. StringBuilder xmlContent = new StringBuilder();
  485. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  486. .append("<manifest>")
  487. .append("<remote name=\"remote1\" fetch=\".\" />")
  488. .append("<default revision=\"")
  489. .append(BRANCH)
  490. .append("\" remote=\"remote1\" />")
  491. .append("<project path=\"foo\" name=\"")
  492. .append(defaultUri)
  493. .append("\" />")
  494. .append("</manifest>");
  495. writeTrashFile("manifest.xml", xmlContent.toString());
  496. RepoCommand command = new RepoCommand(db);
  497. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  498. .setURI(rootUri)
  499. .call();
  500. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  501. BufferedReader reader = new BufferedReader(new FileReader(hello));
  502. String content = reader.readLine();
  503. reader.close();
  504. assertEquals("submodule content should be as expected",
  505. "branch world", content);
  506. }
  507. @Test
  508. public void testRevisionTag() throws Exception {
  509. StringBuilder xmlContent = new StringBuilder();
  510. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  511. .append("<manifest>")
  512. .append("<remote name=\"remote1\" fetch=\".\" />")
  513. .append("<default revision=\"master\" remote=\"remote1\" />")
  514. .append("<project path=\"foo\" name=\"")
  515. .append(defaultUri)
  516. .append("\" revision=\"")
  517. .append(TAG)
  518. .append("\" />")
  519. .append("</manifest>");
  520. writeTrashFile("manifest.xml", xmlContent.toString());
  521. RepoCommand command = new RepoCommand(db);
  522. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  523. .setURI(rootUri)
  524. .call();
  525. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  526. BufferedReader reader = new BufferedReader(new FileReader(hello));
  527. String content = reader.readLine();
  528. reader.close();
  529. assertEquals("submodule content should be as expected",
  530. "branch world", content);
  531. }
  532. @Test
  533. public void testRevisionBare() throws Exception {
  534. Repository remoteDb = createBareRepository();
  535. Repository tempDb = createWorkRepository();
  536. StringBuilder xmlContent = new StringBuilder();
  537. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  538. .append("<manifest>")
  539. .append("<remote name=\"remote1\" fetch=\".\" />")
  540. .append("<default revision=\"").append(BRANCH)
  541. .append("\" remote=\"remote1\" />")
  542. .append("<project path=\"foo\" name=\"").append(defaultUri)
  543. .append("\" />").append("</manifest>");
  544. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  545. xmlContent.toString());
  546. RepoCommand command = new RepoCommand(remoteDb);
  547. command.setPath(
  548. tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  549. .setURI(rootUri).call();
  550. // Clone it
  551. File directory = createTempDirectory("testRevisionBare");
  552. Repository localDb = Git.cloneRepository().setDirectory(directory)
  553. .setURI(remoteDb.getDirectory().toURI().toString()).call()
  554. .getRepository();
  555. // The gitlink should be the same as oldCommitId
  556. String gitlink = localDb.resolve(Constants.HEAD + ":foo").name();
  557. localDb.close();
  558. assertEquals("The gitlink is same as remote head", oldCommitId.name(),
  559. gitlink);
  560. }
  561. @Test
  562. public void testCopyFileBare() throws Exception {
  563. Repository remoteDb = createBareRepository();
  564. Repository tempDb = createWorkRepository();
  565. StringBuilder xmlContent = new StringBuilder();
  566. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  567. .append("<manifest>")
  568. .append("<remote name=\"remote1\" fetch=\".\" />")
  569. .append("<default revision=\"master\" remote=\"remote1\" />")
  570. .append("<project path=\"foo\" name=\"").append(defaultUri)
  571. .append("\" revision=\"").append(BRANCH).append("\" >")
  572. .append("<copyfile src=\"hello.txt\" dest=\"Hello\" />")
  573. .append("<copyfile src=\"hello.txt\" dest=\"foo/Hello\" />")
  574. .append("</project>").append("</manifest>");
  575. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  576. xmlContent.toString());
  577. RepoCommand command = new RepoCommand(remoteDb);
  578. command.setPath(
  579. tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  580. .setURI(rootUri).call();
  581. // Clone it
  582. File directory = createTempDirectory("testCopyFileBare");
  583. Repository localDb = Git.cloneRepository().setDirectory(directory)
  584. .setURI(remoteDb.getDirectory().toURI().toString()).call()
  585. .getRepository();
  586. // The Hello file should exist
  587. File hello = new File(localDb.getWorkTree(), "Hello");
  588. assertTrue("The Hello file should exist", hello.exists());
  589. // The foo/Hello file should be skipped.
  590. File foohello = new File(localDb.getWorkTree(), "foo/Hello");
  591. assertFalse("The foo/Hello file should be skipped", foohello.exists());
  592. localDb.close();
  593. // The content of Hello file should be expected
  594. BufferedReader reader = new BufferedReader(new FileReader(hello));
  595. String content = reader.readLine();
  596. reader.close();
  597. assertEquals("The Hello file should have expected content",
  598. "branch world", content);
  599. }
  600. @Test
  601. public void testReplaceManifestBare() throws Exception {
  602. Repository remoteDb = createBareRepository();
  603. Repository tempDb = createWorkRepository();
  604. StringBuilder xmlContent = new StringBuilder();
  605. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  606. .append("<manifest>")
  607. .append("<remote name=\"remote1\" fetch=\".\" />")
  608. .append("<default revision=\"master\" remote=\"remote1\" />")
  609. .append("<project path=\"foo\" name=\"").append(defaultUri)
  610. .append("\" revision=\"").append(BRANCH).append("\" >")
  611. .append("<copyfile src=\"hello.txt\" dest=\"Hello\" />")
  612. .append("</project>").append("</manifest>");
  613. JGitTestUtil.writeTrashFile(tempDb, "old.xml", xmlContent.toString());
  614. RepoCommand command = new RepoCommand(remoteDb);
  615. command.setPath(tempDb.getWorkTree().getAbsolutePath() + "/old.xml")
  616. .setURI(rootUri).call();
  617. xmlContent = new StringBuilder();
  618. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  619. .append("<manifest>")
  620. .append("<remote name=\"remote1\" fetch=\".\" />")
  621. .append("<default revision=\"master\" remote=\"remote1\" />")
  622. .append("<project path=\"bar\" name=\"").append(defaultUri)
  623. .append("\" revision=\"").append(BRANCH).append("\" >")
  624. .append("<copyfile src=\"hello.txt\" dest=\"Hello.txt\" />")
  625. .append("</project>").append("</manifest>");
  626. JGitTestUtil.writeTrashFile(tempDb, "new.xml", xmlContent.toString());
  627. command = new RepoCommand(remoteDb);
  628. command.setPath(tempDb.getWorkTree().getAbsolutePath() + "/new.xml")
  629. .setURI(rootUri).call();
  630. // Clone it
  631. File directory = createTempDirectory("testReplaceManifestBare");
  632. Repository localDb = Git.cloneRepository().setDirectory(directory)
  633. .setURI(remoteDb.getDirectory().toURI().toString()).call()
  634. .getRepository();
  635. // The Hello file should not exist
  636. File hello = new File(localDb.getWorkTree(), "Hello");
  637. assertFalse("The Hello file shouldn't exist", hello.exists());
  638. // The Hello.txt file should exist
  639. File hellotxt = new File(localDb.getWorkTree(), "Hello.txt");
  640. assertTrue("The Hello.txt file should exist", hellotxt.exists());
  641. // The .gitmodules file should have 'submodule "bar"' and shouldn't
  642. // have
  643. // 'submodule "foo"' lines.
  644. File dotmodules = new File(localDb.getWorkTree(),
  645. Constants.DOT_GIT_MODULES);
  646. localDb.close();
  647. BufferedReader reader = new BufferedReader(new FileReader(dotmodules));
  648. boolean foo = false;
  649. boolean bar = false;
  650. while (true) {
  651. String line = reader.readLine();
  652. if (line == null)
  653. break;
  654. if (line.contains("submodule \"foo\""))
  655. foo = true;
  656. if (line.contains("submodule \"bar\""))
  657. bar = true;
  658. }
  659. reader.close();
  660. assertTrue("The bar submodule should exist", bar);
  661. assertFalse("The foo submodule shouldn't exist", foo);
  662. }
  663. @Test
  664. public void testRemoveOverlappingBare() throws Exception {
  665. Repository remoteDb = createBareRepository();
  666. Repository tempDb = createWorkRepository();
  667. StringBuilder xmlContent = new StringBuilder();
  668. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  669. .append("<manifest>")
  670. .append("<remote name=\"remote1\" fetch=\".\" />")
  671. .append("<default revision=\"master\" remote=\"remote1\" />")
  672. .append("<project path=\"foo/bar\" name=\"").append(groupBUri)
  673. .append("\" />").append("<project path=\"a\" name=\"")
  674. .append(groupAUri).append("\" />")
  675. .append("<project path=\"foo\" name=\"").append(defaultUri)
  676. .append("\" />").append("</manifest>");
  677. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  678. xmlContent.toString());
  679. RepoCommand command = new RepoCommand(remoteDb);
  680. command.setPath(
  681. tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  682. .setURI(rootUri).call();
  683. // Clone it
  684. File directory = createTempDirectory("testRemoveOverlappingBare");
  685. Repository localDb = Git.cloneRepository().setDirectory(directory)
  686. .setURI(remoteDb.getDirectory().toURI().toString()).call()
  687. .getRepository();
  688. // The .gitmodules file should have 'submodule "foo"' and shouldn't
  689. // have
  690. // 'submodule "foo/bar"' lines.
  691. File dotmodules = new File(localDb.getWorkTree(),
  692. Constants.DOT_GIT_MODULES);
  693. localDb.close();
  694. BufferedReader reader = new BufferedReader(new FileReader(dotmodules));
  695. boolean foo = false;
  696. boolean foobar = false;
  697. boolean a = false;
  698. while (true) {
  699. String line = reader.readLine();
  700. if (line == null)
  701. break;
  702. if (line.contains("submodule \"foo\""))
  703. foo = true;
  704. if (line.contains("submodule \"foo/bar\""))
  705. foobar = true;
  706. if (line.contains("submodule \"a\""))
  707. a = true;
  708. }
  709. reader.close();
  710. assertTrue("The foo submodule should exist", foo);
  711. assertFalse("The foo/bar submodule shouldn't exist", foobar);
  712. assertTrue("The a submodule should exist", a);
  713. }
  714. @Test
  715. public void testIncludeTag() throws Exception {
  716. Repository localDb = createWorkRepository();
  717. Repository tempDb = createWorkRepository();
  718. StringBuilder xmlContent = new StringBuilder();
  719. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  720. .append("<manifest>")
  721. .append("<include name=\"_include.xml\" />")
  722. .append("<default revision=\"master\" remote=\"remote1\" />")
  723. .append("</manifest>");
  724. JGitTestUtil.writeTrashFile(
  725. tempDb, "manifest.xml", xmlContent.toString());
  726. xmlContent = new StringBuilder();
  727. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  728. .append("<manifest>")
  729. .append("<remote name=\"remote1\" fetch=\".\" />")
  730. .append("<default revision=\"master\" remote=\"remote1\" />")
  731. .append("<project path=\"foo\" name=\"")
  732. .append(defaultUri)
  733. .append("\" />")
  734. .append("</manifest>");
  735. JGitTestUtil.writeTrashFile(
  736. tempDb, "_include.xml", xmlContent.toString());
  737. RepoCommand command = new RepoCommand(localDb);
  738. command
  739. .setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  740. .setURI(rootUri)
  741. .call();
  742. File hello = new File(localDb.getWorkTree(), "foo/hello.txt");
  743. assertTrue("submodule should be checked out", hello.exists());
  744. BufferedReader reader = new BufferedReader(new FileReader(hello));
  745. String content = reader.readLine();
  746. reader.close();
  747. assertEquals("submodule content should be as expected",
  748. "master world", content);
  749. }
  750. @Test
  751. public void testNonDefaultRemotes() throws Exception {
  752. StringBuilder xmlContent = new StringBuilder();
  753. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  754. .append("<manifest>")
  755. .append("<remote name=\"remote1\" fetch=\".\" />")
  756. .append("<remote name=\"remote2\" fetch=\"")
  757. .append(notDefaultUri)
  758. .append("\" />")
  759. .append("<default revision=\"master\" remote=\"remote1\" />")
  760. .append("<project path=\"foo\" name=\"")
  761. .append(defaultUri)
  762. .append("\" />")
  763. .append("<project path=\"bar\" name=\".\" remote=\"remote2\" />")
  764. .append("</manifest>");
  765. Repository localDb = createWorkRepository();
  766. JGitTestUtil.writeTrashFile(
  767. localDb, "manifest.xml", xmlContent.toString());
  768. RepoCommand command = new RepoCommand(localDb);
  769. command
  770. .setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  771. .setURI(rootUri)
  772. .call();
  773. File file = new File(localDb.getWorkTree(), "foo/hello.txt");
  774. assertTrue("We should have foo", file.exists());
  775. file = new File(localDb.getWorkTree(), "bar/world.txt");
  776. assertTrue("We should have bar", file.exists());
  777. }
  778. @Test
  779. public void testRemoteAlias() throws Exception {
  780. StringBuilder xmlContent = new StringBuilder();
  781. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  782. .append("<manifest>")
  783. .append("<remote name=\"remote1\" fetch=\".\" alias=\"remote2\" />")
  784. .append("<default revision=\"master\" remote=\"remote2\" />")
  785. .append("<project path=\"foo\" name=\"")
  786. .append(defaultUri)
  787. .append("\" />")
  788. .append("</manifest>");
  789. Repository localDb = createWorkRepository();
  790. JGitTestUtil.writeTrashFile(
  791. localDb, "manifest.xml", xmlContent.toString());
  792. RepoCommand command = new RepoCommand(localDb);
  793. command
  794. .setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  795. .setURI(rootUri)
  796. .call();
  797. File file = new File(localDb.getWorkTree(), "foo/hello.txt");
  798. assertTrue("We should have foo", file.exists());
  799. }
  800. @Test
  801. public void testTargetBranch() throws Exception {
  802. Repository remoteDb1 = createBareRepository();
  803. Repository remoteDb2 = createBareRepository();
  804. Repository tempDb = createWorkRepository();
  805. StringBuilder xmlContent = new StringBuilder();
  806. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  807. .append("<manifest>")
  808. .append("<remote name=\"remote1\" fetch=\".\" />")
  809. .append("<default revision=\"master\" remote=\"remote1\" />")
  810. .append("<project path=\"foo\" name=\"").append(defaultUri)
  811. .append("\" />").append("</manifest>");
  812. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  813. xmlContent.toString());
  814. RepoCommand command = new RepoCommand(remoteDb1);
  815. command.setPath(
  816. tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  817. .setURI(rootUri).setTargetBranch("test").call();
  818. ObjectId branchId = remoteDb1
  819. .resolve(Constants.R_HEADS + "test^{tree}");
  820. command = new RepoCommand(remoteDb2);
  821. command.setPath(
  822. tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  823. .setURI(rootUri).call();
  824. ObjectId defaultId = remoteDb2.resolve(Constants.HEAD + "^{tree}");
  825. assertEquals(
  826. "The tree id of branch db and default db should be the same",
  827. branchId, defaultId);
  828. }
  829. @Test
  830. public void testRecordRemoteBranch() throws Exception {
  831. Repository remoteDb = createBareRepository();
  832. Repository tempDb = createWorkRepository();
  833. StringBuilder xmlContent = new StringBuilder();
  834. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  835. .append("<manifest>")
  836. .append("<remote name=\"remote1\" fetch=\".\" />")
  837. .append("<default revision=\"master\" remote=\"remote1\" />")
  838. .append("<project path=\"with-branch\" ")
  839. .append("revision=\"master\" ").append("name=\"")
  840. .append(notDefaultUri).append("\" />")
  841. .append("<project path=\"with-long-branch\" ")
  842. .append("revision=\"refs/heads/master\" ").append("name=\"")
  843. .append(defaultUri).append("\" />").append("</manifest>");
  844. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  845. xmlContent.toString());
  846. RepoCommand command = new RepoCommand(remoteDb);
  847. command.setPath(
  848. tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  849. .setURI(rootUri).setRecordRemoteBranch(true).call();
  850. // Clone it
  851. File directory = createTempDirectory("testBareRepo");
  852. try (Repository localDb = Git.cloneRepository().setDirectory(directory)
  853. .setURI(remoteDb.getDirectory().toURI().toString()).call()
  854. .getRepository();) {
  855. // The .gitmodules file should exist
  856. File gitmodules = new File(localDb.getWorkTree(), ".gitmodules");
  857. assertTrue("The .gitmodules file should exist",
  858. gitmodules.exists());
  859. FileBasedConfig c = new FileBasedConfig(gitmodules, FS.DETECTED);
  860. c.load();
  861. assertEquals(
  862. "Recording remote branches should work for short branch descriptions",
  863. "master",
  864. c.getString("submodule", "with-branch", "branch"));
  865. assertEquals(
  866. "Recording remote branches should work for full ref specs",
  867. "refs/heads/master",
  868. c.getString("submodule", "with-long-branch", "branch"));
  869. }
  870. }
  871. @Test
  872. public void testRecordSubmoduleLabels() throws Exception {
  873. Repository remoteDb = createBareRepository();
  874. Repository tempDb = createWorkRepository();
  875. StringBuilder xmlContent = new StringBuilder();
  876. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  877. .append("<manifest>")
  878. .append("<remote name=\"remote1\" fetch=\".\" />")
  879. .append("<default revision=\"master\" remote=\"remote1\" />")
  880. .append("<project path=\"test\" ")
  881. .append("revision=\"master\" ").append("name=\"")
  882. .append(notDefaultUri).append("\" ")
  883. .append("groups=\"a1,a2\" />").append("</manifest>");
  884. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  885. xmlContent.toString());
  886. RepoCommand command = new RepoCommand(remoteDb);
  887. command.setPath(
  888. tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  889. .setURI(rootUri).setRecordSubmoduleLabels(true).call();
  890. // Clone it
  891. File directory = createTempDirectory("testBareRepo");
  892. try (Repository localDb = Git.cloneRepository().setDirectory(directory)
  893. .setURI(remoteDb.getDirectory().toURI().toString()).call()
  894. .getRepository();) {
  895. // The .gitattributes file should exist
  896. File gitattributes = new File(localDb.getWorkTree(),
  897. ".gitattributes");
  898. assertTrue("The .gitattributes file should exist",
  899. gitattributes.exists());
  900. try (BufferedReader reader = new BufferedReader(
  901. new FileReader(gitattributes));) {
  902. String content = reader.readLine();
  903. assertEquals(".gitattributes content should be as expected",
  904. "/test a1 a2", content);
  905. }
  906. }
  907. }
  908. @Test
  909. public void testRecordShallowRecommendation() throws Exception {
  910. Repository remoteDb = createBareRepository();
  911. Repository tempDb = createWorkRepository();
  912. StringBuilder xmlContent = new StringBuilder();
  913. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  914. .append("<manifest>")
  915. .append("<remote name=\"remote1\" fetch=\".\" />")
  916. .append("<default revision=\"master\" remote=\"remote1\" />")
  917. .append("<project path=\"shallow-please\" ").append("name=\"")
  918. .append(defaultUri).append("\" ").append("clone-depth=\"1\" />")
  919. .append("<project path=\"non-shallow\" ").append("name=\"")
  920. .append(defaultUri).append("\" />").append("</manifest>");
  921. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  922. xmlContent.toString());
  923. RepoCommand command = new RepoCommand(remoteDb);
  924. command.setPath(
  925. tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  926. .setURI(rootUri).setRecommendShallow(true).call();
  927. // Clone it
  928. File directory = createTempDirectory("testBareRepo");
  929. try (Repository localDb = Git.cloneRepository().setDirectory(directory)
  930. .setURI(remoteDb.getDirectory().toURI().toString()).call()
  931. .getRepository();) {
  932. // The .gitmodules file should exist
  933. File gitmodules = new File(localDb.getWorkTree(), ".gitmodules");
  934. assertTrue("The .gitmodules file should exist",
  935. gitmodules.exists());
  936. FileBasedConfig c = new FileBasedConfig(gitmodules, FS.DETECTED);
  937. c.load();
  938. assertEquals("Recording shallow configuration should work", "true",
  939. c.getString("submodule", "shallow-please", "shallow"));
  940. assertNull("Recording non shallow configuration should work",
  941. c.getString("submodule", "non-shallow", "shallow"));
  942. }
  943. }
  944. @Test
  945. public void testRemoteRevision() throws Exception {
  946. StringBuilder xmlContent = new StringBuilder();
  947. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  948. .append("<manifest>")
  949. .append("<remote name=\"remote1\" fetch=\".\" />")
  950. .append("<remote name=\"remote2\" fetch=\".\" revision=\"")
  951. .append(BRANCH)
  952. .append("\" />")
  953. .append("<default remote=\"remote1\" revision=\"master\" />")
  954. .append("<project path=\"foo\" remote=\"remote2\" name=\"")
  955. .append(defaultUri)
  956. .append("\" />")
  957. .append("</manifest>");
  958. writeTrashFile("manifest.xml", xmlContent.toString());
  959. RepoCommand command = new RepoCommand(db);
  960. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  961. .setURI(rootUri)
  962. .call();
  963. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  964. BufferedReader reader = new BufferedReader(new FileReader(hello));
  965. String content = reader.readLine();
  966. reader.close();
  967. assertEquals("submodule content should be as expected",
  968. "branch world", content);
  969. }
  970. @Test
  971. public void testDefaultRemoteRevision() throws Exception {
  972. StringBuilder xmlContent = new StringBuilder();
  973. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  974. .append("<manifest>")
  975. .append("<remote name=\"remote1\" fetch=\".\" revision=\"")
  976. .append(BRANCH)
  977. .append("\" />")
  978. .append("<default remote=\"remote1\" />")
  979. .append("<project path=\"foo\" name=\"")
  980. .append(defaultUri)
  981. .append("\" />")
  982. .append("</manifest>");
  983. writeTrashFile("manifest.xml", xmlContent.toString());
  984. RepoCommand command = new RepoCommand(db);
  985. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  986. .setURI(rootUri)
  987. .call();
  988. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  989. BufferedReader reader = new BufferedReader(new FileReader(hello));
  990. String content = reader.readLine();
  991. reader.close();
  992. assertEquals("submodule content should be as expected",
  993. "branch world", content);
  994. }
  995. private void resolveRelativeUris() {
  996. // Find the longest common prefix ends with "/" as rootUri.
  997. defaultUri = defaultDb.getDirectory().toURI().toString();
  998. notDefaultUri = notDefaultDb.getDirectory().toURI().toString();
  999. groupAUri = groupADb.getDirectory().toURI().toString();
  1000. groupBUri = groupBDb.getDirectory().toURI().toString();
  1001. int start = 0;
  1002. while (start <= defaultUri.length()) {
  1003. int newStart = defaultUri.indexOf('/', start + 1);
  1004. String prefix = defaultUri.substring(0, newStart);
  1005. if (!notDefaultUri.startsWith(prefix) ||
  1006. !groupAUri.startsWith(prefix) ||
  1007. !groupBUri.startsWith(prefix)) {
  1008. start++;
  1009. rootUri = defaultUri.substring(0, start) + "manifest";
  1010. defaultUri = defaultUri.substring(start);
  1011. notDefaultUri = notDefaultUri.substring(start);
  1012. groupAUri = groupAUri.substring(start);
  1013. groupBUri = groupBUri.substring(start);
  1014. return;
  1015. }
  1016. start = newStart;
  1017. }
  1018. }
  1019. void testRelative(String a, String b, String want) {
  1020. String got = RepoCommand.relativize(URI.create(a), URI.create(b)).toString();
  1021. if (!got.equals(want)) {
  1022. fail(String.format("relative('%s', '%s') = '%s', want '%s'", a, b, got, want));
  1023. }
  1024. }
  1025. @Test
  1026. public void relative() {
  1027. testRelative("a/b/", "a/", "../");
  1028. // Normalization:
  1029. testRelative("a/p/..//b/", "a/", "../");
  1030. testRelative("a/b", "a/", "");
  1031. testRelative("a/", "a/b/", "b/");
  1032. testRelative("a/", "a/b", "b");
  1033. testRelative("/a/b/c", "/b/c", "../../b/c");
  1034. testRelative("/abc", "bcd", "bcd");
  1035. testRelative("abc", "def", "def");
  1036. testRelative("abc", "/bcd", "/bcd");
  1037. testRelative("http://a", "a/b", "a/b");
  1038. testRelative("http://base.com/a/", "http://child.com/a/b", "http://child.com/a/b");
  1039. }
  1040. }