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

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