RegularSuperprojectWriter.java

  1. /*
  2.  * Copyright (C) 2021, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.gitrepo;

  11. import static org.eclipse.jgit.lib.Constants.DEFAULT_REMOTE_NAME;
  12. import static org.eclipse.jgit.lib.Constants.R_REMOTES;

  13. import java.io.IOException;
  14. import java.util.List;

  15. import org.eclipse.jgit.api.Git;
  16. import org.eclipse.jgit.api.SubmoduleAddCommand;
  17. import org.eclipse.jgit.api.errors.GitAPIException;
  18. import org.eclipse.jgit.gitrepo.RepoCommand.ManifestErrorException;
  19. import org.eclipse.jgit.gitrepo.RepoProject.CopyFile;
  20. import org.eclipse.jgit.gitrepo.RepoProject.LinkFile;
  21. import org.eclipse.jgit.gitrepo.internal.RepoText;
  22. import org.eclipse.jgit.internal.JGitText;
  23. import org.eclipse.jgit.lib.ObjectId;
  24. import org.eclipse.jgit.lib.ProgressMonitor;
  25. import org.eclipse.jgit.lib.Ref;
  26. import org.eclipse.jgit.lib.Repository;
  27. import org.eclipse.jgit.revwalk.RevCommit;

  28. /**
  29.  * Writes .gitmodules and gitlinks of parsed manifest projects into a regular
  30.  * repository (using git submodule commands)
  31.  *
  32.  * To write on a bare repository, use {@link BareSuperprojectWriter}
  33.  */
  34. class RegularSuperprojectWriter {

  35.     private Repository repo;

  36.     private ProgressMonitor monitor;

  37.     RegularSuperprojectWriter(Repository repo, ProgressMonitor monitor) {
  38.         this.repo = repo;
  39.         this.monitor = monitor;
  40.     }

  41.     RevCommit write(List<RepoProject> repoProjects)
  42.             throws GitAPIException {
  43.         try (Git git = new Git(repo)) {
  44.             for (RepoProject proj : repoProjects) {
  45.                 addSubmodule(proj.getName(), proj.getUrl(), proj.getPath(),
  46.                         proj.getRevision(), proj.getCopyFiles(),
  47.                         proj.getLinkFiles(), git);
  48.             }
  49.             return git.commit().setMessage(RepoText.get().repoCommitMessage)
  50.                     .call();
  51.         } catch (IOException e) {
  52.             throw new ManifestErrorException(e);
  53.         }
  54.     }

  55.     private void addSubmodule(String name, String url, String path,
  56.             String revision, List<CopyFile> copyfiles, List<LinkFile> linkfiles,
  57.             Git git) throws GitAPIException, IOException {
  58.         assert (!repo.isBare());
  59.         assert (git != null);
  60.         if (!linkfiles.isEmpty()) {
  61.             throw new UnsupportedOperationException(
  62.                     JGitText.get().nonBareLinkFilesNotSupported);
  63.         }

  64.         SubmoduleAddCommand add = git.submoduleAdd().setName(name).setPath(path)
  65.                 .setURI(url);
  66.         if (monitor != null) {
  67.             add.setProgressMonitor(monitor);
  68.         }

  69.         Repository subRepo = add.call();
  70.         if (revision != null) {
  71.             try (Git sub = new Git(subRepo)) {
  72.                 sub.checkout().setName(findRef(revision, subRepo)).call();
  73.             }
  74.             subRepo.close();
  75.             git.add().addFilepattern(path).call();
  76.         }
  77.         for (CopyFile copyfile : copyfiles) {
  78.             copyfile.copy();
  79.             git.add().addFilepattern(copyfile.dest).call();
  80.         }
  81.     }

  82.     private static String findRef(String ref, Repository repo)
  83.             throws IOException {
  84.         if (!ObjectId.isId(ref)) {
  85.             Ref r = repo.exactRef(R_REMOTES + DEFAULT_REMOTE_NAME + "/" + ref); //$NON-NLS-1$
  86.             if (r != null) {
  87.                 return r.getName();
  88.             }
  89.         }
  90.         return ref;
  91.     }
  92. }