WIP: feat: cancel git clone ops gracefully
Some checks reported errors
continuous-integration/drone/push Build was killed

See #528
This commit is contained in:
2025-04-22 12:40:31 +02:00
parent 229e8eb9da
commit 706d539dc4
2 changed files with 81 additions and 23 deletions

View File

@ -1,8 +1,11 @@
package git
import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"coopcloud.tech/abra/pkg/log"
"github.com/go-git/go-git/v5"
@ -24,6 +27,13 @@ func gitCloneIgnoreErr(err error) bool {
// Clone runs a git clone which accounts for different default branches.
func Clone(dir, url string) error {
sigIntCh := make(chan os.Signal)
signal.Notify(sigIntCh, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(sigIntCh)
errCh := make(chan error)
go func() {
if _, err := os.Stat(dir); os.IsNotExist(err) {
log.Debugf("git clone: %s", url)
@ -36,7 +46,7 @@ func Clone(dir, url string) error {
if err != nil && gitCloneIgnoreErr(err) {
log.Debugf("git clone: %s cloned successfully", dir)
return nil
errCh <- nil
}
if err != nil {
@ -51,11 +61,11 @@ func Clone(dir, url string) error {
if err != nil && gitCloneIgnoreErr(err) {
log.Debugf("git clone: %s cloned successfully", dir)
return nil
errCh <- nil
}
if err != nil {
return err
errCh <- err
}
}
@ -63,6 +73,18 @@ func Clone(dir, url string) error {
} 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
}

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

@ -0,0 +1,36 @@
package git
import (
"fmt"
"os"
"path"
"syscall"
"testing"
"time"
"coopcloud.tech/abra/pkg/config"
)
func TestCancelGitClone(t *testing.T) {
go func() {
time.Sleep(time.Second * 1)
p, err := os.FindProcess(os.Getpid())
if err != nil {
t.Fatalf("unable to find current process: %s", err)
}
p.Signal(syscall.SIGINT)
}()
gitURL := fmt.Sprintf("%s/%s.git", config.REPOS_BASE_URL, "gitea")
dir := path.Join(config.RECIPES_DIR, "gitea")
if err := Clone(dir, gitURL); err == nil {
t.Fatal("cloning should have been interrupted")
}
if _, err := os.Stat(dir); !os.IsNotExist(err) {
t.Fatal("recipe directory should have been deleted")
}
}