func assertBlobInTree(t *testing.T, repo *git.Repository, tree *git.Tree, key, value string) { e, err := tree.EntryByPath(key) if err != nil || e == nil { t.Fatalf("No blob at key %v.\n\ttree=%#v\n", key, tree) } blob, err := lookupBlob(repo, e.Id) if err != nil { t.Fatalf("No blob at key %v.\\n\terr=%v\n\ttree=%#v\n", key, err, tree) } if string(blob.Contents()) != value { t.Fatalf("blob at key %v != %v.\n\ttree=%#v\n\treal val = %v\n", key, value, tree, string(blob.Contents())) } blob.Free() }
func TreeScope(repo *git.Repository, tree *git.Tree, name string) (*git.Tree, error) { if tree == nil { return nil, fmt.Errorf("tree undefined") } name = TreePath(name) if name == "/" { // Allocate a new Tree object so that the caller // can always call Free() on the result return lookupTree(repo, tree.Id()) } entry, err := tree.EntryByPath(name) if err != nil { return nil, err } return lookupTree(repo, entry.Id) }
func TreeGet(r *git.Repository, t *git.Tree, key string) (string, error) { if t == nil { return "", os.ErrNotExist } key = TreePath(key) e, err := t.EntryByPath(key) if err != nil { return "", err } blob, err := lookupBlob(r, e.Id) if err != nil { return "", err } defer blob.Free() return string(blob.Contents()), nil }
// FindReviewers returns up to 3 of the top reviewers information as determined // by cumulative commit count across all files in `paths`. func (r *Reviewer) FindReviewers(paths []string) (string, error) { var ( rg runGuard rw *gg.RevWalk since time.Time reviewers map[string]int final Stats ) reviewers = make(map[string]int) if len(r.Since) > 0 { var err error since, err = time.Parse("2006-01-02", r.Since) if err != nil { if r.Verbose { fmt.Println("Unable to parse 'since'") } return "", err } } else { // Calculate 6 months ago from today's date and set the 'since' argument since = time.Now().AddDate(0, -6, 0) } // Cleanup defer func() { objs := [...]freeable{ rw, } for _, obj := range objs { if obj != nil { obj.Free() } } }() // Iterate through commits in the review period rg.maybeRun(func() { var err error if rw, err = r.Repo.Walk(); err != nil { rg.err = err rg.msg = "Issue opening revwalk" } rw.Sorting(gg.SortTime | gg.SortTopological) }) rg.maybeRun(func() { var err error // TODO push master, not HEAD if err = rw.PushHead(); err != nil { rg.err = err rg.msg = "Issue pushing HEAD onto revwalk" } }) // For each of our commits in the review period, see if it affects // at least one of the paths changed in the branch. If so, the commit // author is added to the count of contributors with experience with one // of the changed files in our branch. rg.maybeRun(func() { var err error // Revwalk.Iterate walks through commits until the // RevWalkIterator returns false. err = rw.Iterate(func(c *gg.Commit) bool { var ( err error tree *gg.Tree ) defer c.Free() sig := c.Committer() // Stop walking commits since we've passed 'since' if sig.When.Before(since) { return false } tree, err = c.Tree() if err != nil { rg.err = err return false } // Check desired paths to see if one exists in the commit tree for _, p := range paths { te, err := tree.EntryByPath(p) if err != nil { continue } if te != nil { k := reviewerKey(sig) if _, ok := reviewers[k]; ok { reviewers[k]++ } else { reviewers[k] = 1 } // We found a path on the commit, no need to double-count break } } return true }) if err != nil { rg.err = err rg.msg = "Error iterating through rev walk" } }) if rg.err != nil { fmt.Println(rg.msg) fmt.Println(rg.err) return "", rg.err } final = make(Stats, len(reviewers)) idx := 0 for reviewer, count := range reviewers { final[idx] = &Stat{reviewer, count} idx++ } maxStats := 3 if l := len(final); l < maxStats { maxStats = l } topN := chooseTopN(maxStats, final) var buffer bytes.Buffer for i := range topN { buffer.WriteString(topN[i].String()) buffer.WriteString("\n") } return buffer.String(), nil }
func assertBlobNotInTree(t *testing.T, repo *git.Repository, tree *git.Tree, key string) { _, err := tree.EntryByPath(key) if err == nil { t.Fatalf("Key %q still exists in tree", key) } }