5
5
// aren't pure Go either. So we need the git binary for now.
6
6
7
7
// Package git makes a git repository out of a local directory, keeps the
8
- // content committed when the directory changes, and optionaly (if a remote
9
- // repos url is provided), keep it in sync with a remote repository.
8
+ // content committed when the directory content changes, and optionaly (if
9
+ // a remote repos url is provided), keep it in sync with a remote repository.
10
10
package git
11
11
12
12
import (
13
13
"context"
14
14
"fmt"
15
+ "os"
15
16
"os/exec"
16
17
"time"
17
18
@@ -24,6 +25,9 @@ import (
24
25
var (
25
26
timeoutCommands = 60 * time .Second
26
27
checkInterval = 10 * time .Second
28
+ gitAuthor = "Katafygio"
29
+ gitEmail = "katafygio@localhost"
30
+ gitMsg = "Kubernetes cluster change"
27
31
)
28
32
29
33
var appFs = afero .NewOsFs ()
@@ -47,9 +51,9 @@ func New(config *config.KfConfig) *Store {
47
51
Logger : config .Logger ,
48
52
URL : config .GitURL ,
49
53
LocalDir : config .LocalDir ,
50
- Author : "Katafygio" , // XXX should we expose a cli option for that?
51
- Email : "katafygio@localhost" ,
52
- Msg : "Kubernetes cluster change" ,
54
+ Author : gitAuthor ,
55
+ Email : gitEmail ,
56
+ Msg : gitMsg ,
53
57
DryRun : config .DryRun ,
54
58
}
55
59
}
@@ -60,7 +64,7 @@ func (s *Store) Start() (*Store, error) {
60
64
s .stopch = make (chan struct {})
61
65
s .donech = make (chan struct {})
62
66
63
- err := s .Clone ()
67
+ err := s .CloneOrInit ()
64
68
if err != nil {
65
69
return nil , err
66
70
}
@@ -101,6 +105,7 @@ func (s *Store) Git(args ...string) error {
101
105
102
106
cmd := exec .CommandContext (ctx , "git" , args ... ) // #nosec
103
107
cmd .Dir = s .LocalDir
108
+ cmd .Env = append (os .Environ (), fmt .Sprintf ("GIT_DIR=%s/.git" , s .LocalDir ))
104
109
105
110
out , err := cmd .CombinedOutput ()
106
111
if err != nil {
@@ -121,6 +126,7 @@ func (s *Store) Status() (changed bool, err error) {
121
126
122
127
cmd := exec .CommandContext (ctx , "git" , "status" , "--porcelain" ) // #nosec
123
128
cmd .Dir = s .LocalDir
129
+ cmd .Env = append (os .Environ (), fmt .Sprintf ("GIT_DIR=%s/.git" , s .LocalDir ))
124
130
125
131
out , err := cmd .CombinedOutput ()
126
132
if err != nil {
@@ -134,8 +140,9 @@ func (s *Store) Status() (changed bool, err error) {
134
140
return false , nil
135
141
}
136
142
137
- // Clone does git clone, or git init (when there's no GitURL to clone from)
138
- func (s * Store ) Clone () (err error ) {
143
+ // CloneOrInit create a new local repository, either with "git clone" (if a GitURL
144
+ // to clone from is provided), or "git init" (in the absence of GitURL).
145
+ func (s * Store ) CloneOrInit () (err error ) {
139
146
if ! s .DryRun {
140
147
err = appFs .MkdirAll (s .LocalDir , 0700 )
141
148
if err != nil {
0 commit comments