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

Commit 4d43799

Browse files
committed
_examples: branch example improvements
Signed-off-by: Máximo Cuadros <[email protected]>
1 parent 71e438e commit 4d43799

File tree

4 files changed

+66
-61
lines changed

4 files changed

+66
-61
lines changed

_examples/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ Here you can find a list of annotated _go-git_ examples:
66
- [showcase](showcase/main.go) - A small showcase of the capabilities of _go-git_
77
- [open](open/main.go) - Opening a existing repository cloned by _git_
88
- [clone](clone/main.go) - Cloning a repository
9-
- [clone with context](context/main.go) - Cloning a repository with graceful cancellation.
10-
- [log](log/main.go) - Emulate `git log` command output iterating all the commit history from HEAD reference
11-
- [remotes](remotes/main.go) - Working with remotes: adding, removing, etc
12-
- [progress](progress/main.go) - Printing the progress information from the sideband
9+
- [commit](commit/main.go) - Commit changes to the current branch to an existent repository
1310
- [push](push/main.go) - Push repository to default remote (origin)
11+
- [pull](pull/main.go) - Pull changes from a remote repository
1412
- [checkout](checkout/main.go) - Check out a specific commit from a repository
13+
- [log](log/main.go) - Emulate `git log` command output iterating all the commit history from HEAD reference
14+
- [branch](branch/main.go) - How to create and remove branches or any other kind of reference.
1515
- [tag](tag/main.go) - List/print repository tags
16-
- [pull](pull/main.go) - Pull changes from a remote repository
16+
- [remotes](remotes/main.go) - Working with remotes: adding, removing, etc
17+
- [progress](progress/main.go) - Printing the progress information from the sideband
1718
- [revision](revision/main.go) - Solve a revision into a commit
19+
1820
### Advanced
1921
- [custom_http](custom_http/main.go) - Replacing the HTTP client using a custom one
22+
- [clone with context](context/main.go) - Cloning a repository with graceful cancellation.
2023
- [storage](storage/README.md) - Implementing a custom storage system

_examples/branch/main.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
"gopkg.in/src-d/go-git.v4"
7+
. "gopkg.in/src-d/go-git.v4/_examples"
8+
"gopkg.in/src-d/go-git.v4/plumbing"
9+
)
10+
11+
// An example of how to create and remove branches or any other kind of reference.
12+
func main() {
13+
CheckArgs("<url>", "<directory>")
14+
url, directory := os.Args[1], os.Args[2]
15+
16+
// Clone the given repository to the given directory
17+
Info("git clone %s %s", url, directory)
18+
r, err := git.PlainClone(directory, false, &git.CloneOptions{
19+
URL: url,
20+
})
21+
CheckIfError(err)
22+
23+
// Create a new branch to the current HEAD
24+
Info("git branch my-branch")
25+
26+
headRef, err := r.Head()
27+
CheckIfError(err)
28+
29+
// Create a new plumbing.HashReference object with the name of the branch
30+
// and the hash from the HEAD. The reference name should be a full reference
31+
// name and now a abbreviated one, as is used on the git cli.
32+
//
33+
// For tags we should use `refs/tags/%s` instead of `refs/heads/%s` used
34+
// for branches.
35+
ref := plumbing.NewHashReference("refs/heads/my-branch", headRef.Hash())
36+
37+
// The created reference is saved in the storage.
38+
err = r.Storer.SetReference(ref)
39+
CheckIfError(err)
40+
41+
// Or deleted from it.
42+
Info("git branch -D my-branch")
43+
err = r.Storer.RemoveReference(ref.Name())
44+
CheckIfError(err)
45+
}

_examples/branch_add_remove/main.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

_examples/common_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ var examplesTest = flag.Bool("examples", false, "run the examples tests")
1515
var defaultURL = "https://github.com/git-fixtures/basic.git"
1616

1717
var args = map[string][]string{
18-
"branch_add_remove": {defaultURL, tempFolder()},
19-
"checkout": {defaultURL, tempFolder(), "35e85108805c84807bc66a02d91535e1e24b38b9"},
20-
"clone": {defaultURL, tempFolder()},
21-
"context": {defaultURL, tempFolder()},
22-
"commit": {cloneRepository(defaultURL, tempFolder())},
23-
"custom_http": {defaultURL},
24-
"open": {cloneRepository(defaultURL, tempFolder())},
25-
"progress": {defaultURL, tempFolder()},
26-
"push": {setEmptyRemote(cloneRepository(defaultURL, tempFolder()))},
27-
"revision": {cloneRepository(defaultURL, tempFolder()), "master~2^"},
28-
"showcase": {defaultURL, tempFolder()},
29-
"tag": {cloneRepository(defaultURL, tempFolder())},
30-
"pull": {createRepositoryWithRemote(tempFolder(), defaultURL)},
18+
"branch": {defaultURL, tempFolder()},
19+
"checkout": {defaultURL, tempFolder(), "35e85108805c84807bc66a02d91535e1e24b38b9"},
20+
"clone": {defaultURL, tempFolder()},
21+
"context": {defaultURL, tempFolder()},
22+
"commit": {cloneRepository(defaultURL, tempFolder())},
23+
"custom_http": {defaultURL},
24+
"open": {cloneRepository(defaultURL, tempFolder())},
25+
"progress": {defaultURL, tempFolder()},
26+
"push": {setEmptyRemote(cloneRepository(defaultURL, tempFolder()))},
27+
"revision": {cloneRepository(defaultURL, tempFolder()), "master~2^"},
28+
"showcase": {defaultURL, tempFolder()},
29+
"tag": {cloneRepository(defaultURL, tempFolder())},
30+
"pull": {createRepositoryWithRemote(tempFolder(), defaultURL)},
3131
}
3232

3333
var ignored = map[string]bool{}

0 commit comments

Comments
 (0)