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.

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