aboutsummaryrefslogtreecommitdiffstats
path: root/_examples
diff options
context:
space:
mode:
Diffstat (limited to '_examples')
-rw-r--r--_examples/README.md13
-rw-r--r--_examples/branch/main.go45
-rw-r--r--_examples/branch_add_remove/main.go43
-rw-r--r--_examples/common_test.go26
4 files changed, 66 insertions, 61 deletions
diff --git a/_examples/README.md b/_examples/README.md
index 271379b..26639b1 100644
--- a/_examples/README.md
+++ b/_examples/README.md
@@ -6,15 +6,18 @@ Here you can find a list of annotated _go-git_ examples:
- [showcase](showcase/main.go) - A small showcase of the capabilities of _go-git_
- [open](open/main.go) - Opening a existing repository cloned by _git_
- [clone](clone/main.go) - Cloning a repository
-- [clone with context](context/main.go) - Cloning a repository with graceful cancellation.
-- [log](log/main.go) - Emulate `git log` command output iterating all the commit history from HEAD reference
-- [remotes](remotes/main.go) - Working with remotes: adding, removing, etc
-- [progress](progress/main.go) - Printing the progress information from the sideband
+- [commit](commit/main.go) - Commit changes to the current branch to an existent repository
- [push](push/main.go) - Push repository to default remote (origin)
+- [pull](pull/main.go) - Pull changes from a remote repository
- [checkout](checkout/main.go) - Check out a specific commit from a repository
+- [log](log/main.go) - Emulate `git log` command output iterating all the commit history from HEAD reference
+- [branch](branch/main.go) - How to create and remove branches or any other kind of reference.
- [tag](tag/main.go) - List/print repository tags
-- [pull](pull/main.go) - Pull changes from a remote repository
+- [remotes](remotes/main.go) - Working with remotes: adding, removing, etc
+- [progress](progress/main.go) - Printing the progress information from the sideband
- [revision](revision/main.go) - Solve a revision into a commit
+
### Advanced
- [custom_http](custom_http/main.go) - Replacing the HTTP client using a custom one
+- [clone with context](context/main.go) - Cloning a repository with graceful cancellation.
- [storage](storage/README.md) - Implementing a custom storage system
diff --git a/_examples/branch/main.go b/_examples/branch/main.go
new file mode 100644
index 0000000..fa1ad01
--- /dev/null
+++ b/_examples/branch/main.go
@@ -0,0 +1,45 @@
+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"
+)
+
+// An example of how to create and remove branches or any other kind of reference.
+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)
+
+ // Create a new branch to the current HEAD
+ Info("git branch my-branch")
+
+ headRef, err := r.Head()
+ CheckIfError(err)
+
+ // Create a new plumbing.HashReference object with the name of the branch
+ // and the hash from the HEAD. The reference name should be a full reference
+ // name and now a abbreviated one, as is used on the git cli.
+ //
+ // For tags we should use `refs/tags/%s` instead of `refs/heads/%s` used
+ // for branches.
+ ref := plumbing.NewHashReference("refs/heads/my-branch", headRef.Hash())
+
+ // The created reference is saved in the storage.
+ err = r.Storer.SetReference(ref)
+ CheckIfError(err)
+
+ // Or deleted from it.
+ Info("git branch -D my-branch")
+ err = r.Storer.RemoveReference(ref.Name())
+ CheckIfError(err)
+}
diff --git a/_examples/branch_add_remove/main.go b/_examples/branch_add_remove/main.go
deleted file mode 100644
index 4eb4089..0000000
--- a/_examples/branch_add_remove/main.go
+++ /dev/null
@@ -1,43 +0,0 @@
-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 8c17dfb..aa7c9b4 100644
--- a/_examples/common_test.go
+++ b/_examples/common_test.go
@@ -15,19 +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{
- "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)},
+ "branch": {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{}