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

Commit cde0367

Browse files
Colton J. McCurdymcuadros
Colton J. McCurdy
authored andcommitted
examples & documentation: PlainClone with Basic Authentication (Password & Access Token) (#990)
examples: PlainClone with Basic Authentication (Password & Access Token)
1 parent f22a5cb commit cde0367

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

_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
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+
}

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",

0 commit comments

Comments
 (0)