WIP: feat: cancel git clone ops gracefully
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

See #528
This commit is contained in:
decentral1se 2025-04-22 12:40:31 +02:00
parent 229e8eb9da
commit 20c1bf2c21
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410
2 changed files with 54 additions and 23 deletions

View File

@ -1,8 +1,11 @@
package git package git
import ( import (
"fmt"
"os" "os"
"os/signal"
"strings" "strings"
"syscall"
"coopcloud.tech/abra/pkg/log" "coopcloud.tech/abra/pkg/log"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
@ -24,44 +27,63 @@ func gitCloneIgnoreErr(err error) bool {
// Clone runs a git clone which accounts for different default branches. // Clone runs a git clone which accounts for different default branches.
func Clone(dir, url string) error { func Clone(dir, url string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) { sigIntCh := make(chan os.Signal)
log.Debugf("git clone: %s", url) signal.Notify(sigIntCh, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(sigIntCh)
_, err := git.PlainClone(dir, false, &git.CloneOptions{ errCh := make(chan error)
URL: url,
Tags: git.AllTags,
ReferenceName: plumbing.ReferenceName("refs/heads/main"),
SingleBranch: true,
})
if err != nil && gitCloneIgnoreErr(err) { go func() {
log.Debugf("git clone: %s cloned successfully", dir) if _, err := os.Stat(dir); os.IsNotExist(err) {
return nil log.Debugf("git clone: %s", url)
}
if err != nil {
log.Debug("git clone: main branch failed, attempting master branch")
_, err := git.PlainClone(dir, false, &git.CloneOptions{ _, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: url, URL: url,
Tags: git.AllTags, Tags: git.AllTags,
ReferenceName: plumbing.ReferenceName("refs/heads/master"), ReferenceName: plumbing.ReferenceName("refs/heads/main"),
SingleBranch: true, SingleBranch: true,
}) })
if err != nil && gitCloneIgnoreErr(err) { if err != nil && gitCloneIgnoreErr(err) {
log.Debugf("git clone: %s cloned successfully", dir) log.Debugf("git clone: %s cloned successfully", dir)
return nil errCh <- nil
} }
if err != nil { if err != nil {
return err log.Debug("git clone: main branch failed, attempting master branch")
}
}
log.Debugf("git clone: %s cloned successfully", dir) _, err := git.PlainClone(dir, false, &git.CloneOptions{
} else { URL: url,
log.Debugf("git clone: %s already exists", dir) Tags: git.AllTags,
ReferenceName: plumbing.ReferenceName("refs/heads/master"),
SingleBranch: true,
})
if err != nil && gitCloneIgnoreErr(err) {
log.Debugf("git clone: %s cloned successfully", dir)
errCh <- nil
}
if err != nil {
errCh <- err
}
}
log.Debugf("git clone: %s cloned successfully", dir)
} else {
log.Debugf("git clone: %s already exists", dir)
}
}()
select {
case <-sigIntCh:
fmt.Println() // NOTE(d1): newline after ^C
if err := os.RemoveAll(dir); err != nil {
return fmt.Errorf("unable to clean up git clone of %s: %s", url, err)
}
return fmt.Errorf("git clone %s: cancelled due to interrupt", url)
case err := <-errCh:
return err
} }
return nil return nil

9
pkg/git/clone_test.go Normal file
View File

@ -0,0 +1,9 @@
package git
import (
"testing"
)
func TestCancelGitClone(t *testing.T) {
// TODO
}