Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RepoCommand.java 20KB

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