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.

RepoCommand.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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.eclipse.jgit.lib.Constants.DEFAULT_REMOTE_NAME;
  45. import static org.eclipse.jgit.lib.Constants.R_REMOTES;
  46. import java.io.File;
  47. import java.io.FileInputStream;
  48. import java.io.IOException;
  49. import java.io.InputStream;
  50. import java.text.MessageFormat;
  51. import java.util.ArrayList;
  52. import java.util.List;
  53. import java.util.Map;
  54. import java.util.Set;
  55. import org.eclipse.jgit.annotations.Nullable;
  56. import org.eclipse.jgit.api.Git;
  57. import org.eclipse.jgit.api.GitCommand;
  58. import org.eclipse.jgit.api.SubmoduleAddCommand;
  59. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  60. import org.eclipse.jgit.api.errors.GitAPIException;
  61. import org.eclipse.jgit.api.errors.JGitInternalException;
  62. import org.eclipse.jgit.dircache.DirCache;
  63. import org.eclipse.jgit.dircache.DirCacheBuilder;
  64. import org.eclipse.jgit.dircache.DirCacheEntry;
  65. import org.eclipse.jgit.gitrepo.ManifestParser.IncludedFileReader;
  66. import org.eclipse.jgit.gitrepo.RepoProject.CopyFile;
  67. import org.eclipse.jgit.gitrepo.internal.RepoText;
  68. import org.eclipse.jgit.internal.JGitText;
  69. import org.eclipse.jgit.lib.CommitBuilder;
  70. import org.eclipse.jgit.lib.Config;
  71. import org.eclipse.jgit.lib.Constants;
  72. import org.eclipse.jgit.lib.FileMode;
  73. import org.eclipse.jgit.lib.ObjectId;
  74. import org.eclipse.jgit.lib.ObjectInserter;
  75. import org.eclipse.jgit.lib.ObjectReader;
  76. import org.eclipse.jgit.lib.PersonIdent;
  77. import org.eclipse.jgit.lib.ProgressMonitor;
  78. import org.eclipse.jgit.lib.Ref;
  79. import org.eclipse.jgit.lib.RefDatabase;
  80. import org.eclipse.jgit.lib.RefUpdate;
  81. import org.eclipse.jgit.lib.RefUpdate.Result;
  82. import org.eclipse.jgit.lib.Repository;
  83. import org.eclipse.jgit.revwalk.RevCommit;
  84. import org.eclipse.jgit.revwalk.RevWalk;
  85. import org.eclipse.jgit.util.FileUtils;
  86. /**
  87. * A class used to execute a repo command.
  88. *
  89. * This will parse a repo XML manifest, convert it into .gitmodules file and the
  90. * repository config file.
  91. *
  92. * If called against a bare repository, it will replace all the existing content
  93. * of the repository with the contents populated from the manifest.
  94. *
  95. * repo manifest allows projects overlapping, e.g. one project's path is
  96. * "foo" and another project's path is "foo/bar". This won't
  97. * work in git submodule, so we'll skip all the sub projects
  98. * ("foo/bar" in the example) while converting.
  99. *
  100. * @see <a href="https://code.google.com/p/git-repo/">git-repo project page</a>
  101. * @since 3.4
  102. */
  103. public class RepoCommand extends GitCommand<RevCommit> {
  104. private String path;
  105. private String uri;
  106. private String groups;
  107. private String branch;
  108. private String targetBranch = Constants.HEAD;
  109. private boolean recordRemoteBranch = false;
  110. private boolean recordSubmoduleLabels = false;
  111. private boolean recordShallowSubmodules = false;
  112. private PersonIdent author;
  113. private RemoteReader callback;
  114. private InputStream inputStream;
  115. private IncludedFileReader includedReader;
  116. private boolean ignoreRemoteFailures = false;
  117. private List<RepoProject> bareProjects;
  118. private Git git;
  119. private ProgressMonitor monitor;
  120. /**
  121. * A callback to get ref sha1 of a repository from its uri.
  122. *
  123. * We provided a default implementation {@link DefaultRemoteReader} to
  124. * use ls-remote command to read the sha1 from the repository and clone the
  125. * repository to read the file. Callers may have their own quicker
  126. * implementation.
  127. *
  128. * @since 3.4
  129. */
  130. public interface RemoteReader {
  131. /**
  132. * Read a remote ref sha1.
  133. *
  134. * @param uri
  135. * The URI of the remote repository
  136. * @param ref
  137. * The ref (branch/tag/etc.) to read
  138. * @return the sha1 of the remote repository, or null if the ref does
  139. * not exist.
  140. * @throws GitAPIException
  141. */
  142. @Nullable
  143. public ObjectId sha1(String uri, String ref) throws GitAPIException;
  144. /**
  145. * Read a file from a remote repository.
  146. *
  147. * @param uri
  148. * The URI of the remote repository
  149. * @param ref
  150. * The ref (branch/tag/etc.) to read
  151. * @param path
  152. * The relative path (inside the repo) to the file to read
  153. * @return the file content.
  154. * @throws GitAPIException
  155. * @throws IOException
  156. * @since 3.5
  157. */
  158. public byte[] readFile(String uri, String ref, String path)
  159. throws GitAPIException, IOException;
  160. }
  161. /** A default implementation of {@link RemoteReader} callback. */
  162. public static class DefaultRemoteReader implements RemoteReader {
  163. public ObjectId sha1(String uri, String ref) throws GitAPIException {
  164. Map<String, Ref> map = Git
  165. .lsRemoteRepository()
  166. .setRemote(uri)
  167. .callAsMap();
  168. Ref r = RefDatabase.findRef(map, ref);
  169. return r != null ? r.getObjectId() : null;
  170. }
  171. public byte[] readFile(String uri, String ref, String path)
  172. throws GitAPIException, IOException {
  173. File dir = FileUtils.createTempDir("jgit_", ".git", null); //$NON-NLS-1$ //$NON-NLS-2$
  174. Repository repo = Git
  175. .cloneRepository()
  176. .setBare(true)
  177. .setDirectory(dir)
  178. .setURI(uri)
  179. .call()
  180. .getRepository();
  181. try {
  182. return readFileFromRepo(repo, ref, path);
  183. } finally {
  184. repo.close();
  185. FileUtils.delete(dir, FileUtils.RECURSIVE);
  186. }
  187. }
  188. /**
  189. * Read a file from the repository
  190. *
  191. * @param repo
  192. * The repository containing the file
  193. * @param ref
  194. * The ref (branch/tag/etc.) to read
  195. * @param path
  196. * The relative path (inside the repo) to the file to read
  197. * @return the file's content
  198. * @throws GitAPIException
  199. * @throws IOException
  200. * @since 3.5
  201. */
  202. protected byte[] readFileFromRepo(Repository repo,
  203. String ref, String path) throws GitAPIException, IOException {
  204. try (ObjectReader reader = repo.newObjectReader()) {
  205. ObjectId oid = repo.resolve(ref + ":" + path); //$NON-NLS-1$
  206. return reader.open(oid).getBytes(Integer.MAX_VALUE);
  207. }
  208. }
  209. }
  210. @SuppressWarnings("serial")
  211. private static class ManifestErrorException extends GitAPIException {
  212. ManifestErrorException(Throwable cause) {
  213. super(RepoText.get().invalidManifest, cause);
  214. }
  215. }
  216. @SuppressWarnings("serial")
  217. private static class RemoteUnavailableException extends GitAPIException {
  218. RemoteUnavailableException(String uri) {
  219. super(MessageFormat.format(RepoText.get().errorRemoteUnavailable, uri));
  220. }
  221. }
  222. /**
  223. * @param repo
  224. */
  225. public RepoCommand(Repository repo) {
  226. super(repo);
  227. }
  228. /**
  229. * Set path to the manifest XML file.
  230. * <p>
  231. * Calling {@link #setInputStream} will ignore the path set here.
  232. *
  233. * @param path
  234. * (with <code>/</code> as separator)
  235. * @return this command
  236. */
  237. public RepoCommand setPath(String path) {
  238. this.path = path;
  239. return this;
  240. }
  241. /**
  242. * Set the input stream to the manifest XML.
  243. * <p>
  244. * Setting inputStream will ignore the path set. It will be closed in
  245. * {@link #call}.
  246. *
  247. * @param inputStream
  248. * @return this command
  249. * @since 3.5
  250. */
  251. public RepoCommand setInputStream(InputStream inputStream) {
  252. this.inputStream = inputStream;
  253. return this;
  254. }
  255. /**
  256. * Set base URI of the pathes inside the XML
  257. *
  258. * @param uri
  259. * @return this command
  260. */
  261. public RepoCommand setURI(String uri) {
  262. this.uri = uri;
  263. return this;
  264. }
  265. /**
  266. * Set groups to sync
  267. *
  268. * @param groups groups separated by comma, examples: default|all|G1,-G2,-G3
  269. * @return this command
  270. */
  271. public RepoCommand setGroups(String groups) {
  272. this.groups = groups;
  273. return this;
  274. }
  275. /**
  276. * Set default branch.
  277. * <p>
  278. * This is generally the name of the branch the manifest file was in. If
  279. * there's no default revision (branch) specified in manifest and no
  280. * revision specified in project, this branch will be used.
  281. *
  282. * @param branch
  283. * @return this command
  284. */
  285. public RepoCommand setBranch(String branch) {
  286. this.branch = branch;
  287. return this;
  288. }
  289. /**
  290. * Set target branch.
  291. * <p>
  292. * This is the target branch of the super project to be updated. If not set,
  293. * default is HEAD.
  294. * <p>
  295. * For non-bare repositories, HEAD will always be used and this will be
  296. * ignored.
  297. *
  298. * @param branch
  299. * @return this command
  300. * @since 4.1
  301. */
  302. public RepoCommand setTargetBranch(String branch) {
  303. this.targetBranch = Constants.R_HEADS + branch;
  304. return this;
  305. }
  306. /**
  307. * Set whether the branch name should be recorded in .gitmodules.
  308. * <p>
  309. * Submodule entries in .gitmodules can include a "branch" field
  310. * to indicate what remote branch each submodule tracks.
  311. * <p>
  312. * That field is used by "git submodule update --remote" to update
  313. * to the tip of the tracked branch when asked and by Gerrit to
  314. * update the superproject when a change on that branch is merged.
  315. * <p>
  316. * Subprojects that request a specific commit or tag will not have
  317. * a branch name recorded.
  318. * <p>
  319. * Not implemented for non-bare repositories.
  320. *
  321. * @param enable Whether to record the branch name
  322. * @return this command
  323. * @since 4.2
  324. */
  325. public RepoCommand setRecordRemoteBranch(boolean enable) {
  326. this.recordRemoteBranch = enable;
  327. return this;
  328. }
  329. /**
  330. * Set whether the labels field should be recorded as a label in
  331. * .gitattributes.
  332. * <p>
  333. * Not implemented for non-bare repositories.
  334. *
  335. * @param enable Whether to record the labels in the .gitattributes
  336. * @return this command
  337. * @since 4.4
  338. */
  339. public RepoCommand setRecordSubmoduleLabels(boolean enable) {
  340. this.recordSubmoduleLabels = enable;
  341. return this;
  342. }
  343. /**
  344. * Set whether the clone-depth field should be recorded as a shallow
  345. * recommendation in .gitmodules.
  346. * <p>
  347. * Not implemented for non-bare repositories.
  348. *
  349. * @param enable Whether to record the shallow recommendation.
  350. * @return this command
  351. * @since 4.4
  352. */
  353. public RepoCommand setRecommendShallow(boolean enable) {
  354. this.recordShallowSubmodules = enable;
  355. return this;
  356. }
  357. /**
  358. * The progress monitor associated with the clone operation. By default,
  359. * this is set to <code>NullProgressMonitor</code>
  360. *
  361. * @see org.eclipse.jgit.lib.NullProgressMonitor
  362. * @param monitor
  363. * @return this command
  364. */
  365. public RepoCommand setProgressMonitor(final ProgressMonitor monitor) {
  366. this.monitor = monitor;
  367. return this;
  368. }
  369. /**
  370. * Set whether to skip projects whose commits don't exist remotely.
  371. * <p>
  372. * When set to true, we'll just skip the manifest entry and continue
  373. * on to the next one.
  374. * <p>
  375. * When set to false (default), we'll throw an error when remote
  376. * failures occur.
  377. * <p>
  378. * Not implemented for non-bare repositories.
  379. *
  380. * @param ignore Whether to ignore the remote failures.
  381. * @return this command
  382. * @since 4.3
  383. */
  384. public RepoCommand setIgnoreRemoteFailures(boolean ignore) {
  385. this.ignoreRemoteFailures = ignore;
  386. return this;
  387. }
  388. /**
  389. * Set the author/committer for the bare repository commit.
  390. * <p>
  391. * For non-bare repositories, the current user will be used and this will be
  392. * ignored.
  393. *
  394. * @param author
  395. * @return this command
  396. */
  397. public RepoCommand setAuthor(final PersonIdent author) {
  398. this.author = author;
  399. return this;
  400. }
  401. /**
  402. * Set the GetHeadFromUri callback.
  403. *
  404. * This is only used in bare repositories.
  405. *
  406. * @param callback
  407. * @return this command
  408. */
  409. public RepoCommand setRemoteReader(final RemoteReader callback) {
  410. this.callback = callback;
  411. return this;
  412. }
  413. /**
  414. * Set the IncludedFileReader callback.
  415. *
  416. * @param reader
  417. * @return this command
  418. * @since 4.0
  419. */
  420. public RepoCommand setIncludedFileReader(IncludedFileReader reader) {
  421. this.includedReader = reader;
  422. return this;
  423. }
  424. @Override
  425. public RevCommit call() throws GitAPIException {
  426. try {
  427. checkCallable();
  428. if (uri == null || uri.length() == 0)
  429. throw new IllegalArgumentException(
  430. JGitText.get().uriNotConfigured);
  431. if (inputStream == null) {
  432. if (path == null || path.length() == 0)
  433. throw new IllegalArgumentException(
  434. JGitText.get().pathNotConfigured);
  435. try {
  436. inputStream = new FileInputStream(path);
  437. } catch (IOException e) {
  438. throw new IllegalArgumentException(
  439. JGitText.get().pathNotConfigured);
  440. }
  441. }
  442. if (repo.isBare()) {
  443. bareProjects = new ArrayList<RepoProject>();
  444. if (author == null)
  445. author = new PersonIdent(repo);
  446. if (callback == null)
  447. callback = new DefaultRemoteReader();
  448. } else
  449. git = new Git(repo);
  450. ManifestParser parser = new ManifestParser(
  451. includedReader, path, branch, uri, groups, repo);
  452. try {
  453. parser.read(inputStream);
  454. for (RepoProject proj : parser.getFilteredProjects()) {
  455. addSubmodule(proj.getUrl(),
  456. proj.getPath(),
  457. proj.getRevision(),
  458. proj.getCopyFiles(),
  459. proj.getGroups(),
  460. proj.getRecommendShallow());
  461. }
  462. } catch (GitAPIException | IOException e) {
  463. throw new ManifestErrorException(e);
  464. }
  465. } finally {
  466. try {
  467. if (inputStream != null)
  468. inputStream.close();
  469. } catch (IOException e) {
  470. // Just ignore it, it's not important.
  471. }
  472. }
  473. if (repo.isBare()) {
  474. DirCache index = DirCache.newInCore();
  475. DirCacheBuilder builder = index.builder();
  476. ObjectInserter inserter = repo.newObjectInserter();
  477. try (RevWalk rw = new RevWalk(repo)) {
  478. Config cfg = new Config();
  479. StringBuilder attributes = new StringBuilder();
  480. for (RepoProject proj : bareProjects) {
  481. String name = proj.getPath();
  482. String nameUri = proj.getName();
  483. ObjectId objectId;
  484. if (ObjectId.isId(proj.getRevision())
  485. && !ignoreRemoteFailures) {
  486. objectId = ObjectId.fromString(proj.getRevision());
  487. } else {
  488. objectId = callback.sha1(nameUri, proj.getRevision());
  489. if (objectId == null) {
  490. if (ignoreRemoteFailures) {
  491. continue;
  492. }
  493. throw new RemoteUnavailableException(nameUri);
  494. }
  495. if (recordRemoteBranch) {
  496. // can be branch or tag
  497. cfg.setString("submodule", name, "branch", //$NON-NLS-1$ //$NON-NLS-2$
  498. proj.getRevision());
  499. }
  500. if (recordShallowSubmodules && proj.getRecommendShallow() != null) {
  501. // The shallow recommendation is losing information.
  502. // As the repo manifests stores the recommended
  503. // depth in the 'clone-depth' field, while
  504. // git core only uses a binary 'shallow = true/false'
  505. // hint, we'll map any depth to 'shallow = true'
  506. cfg.setBoolean("submodule", name, "shallow", //$NON-NLS-1$ //$NON-NLS-2$
  507. true);
  508. }
  509. }
  510. if (recordSubmoduleLabels) {
  511. StringBuilder rec = new StringBuilder();
  512. rec.append("/"); //$NON-NLS-1$
  513. rec.append(name);
  514. for (String group : proj.getGroups()) {
  515. rec.append(" "); //$NON-NLS-1$
  516. rec.append(group);
  517. }
  518. rec.append("\n"); //$NON-NLS-1$
  519. attributes.append(rec.toString());
  520. }
  521. cfg.setString("submodule", name, "path", name); //$NON-NLS-1$ //$NON-NLS-2$
  522. cfg.setString("submodule", name, "url", nameUri); //$NON-NLS-1$ //$NON-NLS-2$
  523. // create gitlink
  524. DirCacheEntry dcEntry = new DirCacheEntry(name);
  525. dcEntry.setObjectId(objectId);
  526. dcEntry.setFileMode(FileMode.GITLINK);
  527. builder.add(dcEntry);
  528. for (CopyFile copyfile : proj.getCopyFiles()) {
  529. byte[] src = callback.readFile(
  530. nameUri, proj.getRevision(), copyfile.src);
  531. objectId = inserter.insert(Constants.OBJ_BLOB, src);
  532. dcEntry = new DirCacheEntry(copyfile.dest);
  533. dcEntry.setObjectId(objectId);
  534. dcEntry.setFileMode(FileMode.REGULAR_FILE);
  535. builder.add(dcEntry);
  536. }
  537. }
  538. String content = cfg.toText();
  539. // create a new DirCacheEntry for .gitmodules file.
  540. final DirCacheEntry dcEntry = new DirCacheEntry(Constants.DOT_GIT_MODULES);
  541. ObjectId objectId = inserter.insert(Constants.OBJ_BLOB,
  542. content.getBytes(Constants.CHARACTER_ENCODING));
  543. dcEntry.setObjectId(objectId);
  544. dcEntry.setFileMode(FileMode.REGULAR_FILE);
  545. builder.add(dcEntry);
  546. if (recordSubmoduleLabels) {
  547. // create a new DirCacheEntry for .gitattributes file.
  548. final DirCacheEntry dcEntryAttr = new DirCacheEntry(Constants.DOT_GIT_ATTRIBUTES);
  549. ObjectId attrId = inserter.insert(Constants.OBJ_BLOB,
  550. attributes.toString().getBytes(Constants.CHARACTER_ENCODING));
  551. dcEntryAttr.setObjectId(attrId);
  552. dcEntryAttr.setFileMode(FileMode.REGULAR_FILE);
  553. builder.add(dcEntryAttr);
  554. }
  555. builder.finish();
  556. ObjectId treeId = index.writeTree(inserter);
  557. // Create a Commit object, populate it and write it
  558. ObjectId headId = repo.resolve(targetBranch + "^{commit}"); //$NON-NLS-1$
  559. CommitBuilder commit = new CommitBuilder();
  560. commit.setTreeId(treeId);
  561. if (headId != null)
  562. commit.setParentIds(headId);
  563. commit.setAuthor(author);
  564. commit.setCommitter(author);
  565. commit.setMessage(RepoText.get().repoCommitMessage);
  566. ObjectId commitId = inserter.insert(commit);
  567. inserter.flush();
  568. RefUpdate ru = repo.updateRef(targetBranch);
  569. ru.setNewObjectId(commitId);
  570. ru.setExpectedOldObjectId(headId != null ? headId : ObjectId.zeroId());
  571. Result rc = ru.update(rw);
  572. switch (rc) {
  573. case NEW:
  574. case FORCED:
  575. case FAST_FORWARD:
  576. // Successful. Do nothing.
  577. break;
  578. case REJECTED:
  579. case LOCK_FAILURE:
  580. throw new ConcurrentRefUpdateException(
  581. MessageFormat.format(
  582. JGitText.get().cannotLock, targetBranch),
  583. ru.getRef(),
  584. rc);
  585. default:
  586. throw new JGitInternalException(MessageFormat.format(
  587. JGitText.get().updatingRefFailed,
  588. targetBranch, commitId.name(), rc));
  589. }
  590. return rw.parseCommit(commitId);
  591. } catch (IOException e) {
  592. throw new ManifestErrorException(e);
  593. }
  594. } else {
  595. return git
  596. .commit()
  597. .setMessage(RepoText.get().repoCommitMessage)
  598. .call();
  599. }
  600. }
  601. private void addSubmodule(String url, String name, String revision,
  602. List<CopyFile> copyfiles, Set<String> groups, String recommendShallow)
  603. throws GitAPIException, IOException {
  604. if (repo.isBare()) {
  605. RepoProject proj = new RepoProject(url, name, revision, null, groups, recommendShallow);
  606. proj.addCopyFiles(copyfiles);
  607. bareProjects.add(proj);
  608. } else {
  609. SubmoduleAddCommand add = git
  610. .submoduleAdd()
  611. .setPath(name)
  612. .setURI(url);
  613. if (monitor != null)
  614. add.setProgressMonitor(monitor);
  615. Repository subRepo = add.call();
  616. if (revision != null) {
  617. try (Git sub = new Git(subRepo)) {
  618. sub.checkout().setName(findRef(revision, subRepo))
  619. .call();
  620. }
  621. subRepo.close();
  622. git.add().addFilepattern(name).call();
  623. }
  624. for (CopyFile copyfile : copyfiles) {
  625. copyfile.copy();
  626. git.add().addFilepattern(copyfile.dest).call();
  627. }
  628. }
  629. }
  630. private static String findRef(String ref, Repository repo)
  631. throws IOException {
  632. if (!ObjectId.isId(ref)) {
  633. Ref r = repo.exactRef(R_REMOTES + DEFAULT_REMOTE_NAME + "/" + ref); //$NON-NLS-1$
  634. if (r != null)
  635. return r.getName();
  636. }
  637. return ref;
  638. }
  639. }