code.gitea.io/gitea@v1.22.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  // LineBlame returns the latest commit at the given line
    11  func (repo *Repository) LineBlame(revision, path, file string, line uint) (*Commit, error) {
    12  	res, _, err := NewCommand(repo.Ctx, "blame").
    13  		AddOptionFormat("-L %d,%d", line, line).
    14  		AddOptionValues("-p", revision).
    15  		AddDashesAndList(file).RunStdString(&RunOpts{Dir: path})
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  	if len(res) < 40 {
    20  		return nil, fmt.Errorf("invalid result of blame: %s", res)
    21  	}
    22  	return repo.GetCommit(res[:40])
    23  }