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

examples: add example for pulling changes #546

Merged
merged 1 commit into from
Aug 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Here you can find a list of annotated _go-git_ examples:
- [push](push/main.go) - Push repository to default remote (origin)
- [checkout](checkout/main.go) - check out a specific commit from a repository
- [tag](tag/main.go) - list/print repository tags
- [pull](pull/main.go) - pull changes from a remote repository
### Advanced
- [custom_http](custom_http/main.go) - Replacing the HTTP client using a custom one
- [storage](storage/README.md) - Implementing a custom storage system
25 changes: 24 additions & 1 deletion _examples/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var args = map[string][]string{
"push": []string{setEmptyRemote(cloneRepository(defaultURL, tempFolder()))},
"showcase": []string{defaultURL, tempFolder()},
"tag": []string{cloneRepository(defaultURL, tempFolder())},
"pull": []string{createRepositoryWithRemote(tempFolder(), defaultURL)},
}

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

func createBareRepository(dir string) string {
cmd := exec.Command("git", "init", "--bare", dir)
return createRepository(dir, true)
}

func createRepository(dir string, isBare bool) string {
var cmd *exec.Cmd
if isBare {
cmd = exec.Command("git", "init", "--bare", dir)
} else {
cmd = exec.Command("git", "init", dir)
}
err := cmd.Run()
CheckIfError(err)

return dir
}

func createRepositoryWithRemote(local, remote string) string {
createRepository(local, false)
addRemote(local, remote)
return local
}

func setEmptyRemote(dir string) string {
remote := createBareRepository(tempFolder())
setRemote(dir, remote)
Expand All @@ -108,6 +124,13 @@ func setRemote(local, remote string) {
CheckIfError(err)
}

func addRemote(local, remote string) {
cmd := exec.Command("git", "remote", "add", "origin", remote)
cmd.Dir = local
err := cmd.Run()
CheckIfError(err)
}

func testExample(t *testing.T, name, example string) {
cmd := exec.Command("go", append([]string{
"run", filepath.Join(example),
Expand Down
36 changes: 36 additions & 0 deletions _examples/pull/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"os"

"gopkg.in/src-d/go-git.v4"
. "gopkg.in/src-d/go-git.v4/_examples"
)

// Pull changes from a remote repository
func main() {
CheckArgs("<path>")
path := os.Args[1]

// We instance a new repository targeting the given path (the .git folder)
r, err := git.PlainOpen(path)
CheckIfError(err)

// Get the working directory for the repository
w, err := r.Worktree()
CheckIfError(err)

// Pull the latest changes from the origin remote and merge into the current branch
Info("git pull origin")
err = w.Pull(&git.PullOptions{RemoteName: "origin"})
CheckIfError(err)

// Print the latest commit that was just pulled
ref, err := r.Head()
CheckIfError(err)
commit, err := r.CommitObject(ref.Hash())
CheckIfError(err)

fmt.Println(commit)
}