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

Commit 3fe6f65

Browse files
committed
Merge branch 'master' of github.com:src-d/go-git into mccurdyc/Issue#969/fix-flaky-ssh-test
2 parents 43d4551 + 959dc01 commit 3fe6f65

File tree

23 files changed

+421
-28
lines changed

23 files changed

+421
-28
lines changed

CONTRIBUTING.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ This can be done easily using the [`-s`](https://github.com/git/git/blob/b2c150d
2121

2222
The official support channels, for both users and contributors, are:
2323

24-
- GitHub [issues](https://github.com/src-d/go-git/issues)*
24+
- [StackOverflow go-git tag](https://stackoverflow.com/questions/tagged/go-git) for user questions.
25+
- GitHub [Issues](https://github.com/src-d/go-git/issues)* for bug reports and feature requests.
2526
- Slack: #go-git room in the [source{d} Slack](https://join.slack.com/t/sourced-community/shared_invite/enQtMjc4Njk5MzEyNzM2LTFjNzY4NjEwZGEwMzRiNTM4MzRlMzQ4MmIzZjkwZmZlM2NjODUxZmJjNDI1OTcxNDAyMmZlNmFjODZlNTg0YWM)
2627

2728
*Before opening a new issue or submitting a new pull request, it's helpful to

LICENSE

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright 2017 Sourced Technologies, S.L.
189+
Copyright 2018 Sourced Technologies, S.L.
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.
@@ -198,4 +198,4 @@
198198
distributed under the License is distributed on an "AS IS" BASIS,
199199
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200200
See the License for the specific language governing permissions and
201-
limitations under the License.
201+
limitations under the License.

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
It can be used to manipulate git repositories at low level *(plumbing)* or high level *(porcelain)*, through an idiomatic Go API. It also supports several types of storage, such as in-memory filesystems, or custom implementations thanks to the [`Storer`](https://godoc.org/gopkg.in/src-d/go-git.v4/plumbing/storer) interface.
77

8-
It's being actively develop since 2015 and is being use extensively by [source{d}](https://sourced.tech/) and [Keybase](https://keybase.io/blog/encrypted-git-for-everyone), and by many other libraries and tools.
8+
It's being actively developed since 2015 and is being used extensively by [source{d}](https://sourced.tech/) and [Keybase](https://keybase.io/blog/encrypted-git-for-everyone), and by many other libraries and tools.
99

1010
Comparison with git
1111
-------------------
1212

1313
*go-git* aims to be fully compatible with [git](https://github.com/git/git), all the *porcelain* operations are implemented to work exactly as *git* does.
1414

15-
*git* is a humongous project with years of development by thousands of contributors, making it challenging for *go-git* implement all the features. You can find a comparison of *go-git* vs *git* in the [compatibility documentation](COMPATIBILITY.md).
15+
*git* is a humongous project with years of development by thousands of contributors, making it challenging for *go-git* to implement all the features. You can find a comparison of *go-git* vs *git* in the [compatibility documentation](COMPATIBILITY.md).
1616

1717

1818
Installation
@@ -24,12 +24,12 @@ The recommended way to install *go-git* is:
2424
go get -u gopkg.in/src-d/go-git.v4/...
2525
```
2626

27-
> We use [gopkg.in](http://labix.org/gopkg.in) for having a versioned API, this means that when `go get` clones the package, is the latest tag matching `v4.*` cloned and not the master branch.
27+
> We use [gopkg.in](http://labix.org/gopkg.in) to version the API, this means that when `go get` clones the package, it's the latest tag matching `v4.*` that is cloned and not the master branch.
2828
2929
Examples
3030
--------
3131

32-
> Please note that the functions `CheckIfError` and `Info` used in the examples are from the [examples package](https://github.com/src-d/go-git/blob/master/_examples/common.go#L17) just to be used in the examples.
32+
> Please note that the `CheckIfError` and `Info` functions used in the examples are from the [examples package](https://github.com/src-d/go-git/blob/master/_examples/common.go#L17) just to be used in the examples.
3333
3434

3535
### Basic example
@@ -71,7 +71,7 @@ r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
7171

7272
CheckIfError(err)
7373

74-
// Gets the HEAD history from HEAD, just like does:
74+
// Gets the HEAD history from HEAD, just like this command:
7575
Info("git log")
7676

7777
// ... retrieves the branch pointed by HEAD
@@ -110,7 +110,7 @@ Date: Fri Nov 11 13:23:22 2016 +0100
110110
...
111111
```
112112

113-
You can find this [example](_examples/log/main.go) and many others at the [examples](_examples) folder
113+
You can find this [example](_examples/log/main.go) and many others in the [examples](_examples) folder.
114114

115115
Contribute
116116
----------

_examples/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ 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+
- [username and password](clone/auth/basic/username_password/main.go) - Cloning a repository
10+
using a username and password
11+
- [personal access token](clone/auth/basic/access_token/main.go) - Cloning
12+
a repository using a GitHub personal access token
913
- [commit](commit/main.go) - Commit changes to the current branch to an existent repository
1014
- [push](push/main.go) - Push repository to default remote (origin)
1115
- [pull](pull/main.go) - Pull changes from a remote repository

_examples/checkout/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func main() {
3838
})
3939
CheckIfError(err)
4040

41-
// ... retrieving the commit being pointed by HEAD, it's shows that the
42-
// repository is poiting to the giving commit in detached mode
41+
// ... retrieving the commit being pointed by HEAD, it shows that the
42+
// repository is pointing to the giving commit in detached mode
4343
Info("git show-ref --head HEAD")
4444
ref, err = r.Head()
4545
CheckIfError(err)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
git "gopkg.in/src-d/go-git.v4"
8+
. "gopkg.in/src-d/go-git.v4/_examples"
9+
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
10+
)
11+
12+
func main() {
13+
CheckArgs("<url>", "<directory>", "<github_access_token>")
14+
url, directory, token := os.Args[1], os.Args[2], os.Args[3]
15+
16+
// Clone the given repository to the given directory
17+
Info("git clone %s %s", url, directory)
18+
19+
r, err := git.PlainClone(directory, false, &git.CloneOptions{
20+
// The intended use of a GitHub personal access token is in replace of your password
21+
// because access tokens can easily be revoked.
22+
// https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
23+
Auth: &http.BasicAuth{
24+
Username: "abc123", // yes, this can be anything except an empty string
25+
Password: token,
26+
},
27+
URL: url,
28+
Progress: os.Stdout,
29+
})
30+
CheckIfError(err)
31+
32+
// ... retrieving the branch being pointed by HEAD
33+
ref, err := r.Head()
34+
CheckIfError(err)
35+
// ... retrieving the commit object
36+
commit, err := r.CommitObject(ref.Hash())
37+
CheckIfError(err)
38+
39+
fmt.Println(commit)
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
git "gopkg.in/src-d/go-git.v4"
8+
. "gopkg.in/src-d/go-git.v4/_examples"
9+
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
10+
)
11+
12+
func main() {
13+
CheckArgs("<url>", "<directory>", "<github_username>", "<github_password>")
14+
url, directory, username, password := os.Args[1], os.Args[2], os.Args[3], os.Args[4]
15+
16+
// Clone the given repository to the given directory
17+
Info("git clone %s %s", url, directory)
18+
19+
r, err := git.PlainClone(directory, false, &git.CloneOptions{
20+
Auth: &http.BasicAuth{
21+
Username: username,
22+
Password: password,
23+
},
24+
URL: url,
25+
Progress: os.Stdout,
26+
})
27+
CheckIfError(err)
28+
29+
// ... retrieving the branch being pointed by HEAD
30+
ref, err := r.Head()
31+
CheckIfError(err)
32+
// ... retrieving the commit object
33+
commit, err := r.CommitObject(ref.Hash())
34+
CheckIfError(err)
35+
36+
fmt.Println(commit)
37+
}

_examples/commit/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
"gopkg.in/src-d/go-git.v4/plumbing/object"
1313
)
1414

15-
// Basic example of how to commit changes to the current branch to an existent
15+
// Basic example of how to commit changes to the current branch to an existing
1616
// repository.
1717
func main() {
1818
CheckArgs("<directory>")
1919
directory := os.Args[1]
2020

21-
// Opens an already existent repository.
21+
// Opens an already existing repository.
2222
r, err := git.PlainOpen(directory)
2323
CheckIfError(err)
2424

@@ -44,7 +44,7 @@ func main() {
4444

4545
fmt.Println(status)
4646

47-
// Commits the current staging are to the repository, with the new file
47+
// Commits the current staging area to the repository, with the new file
4848
// just created. We should provide the object.Signature of Author of the
4949
// commit.
5050
Info("git commit -m \"example go-git commit\"")

_examples/log/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func main() {
2323
})
2424
CheckIfError(err)
2525

26-
// Gets the HEAD history from HEAD, just like does:
26+
// Gets the HEAD history from HEAD, just like this command:
2727
Info("git log")
2828

2929
// ... retrieves the branch pointed by HEAD

_examples/open/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func main() {
1414
CheckArgs("<path>")
1515
path := os.Args[1]
1616

17-
// We instance a new repository targeting the given path (the .git folder)
17+
// We instanciate a new repository targeting the given path (the .git folder)
1818
r, err := git.PlainOpen(path)
1919
CheckIfError(err)
2020

_examples/pull/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func main() {
1313
CheckArgs("<path>")
1414
path := os.Args[1]
1515

16-
// We instance a new repository targeting the given path (the .git folder)
16+
// We instance\iate a new repository targeting the given path (the .git folder)
1717
r, err := git.PlainOpen(path)
1818
CheckIfError(err)
1919

_examples/showcase/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// - Get the HEAD reference
1717
// - Using the HEAD reference, obtain the commit this reference is pointing to
1818
// - Print the commit content
19-
// - Using the commit, iterate all its files and print them
19+
// - Using the commit, iterate over all its files and print them
2020
// - Print all the commit history with commit messages, short hash and the
2121
// first line of the commit message
2222
func main() {

_examples/storage/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
### and what this means ...
9-
*git* has as very well defined storage system, the `.git` directory, present on any repository. This is the place where `git` stores al the [`objects`](https://git-scm.com/book/en/v2/Git-Internals-Git-Objects), [`references`](https://git-scm.com/book/es/v2/Git-Internals-Git-References) and [`configuration`](https://git-scm.com/docs/git-config#_configuration_file). This information is stored in plain files.
9+
*git* has a very well defined storage system, the `.git` directory, present on any repository. This is the place where `git` stores all the [`objects`](https://git-scm.com/book/en/v2/Git-Internals-Git-Objects), [`references`](https://git-scm.com/book/es/v2/Git-Internals-Git-References) and [`configuration`](https://git-scm.com/docs/git-config#_configuration_file). This information is stored in plain files.
1010

1111
Our original **go-git** version was designed to work in memory, some time after we added support to read the `.git`, and now we have added support for fully customized [storages](https://godoc.org/gopkg.in/src-d/go-git.v4/storage#Storer).
1212

_examples/tag/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func main() {
1515
CheckArgs("<path>")
1616
path := os.Args[1]
1717

18-
// We instance a new repository targeting the given path (the .git folder)
18+
// We instanciate a new repository targeting the given path (the .git folder)
1919
r, err := git.PlainOpen(path)
2020
CheckIfError(err)
2121

example_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"gopkg.in/src-d/go-git.v4"
1212
"gopkg.in/src-d/go-git.v4/config"
1313
"gopkg.in/src-d/go-git.v4/plumbing"
14+
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
1415
"gopkg.in/src-d/go-git.v4/storage/memory"
1516

1617
"gopkg.in/src-d/go-billy.v4/memfs"
@@ -69,6 +70,52 @@ func ExamplePlainClone() {
6970
// Output: Initial changelog
7071
}
7172

73+
func ExamplePlainClone_usernamePassword() {
74+
// Tempdir to clone the repository
75+
dir, err := ioutil.TempDir("", "clone-example")
76+
if err != nil {
77+
log.Fatal(err)
78+
}
79+
80+
defer os.RemoveAll(dir) // clean up
81+
82+
// Clones the repository into the given dir, just as a normal git clone does
83+
_, err = git.PlainClone(dir, false, &git.CloneOptions{
84+
URL: "https://github.com/git-fixtures/basic.git",
85+
Auth: &http.BasicAuth{
86+
Username: "username",
87+
Password: "password",
88+
},
89+
})
90+
91+
if err != nil {
92+
log.Fatal(err)
93+
}
94+
}
95+
96+
func ExamplePlainClone_accessToken() {
97+
// Tempdir to clone the repository
98+
dir, err := ioutil.TempDir("", "clone-example")
99+
if err != nil {
100+
log.Fatal(err)
101+
}
102+
103+
defer os.RemoveAll(dir) // clean up
104+
105+
// Clones the repository into the given dir, just as a normal git clone does
106+
_, err = git.PlainClone(dir, false, &git.CloneOptions{
107+
URL: "https://github.com/git-fixtures/basic.git",
108+
Auth: &http.BasicAuth{
109+
Username: "abc123", // anything except an empty string
110+
Password: "github_access_token",
111+
},
112+
})
113+
114+
if err != nil {
115+
log.Fatal(err)
116+
}
117+
}
118+
72119
func ExampleRepository_References() {
73120
r, _ := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
74121
URL: "https://github.com/git-fixtures/basic.git",

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/pkg/errors v0.8.0 // indirect
1717
github.com/pmezard/go-difflib v1.0.0 // indirect
1818
github.com/sergi/go-diff v1.0.0
19-
github.com/src-d/gcfg v1.3.0
19+
github.com/src-d/gcfg v1.4.0
2020
github.com/stretchr/testify v1.2.2 // indirect
2121
github.com/xanzy/ssh-agent v0.2.0
2222
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
3535
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
3636
github.com/src-d/gcfg v1.3.0 h1:2BEDr8r0I0b8h/fOqwtxCEiq2HJu8n2JGZJQFGXWLjg=
3737
github.com/src-d/gcfg v1.3.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
38+
github.com/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4=
39+
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
3840
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
3941
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
4042
github.com/xanzy/ssh-agent v0.2.0 h1:Adglfbi5p9Z0BmK2oKU9nTG+zKfniSfnaMYB+ULd+Ro=

plumbing/reference.go

+30
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,36 @@ func (r ReferenceType) String() string {
5555
// ReferenceName reference name's
5656
type ReferenceName string
5757

58+
// NewBranchReferenceName returns a reference name describing a branch based on
59+
// his short name.
60+
func NewBranchReferenceName(name string) ReferenceName {
61+
return ReferenceName(refHeadPrefix + name)
62+
}
63+
64+
// NewNoteReferenceName returns a reference name describing a note based on his
65+
// short name.
66+
func NewNoteReferenceName(name string) ReferenceName {
67+
return ReferenceName(refNotePrefix + name)
68+
}
69+
70+
// NewRemoteReferenceName returns a reference name describing a remote branch
71+
// based on his short name and the remote name.
72+
func NewRemoteReferenceName(remote, name string) ReferenceName {
73+
return ReferenceName(refRemotePrefix + fmt.Sprintf("%s/%s", remote, name))
74+
}
75+
76+
// NewRemoteHEADReferenceName returns a reference name describing a the HEAD
77+
// branch of a remote.
78+
func NewRemoteHEADReferenceName(remote string) ReferenceName {
79+
return ReferenceName(refRemotePrefix + fmt.Sprintf("%s/%s", remote, HEAD))
80+
}
81+
82+
// NewTagReferenceName returns a reference name describing a tag based on short
83+
// his name.
84+
func NewTagReferenceName(name string) ReferenceName {
85+
return ReferenceName(refTagPrefix + name)
86+
}
87+
5888
// IsBranch check if a reference is a branch
5989
func (r ReferenceName) IsBranch() bool {
6090
return strings.HasPrefix(string(r), refHeadPrefix)

0 commit comments

Comments
 (0)