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

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