Exemple #1
0
  /**
   * find out which branch that specified commit come from.
   *
   * @param commit
   * @return branch name.
   * @throws GitException
   */
  public String getBranchName(RevCommit commit) throws GitException {
    Set<Entry<String, Ref>> refs = repository.getAllRefs().entrySet();
    if (this.masterRef == null) {
      this.masterRef = this.findMasterRef();
    }
    boolean commitBelongsToBranch;
    boolean commitBelongsToMaster;
    List<String> foundInBranches = new ArrayList<String>();
    try {
      RevWalk walker = new RevWalk(this.repository);
      RevCommit targetCommit = walker.parseCommit(this.repository.resolve(commit.getName()));
      for (Entry<String, Ref> ref : refs) {
        if (ref.getKey().startsWith(Constants.R_HEADS) && !ref.getKey().equals(this.masterRef)) {
          commitBelongsToBranch =
              walker.isMergedInto(targetCommit, walker.parseCommit(ref.getValue().getObjectId()));
          commitBelongsToMaster = false;
          if (this.masterRef != null) {
            commitBelongsToMaster =
                walker.isMergedInto(targetCommit, walker.parseCommit(this.masterRef.getObjectId()));
          }
          if (commitBelongsToBranch && !commitBelongsToMaster) {
            foundInBranches.add(ref.getValue().getName());
          }
        }
      }

    } catch (GitException | MissingObjectException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IncorrectObjectTypeException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    if (foundInBranches.size() == 0) {
      return "master";
    }
    return StringUtils.join(foundInBranches, ", ");
  }
 /**
  * Join a collection of Strings together using the specified separator.
  *
  * @param parts Strings to join
  * @param separator used to join
  * @return a String with all the joined parts
  */
 public static String join(Collection<String> parts, String separator) {
   return StringUtils.join(parts, separator, separator);
 }