Monorepo for Tangled
tangled.org
1package gitea
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "os/exec"
8 "strings"
9
10 "github.com/go-git/go-git/v5/plumbing"
11 "github.com/go-git/go-git/v5/plumbing/object"
12)
13
14func GetCommitByPathWithID(ctx context.Context, oid plumbing.Hash, repoPath, relpath string) (*object.Commit, error) {
15 if len(relpath) == 0 {
16 return nil, errors.New("relpath should not be empty")
17 }
18 // File name starts with ':' must be escaped.
19 if relpath[0] == ':' {
20 relpath = `\` + relpath
21 }
22
23 out, err := exec.CommandContext(ctx,
24 "git",
25 "-C", repoPath,
26 "log",
27 "-1",
28 "--pretty=format:%H",
29 oid.String(),
30 "--", relpath,
31 ).Output()
32 if err != nil {
33 return nil, err
34 }
35
36 rev := plumbing.NewHash(strings.TrimSpace(string(out)))
37 if rev.IsZero() {
38 return nil, fmt.Errorf("invalid commit id: %q", string(out))
39 }
40
41 return GetCommit(ctx, repoPath, rev.String())
42}