Esempio n. 1
0
  public List<Branch> filterMatchingBranches(Collection<Branch> branches) {
    List<Branch> items = new ArrayList<Branch>();

    for (Branch b : branches) {
      if (matches(b.getName())) items.add(b);
    }

    return items;
  }
 public Branch trunk(Session session) {
   if (getBranches() != null) {
     for (Branch b : getBranches()) {
       if (Branch.TRUNK.equals(b.getName())) {
         return b;
       }
     }
   }
   return null;
 }
Esempio n. 3
0
  /**
   * Set up submodule URLs so that they correspond to the remote pertaining to the revision that has
   * been checked out.
   */
  public void setupSubmoduleUrls(Revision rev, TaskListener listener) throws GitException {
    String remote = null;

    // try to locate the remote repository from where this commit came from
    // (by using the heuristics that the branch name, if available, contains the remote name)
    // if we can figure out the remote, the other setupSubmoduleUrls method
    // look at its URL, and if it's a non-bare repository, we attempt to retrieve modules
    // from this checked out copy.
    //
    // the idea is that you have something like tree-structured repositories: at the root you have
    // corporate central repositories that you
    // ultimately push to, which all .gitmodules point to, then you have intermediate team local
    // repository,
    // which is assumed to be a non-bare repository (say, a checked out copy on a shared server
    // accessed via SSH)
    //
    // the abovementioned behaviour of the Git plugin makes it pick up submodules from this team
    // local repository,
    // not the corporate central.
    //
    // (Kohsuke: I have a bit of hesitation/doubt about such a behaviour change triggered by
    // seemingly indirect
    // evidence of whether the upstream is bare or not (not to mention the fact that you can't
    // reliably
    // figure out if the repository is bare or not just from the URL), but that's what apparently
    // has been implemented
    // and we care about the backward compatibility.)
    //
    // note that "figuring out which remote repository the commit came from" isn't a well-defined
    // question, and this is really a heuristics. The user might be telling us to build a specific
    // SHA1.
    // or maybe someone pushed directly to the workspace and so it may not correspond to any remote
    // branch.
    // so if we fail to figure this out, we back out and avoid being too clever. See JENKINS-10060
    // as an example
    // of where our trying to be too clever here is breaking stuff for people.
    for (Branch br : rev.getBranches()) {
      String b = br.getName();
      if (b != null) {
        int slash = b.indexOf('/');

        if (slash != -1) remote = getDefaultRemote(b.substring(0, slash));
      }

      if (remote != null) break;
    }

    if (remote == null) remote = getDefaultRemote();

    if (remote != null) setupSubmoduleUrls(remote, listener);
  }
 private void setupNearestBranchesAdapter() {
   Log.d(APP_TAG, "Branches after sort = " + branches);
   // initialize our list to pass to the adapter. The adapter
   // needs the names and the distances of the branches to show
   // the information
   ArrayList<Pair<String, Double>> branchesWithDistance = new ArrayList<Pair<String, Double>>();
   for (Branch branch : nearestBranchesList) {
     String name = branch.getName();
     Double distance = branch.getDistFromUser();
     Pair<String, Double> pair = new Pair<String, Double>(name, distance);
     branchesWithDistance.add(pair);
   }
   nearestBranchesAdapter = new BranchLocationAdapter(getActivity(), branchesWithDistance);
   this.setListAdapter(nearestBranchesAdapter);
 }
