1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
package commit
import (
"context"
"errors"
"path/filepath"
"gitlab.com/gitlab-org/gitaly/v16/internal/git"
"gitlab.com/gitlab-org/gitaly/v16/internal/git/catfile"
"gitlab.com/gitlab-org/gitaly/v16/internal/git/log"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/storage"
"gitlab.com/gitlab-org/gitaly/v16/internal/structerr"
"gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
)
func (s *server) LastCommitForPath(ctx context.Context, in *gitalypb.LastCommitForPathRequest) (*gitalypb.LastCommitForPathResponse, error) {
if err := validateLastCommitForPathRequest(s.locator, in); err != nil {
return nil, structerr.NewInvalidArgument("%w", err)
}
resp, err := s.lastCommitForPath(ctx, in)
if err != nil {
return nil, structerr.NewInternal("%w", err)
}
return resp, nil
}
func (s *server) lastCommitForPath(ctx context.Context, in *gitalypb.LastCommitForPathRequest) (*gitalypb.LastCommitForPathResponse, error) {
path := string(in.GetPath())
if len(path) == 0 || path == "/" {
path = "."
}
repo := s.localrepo(in.GetRepository())
objectReader, cancel, err := s.catfileCache.ObjectReader(ctx, repo)
if err != nil {
return nil, err
}
defer cancel()
options := in.GetGlobalOptions()
// Preserve backwards compatibility with legacy LiteralPathspec
// flag: These protobuf changes were not shipped in 13.1, so can be
// remove before 13.2.
if in.GetLiteralPathspec() {
if options == nil {
options = &gitalypb.GlobalOptions{}
}
options.LiteralPathspecs = true
}
commit, err := log.LastCommitForPath(ctx, s.gitCmdFactory, objectReader, repo, git.Revision(in.GetRevision()), path, options)
if errors.As(err, &catfile.NotFoundError{}) {
return &gitalypb.LastCommitForPathResponse{}, nil
}
return &gitalypb.LastCommitForPathResponse{Commit: commit}, err
}
func validateLastCommitForPathRequest(locator storage.Locator, in *gitalypb.LastCommitForPathRequest) error {
if err := locator.ValidateRepository(in.GetRepository()); err != nil {
return err
}
if err := git.ValidateRevision(in.Revision); err != nil {
return err
}
path := string(in.GetPath())
switch {
case path == "", path == "/":
// We map both the empty path and "/" to instead refer to the root directory, so these are fine.
case filepath.IsAbs(path):
// Strictly speaking this is already handled by `filepath.IsLocal()`, but handling this case explicitly
// allows us to generate a better error message.
return structerr.NewInvalidArgument("path is an absolute path").WithMetadata("path", path)
case !filepath.IsLocal(path):
return structerr.NewInvalidArgument("path escapes repository").WithMetadata("path", path)
}
return nil
}
|