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

Commit 118a8c7

Browse files
authored
Merge pull request #546 from orirawlings/pullExample
examples: add example for pulling changes
2 parents b51e316 + 5ed7c5c commit 118a8c7

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

_examples/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Here you can find a list of annotated _go-git_ examples:
1212
- [push](push/main.go) - Push repository to default remote (origin)
1313
- [checkout](checkout/main.go) - check out a specific commit from a repository
1414
- [tag](tag/main.go) - list/print repository tags
15+
- [pull](pull/main.go) - pull changes from a remote repository
1516
### Advanced
1617
- [custom_http](custom_http/main.go) - Replacing the HTTP client using a custom one
1718
- [storage](storage/README.md) - Implementing a custom storage system

_examples/common_test.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var args = map[string][]string{
2424
"push": []string{setEmptyRemote(cloneRepository(defaultURL, tempFolder()))},
2525
"showcase": []string{defaultURL, tempFolder()},
2626
"tag": []string{cloneRepository(defaultURL, tempFolder())},
27+
"pull": []string{createRepositoryWithRemote(tempFolder(), defaultURL)},
2728
}
2829

2930
var ignored = map[string]bool{}
@@ -88,13 +89,28 @@ func cloneRepository(url, folder string) string {
8889
}
8990

9091
func createBareRepository(dir string) string {
91-
cmd := exec.Command("git", "init", "--bare", dir)
92+
return createRepository(dir, true)
93+
}
94+
95+
func createRepository(dir string, isBare bool) string {
96+
var cmd *exec.Cmd
97+
if isBare {
98+
cmd = exec.Command("git", "init", "--bare", dir)
99+
} else {
100+
cmd = exec.Command("git", "init", dir)
101+
}
92102
err := cmd.Run()
93103
CheckIfError(err)
94104

95105
return dir
96106
}
97107

108+
func createRepositoryWithRemote(local, remote string) string {
109+
createRepository(local, false)
110+
addRemote(local, remote)
111+
return local
112+
}
113+
98114
func setEmptyRemote(dir string) string {
99115
remote := createBareRepository(tempFolder())
100116
setRemote(dir, remote)
@@ -108,6 +124,13 @@ func setRemote(local, remote string) {
108124
CheckIfError(err)
109125
}
110126

127+
func addRemote(local, remote string) {
128+
cmd := exec.Command("git", "remote", "add", "origin", remote)
129+
cmd.Dir = local
130+
err := cmd.Run()
131+
CheckIfError(err)
132+
}
133+
111134
func testExample(t *testing.T, name, example string) {
112135
cmd := exec.Command("go", append([]string{
113136
"run", filepath.Join(example),

_examples/pull/main.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"gopkg.in/src-d/go-git.v4"
8+
. "gopkg.in/src-d/go-git.v4/_examples"
9+
)
10+
11+
// Pull changes from a remote repository
12+
func main() {
13+
CheckArgs("<path>")
14+
path := os.Args[1]
15+
16+
// We instance a new repository targeting the given path (the .git folder)
17+
r, err := git.PlainOpen(path)
18+
CheckIfError(err)
19+
20+
// Get the working directory for the repository
21+
w, err := r.Worktree()
22+
CheckIfError(err)
23+
24+
// Pull the latest changes from the origin remote and merge into the current branch
25+
Info("git pull origin")
26+
err = w.Pull(&git.PullOptions{RemoteName: "origin"})
27+
CheckIfError(err)
28+
29+
// Print the latest commit that was just pulled
30+
ref, err := r.Head()
31+
CheckIfError(err)
32+
commit, err := r.CommitObject(ref.Hash())
33+
CheckIfError(err)
34+
35+
fmt.Println(commit)
36+
}

0 commit comments

Comments
 (0)