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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
package repository
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"gitlab.com/gitlab-org/gitaly/internal/git"
"gitlab.com/gitlab-org/gitaly/internal/git/catfile"
"gitlab.com/gitlab-org/gitaly/internal/helper"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const attributesFileMode os.FileMode = 0644
func applyGitattributes(c *catfile.Batch, repoPath string, revision []byte) error {
infoPath := path.Join(repoPath, "info")
attributesPath := path.Join(infoPath, "attributes")
_, err := c.Info(string(revision))
if err != nil {
if catfile.IsNotFound(err) {
return status.Errorf(codes.InvalidArgument, "Revision doesn't exist")
}
return err
}
blobInfo, err := c.Info(fmt.Sprintf("%s:.gitattributes", revision))
if err != nil && !catfile.IsNotFound(err) {
return err
}
if catfile.IsNotFound(err) || blobInfo.Type != "blob" {
// Remove info/attributes file if there's no .gitattributes file
if err := os.Remove(attributesPath); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
// Create /info folder if it doesn't exist
if err := os.MkdirAll(infoPath, 0755); err != nil {
return err
}
tempFile, err := ioutil.TempFile(infoPath, "attributes")
if err != nil {
return status.Errorf(codes.Internal, "ApplyGitAttributes: creating temp file: %v", err)
}
defer os.Remove(tempFile.Name())
blobObj, err := c.Blob(blobInfo.Oid)
if err != nil {
return err
}
// Write attributes to temp file
if _, err := io.CopyN(tempFile, blobObj.Reader, blobInfo.Size); err != nil {
return err
}
if err := tempFile.Close(); err != nil {
return err
}
// Change the permission of tempFile as the permission of file attributesPath
if err := os.Chmod(tempFile.Name(), attributesFileMode); err != nil {
return err
}
// Rename temp file and return the result
return os.Rename(tempFile.Name(), attributesPath)
}
func (s *server) ApplyGitattributes(ctx context.Context, in *gitalypb.ApplyGitattributesRequest) (*gitalypb.ApplyGitattributesResponse, error) {
repo := in.GetRepository()
repoPath, err := helper.GetValidatedRepoPath(repo, s.storages)
if err != nil {
return nil, err
}
if err := git.ValidateRevision(in.GetRevision()); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "ApplyGitAttributes: revision: %v", err)
}
c, err := catfile.New(ctx, repo)
if err != nil {
return nil, err
}
if err := applyGitattributes(c, repoPath, in.GetRevision()); err != nil {
return nil, err
}
return &gitalypb.ApplyGitattributesResponse{}, nil
}
|