1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package main
import (
"context"
"os"
"os/signal"
"gopkg.in/src-d/go-git.v4"
. "gopkg.in/src-d/go-git.v4/_examples"
)
// Graceful cancellation example of a basic git operation such as Clone.
func main() {
CheckArgs("<url>", "<directory>")
url := os.Args[1]
directory := os.Args[2]
// Clone the given repository to the given directory
Info("git clone %s %s", url, directory)
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
// The context is the mechanism used by go-git, to support deadlines and
// cancellation signals.
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // cancel when we are finished consuming integers
go func() {
<-stop
Warning("\nSignal detected, canceling operation...")
cancel()
}()
Warning("To gracefully stop the clone operation, push Crtl-C.")
// Using PlainCloneContext we can provide to a context, if the context
// is cancelled, the clone operation stops gracefully.
_, err := git.PlainCloneContext(ctx, directory, false, &git.CloneOptions{
URL: url,
Progress: os.Stdout,
})
// If the context was cancelled, an error is returned.
CheckIfError(err)
}
|