/
service.go
163 lines (134 loc) · 3.17 KB
/
service.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
// Copyright 2024 Daytona Platforms Inc.
// SPDX-License-Identifier: Apache-2.0
package git
import (
"bytes"
"os"
"github.com/daytonaio/daytona/pkg/serverapiclient"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"gopkg.in/ini.v1"
)
type Service struct {
ProjectDir string
GitConfigFileName string
}
func (s *Service) CloneRepository(project *serverapiclient.Project, authToken *string) error {
cloneOptions := &git.CloneOptions{
URL: *project.Repository.Url,
Progress: os.Stdout,
SingleBranch: true,
InsecureSkipTLS: true,
}
if authToken != nil {
cloneOptions.Auth = &http.BasicAuth{
Username: "daytona",
Password: *authToken,
}
}
if s.shouldCloneBranch(project) {
cloneOptions.ReferenceName = plumbing.ReferenceName("refs/heads/" + *project.Repository.Branch)
}
_, err := git.PlainClone(s.ProjectDir, false, cloneOptions)
if err != nil {
return err
}
if s.shouldCheckoutSha(project) {
repo, err := git.PlainOpen(s.ProjectDir)
if err != nil {
return err
}
w, err := repo.Worktree()
if err != nil {
return err
}
err = w.Checkout(&git.CheckoutOptions{
Hash: plumbing.NewHash(*project.Repository.Sha),
})
if err != nil {
return err
}
}
return err
}
func (s *Service) RepositoryExists(project *serverapiclient.Project) (bool, error) {
_, err := os.Stat(s.ProjectDir)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func (s *Service) SetGitConfig(userData *serverapiclient.GitUser) error {
gitConfigFileName := s.GitConfigFileName
var gitConfigContent []byte
gitConfigContent, err := os.ReadFile(gitConfigFileName)
if err != nil {
gitConfigContent = []byte{}
}
cfg, err := ini.Load(gitConfigContent)
if err != nil {
return err
}
if !cfg.HasSection("credential") {
_, err := cfg.NewSection("credential")
if err != nil {
return err
}
}
_, err = cfg.Section("credential").NewKey("helper", "/usr/local/bin/daytona git-cred")
if err != nil {
return err
}
if userData != nil {
if !cfg.HasSection("user") {
_, err := cfg.NewSection("user")
if err != nil {
return err
}
}
if userData.Name != nil {
_, err := cfg.Section("user").NewKey("name", *userData.Name)
if err != nil {
return err
}
}
if userData.Email != nil {
_, err := cfg.Section("user").NewKey("email", *userData.Email)
if err != nil {
return err
}
}
}
var buf bytes.Buffer
_, err = cfg.WriteTo(&buf)
if err != nil {
return err
}
err = os.WriteFile(gitConfigFileName, buf.Bytes(), 0644)
if err != nil {
return err
}
return nil
}
func (s *Service) shouldCloneBranch(project *serverapiclient.Project) bool {
if project.Repository.Branch == nil {
return false
}
if project.Repository.Sha == nil {
return true
}
return *project.Repository.Branch == *project.Repository.Sha
}
func (s *Service) shouldCheckoutSha(project *serverapiclient.Project) bool {
if project.Repository.Sha == nil {
return false
}
if project.Repository.Branch == nil {
return true
}
return *project.Repository.Branch == *project.Repository.Sha
}