Esempio n. 5
0
  public List<Branch> getRemoteBranches() throws GitException, IOException {
    Repository db = getRepository();
    Map<String, Ref> refs = db.getAllRefs();
    List<Branch> branches = new ArrayList<Branch>();

    for (Ref candidate : refs.values()) {
      if (candidate.getName().startsWith(Constants.R_REMOTES)) {
        Branch buildBranch = new Branch(candidate);
        listener.getLogger().println("Seen branch in repository " + buildBranch.getName());
        branches.add(buildBranch);
      }
    }

    return branches;
  }
  /**
   * In order to determine which Revisions to build.
   *
   * <p>Does the following : 1. Find all the branch revisions 2. Filter out branches that we don't
   * care about from the revisions. Any Revisions with no interesting branches are dropped. 3. Get
   * rid of any revisions that are wholly subsumed by another revision we're considering. 4. Get rid
   * of any revisions that we've already built. 5. Sort revisions from old to new.
   *
   * <p>NB: Alternate BuildChooser implementations are possible - this may be beneficial if "only 1"
   * branch is to be built, as much of this work is irrelevant in that usecase.
   *
   * @throws IOException
   * @throws GitException
   */
  private List<Revision> getAdvancedCandidateRevisions(
      boolean isPollCall,
      TaskListener listener,
      GitUtils utils,
      BuildData data,
      BuildChooserContext context)
      throws GitException, IOException, InterruptedException {
    EnvVars env = context.getBuild().getEnvironment();

    // 1. Get all the (branch) revisions that exist
    List<Revision> revs = new ArrayList<Revision>(utils.getAllBranchRevisions());
    verbose(listener, "Starting with all the branches: {0}", revs);

    // 2. Filter out any revisions that don't contain any branches that we
    // actually care about (spec)
    for (Iterator<Revision> i = revs.iterator(); i.hasNext(); ) {
      Revision r = i.next();

      // filter out uninteresting branches
      for (Iterator<Branch> j = r.getBranches().iterator(); j.hasNext(); ) {
        Branch b = j.next();
        boolean keep = false;
        for (BranchSpec bspec : gitSCM.getBranches()) {
          if (bspec.matches(b.getName(), env)) {
            keep = true;
            break;
          }
        }

        if (!keep) {
          verbose(listener, "Ignoring {0} because it doesn''t match branch specifier", b);
          j.remove();
        }
      }

      // filter out HEAD ref if it's not the only ref
      if (r.getBranches().size() > 1) {
        for (Iterator<Branch> j = r.getBranches().iterator(); j.hasNext(); ) {
          Branch b = j.next();
          if (HEAD.matches(b.getName(), env)) {
            verbose(
                listener,
                "Ignoring {0} because there''s named branch for this revision",
                b.getName());
            j.remove();
          }
        }
      }

      if (r.getBranches().size() == 0) {
        verbose(
            listener,
            "Ignoring {0} because we don''t care about any of the branches that point to it",
            r);
        i.remove();
      }
    }

    verbose(listener, "After branch filtering: {0}", revs);

    // 3. We only want 'tip' revisions
    revs = utils.filterTipBranches(revs);
    verbose(listener, "After non-tip filtering: {0}", revs);

    // 4. Finally, remove any revisions that have already been built.
    verbose(listener, "Removing what''s already been built: {0}", data.getBuildsByBranchName());
    Revision lastBuiltRevision = data.getLastBuiltRevision();
    for (Iterator<Revision> i = revs.iterator(); i.hasNext(); ) {
      Revision r = i.next();

      if (data.hasBeenBuilt(r.getSha1())) {
        i.remove();

        // keep track of new branches pointing to the last built revision
        if (lastBuiltRevision != null && lastBuiltRevision.getSha1().equals(r.getSha1())) {
          lastBuiltRevision = r;
        }
      }
    }
    verbose(listener, "After filtering out what''s already been built: {0}", revs);

    // if we're trying to run a build (not an SCM poll) and nothing new
    // was found then just run the last build again but ensure that the branch list
    // is ordered according to the configuration. Sorting the branch list ensures
    // a deterministic value for GIT_BRANCH and allows a git-flow style workflow
    // with fast-forward merges between branches
    if (!isPollCall && revs.isEmpty() && lastBuiltRevision != null) {
      verbose(
          listener,
          "Nothing seems worth building, so falling back to the previously built revision: {0}",
          data.getLastBuiltRevision());
      return Collections.singletonList(
          utils.sortBranchesForRevision(lastBuiltRevision, gitSCM.getBranches(), env));
    }

    // 5. sort them by the date of commit, old to new
    // this ensures the fairness in scheduling.
    final List<Revision> in = revs;
    return utils.git.withRepository(
        new RepositoryCallback<List<Revision>>() {
          public List<Revision> invoke(Repository repo, VirtualChannel channel)
              throws IOException, InterruptedException {
            Collections.sort(in, new CommitTimeComparator(repo));
            return in;
          }
        });
  }