Skip to content
This repository has been archived by the owner on Apr 12, 2019. It is now read-only.

Commit

Permalink
Add line blame (#116)
Browse files Browse the repository at this point in the history
* Add line blame

Signed-off-by: Jonas Franz <info@jonasfranz.software>

* Use uint instead of int to prevent failures

Signed-off-by: Jonas Franz <info@jonasfranz.software>
  • Loading branch information
jonasfranz authored and appleboy committed May 13, 2018
1 parent f1ecc13 commit 258a447
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions repo_blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@

package git

import "fmt"

// FileBlame return the Blame object of file
func (repo *Repository) FileBlame(revision, path, file string) ([]byte, error) {
return NewCommand("blame", "--root", file).RunInDirBytes(path)
}

// LineBlame returns the latest commit at the given line
func (repo *Repository) LineBlame(revision, path, file string, line uint) (*Commit, error) {
res, err := NewCommand("blame", fmt.Sprintf("-L %d,%d", line, line), "-p", revision, file).RunInDir(path)
if err != nil {
return nil, err
}
if len(res) < 40 {
return nil, fmt.Errorf("invalid result of blame: %s", res)
}
return repo.GetCommit(string(res[:40]))
}

0 comments on commit 258a447

Please sign in to comment.