/
work_tree.go
315 lines (261 loc) · 10.4 KB
/
work_tree.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package true_git
import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/werf/lockgate"
"github.com/werf/logboek"
"github.com/werf/werf/pkg/util"
"github.com/werf/werf/pkg/util/timestamps"
"github.com/werf/werf/pkg/werf"
)
var ErrInvalidDotGit = errors.New("invalid file format: expected gitdir record")
type WithWorkTreeOptions struct {
HasSubmodules bool
}
func WithWorkTree(ctx context.Context, gitDir, workTreeCacheDir string, commit string, opts WithWorkTreeOptions, f func(workTreeDir string) error) error {
return withWorkTreeCacheLock(ctx, workTreeCacheDir, func() error {
var err error
gitDir, err = filepath.Abs(gitDir)
if err != nil {
return fmt.Errorf("bad git dir %s: %w", gitDir, err)
}
workTreeCacheDir, err = filepath.Abs(workTreeCacheDir)
if err != nil {
return fmt.Errorf("bad work tree cache dir %s: %w", workTreeCacheDir, err)
}
workTreeDir, err := prepareWorkTree(ctx, gitDir, workTreeCacheDir, commit, opts.HasSubmodules)
if err != nil {
return fmt.Errorf("cannot prepare worktree: %w", err)
}
return f(workTreeDir)
})
}
func withWorkTreeCacheLock(ctx context.Context, workTreeCacheDir string, f func() error) error {
lockName := fmt.Sprintf("git_work_tree_cache %s", workTreeCacheDir)
return werf.WithHostLock(ctx, lockName, lockgate.AcquireOptions{Timeout: 600 * time.Second}, f)
}
func prepareWorkTree(ctx context.Context, repoDir, workTreeCacheDir string, commit string, withSubmodules bool) (string, error) {
if err := os.MkdirAll(workTreeCacheDir, os.ModePerm); err != nil {
return "", fmt.Errorf("unable to create dir %s: %w", workTreeCacheDir, err)
}
lastAccessAtPath := filepath.Join(workTreeCacheDir, "last_access_at")
if err := timestamps.WriteTimestampFile(lastAccessAtPath, time.Now()); err != nil {
return "", fmt.Errorf("error writing timestamp file %q: %w", lastAccessAtPath, err)
}
gitDirPath := filepath.Join(workTreeCacheDir, "git_dir")
if _, err := os.Stat(gitDirPath); os.IsNotExist(err) {
if err := ioutil.WriteFile(gitDirPath, []byte(repoDir+"\n"), 0o644); err != nil {
return "", fmt.Errorf("error writing %s: %w", gitDirPath, err)
}
} else if err != nil {
return "", fmt.Errorf("unable to access %s: %w", gitDirPath, err)
}
workTreeDir := filepath.Join(workTreeCacheDir, "worktree")
currentCommit := ""
currentCommitPath := filepath.Join(workTreeCacheDir, "current_commit")
_, err := os.Stat(workTreeDir)
switch {
case os.IsNotExist(err):
case err != nil:
return "", fmt.Errorf("error accessing %q: %w", workTreeDir, err)
default:
resolvedWorkTreeDir, err := filepath.EvalSymlinks(workTreeDir)
if err != nil {
return "", fmt.Errorf("unable to eval symlinks of %q: %w", workTreeDir, err)
}
isWorkTreeRegistered := false
if workTreeList, err := GetWorkTreeList(ctx, repoDir); err != nil {
return "", fmt.Errorf("unable to get worktree list for repo %s: %w", repoDir, err)
} else {
for _, workTreeDesc := range workTreeList {
if filepath.ToSlash(workTreeDesc.Path) == filepath.ToSlash(resolvedWorkTreeDir) {
isWorkTreeRegistered = true
}
}
}
if !isWorkTreeRegistered {
logboek.Context(ctx).Default().LogFDetails("Detected unregistered work tree dir %q of repo %s\n", workTreeDir, repoDir)
}
isWorkTreeConsistent, err := verifyWorkTreeConsistency(ctx, repoDir, workTreeDir)
if err != nil {
return "", fmt.Errorf("unable to verify work tree %q consistency: %w", workTreeDir, err)
}
if !isWorkTreeConsistent {
logboek.Context(ctx).Default().LogFDetails("Detected inconsistent work tree dir %q of repo %s\n", workTreeDir, repoDir)
}
if !isWorkTreeRegistered || !isWorkTreeConsistent {
logboek.Context(ctx).Default().LogF("Removing invalidated work tree dir %q of repo %s\n", workTreeDir, repoDir)
if err := os.RemoveAll(currentCommitPath); err != nil {
return "", fmt.Errorf("unable to remove %s: %w", currentCommitPath, err)
}
if err := os.RemoveAll(workTreeDir); err != nil {
return "", fmt.Errorf("unable to remove invalidated work tree dir %s: %w", workTreeDir, err)
}
} else {
currentCommitPathExists := true
if _, err := os.Stat(currentCommitPath); os.IsNotExist(err) {
currentCommitPathExists = false
} else if err != nil {
return "", fmt.Errorf("unable to access %s: %w", currentCommitPath, err)
}
if currentCommitPathExists {
if data, err := ioutil.ReadFile(currentCommitPath); err == nil {
currentCommit = strings.TrimSpace(string(data))
if currentCommit == commit {
return workTreeDir, nil
}
} else {
return "", fmt.Errorf("error reading %s: %w", currentCommitPath, err)
}
if err := os.RemoveAll(currentCommitPath); err != nil {
return "", fmt.Errorf("unable to remove %s: %w", currentCommitPath, err)
}
}
}
}
// Switch worktree state to the desired commit.
// If worktree already exists — it will be used as a cache.
logProcessMsg := fmt.Sprintf("Switch work tree %s to commit %s", workTreeDir, commit)
if err := logboek.Context(ctx).Info().LogProcess(logProcessMsg).DoError(func() error {
logboek.Context(ctx).Info().LogFDetails("Work tree dir: %s\n", workTreeDir)
logboek.Context(ctx).Info().LogFDetails("Commit: %s\n", commit)
if currentCommit != "" {
logboek.Context(ctx).Info().LogFDetails("Current commit: %s\n", currentCommit)
}
return switchWorkTree(ctx, repoDir, workTreeDir, commit, withSubmodules)
}); err != nil {
return "", fmt.Errorf("unable to switch work tree %s to commit %s: %w", workTreeDir, commit, err)
}
if err := ioutil.WriteFile(currentCommitPath, []byte(commit+"\n"), 0o644); err != nil {
return "", fmt.Errorf("error writing %s: %w", currentCommitPath, err)
}
return workTreeDir, nil
}
func verifyWorkTreeConsistency(ctx context.Context, repoDir, workTreeDir string) (bool, error) {
resolvedGitDir, err := resolveDotGitFile(ctx, filepath.Join(workTreeDir, ".git"))
if err != nil {
return false, fmt.Errorf("unable to resolve dot-git file %q: %w", filepath.Join(workTreeDir, ".git"), err)
}
if !util.IsSubpathOfBasePath(repoDir, resolvedGitDir) {
return false, nil
}
_, err = os.Stat(resolvedGitDir)
switch {
case os.IsNotExist(err):
return false, nil
case err != nil:
return false, fmt.Errorf("error accessing resolved dot git dir %q: %w", resolvedGitDir, err)
}
return true, nil
}
func resolveDotGitFile(ctx context.Context, dotGitPath string) (string, error) {
data, err := os.ReadFile(dotGitPath)
if err != nil {
return "", fmt.Errorf("error reading %q: %w", dotGitPath, err)
}
lines := util.SplitLines(string(data))
if len(lines) == 0 {
goto InvalidDotGit
}
if !strings.HasPrefix(lines[0], "gitdir: ") {
goto InvalidDotGit
}
return strings.TrimSpace(strings.TrimPrefix(lines[0], "gitdir: ")), nil
InvalidDotGit:
return "", ErrInvalidDotGit
}
func switchWorkTree(ctx context.Context, repoDir, workTreeDir string, commit string, withSubmodules bool) error {
_, err := os.Stat(workTreeDir)
switch {
case os.IsNotExist(err):
wtAddCmd := NewGitCmd(ctx, &GitCmdOptions{RepoDir: repoDir}, "worktree", "add", "--force", "--detach", workTreeDir, commit)
if err = wtAddCmd.Run(ctx); err != nil {
return fmt.Errorf("git worktree add command failed: %w", err)
}
case err != nil:
return fmt.Errorf("error accessing %s: %w", workTreeDir, err)
default:
checkoutCmd := NewGitCmd(ctx, &GitCmdOptions{RepoDir: workTreeDir}, "checkout", "--force", "--detach", commit)
if err = checkoutCmd.Run(ctx); err != nil {
return fmt.Errorf("git checkout command failed: %w", err)
}
}
resetCmd := NewGitCmd(ctx, &GitCmdOptions{RepoDir: workTreeDir}, "reset", "--hard", commit)
if err = resetCmd.Run(ctx); err != nil {
return fmt.Errorf("git reset command failed: %w", err)
}
cleanCmd := NewGitCmd(ctx, &GitCmdOptions{RepoDir: workTreeDir}, "--work-tree", workTreeDir, "clean", "-d", "-f", "-f", "-x")
if err = cleanCmd.Run(ctx); err != nil {
return fmt.Errorf("git worktree clean command failed: %w", err)
}
if withSubmodules {
if err = syncSubmodules(ctx, repoDir, workTreeDir); err != nil {
return fmt.Errorf("cannot sync submodules: %w", err)
}
if err = updateSubmodules(ctx, repoDir, workTreeDir); err != nil {
return fmt.Errorf("cannot update submodules: %w", err)
}
submResetArgs := []string{
"--work-tree", workTreeDir, "submodule", "foreach", "--recursive",
}
submResetArgs = append(submResetArgs, append([]string{"git"}, append(getCommonGitOptions(), "reset", "--hard")...)...)
submResetCmd := NewGitCmd(ctx, &GitCmdOptions{RepoDir: workTreeDir}, submResetArgs...)
if err = submResetCmd.Run(ctx); err != nil {
return fmt.Errorf("git submodules reset commands failed: %w", err)
}
submCleanArgs := []string{
"--work-tree", workTreeDir, "submodule", "foreach", "--recursive",
}
submCleanArgs = append(submCleanArgs, append([]string{"git"}, append(getCommonGitOptions(), "clean", "-d", "-f", "-f", "-x")...)...)
submCleanCmd := NewGitCmd(ctx, &GitCmdOptions{RepoDir: workTreeDir}, submCleanArgs...)
if err = submCleanCmd.Run(ctx); err != nil {
return fmt.Errorf("git submodules clean commands failed: %w", err)
}
}
return nil
}
func ResolveRepoDir(ctx context.Context, repoDir string) (string, error) {
revParseCmd := NewGitCmd(ctx, nil, "--git-dir", repoDir, "rev-parse", "--git-dir")
if err := revParseCmd.Run(ctx); err != nil {
return "", fmt.Errorf("git parse git-dir command failed: %w", err)
}
return strings.TrimSpace(revParseCmd.OutBuf.String()), nil
}
type WorktreeDescriptor struct {
Path string
Head string
Branch string
}
func GetWorkTreeList(ctx context.Context, repoDir string) ([]WorktreeDescriptor, error) {
wtListCmd := NewGitCmd(ctx, &GitCmdOptions{RepoDir: repoDir}, "worktree", "list", "--porcelain")
if err := wtListCmd.Run(ctx); err != nil {
return nil, fmt.Errorf("git worktree list command failed: %w", err)
}
var worktreeDesc *WorktreeDescriptor
var res []WorktreeDescriptor
for _, line := range strings.Split(wtListCmd.OutBuf.String(), "\n") {
if line == "" && worktreeDesc == nil {
continue
} else if worktreeDesc == nil {
worktreeDesc = &WorktreeDescriptor{}
}
switch {
case strings.HasPrefix(line, "worktree "):
worktreeDesc.Path = strings.TrimPrefix(line, "worktree ")
case strings.HasPrefix(line, "HEAD "):
worktreeDesc.Head = strings.TrimPrefix(line, "HEAD ")
case strings.HasPrefix(line, "branch "):
worktreeDesc.Branch = strings.TrimPrefix(line, "branch ")
case line == "":
res = append(res, *worktreeDesc)
worktreeDesc = nil
}
}
return res, nil
}