예제 #1
0
 @Override
 public void decorateFetchCommand(
     GitSCM scm, GitClient git, TaskListener listener, FetchCommand cmd)
     throws IOException, InterruptedException, GitException {
   cmd.tags(!noTags);
   cmd.timeout(timeout);
 }
  public Map<String, String> generateContents(AbstractProject<?, ?> project, GitSCM git)
      throws IOException, InterruptedException {

    Map<String, String> paramList = new LinkedHashMap<String, String>();
    // for (AbstractProject<?,?> project :
    // Hudson.getInstance().getItems(AbstractProject.class)) {
    if (project.getSomeWorkspace() == null) {
      this.errorMessage = "noWorkspace";
    }

    EnvVars environment = null;

    try {
      environment = project.getSomeBuildWithWorkspace().getEnvironment(TaskListener.NULL);
    } catch (Exception e) {
    }

    for (RemoteConfig repository : git.getRepositories()) {
      LOGGER.log(
          Level.INFO,
          "generateContents contenttype " + type + " RemoteConfig " + repository.getURIs());
      for (URIish remoteURL : repository.getURIs()) {
        GitClient newgit =
            git.createClient(
                TaskListener.NULL, environment, new Run(project) {}, project.getSomeWorkspace());
        FilePath wsDir = null;
        if (project.getSomeBuildWithWorkspace() != null) {
          wsDir = project.getSomeBuildWithWorkspace().getWorkspace();
          if (wsDir == null || !wsDir.exists()) {
            LOGGER.log(
                Level.WARNING, "generateContents create wsDir " + wsDir + " for " + remoteURL);
            wsDir.mkdirs();
            if (!wsDir.exists()) {
              LOGGER.log(Level.SEVERE, "generateContents wsDir.mkdirs() failed.");
              String errMsg = "!Failed To Create Workspace";
              return Collections.singletonMap(errMsg, errMsg);
            }
            newgit.init();
            newgit.clone(remoteURL.toASCIIString(), "origin", false, null);
            LOGGER.log(Level.INFO, "generateContents clone done");
          }
        } else {
          // probably our first build. We cannot yet fill in any
          // values.
          LOGGER.log(Level.INFO, "getSomeBuildWithWorkspace is null");
          String errMsg = "!No workspace. Please build the project at least once";
          return Collections.singletonMap(errMsg, errMsg);
        }

        long time = -System.currentTimeMillis();
        FetchCommand fetch = newgit.fetch_().from(remoteURL, repository.getFetchRefSpecs());
        fetch.execute();
        LOGGER.finest("Took " + (time + System.currentTimeMillis()) + "ms to fetch");
        if (type.equalsIgnoreCase(PARAMETER_TYPE_REVISION)) {
          List<ObjectId> oid;

          if (this.branch != null && !this.branch.isEmpty()) {
            oid = newgit.revList(this.branch);
          } else {
            oid = newgit.revListAll();
          }

          for (ObjectId noid : oid) {
            Revision r = new Revision(noid);
            paramList.put(r.getSha1String(), prettyRevisionInfo(newgit, r));
          }
        }
        if (type.equalsIgnoreCase(PARAMETER_TYPE_TAG)
            || type.equalsIgnoreCase(PARAMETER_TYPE_TAG_BRANCH)) {

          Set<String> tagSet = newgit.getTagNames(tagFilter);
          ArrayList<String> orderedTagNames;

          if (this.getSortMode().getIsSorting()) {
            orderedTagNames = sortByName(tagSet);
            if (this.getSortMode().getIsDescending()) Collections.reverse(orderedTagNames);
          } else {
            orderedTagNames = new ArrayList<String>(tagSet);
          }

          for (String tagName : orderedTagNames) {
            paramList.put(tagName, tagName);
          }
        }
        if (type.equalsIgnoreCase(PARAMETER_TYPE_BRANCH)
            || type.equalsIgnoreCase(PARAMETER_TYPE_TAG_BRANCH)) {
          time = -System.currentTimeMillis();
          Set<String> branchSet = new HashSet<String>();
          final boolean wildcard = "*".equals(branchfilter);
          for (Branch branch : newgit.getRemoteBranches()) {
            // It'd be nice if we could filter on remote branches via the GitClient,
            // but that's not an option.
            final String branchName = branch.getName();
            if (wildcard || branchName.matches(branchfilter)) {
              branchSet.add(branchName);
            }
          }
          LOGGER.finest("Took " + (time + System.currentTimeMillis()) + "ms to fetch branches");

          time = -System.currentTimeMillis();
          List<String> orderedBranchNames;
          if (this.getSortMode().getIsSorting()) {
            orderedBranchNames = sortByName(branchSet);
            if (this.getSortMode().getIsDescending()) Collections.reverse(orderedBranchNames);
          } else {
            orderedBranchNames = new ArrayList<String>(branchSet);
          }

          for (String branchName : orderedBranchNames) {
            paramList.put(branchName, branchName);
          }
          LOGGER.finest(
              "Took " + (time + System.currentTimeMillis()) + "ms to sort and add to param list.");
        }
      }
      break;
    }
    return paramList;
  }