func patch(c *object.Commit, path string) ([]diffmatchpatch.Diff, error) { // get contents of the file in the commit file, err := c.File(path) if err != nil { return nil, err } content, err := file.Contents() if err != nil { return nil, err } // get contents of the file in the first parent of the commit var contentParent string iter := c.Parents() parent, err := iter.Next() if err != nil { return nil, err } file, err = parent.File(path) if err != nil { contentParent = "" } else { contentParent, err = file.Contents() if err != nil { return nil, err } } // compare the contents of parent and child return diff.Do(content, contentParent), nil }
// TODO: benchmark this making git.object.Commit.parent public instead of using // an iterator func parentsContainingPath(path string, c *object.Commit) []*object.Commit { var result []*object.Commit iter := c.Parents() for { parent, err := iter.Next() if err != nil { if err == io.EOF { return result } panic("unreachable") } if _, err := parent.File(path); err == nil { result = append(result, parent) } } }