func init() { var err error defaultDiffOptions, err = git2go.DefaultDiffOptions() if err != nil { log.Fatalf("Failed to load default git (libgit2/git2go) diff options: %s.", err) } defaultDiffOptions.IdAbbrev = 40 }
// FindFiles returns a list of paths to files that have been changed // in this branch with respect to `master`. func (r *Reviewer) FindFiles() ([]string, error) { var ( rg runGuard lines []string mBranch *gg.Branch hRef *gg.Reference hCom *gg.Commit mCom *gg.Commit mTree *gg.Tree hTree *gg.Tree opts gg.DiffOptions diff *gg.Diff ) if r.Repo == nil { return lines, errors.New("repo not initialized") } defer func() { objs := [...]freeable{ mBranch, hRef, hCom, mCom, mTree, hTree, } for _, obj := range objs { if obj != nil { obj.Free() } } if err := diff.Free(); err != nil && r.Verbose { fmt.Printf("Issue cleaning up diff: '%s'\n", err) } }() rg.maybeRun(func() { var err error if mBranch, err = r.Repo.LookupBranch("master", gg.BranchLocal); err != nil { rg.err = err rg.msg = "issue opening master branch" } }) rg.maybeRun(func() { var err error if mCom, err = r.Repo.LookupCommit(mBranch.Reference.Target()); err != nil { rg.err = err rg.msg = "issue opening commit at master" } }) rg.maybeRun(func() { var err error if hRef, err = r.Repo.Head(); err != nil { rg.err = err rg.msg = "issue opening repo at HEAD" } }) rg.maybeRun(func() { var err error if hCom, err = r.Repo.LookupCommit(hRef.Target()); err != nil { rg.err = err rg.msg = "issue opening commit at HEAD" } }) rg.maybeRun(func() { var err error if mTree, err = mCom.Tree(); err != nil { rg.err = err rg.msg = "issue opening tree at master" } }) rg.maybeRun(func() { var err error if hTree, err = hCom.Tree(); err != nil { rg.err = err rg.msg = "issue opening tree at HEAD" } }) rg.maybeRun(func() { var err error if opts, err = gg.DefaultDiffOptions(); err != nil { rg.err = err rg.msg = "issue creating diff options" } }) rg.maybeRun(func() { var err error if diff, err = r.Repo.DiffTreeToTree(mTree, hTree, &opts); err != nil { rg.err = err rg.msg = "issue finding diff" } }) rg.maybeRun(func() { diff.ForEach(func(file gg.DiffDelta, progress float64) ( gg.DiffForEachHunkCallback, error) { lines = append(lines, file.OldFile.Path) return nil, nil }, gg.DiffDetailFiles) }) if rg.err != nil && rg.msg != "" && r.Verbose { fmt.Printf("Error finding diff files: '%s'\n", rg.msg) } return lines, rg.err }