Skip to content

Commit 29f197e

Browse files
committed
Prevent git from considering .git in parent directories
When the target directory doesn't (yet) contains .git, git will look for this .git directory recursively on parent directories. We'd rather have git to fail rather than using a random .git in an upper directory.
1 parent 33eb3d1 commit 29f197e

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

pkg/store/git/git.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
// aren't pure Go either. So we need the git binary for now.
66

77
// 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.
1010
package git
1111

1212
import (
1313
"context"
1414
"fmt"
15+
"os"
1516
"os/exec"
1617
"time"
1718

@@ -24,6 +25,9 @@ import (
2425
var (
2526
timeoutCommands = 60 * time.Second
2627
checkInterval = 10 * time.Second
28+
gitAuthor = "Katafygio"
29+
gitEmail = "katafygio@localhost"
30+
gitMsg = "Kubernetes cluster change"
2731
)
2832

2933
var appFs = afero.NewOsFs()
@@ -47,9 +51,9 @@ func New(config *config.KfConfig) *Store {
4751
Logger: config.Logger,
4852
URL: config.GitURL,
4953
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,
5357
DryRun: config.DryRun,
5458
}
5559
}
@@ -60,7 +64,7 @@ func (s *Store) Start() (*Store, error) {
6064
s.stopch = make(chan struct{})
6165
s.donech = make(chan struct{})
6266

63-
err := s.Clone()
67+
err := s.CloneOrInit()
6468
if err != nil {
6569
return nil, err
6670
}
@@ -101,6 +105,7 @@ func (s *Store) Git(args ...string) error {
101105

102106
cmd := exec.CommandContext(ctx, "git", args...) // #nosec
103107
cmd.Dir = s.LocalDir
108+
cmd.Env = append(os.Environ(), fmt.Sprintf("GIT_DIR=%s/.git", s.LocalDir))
104109

105110
out, err := cmd.CombinedOutput()
106111
if err != nil {
@@ -121,6 +126,7 @@ func (s *Store) Status() (changed bool, err error) {
121126

122127
cmd := exec.CommandContext(ctx, "git", "status", "--porcelain") // #nosec
123128
cmd.Dir = s.LocalDir
129+
cmd.Env = append(os.Environ(), fmt.Sprintf("GIT_DIR=%s/.git", s.LocalDir))
124130

125131
out, err := cmd.CombinedOutput()
126132
if err != nil {
@@ -134,8 +140,9 @@ func (s *Store) Status() (changed bool, err error) {
134140
return false, nil
135141
}
136142

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) {
139146
if !s.DryRun {
140147
err = appFs.MkdirAll(s.LocalDir, 0700)
141148
if err != nil {

pkg/store/git/git_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestGit(t *testing.T) {
106106
repo.LocalDir = newdir
107107
repo.URL = dir
108108

109-
err = repo.Clone()
109+
err = repo.CloneOrInit()
110110
if err != nil {
111111
t.Errorf("clone failed: %v", err)
112112
}

0 commit comments

Comments
 (0)