aboutsummaryrefslogtreecommitdiffstats
path: root/_examples/pull
diff options
context:
space:
mode:
authorOri Rawlings <orirawlings@gmail.com>2017-08-13 20:41:51 -0500
committerOri Rawlings <orirawlings@gmail.com>2017-08-13 20:42:23 -0500
commit5ed7c5ca0320fdb300a7e409d82dfb21022fab39 (patch)
treedb447922d38f526ac7c8fe65ecf9e26fa59a07ab /_examples/pull
parentb51e316bf61ff1b9e83486f2010854fb9089d9dc (diff)
downloadgo-git-5ed7c5ca0320fdb300a7e409d82dfb21022fab39.tar.gz
Add example for pulling changes
Diffstat (limited to '_examples/pull')
-rw-r--r--_examples/pull/main.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/_examples/pull/main.go b/_examples/pull/main.go
new file mode 100644
index 0000000..ae751d2
--- /dev/null
+++ b/_examples/pull/main.go
@@ -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)
+}