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

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