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

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