From cde0367919945862cab219228305b59b4b97cd13 Mon Sep 17 00:00:00 2001 From: "Colton J. McCurdy" Date: Thu, 25 Oct 2018 14:49:54 -0400 Subject: examples & documentation: PlainClone with Basic Authentication (Password & Access Token) (#990) examples: PlainClone with Basic Authentication (Password & Access Token) --- _examples/README.md | 4 +++ _examples/clone/auth/basic/access_token/main.go | 40 ++++++++++++++++++++++ .../clone/auth/basic/username_password/main.go | 37 ++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 _examples/clone/auth/basic/access_token/main.go create mode 100644 _examples/clone/auth/basic/username_password/main.go (limited to '_examples') diff --git a/_examples/README.md b/_examples/README.md index 26639b1..cf9c2d3 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 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 0000000..7f6d121 --- /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/username_password/main.go b/_examples/clone/auth/basic/username_password/main.go new file mode 100644 index 0000000..754558c --- /dev/null +++ b/_examples/clone/auth/basic/username_password/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) +} -- cgit From 190bfd6aa60022afd0ef830342cfb07e33c45f37 Mon Sep 17 00:00:00 2001 From: Lukasz Kokot Date: Sun, 28 Oct 2018 20:55:22 -0400 Subject: Fix spelling and grammar in docs and example Signed-off-by: Lukasz Kokot --- _examples/checkout/main.go | 4 ++-- _examples/commit/main.go | 6 +++--- _examples/log/main.go | 2 +- _examples/open/main.go | 2 +- _examples/pull/main.go | 2 +- _examples/showcase/main.go | 2 +- _examples/storage/README.md | 2 +- _examples/tag/main.go | 2 +- 8 files changed, 11 insertions(+), 11 deletions(-) (limited to '_examples') diff --git a/_examples/checkout/main.go b/_examples/checkout/main.go index 2c54550..5969eb4 100644 --- a/_examples/checkout/main.go +++ b/_examples/checkout/main.go @@ -38,8 +38,8 @@ func main() { }) CheckIfError(err) - // ... retrieving the commit being pointed by HEAD, it's shows that the - // repository is poiting to the giving commit in detached mode + // ... retrieving the commit being pointed by HEAD, it shows that the + // repository is pointing to the giving commit in detached mode Info("git show-ref --head HEAD") ref, err = r.Head() CheckIfError(err) diff --git a/_examples/commit/main.go b/_examples/commit/main.go index 556cb9c..ec296b9 100644 --- a/_examples/commit/main.go +++ b/_examples/commit/main.go @@ -12,13 +12,13 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing/object" ) -// Basic example of how to commit changes to the current branch to an existent +// Basic example of how to commit changes to the current branch to an existing // repository. func main() { CheckArgs("") directory := os.Args[1] - // Opens an already existent repository. + // Opens an already existing repository. r, err := git.PlainOpen(directory) CheckIfError(err) @@ -44,7 +44,7 @@ func main() { fmt.Println(status) - // Commits the current staging are to the repository, with the new file + // Commits the current staging area to the repository, with the new file // just created. We should provide the object.Signature of Author of the // commit. Info("git commit -m \"example go-git commit\"") diff --git a/_examples/log/main.go b/_examples/log/main.go index 714d58f..ba0597a 100644 --- a/_examples/log/main.go +++ b/_examples/log/main.go @@ -23,7 +23,7 @@ func main() { }) CheckIfError(err) - // Gets the HEAD history from HEAD, just like does: + // Gets the HEAD history from HEAD, just like this command: Info("git log") // ... retrieves the branch pointed by HEAD diff --git a/_examples/open/main.go b/_examples/open/main.go index b890423..dec183e 100644 --- a/_examples/open/main.go +++ b/_examples/open/main.go @@ -14,7 +14,7 @@ func main() { CheckArgs("") path := os.Args[1] - // We instance a new repository targeting the given path (the .git folder) + // We instanciate a new repository targeting the given path (the .git folder) r, err := git.PlainOpen(path) CheckIfError(err) diff --git a/_examples/pull/main.go b/_examples/pull/main.go index ae751d2..06369fa 100644 --- a/_examples/pull/main.go +++ b/_examples/pull/main.go @@ -13,7 +13,7 @@ func main() { CheckArgs("") path := os.Args[1] - // We instance a new repository targeting the given path (the .git folder) + // We instance\iate a new repository targeting the given path (the .git folder) r, err := git.PlainOpen(path) CheckIfError(err) diff --git a/_examples/showcase/main.go b/_examples/showcase/main.go index aeeddb8..85f2b58 100644 --- a/_examples/showcase/main.go +++ b/_examples/showcase/main.go @@ -16,7 +16,7 @@ import ( // - Get the HEAD reference // - Using the HEAD reference, obtain the commit this reference is pointing to // - Print the commit content -// - Using the commit, iterate all its files and print them +// - Using the commit, iterate over all its files and print them // - Print all the commit history with commit messages, short hash and the // first line of the commit message func main() { diff --git a/_examples/storage/README.md b/_examples/storage/README.md index fc72e6f..b002515 100644 --- a/_examples/storage/README.md +++ b/_examples/storage/README.md @@ -6,7 +6,7 @@ ### and what this means ... -*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. +*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. 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). diff --git a/_examples/tag/main.go b/_examples/tag/main.go index 190c3ad..1e6212b 100644 --- a/_examples/tag/main.go +++ b/_examples/tag/main.go @@ -15,7 +15,7 @@ func main() { CheckArgs("") path := os.Args[1] - // We instance a new repository targeting the given path (the .git folder) + // We instanciate a new repository targeting the given path (the .git folder) r, err := git.PlainOpen(path) CheckIfError(err) -- cgit