aboutsummaryrefslogtreecommitdiffstats
path: root/_examples
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2018-01-15 17:11:24 +0100
committerGitHub <noreply@github.com>2018-01-15 17:11:24 +0100
commit71e438ee1c3bdca714f138fcc6bb8c084989a521 (patch)
treeedbbfbd7b468eab30142025abd154fc9b0a5cb0e /_examples
parent861e3996c09e960bfa64ec1c4141b23f6072ccee (diff)
parent483e1911fcb54ebaa115c7c612438e32238928d9 (diff)
downloadgo-git-71e438ee1c3bdca714f138fcc6bb8c084989a521.tar.gz
Merge pull request #714 from marwan-at-work/branch-minip
add branch add/remove example
Diffstat (limited to '_examples')
-rw-r--r--_examples/branch_add_remove/main.go43
-rw-r--r--_examples/common_test.go25
2 files changed, 56 insertions, 12 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)
+}
diff --git a/_examples/common_test.go b/_examples/common_test.go
index 6f8df45..8c17dfb 100644
--- a/_examples/common_test.go
+++ b/_examples/common_test.go
@@ -15,18 +15,19 @@ var examplesTest = flag.Bool("examples", false, "run the examples tests")
var defaultURL = "https://github.com/git-fixtures/basic.git"
var args = map[string][]string{
- "checkout": {defaultURL, tempFolder(), "35e85108805c84807bc66a02d91535e1e24b38b9"},
- "clone": {defaultURL, tempFolder()},
- "context": {defaultURL, tempFolder()},
- "commit": {cloneRepository(defaultURL, tempFolder())},
- "custom_http": {defaultURL},
- "open": {cloneRepository(defaultURL, tempFolder())},
- "progress": {defaultURL, tempFolder()},
- "push": {setEmptyRemote(cloneRepository(defaultURL, tempFolder()))},
- "revision": {cloneRepository(defaultURL, tempFolder()), "master~2^"},
- "showcase": {defaultURL, tempFolder()},
- "tag": {cloneRepository(defaultURL, tempFolder())},
- "pull": {createRepositoryWithRemote(tempFolder(), defaultURL)},
+ "branch_add_remove": {defaultURL, tempFolder()},
+ "checkout": {defaultURL, tempFolder(), "35e85108805c84807bc66a02d91535e1e24b38b9"},
+ "clone": {defaultURL, tempFolder()},
+ "context": {defaultURL, tempFolder()},
+ "commit": {cloneRepository(defaultURL, tempFolder())},
+ "custom_http": {defaultURL},
+ "open": {cloneRepository(defaultURL, tempFolder())},
+ "progress": {defaultURL, tempFolder()},
+ "push": {setEmptyRemote(cloneRepository(defaultURL, tempFolder()))},
+ "revision": {cloneRepository(defaultURL, tempFolder()), "master~2^"},
+ "showcase": {defaultURL, tempFolder()},
+ "tag": {cloneRepository(defaultURL, tempFolder())},
+ "pull": {createRepositoryWithRemote(tempFolder(), defaultURL)},
}
var ignored = map[string]bool{}