From 3a90e38a2bd3cc6f72626149ee6018d9d0e3acc7 Mon Sep 17 00:00:00 2001 From: Colton McCurdy Date: Thu, 18 Oct 2018 20:13:38 -0400 Subject: [PATCH 1/8] git_test: Added GoDoc examples for cloning with BasicAuth and TokenAuth. Signed-off-by: Colton McCurdy --- example_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/example_test.go b/example_test.go index ef7e3d375..486252482 100644 --- a/example_test.go +++ b/example_test.go @@ -11,6 +11,7 @@ import ( "gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/plumbing/transport/http" "gopkg.in/src-d/go-git.v4/storage/memory" "gopkg.in/src-d/go-billy.v4/memfs" @@ -69,6 +70,70 @@ func ExamplePlainClone() { // Output: Initial changelog } +func ExamplePlainClone_withBasicAuth() { + // Tempdir to clone the repository + dir, err := ioutil.TempDir("", "clone-example") + if err != nil { + log.Fatal(err) + } + + defer os.RemoveAll(dir) // clean up + + // Clones the repository into the given dir, just as a normal git clone does + _, err = git.PlainClone(dir, false, &git.CloneOptions{ + URL: "https://github.com/git-fixtures/basic.git", + Auth: &http.BasicAuth{ + Username: "username", + Password: "password", + }, + }) + + if err != nil { + log.Fatal(err) + } + + // Prints the content of the CHANGELOG file from the cloned repository + changelog, err := os.Open(filepath.Join(dir, "CHANGELOG")) + if err != nil { + log.Fatal(err) + } + + io.Copy(os.Stdout, changelog) + // Output: Initial changelog +} + +func ExamplePlainClone_withTokenAuth() { + // Tempdir to clone the repository + dir, err := ioutil.TempDir("", "clone-example") + if err != nil { + log.Fatal(err) + } + + defer os.RemoveAll(dir) // clean up + + // Clones the repository into the given dir, just as a normal git clone does + _, err = git.PlainClone(dir, false, &git.CloneOptions{ + URL: "https://github.com/git-fixtures/basic.git", + Auth: &http.BasicAuth{ + Username: "abc123", + Password: "github_access_token", + }, + }) + + if err != nil { + log.Fatal(err) + } + + // Prints the content of the CHANGELOG file from the cloned repository + changelog, err := os.Open(filepath.Join(dir, "CHANGELOG")) + if err != nil { + log.Fatal(err) + } + + io.Copy(os.Stdout, changelog) + // Output: Initial changelog +} + func ExampleRepository_References() { r, _ := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ URL: "https://github.com/git-fixtures/basic.git", From 398ec42a0f976f5258608f4d3a8a55f92043d65f Mon Sep 17 00:00:00 2001 From: Colton McCurdy Date: Thu, 18 Oct 2018 20:14:55 -0400 Subject: [PATCH 2/8] _examples: Added cli example for cloning with BasicAuth. Signed-off-by: Colton McCurdy --- _examples/clone/auth/basic/main.go | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 _examples/clone/auth/basic/main.go diff --git a/_examples/clone/auth/basic/main.go b/_examples/clone/auth/basic/main.go new file mode 100644 index 000000000..754558c76 --- /dev/null +++ b/_examples/clone/auth/basic/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "os" + + git "gopkg.in/src-d/go-git.v4" + . "gopkg.in/src-d/go-git.v4/_examples" + "gopkg.in/src-d/go-git.v4/plumbing/transport/http" +) + +func main() { + CheckArgs("", "", "", "") + url, directory, username, password := os.Args[1], os.Args[2], os.Args[3], os.Args[4] + + // Clone the given repository to the given directory + Info("git clone %s %s", url, directory) + + r, err := git.PlainClone(directory, false, &git.CloneOptions{ + Auth: &http.BasicAuth{ + Username: username, + Password: password, + }, + URL: url, + Progress: os.Stdout, + }) + CheckIfError(err) + + // ... retrieving the branch being pointed by HEAD + ref, err := r.Head() + CheckIfError(err) + // ... retrieving the commit object + commit, err := r.CommitObject(ref.Hash()) + CheckIfError(err) + + fmt.Println(commit) +} From d0ed720e7f1f7ad3593a6a04f0c1fcd31fe84e86 Mon Sep 17 00:00:00 2001 From: Colton McCurdy Date: Thu, 18 Oct 2018 20:15:10 -0400 Subject: [PATCH 3/8] _examples: Added cli example for cloning with TokenAuth. Signed-off-by: Colton McCurdy --- _examples/clone/auth/token/main.go | 50 ++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 _examples/clone/auth/token/main.go diff --git a/_examples/clone/auth/token/main.go b/_examples/clone/auth/token/main.go new file mode 100644 index 000000000..d1ecb670c --- /dev/null +++ b/_examples/clone/auth/token/main.go @@ -0,0 +1,50 @@ +package main + +import ( + "fmt" + "os" + + git "gopkg.in/src-d/go-git.v4" + . "gopkg.in/src-d/go-git.v4/_examples" + "gopkg.in/src-d/go-git.v4/plumbing/transport/http" +) + +func main() { + CheckArgs("", "", "") + url, directory, token := os.Args[1], os.Args[2], os.Args[3] + + // Clone the given repository to the given directory + Info("git clone %s %s", url, directory) + + r, err := git.PlainClone(directory, false, &git.CloneOptions{ + // go-git does not yet support TokenAuth (https://godoc.org/gopkg.in/src-d/go-git.v4/plumbing/transport/http#TokenAuth) + // + // Auth: &http.TokenAuth{ + // Token: token, + // }, + // + // you will recieve and error similar to the following: + // unexpected client error: unexpected requesting ... status code: 400 + // + // instead, you must use BasicAuth with your GitHub Access Token as the password + // and the Username can be anything. + // + // here is a StackOverflow post: https://stackoverflow.com/questions/47359377/go-git-how-to-authenticate-at-remote-server + Auth: &http.BasicAuth{ + Username: "abc123", // yes, this can be anything except an empty string + Password: token, + }, + URL: url, + Progress: os.Stdout, + }) + CheckIfError(err) + + // ... retrieving the branch being pointed by HEAD + ref, err := r.Head() + CheckIfError(err) + // ... retrieving the commit object + commit, err := r.CommitObject(ref.Hash()) + CheckIfError(err) + + fmt.Println(commit) +} From f9378ac4b4b4a0f0e1b22552cf54715f05e7c7f1 Mon Sep 17 00:00:00 2001 From: Colton McCurdy Date: Thu, 18 Oct 2018 20:39:03 -0400 Subject: [PATCH 4/8] git_test: Updated GoDoc examples to pass tests. Signed-off-by: Colton McCurdy --- example_test.go | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/example_test.go b/example_test.go index 486252482..38eb46279 100644 --- a/example_test.go +++ b/example_test.go @@ -91,15 +91,6 @@ func ExamplePlainClone_withBasicAuth() { if err != nil { log.Fatal(err) } - - // Prints the content of the CHANGELOG file from the cloned repository - changelog, err := os.Open(filepath.Join(dir, "CHANGELOG")) - if err != nil { - log.Fatal(err) - } - - io.Copy(os.Stdout, changelog) - // Output: Initial changelog } func ExamplePlainClone_withTokenAuth() { @@ -115,7 +106,7 @@ func ExamplePlainClone_withTokenAuth() { _, err = git.PlainClone(dir, false, &git.CloneOptions{ URL: "https://github.com/git-fixtures/basic.git", Auth: &http.BasicAuth{ - Username: "abc123", + Username: "abc123", // anything except an empty string Password: "github_access_token", }, }) @@ -123,15 +114,6 @@ func ExamplePlainClone_withTokenAuth() { if err != nil { log.Fatal(err) } - - // Prints the content of the CHANGELOG file from the cloned repository - changelog, err := os.Open(filepath.Join(dir, "CHANGELOG")) - if err != nil { - log.Fatal(err) - } - - io.Copy(os.Stdout, changelog) - // Output: Initial changelog } func ExampleRepository_References() { From a9cee332e4731f21e9b8c195d0d3e2f408bbf71e Mon Sep 17 00:00:00 2001 From: Colton McCurdy Date: Wed, 24 Oct 2018 08:14:46 -0400 Subject: [PATCH 5/8] git_test: Updated PlainClone with BasicAuth examples' names. Signed-off-by: Colton McCurdy --- example_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example_test.go b/example_test.go index 38eb46279..691b4ac53 100644 --- a/example_test.go +++ b/example_test.go @@ -70,7 +70,7 @@ func ExamplePlainClone() { // Output: Initial changelog } -func ExamplePlainClone_withBasicAuth() { +func ExamplePlainClone_usernamePassword() { // Tempdir to clone the repository dir, err := ioutil.TempDir("", "clone-example") if err != nil { @@ -93,7 +93,7 @@ func ExamplePlainClone_withBasicAuth() { } } -func ExamplePlainClone_withTokenAuth() { +func ExamplePlainClone_accessToken() { // Tempdir to clone the repository dir, err := ioutil.TempDir("", "clone-example") if err != nil { From 527792330612a4163e3cf33db77987414b9f387a Mon Sep 17 00:00:00 2001 From: Colton McCurdy Date: Wed, 24 Oct 2018 08:15:40 -0400 Subject: [PATCH 6/8] _examples: Moved and updated comments in BasicAuth cli examples. Signed-off-by: Colton McCurdy --- .../clone/auth/basic/access_token/main.go | 40 +++++++++++++++++++ .../basic/{ => username_password}/main.go | 0 2 files changed, 40 insertions(+) create mode 100644 _examples/clone/auth/basic/access_token/main.go rename _examples/clone/auth/basic/{ => username_password}/main.go (100%) diff --git a/_examples/clone/auth/basic/access_token/main.go b/_examples/clone/auth/basic/access_token/main.go new file mode 100644 index 000000000..7f6d121d1 --- /dev/null +++ b/_examples/clone/auth/basic/access_token/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + "os" + + git "gopkg.in/src-d/go-git.v4" + . "gopkg.in/src-d/go-git.v4/_examples" + "gopkg.in/src-d/go-git.v4/plumbing/transport/http" +) + +func main() { + CheckArgs("", "", "") + url, directory, token := os.Args[1], os.Args[2], os.Args[3] + + // Clone the given repository to the given directory + Info("git clone %s %s", url, directory) + + r, err := git.PlainClone(directory, false, &git.CloneOptions{ + // The intended use of a GitHub personal access token is in replace of your password + // because access tokens can easily be revoked. + // https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/ + Auth: &http.BasicAuth{ + Username: "abc123", // yes, this can be anything except an empty string + Password: token, + }, + URL: url, + Progress: os.Stdout, + }) + CheckIfError(err) + + // ... retrieving the branch being pointed by HEAD + ref, err := r.Head() + CheckIfError(err) + // ... retrieving the commit object + commit, err := r.CommitObject(ref.Hash()) + CheckIfError(err) + + fmt.Println(commit) +} diff --git a/_examples/clone/auth/basic/main.go b/_examples/clone/auth/basic/username_password/main.go similarity index 100% rename from _examples/clone/auth/basic/main.go rename to _examples/clone/auth/basic/username_password/main.go From cf4962e7fd8aa29553f076a2fec20ac69d74bbf5 Mon Sep 17 00:00:00 2001 From: Colton McCurdy Date: Wed, 24 Oct 2018 08:18:58 -0400 Subject: [PATCH 7/8] _examples: Moved and updated comments in BasicAuth cli examples. Signed-off-by: Colton McCurdy --- _examples/clone/auth/token/main.go | 50 ------------------------------ 1 file changed, 50 deletions(-) delete mode 100644 _examples/clone/auth/token/main.go diff --git a/_examples/clone/auth/token/main.go b/_examples/clone/auth/token/main.go deleted file mode 100644 index d1ecb670c..000000000 --- a/_examples/clone/auth/token/main.go +++ /dev/null @@ -1,50 +0,0 @@ -package main - -import ( - "fmt" - "os" - - git "gopkg.in/src-d/go-git.v4" - . "gopkg.in/src-d/go-git.v4/_examples" - "gopkg.in/src-d/go-git.v4/plumbing/transport/http" -) - -func main() { - CheckArgs("", "", "") - url, directory, token := os.Args[1], os.Args[2], os.Args[3] - - // Clone the given repository to the given directory - Info("git clone %s %s", url, directory) - - r, err := git.PlainClone(directory, false, &git.CloneOptions{ - // go-git does not yet support TokenAuth (https://godoc.org/gopkg.in/src-d/go-git.v4/plumbing/transport/http#TokenAuth) - // - // Auth: &http.TokenAuth{ - // Token: token, - // }, - // - // you will recieve and error similar to the following: - // unexpected client error: unexpected requesting ... status code: 400 - // - // instead, you must use BasicAuth with your GitHub Access Token as the password - // and the Username can be anything. - // - // here is a StackOverflow post: https://stackoverflow.com/questions/47359377/go-git-how-to-authenticate-at-remote-server - Auth: &http.BasicAuth{ - Username: "abc123", // yes, this can be anything except an empty string - Password: token, - }, - URL: url, - Progress: os.Stdout, - }) - CheckIfError(err) - - // ... retrieving the branch being pointed by HEAD - ref, err := r.Head() - CheckIfError(err) - // ... retrieving the commit object - commit, err := r.CommitObject(ref.Hash()) - CheckIfError(err) - - fmt.Println(commit) -} From c3ef7c07a2ac5a27a579c7c292ce69030ee591ca Mon Sep 17 00:00:00 2001 From: Colton McCurdy Date: Wed, 24 Oct 2018 12:43:17 -0400 Subject: [PATCH 8/8] _examples: Adding password and GitHub personal access token cloning to the list of examples. Signed-off-by: Colton McCurdy --- _examples/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/_examples/README.md b/_examples/README.md index 26639b14a..cf9c2d3a6 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -6,6 +6,10 @@ 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 + - [username and password](clone/auth/basic/username_password/main.go) - Cloning a repository + using a username and password + - [personal access token](clone/auth/basic/access_token/main.go) - Cloning + a repository using a GitHub personal access token - [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