Compare commits
1 Commits
7c1d6cabbe
...
e3d5bb6a3e
Author | SHA1 | Date | |
---|---|---|---|
e3d5bb6a3e |
@ -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,18 +27,23 @@ 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)
|
||||
ctx := context.Background()
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
|
||||
errCh := make(chan error)
|
||||
waitCh := make(chan struct{})
|
||||
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, os.Interrupt)
|
||||
defer func() {
|
||||
signal.Stop(c)
|
||||
cancel()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
log.Debugf("git clone: %s", url)
|
||||
|
||||
_, err := git.PlainClone(dir, false, &git.CloneOptions{
|
||||
_, err := git.PlainCloneContext(ctx, dir, false, &git.CloneOptions{
|
||||
URL: url,
|
||||
Tags: git.AllTags,
|
||||
ReferenceName: plumbing.ReferenceName("refs/heads/main"),
|
||||
@ -50,10 +55,14 @@ 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.PlainClone(dir, false, &git.CloneOptions{
|
||||
_, err := git.PlainCloneContext(ctx, dir, false, &git.CloneOptions{
|
||||
URL: url,
|
||||
Tags: git.AllTags,
|
||||
ReferenceName: plumbing.ReferenceName("refs/heads/master"),
|
||||
@ -75,18 +84,17 @@ func Clone(dir, url string) error {
|
||||
log.Debugf("git clone: %s already exists", dir)
|
||||
}
|
||||
|
||||
close(waitCh)
|
||||
errCh <- nil
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-waitCh:
|
||||
return nil
|
||||
case <-sigIntCh:
|
||||
case <-c:
|
||||
cancel()
|
||||
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("unable to clean up git clone of %s: %s", dir, err)
|
||||
}
|
||||
return fmt.Errorf("git clone %s: cancelled due to interrupt", url)
|
||||
return fmt.Errorf("git clone %s: cancelled due to interrupt", dir)
|
||||
case err := <-errCh:
|
||||
return err
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user