-
Notifications
You must be signed in to change notification settings - Fork 5
/
git.go
94 lines (86 loc) · 2.83 KB
/
git.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
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/opendevstack/ods-pipeline/internal/command"
"github.com/opendevstack/ods-pipeline/pkg/logging"
)
// gitCheckoutParams holds the parameters configuring the checkout.
type gitCheckoutParams struct {
repoURL string
bitbucketAccessToken string
recurseSubmodules string
depth string
fullRef string
}
// gitCheckout encapsulates the steps required to perform a Git checkout
func gitCheckout(p gitCheckoutParams) (err error) {
fetchArgs := []string{"--recurse-submodules=" + p.recurseSubmodules}
if p.depth != "" {
fetchArgs = append(fetchArgs, "--depth="+p.depth)
}
fetchArgs = append(fetchArgs, "origin", "--update-head-ok", "--force", p.fullRef)
steps := [][]string{
{"init"},
// Even though Tekton prepares credentials to be used for each task,
// we set the auth explicitly here. The motivation is that Tekton uses
// basic auth to pass the username/token, which fails in environments
// that have basic auth disabled for Bitbucket.
{"config",
fmt.Sprintf("http.%s.extraHeader", p.repoURL),
fmt.Sprintf("Authorization: Bearer %s", p.bitbucketAccessToken),
},
{"config",
fmt.Sprintf("http.%s/info/lfs.extraHeader", p.repoURL),
fmt.Sprintf("Authorization: Bearer %s", p.bitbucketAccessToken),
},
{"remote", "add", "origin", p.repoURL},
append([]string{"fetch"}, fetchArgs...),
{"checkout", "-f", "FETCH_HEAD"},
}
for _, args := range steps {
if err == nil {
err = runGitCmd(args...)
}
}
return
}
// runGitCmd executes git with given args.
func runGitCmd(args ...string) error {
var output bytes.Buffer
err := command.Run("git", args, []string{}, &output, &output)
if err != nil {
return fmt.Errorf("git %v: %w\n%s", args, err, output.String())
}
return nil
}
func getCommitSHA(dir string) (string, error) {
content, err := os.ReadFile(filepath.Join(dir, ".git/HEAD"))
if err != nil {
return "", err
}
return strings.TrimSpace(string(content)), nil
}
func gitLfsInUse(logger logging.LeveledLoggerInterface, dir string) (lfs bool, err error) {
stdout, stderr, err := command.RunBufferedInDir("git", []string{"lfs", "ls-files", "--all"}, dir)
if err != nil {
return false, fmt.Errorf("cannot list git lfs files: %s (%w)", stderr, err)
}
return strings.TrimSpace(string(stdout)) != "", err
}
func gitLfsEnableAndPullFiles(logger logging.LeveledLoggerInterface, dir string) (err error) {
stdout, stderr, err := command.RunBufferedInDir("git", []string{"lfs", "install"}, dir)
if err != nil {
return fmt.Errorf("lfs install: %s (%w)", stderr, err)
}
logger.Infof(string(stdout))
stdout, stderr, err = command.RunBufferedInDir("git", []string{"lfs", "pull"}, dir)
if err != nil {
return fmt.Errorf("lfs pull: %s (%w)", stderr, err)
}
logger.Infof(string(stdout))
return err
}