Skip to content

Commit 862bcc6

Browse files
committed
cmd/rofl: Fix git repository initialization
1 parent 552aff6 commit 862bcc6

1 file changed

Lines changed: 50 additions & 11 deletions

File tree

cmd/rofl/mgmt.go

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,60 @@ var (
117117
cobra.CheckErr(fmt.Errorf("failed to write manifest: %w", err))
118118
}
119119

120-
// Initialize git repository if not already present.
121-
_, err = os.Stat(".git")
122-
if err != nil && errors.Is(err, os.ErrNotExist) {
123-
// Git directory doesn't exist, try to initialize.
124-
gitInitCmd := exec.Command("git", "init")
125-
if err = gitInitCmd.Run(); err != nil {
126-
fmt.Printf("Git repository not initialized: %s.\n", err)
120+
// Save current working directory.
121+
prevWD, err := os.Getwd()
122+
cobra.CheckErr(err)
123+
curWD := prevWD
124+
125+
// Find root of git repository if present.
126+
var foundGit bool
127+
var curPath string
128+
for !foundGit {
129+
curPath = filepath.Dir(curWD)
130+
if curPath == filepath.VolumeName(os.TempDir())+string(os.PathSeparator) {
131+
// Root of filesystem reached, stop traversal.
132+
break
133+
}
134+
135+
_, err = os.Stat(filepath.Join(curPath, ".git"))
136+
if err != nil {
137+
if errors.Is(err, os.ErrNotExist) {
138+
err = os.Chdir("..")
139+
}
140+
cobra.CheckErr(err)
127141
} else {
128-
fmt.Printf("Git repository initialized.\n")
142+
foundGit = true
143+
break
129144
}
145+
146+
curWD, err = os.Getwd()
147+
cobra.CheckErr(err)
148+
}
149+
150+
// Move back to the app directory.
151+
cobra.CheckErr(os.Chdir(prevWD))
152+
153+
gitPath := "."
154+
if !foundGit {
155+
// Initialize a new git repository in the app directory.
156+
_, err = os.Stat(filepath.Join(gitPath, ".git"))
157+
if err != nil && errors.Is(err, os.ErrNotExist) {
158+
// Git directory doesn't exist, try to initialize.
159+
gitInitCmd := exec.Command("git", "init")
160+
if err = gitInitCmd.Run(); err != nil {
161+
fmt.Printf("Git repository not initialized: %s.\n", err)
162+
} else {
163+
fmt.Printf("Git repository initialized.\n")
164+
}
165+
}
166+
} else {
167+
// Git repository already exists.
168+
gitPath = curPath
130169
}
131170

132-
// Initialize .gitignore.
171+
// Initialize .gitignore in the appropriate path.
133172
needToAddOrcIgnore := true
134-
gitignore, err := os.Open(".gitignore")
173+
gitignore, err := os.Open(filepath.Join(gitPath, ".gitignore"))
135174
if err == nil {
136175
s := bufio.NewScanner(gitignore)
137176
for s.Scan() {
@@ -144,7 +183,7 @@ var (
144183
_ = gitignore.Close()
145184
}
146185
if needToAddOrcIgnore {
147-
gitignore, err := os.OpenFile(".gitignore", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
186+
gitignore, err := os.OpenFile(filepath.Join(gitPath, ".gitignore"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
148187
if err == nil {
149188
_, _ = gitignore.Write([]byte("*.orc\n"))
150189
_ = gitignore.Close()

0 commit comments

Comments
 (0)