gitea源码

1234567891011121314151617181920212223242526
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "fmt"
  6. "code.gitea.io/gitea/modules/git/gitcmd"
  7. )
  8. // LineBlame returns the latest commit at the given line
  9. func (repo *Repository) LineBlame(revision, path, file string, line uint) (*Commit, error) {
  10. res, _, err := gitcmd.NewCommand("blame").
  11. AddOptionFormat("-L %d,%d", line, line).
  12. AddOptionValues("-p", revision).
  13. AddDashesAndList(file).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: path})
  14. if err != nil {
  15. return nil, err
  16. }
  17. if len(res) < 40 {
  18. return nil, fmt.Errorf("invalid result of blame: %s", res)
  19. }
  20. return repo.GetCommit(res[:40])
  21. }