示例#1
0
  /**
   * Fetch information from a particular remote repository. Attempt to fetch from submodules, if
   * they exist in the local WC
   *
   * @param git
   * @param listener
   * @param remoteRepository
   * @throws
   */
  private void fetchFrom(
      IGitAPI git, File workspace, TaskListener listener, RemoteConfig remoteRepository) {
    try {
      listener.getLogger().println("Fetching from " + remoteRepository.getName());
      git.fetch(remoteRepository);

      List<IndexEntry> submodules = new GitUtils(listener, git).getSubmodules("HEAD");

      for (IndexEntry submodule : submodules) {
        try {
          RemoteConfig submoduleRemoteRepository =
              getSubmoduleRepository(remoteRepository, submodule.getFile());

          File subdir = new File(workspace, submodule.getFile());
          IGitAPI subGit =
              new GitAPI(git.getGitExe(), new FilePath(subdir), listener, git.getEnvironment());

          subGit.fetch(submoduleRemoteRepository);
        } catch (Exception ex) {
          listener.error(
              "Problem fetching from "
                  + remoteRepository.getName()
                  + " - could be unavailable. Continuing anyway");
        }
      }
    } catch (GitException ex) {
      listener.error(
          "Problem fetching from "
              + remoteRepository.getName()
              + " / "
              + remoteRepository.getName()
              + " - could be unavailable. Continuing anyway");
    }
  }
示例#2
0
 private void saveRemote(final URIish uri) throws URISyntaxException, IOException {
   final RemoteConfig rc = new RemoteConfig(db.getConfig(), remoteName);
   rc.addURI(uri);
   rc.addFetchRefSpec(
       new RefSpec()
           .setForceUpdate(true)
           .setSourceDestination(
               Constants.R_HEADS + "*", Constants.R_REMOTES + remoteName + "/*"));
   rc.update(db.getConfig());
   db.getConfig().save();
 }
 /** {@inheritDoc} */
 public RemoteConfig legacyUnmarshal(
     final HierarchicalStreamReader reader, final UnmarshallingContext context) {
   final org.spearce.jgit.transport.RemoteConfig remoteConfig =
       new org.spearce.jgit.transport.RemoteConfig();
   CustomObjectInputStream.StreamCallback callback =
       new LegacyInputStreamCallback(reader, context, remoteConfig);
   try {
     CustomObjectInputStream objectInput =
         CustomObjectInputStream.getInstance(context, callback, null);
     remoteConfig.readExternal(objectInput);
     objectInput.popCallback();
     return remoteConfig.toRemote();
   } catch (Exception e) {
     throw new ConversionException("Unmarshal failed", e);
   }
 }
示例#4
0
  public RemoteConfig getSubmoduleRepository(RemoteConfig orig, String name) {
    // Attempt to guess the submodule URL??

    String refUrl = orig.getURIs().get(0).toPrivateString();

    if (refUrl.endsWith("/.git")) {
      refUrl = refUrl.substring(0, refUrl.length() - 4);
    }

    if (!refUrl.endsWith("/")) refUrl += "/";

    refUrl += name;

    if (!refUrl.endsWith("/")) refUrl += "/";

    refUrl += ".git";

    return newRemoteConfig(name, refUrl, orig.getFetchRefSpecs().get(0));
  }
示例#5
0
  private RemoteConfig newRemoteConfig(String name, String refUrl, RefSpec refSpec) {

    File temp = null;
    try {
      temp = File.createTempFile("tmp", "config");
      RepositoryConfig repoConfig = new RepositoryConfig(null, temp);
      // Make up a repo config from the request parameters

      repoConfig.setString("remote", name, "url", refUrl);
      repoConfig.setString("remote", name, "fetch", refSpec.toString());
      repoConfig.save();
      return RemoteConfig.getAllRemoteConfigs(repoConfig).get(0);
    } catch (Exception ex) {
      throw new GitException("Error creating temp file");
    } finally {
      if (temp != null) temp.delete();
    }
  }
示例#6
0
    public SCM newInstance(StaplerRequest req) throws FormException {
      List<RemoteConfig> remoteRepositories;
      File temp;

      try {
        temp = File.createTempFile("tmp", "config");

      } catch (IOException e1) {
        throw new GitException("Error creating repositories", e1);
      }

      RepositoryConfig repoConfig = new RepositoryConfig(null, temp);
      // Make up a repo config from the request parameters

      String[] urls = req.getParameterValues("git.repo.url");
      String[] names = req.getParameterValues("git.repo.name");

      names = GitUtils.fixupNames(names, urls);

      String[] refs = req.getParameterValues("git.repo.refspec");
      if (names != null) {
        for (int i = 0; i < names.length; i++) {
          String name = names[i];
          name = name.replace(' ', '_');

          if (refs[i] == null || refs[i].length() == 0) {
            refs[i] = "+refs/heads/*:refs/remotes/" + name + "/*";
          }

          repoConfig.setString("remote", name, "url", urls[i]);
          repoConfig.setString("remote", name, "fetch", refs[i]);
        }
      }

      try {
        repoConfig.save();
        remoteRepositories = RemoteConfig.getAllRemoteConfigs(repoConfig);
      } catch (Exception e) {
        throw new GitException("Error creating repositories", e);
      }

      temp.delete();

      List<BranchSpec> branches = new ArrayList<BranchSpec>();
      String[] branchData = req.getParameterValues("git.branch");
      for (int i = 0; i < branchData.length; i++) {
        branches.add(new BranchSpec(branchData[i]));
      }

      if (branches.size() == 0) {
        branches.add(new BranchSpec("*/master"));
      }

      PreBuildMergeOptions mergeOptions = new PreBuildMergeOptions();
      if (req.getParameter("git.mergeTarget") != null
          && req.getParameter("git.mergeTarget").trim().length() > 0) {
        mergeOptions.setMergeTarget(req.getParameter("git.mergeTarget"));
      }

      Collection<SubmoduleConfig> submoduleCfg = new ArrayList<SubmoduleConfig>();

      GitWeb gitWeb = null;
      String gitWebUrl = req.getParameter("gitweb.url");
      if (gitWebUrl != null && gitWebUrl.length() > 0) {
        try {
          gitWeb = new GitWeb(gitWebUrl);
        } catch (MalformedURLException e) {
          throw new GitException("Error creating GitWeb", e);
        }
      }

      return new GitSCM(
          remoteRepositories,
          branches,
          mergeOptions,
          req.getParameter("git.generate") != null,
          submoduleCfg,
          req.getParameter("git.clean") != null,
          gitWeb);
    }