package hudson.plugins.git.util;

import hudson.model.TaskListener;
import hudson.plugins.git.Branch;
import hudson.plugins.git.IGitAPI;
import hudson.plugins.git.IndexEntry;
import hudson.plugins.git.Revision;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class GitUtils
{
  IGitAPI git;
  TaskListener listener;

  public GitUtils(TaskListener listener, IGitAPI git)
  {
    this.git = git;
    this.listener = listener;
  }

  public List<IndexEntry> getSubmodules(String treeIsh)
  {
    List<IndexEntry> submodules = git.lsTree(treeIsh);

    // Remove anything that isn't a submodule
    for (Iterator<IndexEntry> it = submodules.iterator(); it.hasNext();)
    {
      if (!it.next().getMode().equals("160000"))
      {
        it.remove();
      }
    }
    return submodules;
  }
  
  private String listBranches(Collection<Branch> list)
  {
    String txt = "";
    for (Branch branch : list)
    {
      txt += branch.getName() + " ";
    }
    return txt;
  }

  public Collection<Revision> getAllBranchRevisions()
  {
    Map<String, Revision> revisions = new HashMap<String, Revision>();
    List<Branch> branches = git.getBranches();
    for (Branch b : branches)
    {
      Revision r = revisions.get(b.getSHA1());
      if (r == null)
      {
        r = new Revision(b.getSHA1());
        revisions.put(b.getSHA1(), r);
      }
      r.getBranches().add(b);
    }
    return revisions.values();
  }
  
  /**
   * Return a list of 'tip' branches (I.E. branches that aren't included entirely within another branch).
   * 
   * @param git
   * @return
   */
  public Collection<Revision> getTipBranches()
  {
    Collection<Revision> revisions = getAllBranchRevisions();

    for (Iterator<Revision> it = revisions.iterator(); it.hasNext();)
    {
      Revision r = it.next();
      Collection<Branch> contained = git.getBranchesContaining(r.getSha1());

      boolean remove = false;

      for (Branch candidate : contained)
      {
        if (!candidate.getSHA1().equals(r.getSha1()))
        {
          it.remove();
          break;
        }
      }
      // if (candidate.getName().startsWith("origin/") && !candidate.getName().equals("origin/HEAD")) count++;
      
    }
    
    return revisions;
  }
  
 
}






@@ -878,189 +878,8 @@
 %0A  %0A
-  private String listBranches(Collection%3CBranch%3E list)%0A  %7B%0A    String txt = %22%22;%0A    for (Branch branch : list)%0A    %7B%0A      txt += branch.getName() + %22 %22;%0A    %7D%0A    return txt;%0A  %7D%0A%0A
   pu
@@ -1777,39 +1777,8 @@
 );%0A%0A
-      boolean remove = false;%0A%0A
     
@@ -1814,16 +1814,16 @@
 tained)%0A
+
       %7B%0A
@@ -1948,126 +1948,8 @@
   %7D%0A
-      // if (candidate.getName().startsWith(%22origin/%22) && !candidate.getName().equals(%22origin/HEAD%22)) count++;%0A      %0A
     
@@ -1985,16 +1985,5 @@
   %7D%0A
-  %0A %0A%7D%0A%0A%0A%0A%0A%0A
+%7D

Remove dead code