code.gitea.io/gitea@v1.19.3/modules/git/repo_blame.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package git
     5  
     6  import (
     7  	"fmt"
     8  )
     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").AddDashesAndList(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").
    19  		AddOptionFormat("-L %d,%d", line, line).
    20  		AddOptionValues("-p", revision).
    21  		AddDashesAndList(file).RunStdString(&RunOpts{Dir: path})
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	if len(res) < 40 {
    26  		return nil, fmt.Errorf("invalid result of blame: %s", res)
    27  	}
    28  	return repo.GetCommit(res[:40])
    29  }