您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RepoCommand.java 19KB

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