diff options
author | marwan-at-work <marwan.sameer@gmail.com> | 2018-01-09 20:16:14 -0500 |
---|---|---|
committer | marwan-at-work <marwan.sameer@gmail.com> | 2018-01-09 20:16:14 -0500 |
commit | 483e1911fcb54ebaa115c7c612438e32238928d9 (patch) | |
tree | 0b5c74443d244e27d6f776f10f10ca8e5fe038bd /_examples/branch_add_remove/main.go | |
parent | bf3b1f1fb9e0a04d0f87511a7ded2562b48a19d8 (diff) | |
download | go-git-483e1911fcb54ebaa115c7c612438e32238928d9.tar.gz |
add branch add/remove example
Diffstat (limited to '_examples/branch_add_remove/main.go')
-rw-r--r-- | _examples/branch_add_remove/main.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/_examples/branch_add_remove/main.go b/_examples/branch_add_remove/main.go new file mode 100644 index 0000000..4eb4089 --- /dev/null +++ b/_examples/branch_add_remove/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "os" + + "gopkg.in/src-d/go-git.v4" + . "gopkg.in/src-d/go-git.v4/_examples" + "gopkg.in/src-d/go-git.v4/plumbing" +) + +// Basic example of how to checkout a specific commit. +func main() { + CheckArgs("<url>", "<directory>") + url, directory := os.Args[1], os.Args[2] + + // Clone the given repository to the given directory + Info("git clone %s %s", url, directory) + r, err := git.PlainClone(directory, false, &git.CloneOptions{ + URL: url, + }) + CheckIfError(err) + + // ... retrieving the commit being pointed by HEAD + Info("git checkout -b <branch-name>") + + headRef, err := r.Head() + CheckIfError(err) + + // refs/heads/ is mandatory since go-git deals + // with references not branches. + branchName := "refs/heads/myBranch" + + // You can use plumbing.NewReferenceFromStrings if you want to checkout a branch at a specific commit. + ref := plumbing.NewHashReference(plumbing.ReferenceName(branchName), headRef.Hash()) + + err = r.Storer.SetReference(ref) + CheckIfError(err) + + Info("git branch -D <branch-name>") + + err = r.Storer.RemoveReference(ref.Name()) + CheckIfError(err) +} |