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

Commit 71e438e

Browse files
authored
Merge pull request #714 from marwan-at-work/branch-minip
add branch add/remove example
2 parents 861e399 + 483e191 commit 71e438e

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

_examples/branch_add_remove/main.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
// Basic example of how to checkout a specific commit.
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+
// ... retrieving the commit being pointed by HEAD
24+
Info("git checkout -b <branch-name>")
25+
26+
headRef, err := r.Head()
27+
CheckIfError(err)
28+
29+
// refs/heads/ is mandatory since go-git deals
30+
// with references not branches.
31+
branchName := "refs/heads/myBranch"
32+
33+
// You can use plumbing.NewReferenceFromStrings if you want to checkout a branch at a specific commit.
34+
ref := plumbing.NewHashReference(plumbing.ReferenceName(branchName), headRef.Hash())
35+
36+
err = r.Storer.SetReference(ref)
37+
CheckIfError(err)
38+
39+
Info("git branch -D <branch-name>")
40+
41+
err = r.Storer.RemoveReference(ref.Name())
42+
CheckIfError(err)
43+
}

_examples/common_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +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-
"checkout": {defaultURL, tempFolder(), "35e85108805c84807bc66a02d91535e1e24b38b9"},
19-
"clone": {defaultURL, tempFolder()},
20-
"context": {defaultURL, tempFolder()},
21-
"commit": {cloneRepository(defaultURL, tempFolder())},
22-
"custom_http": {defaultURL},
23-
"open": {cloneRepository(defaultURL, tempFolder())},
24-
"progress": {defaultURL, tempFolder()},
25-
"push": {setEmptyRemote(cloneRepository(defaultURL, tempFolder()))},
26-
"revision": {cloneRepository(defaultURL, tempFolder()), "master~2^"},
27-
"showcase": {defaultURL, tempFolder()},
28-
"tag": {cloneRepository(defaultURL, tempFolder())},
29-
"pull": {createRepositoryWithRemote(tempFolder(), defaultURL)},
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)},
3031
}
3132

3233
var ignored = map[string]bool{}

0 commit comments

Comments
 (0)