Skip to content

Commit 6929d09

Browse files
committed
run-resourcewatch: Don't run git commands with bash
1 parent 36a5d5b commit 6929d09

File tree

1 file changed

+23
-47
lines changed

1 file changed

+23
-47
lines changed

pkg/resourcewatch/storage/git_store.go

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -243,23 +243,26 @@ func resourceFilename(gvr schema.GroupVersionResource, namespace, name string) s
243243
return filepath.Join("namespaces", namespace, groupStr, gvr.Resource, name+".yaml")
244244
}
245245

246+
func (s *GitStorage) exec(command string, args ...string) error {
247+
osCommand := exec.Command(command, args...)
248+
osCommand.Dir = s.path
249+
output, err := osCommand.CombinedOutput()
250+
if err != nil {
251+
klog.Errorf("Ran %s %v\n%v\n\n", command, args, string(output))
252+
return err
253+
}
254+
return nil
255+
}
256+
246257
func (s *GitStorage) commitAdd(path, author, ocCommand string) error {
247258
authorString := fmt.Sprintf("%s <[email protected]>", author)
248259
commitMessage := fmt.Sprintf("added %s", ocCommand)
249-
command := fmt.Sprintf(`git add %q && git commit --author=%q -m %q`, path, authorString, commitMessage)
250260

251-
osCommand := exec.Command("bash", "-e", "-c", command)
252-
osCommand.Dir = s.path
253-
output, err := osCommand.CombinedOutput()
254-
if err != nil {
255-
klog.Errorf("Ran %v\n%v\n\n", command, string(output))
256-
// sometimes git can leave behind an index.lock. This process should be the only one working in this git repo
257-
// so simply remove the lock file.
258-
if _, statErr := os.Stat(filepath.Join(s.path, ".git/index.lock")); statErr == nil {
259-
if deleteErr := os.Remove(filepath.Join(s.path, ".git/index.lock")); deleteErr != nil {
260-
klog.Errorf("Error removing .git/index.lock: %v", deleteErr)
261-
}
262-
}
261+
if err := s.exec("git", "add", path); err != nil {
262+
return err
263+
}
264+
265+
if err := s.exec("git", "commit", "--author", authorString, "-m", commitMessage); err != nil {
263266
return err
264267
}
265268

@@ -270,31 +273,13 @@ func (s *GitStorage) commitAdd(path, author, ocCommand string) error {
270273
func (s *GitStorage) commitModify(path, author, ocCommand string) error {
271274
authorString := fmt.Sprintf("%s <[email protected]>", author)
272275
commitMessage := fmt.Sprintf("modifed %s", ocCommand)
273-
command := fmt.Sprintf(`git add %q && git commit --author=%q -m %q`, path, authorString, commitMessage)
274-
275-
osCommand := exec.Command("bash", "-e", "-c", command)
276-
osCommand.Dir = s.path
277-
output, err := osCommand.CombinedOutput()
278-
if err != nil {
279-
klog.Errorf("Ran %v\n%v\n\n", command, string(output))
280-
// sometimes git can leave behind an index.lock. This process should be the only one working in this git repo
281-
// so simply remove the lock file.
282-
if _, statErr := os.Stat(filepath.Join(s.path, ".git/index.lock")); statErr == nil {
283-
if deleteErr := os.Remove(filepath.Join(s.path, ".git/index.lock")); deleteErr != nil {
284-
klog.Errorf("Error removing .git/index.lock: %v", deleteErr)
285-
}
286-
}
287-
// if nothing changed in the modify don't keep trying over and over
288-
if strings.Contains(string(output), "nothing to commit") {
289-
klog.Info("Exiting commitModify as nothing to commit")
290-
return nil
291-
}
292276

277+
if err := s.exec("git", "add", path); err != nil {
293278
return err
294279
}
295280

296-
if output != nil {
297-
klog.Infof("Ran %v\n%v\n\n", command, string(output))
281+
if err := s.exec("git", "commit", "--author", authorString, "-m", commitMessage); err != nil {
282+
return err
298283
}
299284

300285
klog.Infof("Modified: %v -- %v updated %v", path, author, ocCommand)
@@ -305,20 +290,11 @@ func (s *GitStorage) commitRemove(path, author, ocCommand string) error {
305290
authorString := fmt.Sprintf("%s <[email protected]>", author)
306291
commitMessage := fmt.Sprintf("removed %s", ocCommand)
307292

308-
command := fmt.Sprintf(`git rm %q && git commit --author=%q -m %q`, path, authorString, commitMessage)
293+
if err := s.exec("git", "rm", path); err != nil {
294+
return err
295+
}
309296

310-
osCommand := exec.Command("bash", "-e", "-c", command)
311-
osCommand.Dir = s.path
312-
output, err := osCommand.CombinedOutput()
313-
if err != nil {
314-
klog.Errorf("Ran %v\n%v\n\n", command, string(output))
315-
// sometimes git can leave behind an index.lock. This process should be the only one working in this git repo
316-
// so simply remove the lock file.
317-
if _, statErr := os.Stat(filepath.Join(s.path, ".git/index.lock")); statErr == nil {
318-
if deleteErr := os.Remove(filepath.Join(s.path, ".git/index.lock")); deleteErr != nil {
319-
klog.Errorf("Error removing .git/index.lock: %v", deleteErr)
320-
}
321-
}
297+
if err := s.exec("git", "commit", "--author", authorString, "-m", commitMessage); err != nil {
322298
return err
323299
}
324300

0 commit comments

Comments
 (0)