Compare commits

..

1 Commits

Author SHA1 Message Date
7c1d6cabbe
WIP: feat: cancel git clone ops gracefully
Some checks failed
continuous-integration/drone/push Build is failing
See #528
2025-04-22 21:05:06 +02:00

View File

@ -1,11 +1,11 @@
package git
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"coopcloud.tech/abra/pkg/log"
"github.com/go-git/go-git/v5"
@ -27,23 +27,18 @@ func gitCloneIgnoreErr(err error) bool {
// Clone runs a git clone which accounts for different default branches.
func Clone(dir, url string) error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
sigIntCh := make(chan os.Signal)
signal.Notify(sigIntCh, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(sigIntCh)
errCh := make(chan error)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
defer func() {
signal.Stop(c)
cancel()
}()
waitCh := make(chan struct{})
go func() {
if _, err := os.Stat(dir); os.IsNotExist(err) {
log.Debugf("git clone: %s", url)
_, err := git.PlainCloneContext(ctx, dir, false, &git.CloneOptions{
_, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: url,
Tags: git.AllTags,
ReferenceName: plumbing.ReferenceName("refs/heads/main"),
@ -55,14 +50,10 @@ func Clone(dir, url string) error {
errCh <- nil
}
if err := ctx.Err(); err != nil {
errCh <- err
}
if err != nil {
log.Debug("git clone: main branch failed, attempting master branch")
_, err := git.PlainCloneContext(ctx, dir, false, &git.CloneOptions{
_, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: url,
Tags: git.AllTags,
ReferenceName: plumbing.ReferenceName("refs/heads/master"),
@ -84,17 +75,18 @@ func Clone(dir, url string) error {
log.Debugf("git clone: %s already exists", dir)
}
errCh <- nil
close(waitCh)
}()
select {
case <-c:
cancel()
case <-waitCh:
return nil
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", dir, err)
return fmt.Errorf("unable to clean up git clone of %s: %s", url, err)
}
return fmt.Errorf("git clone %s: cancelled due to interrupt", dir)
return fmt.Errorf("git clone %s: cancelled due to interrupt", url)
case err := <-errCh:
return err
}