github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/repo_blame.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package git
     7  
     8  import "fmt"
     9  
    10  // FileBlame return the Blame object of file
    11  func (repo *Repository) FileBlame(revision, path, file string) ([]byte, error) {
    12  	stdout, _, err := NewCommand(repo.Ctx, "blame", "--root", "--", file).RunStdBytes(&RunOpts{Dir: path})
    13  	return stdout, err
    14  }
    15  
    16  // LineBlame returns the latest commit at the given line
    17  func (repo *Repository) LineBlame(revision, path, file string, line uint) (*Commit, error) {
    18  	res, _, err := NewCommand(repo.Ctx, "blame", fmt.Sprintf("-L %d,%d", line, line), "-p", revision, "--", file).RunStdString(&RunOpts{Dir: path})
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  	if len(res) < 40 {
    23  		return nil, fmt.Errorf("invalid result of blame: %s", res)
    24  	}
    25  	return repo.GetCommit(res[:40])
    26  }