Debian Codegraph @

locking/lockable.go

raw symbols SCIP
1 package locking
2
3 import (
4 "os"
5 "path/filepath"
6 "strings"
7
8 "github.com/git-lfs/git-lfs/v3/errors"
9 "github.com/git-lfs/git-lfs/v3/filepathfilter"
10 "github.com/git-lfs/git-lfs/v3/git"
11 "github.com/git-lfs/git-lfs/v3/git/gitattr"
12 "github.com/git-lfs/git-lfs/v3/tools"
13 "github.com/git-lfs/git-lfs/v3/tr"
14 )
15
16 // GetLockablePatterns returns a list of patterns in .gitattributes which are
17 // marked as lockable
18 func (c *Client) GetLockablePatterns() []string {
19 c.ensureLockablesLoaded()
20 return c.lockablePatterns
21 }
22
23 // getLockableFilter returns the internal filter used to check if a file is lockable
24 func (c *Client) getLockableFilter() *filepathfilter.Filter {
25 c.ensureLockablesLoaded()
26 return c.lockableFilter
27 }
28
29 func (c *Client) ensureLockablesLoaded() {
30 c.lockableMutex.Lock()
31 defer c.lockableMutex.Unlock()
32
33 // Only load once
34 if c.lockablePatterns == nil {
35 c.refreshLockablePatterns()
36 }
37 }
38
39 // Internal function to repopulate lockable patterns
40 // You must have locked the c.lockableMutex in the caller
41 func (c *Client) refreshLockablePatterns() {
42 paths := gitattr.GetAttributePaths(gitattr.NewMacroProcessor(), c.LocalWorkingDir, c.LocalGitDir)
43 // Always make non-nil even if empty
44 c.lockablePatterns = make([]string, 0, len(paths))
45 for _, p := range paths {
46 if p.Lockable {
47 c.lockablePatterns = append(c.lockablePatterns, filepath.ToSlash(p.Path))
48 }
49 }
50 c.lockableFilter = filepathfilter.New(c.lockablePatterns, nil, filepathfilter.GitAttributes, c.cfg.Git, filepathfilter.DefaultValue(false))
51 }
52
53 // IsFileLockable returns whether a specific file path is marked as Lockable,
54 // ie has the 'lockable' attribute in .gitattributes
55 // Lockable patterns are cached once for performance, unless you call RefreshLockablePatterns
56 // path should be relative to repository root
57 func (c *Client) IsFileLockable(path string) bool {
58 return c.getLockableFilter().Allows(path)
59 }
60
61 // FixAllLockableFileWriteFlags recursively scans the repo looking for files which
62 // are lockable, and makes sure their write flags are set correctly based on
63 // whether they are currently locked or unlocked.
64 // Files which are unlocked are made read-only, files which are locked are made
65 // writeable.
66 // This function can be used after a clone or checkout to ensure that file
67 // state correctly reflects the locking state
68 func (c *Client) FixAllLockableFileWriteFlags() error {
69 return c.fixFileWriteFlags(c.LocalWorkingDir, c.LocalWorkingDir, c.getLockableFilter(), nil)
70 }
71
72 // FixFileWriteFlagsInDir scans dir (which can either be a relative dir
73 // from the root of the repo, or an absolute dir within the repo) looking for
74 // files to change permissions for.
75 // If lockablePatterns is non-nil, then any file matching those patterns will be
76 // checked to see if it is currently locked by the current committer, and if so
77 // it will be writeable, and if not locked it will be read-only.
78 // If unlockablePatterns is non-nil, then any file matching those patterns will
79 // be made writeable if it is not already. This can be used to reset files to
80 // writeable when their 'lockable' attribute is turned off.
81 func (c *Client) FixFileWriteFlagsInDir(dir string, lockablePatterns, unlockablePatterns []string) error {
82
83 // early-out if no patterns
84 if len(lockablePatterns) == 0 && len(unlockablePatterns) == 0 {
85 return nil
86 }
87
88 absPath := dir
89 if !filepath.IsAbs(dir) {
90 absPath = filepath.Join(c.LocalWorkingDir, dir)
91 }
92 stat, err := os.Stat(absPath)
93 if err != nil {
94 return err
95 }
96 if !stat.IsDir() {
97 return errors.New(tr.Tr.Get("%q is not a valid directory", dir))
98 }
99
100 var lockableFilter *filepathfilter.Filter
101 var unlockableFilter *filepathfilter.Filter
102 if lockablePatterns != nil {
103 lockableFilter = filepathfilter.New(lockablePatterns, nil, filepathfilter.GitAttributes, c.cfg.Git)
104 }
105 if unlockablePatterns != nil {
106 unlockableFilter = filepathfilter.New(unlockablePatterns, nil, filepathfilter.GitAttributes, c.cfg.Git)
107 }
108
109 return c.fixFileWriteFlags(absPath, c.LocalWorkingDir, lockableFilter, unlockableFilter)
110 }
111
112 // Internal implementation of fixing file write flags with precompiled filters
113 func (c *Client) fixFileWriteFlags(absPath, workingDir string, lockable, unlockable *filepathfilter.Filter) error {
114
115 // Build a list of files
116 lsFiles, err := git.NewLsFiles(workingDir, !c.ModifyIgnoredFiles, false)
117 if err != nil {
118 return err
119 }
120
121 for f := range lsFiles.Files {
122 err = c.fixSingleFileWriteFlags(f, lockable, unlockable)
123 if err != nil {
124 return err
125 }
126 }
127
128 return nil
129 }
130
131 // FixLockableFileWriteFlags checks each file in the provided list, and for
132 // those which are lockable, makes sure their write flags are set correctly
133 // based on whether they are currently locked or unlocked. Files which are
134 // unlocked are made read-only, files which are locked are made writeable.
135 // Files which are not lockable are ignored.
136 // This function can be used after a clone or checkout to ensure that file
137 // state correctly reflects the locking state, and is more efficient than
138 // FixAllLockableFileWriteFlags when you know which files changed
139 func (c *Client) FixLockableFileWriteFlags(files []string) error {
140 // early-out if no lockable patterns
141 if len(c.GetLockablePatterns()) == 0 {
142 return nil
143 }
144
145 var multiErr error
146 for _, f := range files {
147 err := c.fixSingleFileWriteFlags(f, c.getLockableFilter(), nil)
148 if err != nil {
149 multiErr = errors.Join(multiErr, err)
150 }
151 }
152
153 return multiErr
154 }
155
156 // fixSingleFileWriteFlags fixes write flags on a single file
157 // If lockablePatterns is non-nil, then any file matching those patterns will be
158 // checked to see if it is currently locked by the current committer, and if so
159 // it will be writeable, and if not locked it will be read-only.
160 // If unlockablePatterns is non-nil, then any file matching those patterns will
161 // be made writeable if it is not already. This can be used to reset files to
162 // writeable when their 'lockable' attribute is turned off.
163 func (c *Client) fixSingleFileWriteFlags(file string, lockable, unlockable *filepathfilter.Filter) error {
164 // Convert to git-style forward slash separators if necessary
165 // Necessary to match attributes
166 if filepath.Separator == '\\' {
167 file = strings.Replace(file, "\\", "/", -1)
168 }
169 if lockable != nil && lockable.Allows(file) {
170 // Lockable files are writeable only if they're currently locked
171 err := tools.SetFileWriteFlag(file, c.IsFileLockedByCurrentCommitter(file))
172 // Ignore not exist errors
173 if err != nil && !os.IsNotExist(err) {
174 return err
175 }
176 } else if unlockable != nil && unlockable.Allows(file) {
177 // Unlockable files are always writeable
178 // We only check files which match the incoming patterns to avoid
179 // checking every file in the system all the time, and only do it
180 // when a file has had its lockable attribute removed
181 err := tools.SetFileWriteFlag(file, true)
182 if err != nil && !os.IsNotExist(err) {
183 return err
184 }
185 }
186 return nil
187 }