-
Notifications
You must be signed in to change notification settings - Fork 199
/
repository.go
89 lines (68 loc) · 2.32 KB
/
repository.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
package true_git
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/Masterminds/semver"
"github.com/go-git/go-git/v5"
"github.com/werf/werf/pkg/util"
)
func GitOpenWithCustomWorktreeDir(gitDir, worktreeDir string) (*git.Repository, error) {
return git.PlainOpenWithOptions(worktreeDir, &git.PlainOpenOptions{EnableDotGitCommonDir: true})
}
type FetchOptions struct {
All bool
TagsOnly bool
Prune bool
PruneTags bool
Unshallow bool
RefSpecs map[string]string
}
func IsShallowFileChangedSinceWeReadIt(err error) bool {
return err != nil && strings.Contains(err.Error(), "shallow file has changed since we read it")
}
func Fetch(ctx context.Context, path string, options FetchOptions) error {
commandArgs := []string{"fetch"}
if options.Unshallow {
commandArgs = append(commandArgs, "--unshallow")
}
if options.All {
commandArgs = append(commandArgs, "--all")
}
if options.TagsOnly {
commandArgs = append(commandArgs, "--tags")
}
if options.Prune || options.PruneTags {
commandArgs = append(commandArgs, "--prune")
if options.PruneTags && !gitVersion.LessThan(semver.MustParse("2.17.0")) {
commandArgs = append(commandArgs, "--prune-tags")
}
}
for remote, refSpec := range options.RefSpecs {
commandArgs = append(commandArgs, remote, refSpec)
}
gitCmd := NewGitCmd(ctx, &GitCmdOptions{RepoDir: path}, commandArgs...)
return gitCmd.Run(ctx)
}
func GetLastBranchCommitSHA(ctx context.Context, repoPath, branch string) (string, error) {
revParseCmd := NewGitCmd(ctx, &GitCmdOptions{RepoDir: repoPath}, "rev-parse", branch)
if err := revParseCmd.Run(ctx); err != nil {
return "", fmt.Errorf("git rev parse branch command failed: %w", err)
}
return strings.TrimSpace(revParseCmd.OutBuf.String()), nil
}
func IsShallowClone(ctx context.Context, path string) (bool, error) {
if gitVersion.LessThan(semver.MustParse("2.15.0")) {
exist, err := util.FileExists(filepath.Join(path, ".git", "shallow"))
if err != nil {
return false, err
}
return exist, nil
}
checkShallowCmd := NewGitCmd(ctx, &GitCmdOptions{RepoDir: path}, "rev-parse", "--is-shallow-repository")
if err := checkShallowCmd.Run(ctx); err != nil {
return false, fmt.Errorf("git shallow repository check command failed: %w", err)
}
return strings.TrimSpace(checkShallowCmd.OutBuf.String()) == "true", nil
}