0

I need to find all branches (tags) with a commit. On CLI using git command it's possible by using git branch --contains <commit> command.

How do to something like that using go-git library?

Alex Raeder
  • 661
  • 6
  • 10

1 Answers1

0

I tried to make a solution and got this:

func FindBranchesByCoommit(r *git.Repository, c *object.Commit) (branches []*plumbing.Reference, err error) {
    brIt, err := r.Branches()
    if err != nil {
        return
    }
    defer brIt.Close()

    if err := brIt.ForEach(func(ref *plumbing.Reference) error {
        com, err := r.CommitObject(ref.Hash())
        if err != nil {
            return err
        }

        comIt, err := r.Log(&git.LogOptions{From: com.Hash})
        if err != nil {
            return err
        }
        defer comIt.Close()

        for {
            if com, err := comIt.Next(); err == nil {
                if c.Hash == com.Hash {
                    branches = append(branches, ref)
                    break
                }
            } else if err == io.EOF {
                break
            } else {
                return err
            }
        }

        return nil
    }); err != nil {
        return nil, err
    }

    return
}
Alex Raeder
  • 661
  • 6
  • 10