Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 6f3b2d8

Browse files
authored
Merge pull request #555 from mcuadros/ctx-example
_examples: context
2 parents bd81c1f + fc560b8 commit 6f3b2d8

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

_examples/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Here you can find a list of annotated _go-git_ examples:
66
- [showcase](showcase/main.go) - A small showcase of the capabilities of _go-git_
77
- [open](open/main.go) - Opening a existing repository cloned by _git_
88
- [clone](clone/main.go) - Cloning a repository
9+
- [clone with context](context/main.go) - Cloning a repository with graceful cancellation.
910
- [log](log/main.go) - Emulate `git log` command output iterating all the commit history from HEAD reference
1011
- [remotes](remotes/main.go) - Working with remotes: adding, removing, etc
1112
- [progress](progress/main.go) - Printing the progress information from the sideband

_examples/common_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var defaultURL = "https://github.com/git-fixtures/basic.git"
1717
var args = map[string][]string{
1818
"checkout": []string{defaultURL, tempFolder(), "35e85108805c84807bc66a02d91535e1e24b38b9"},
1919
"clone": []string{defaultURL, tempFolder()},
20+
"context": []string{defaultURL, tempFolder()},
2021
"commit": []string{cloneRepository(defaultURL, tempFolder())},
2122
"custom_http": []string{defaultURL},
2223
"open": []string{cloneRepository(defaultURL, tempFolder())},

_examples/context/main.go

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

0 commit comments

Comments
 (0)