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

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