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

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