pax_global_header00006660000000000000000000000064125054065430014516gustar00rootroot0000000000000052 comment=d154598bacbf4501c095a309753c5d4af66caa81 golang-github-tcnksm-go-gitconfig-0.1.2/000077500000000000000000000000001250540654300201145ustar00rootroot00000000000000golang-github-tcnksm-go-gitconfig-0.1.2/.gitignore000066400000000000000000000000071250540654300221010ustar00rootroot00000000000000*.test golang-github-tcnksm-go-gitconfig-0.1.2/CHANGELOG.md000066400000000000000000000016031250540654300217250ustar00rootroot00000000000000## 0.1.2 (2015-03-28) Add new functions ### Added - `GithubUser()` extracts `github.user` ([**dstokes**](https://github.com/dstokes)), [#5](https://github.com/tcnksm/go-gitconfig/pull/5/commits) ### Deprecated - Nothing ### Removed - Nothing ### Fixed - Nothing ## 0.1.1 (2014-10-28) Add new functions ### Added - `GithubToken()` extracts `github.token` ([**@sona-tar**](https://github.com/sona-tar), [#3](https://github.com/tcnksm/go-gitconfig/pull/3)) - `Entire()` try to extract value from entire git config. It's able to extract values from included config ([**@sona-tar**](https://github.com/sona-tar), [#3](https://github.com/tcnksm/go-gitconfig/pull/3)) ### Deprecated - Nothing ### Removed - Nothing ### Fixed - Nothing ## 0.1.0 (2014-09-08) Initial release ### Added - Fundamental features ### Deprecated - Nothing ### Removed - Nothing ### Fixed - Nothing golang-github-tcnksm-go-gitconfig-0.1.2/LICENSE000066400000000000000000000020461250540654300211230ustar00rootroot00000000000000Copyright (c) 2014 tcnksm MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.golang-github-tcnksm-go-gitconfig-0.1.2/README.md000066400000000000000000000052451250540654300214010ustar00rootroot00000000000000go-gitconfig ==== [![GitHub release](http://img.shields.io/github/release/tcnksm/go-gitconfig.svg?style=flat-square)][release] [![Wercker](http://img.shields.io/wercker/ci/544ee33aea87f6374f001483.svg?style=flat-square)][wercker] [![Coveralls](http://img.shields.io/coveralls/tcnksm/go-gitconfig.svg?style=flat-square)][coveralls] [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)][license] [![Go Documentation](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)][godocs] [release]: https://github.com/tcnksm/go-gitconfig/releases [wercker]: https://app.wercker.com/project/bykey/89c5a6e50a0daceec971ff5ce210164a [coveralls]: https://coveralls.io/r/tcnksm/go-gitconfig [license]: https://github.com/tcnksm/go-gitconfig/blob/master/LICENSE [godocs]: http://godoc.org/github.com/tcnksm/go-gitconfig `go-gitconfig` is a pacakge to use `gitconfig` values in Golang. Sometimes you want to extract username or its email address **implicitly** in your tool. Now most of developer use `git`, so we can use its configuration variables. `go-gitconfig` is for that. `go-gitconfig` is very small, so it may not be included what you want to use. If you want to use more git specific variable, check [Other](##VS). ## Usage If you want to use git user name defined in `~/.gitconfig`: ```go username, err := gitconfig.Username() ``` Or git user email defined in `~/.gitconfig`: ```go email, err := gitconfig.Email() ``` Or, if you want to extract origin url of current project (from `.git/config`): ```go url, err := gitconfig.OriginURL() ``` You can also extract value by key: ```go editor, err := gitconfig.Global("core.editor") ``` ```go remote, err := gitconfig.Local("branch.master.remote") ``` See more details in document at [https://godoc.org/github.com/tcnksm/go-gitconfig](https://godoc.org/github.com/tcnksm/go-gitconfig). ## Install To install, use `go get`: ```bash $ go get -d github.com/tcnksm/go-gitconfig ``` ## VS. - [speedata/gogit](https://github.com/speedata/gogit) - [libgit2/git2go](https://github.com/libgit2/git2go) These packages have many features to use git from golang. `go-gitconfig` is very simple alternative and focus to extract information from gitconfig. `go-gitconfig` is used in [tcnksm/ghr](https://github.com/tcnksm/ghr). ## Contribution 1. Fork ([https://github.com/tcnksm/go-gitconfig/fork](https://github.com/tcnksm/go-gitconfig/fork)) 1. Create a feature branch 1. Commit your changes 1. Rebase your local changes against the master branch 1. Run test suite with the `go test ./...` command and confirm that it passes 1. Run `gofmt -s` 1. Create new Pull Request ## Author [tcnksm](https://github.com/tcnksm) golang-github-tcnksm-go-gitconfig-0.1.2/gitconfig.go000066400000000000000000000054671250540654300224300ustar00rootroot00000000000000// Package gitconfig enables you to use `~/.gitconfig` values in Golang. // // For a full guide visit http://github.com/tcnksm/go-gitconfig // // package main // // import ( // "github.com/tcnksm/go-gitconfig" // "fmt" // ) // // func main() { // user, err := gitconfig.Global("user.name") // if err == nil { // fmt.Println(user) // } // } // package gitconfig import ( "bytes" "fmt" "io/ioutil" "os/exec" "regexp" "strings" "syscall" ) // Entire extracts configuration value from `$HOME/.gitconfig` file , // `$GIT_CONFIG`, /etc/gitconfig or include.path files. func Entire(key string) (string, error) { return execGitConfig(key) } // Global extracts configuration value from `$HOME/.gitconfig` file or `$GIT_CONFIG`. func Global(key string) (string, error) { return execGitConfig("--global", key) } // Local extracts configuration value from current project repository. func Local(key string) (string, error) { return execGitConfig("--local", key) } // GithubUser extracts github.user name from `Entire gitconfig` // This is same as Entire("github.user") func GithubUser() (string, error) { return Entire("github.user") } // Username extracts git user name from `Entire gitconfig`. // This is same as Entire("user.name") func Username() (string, error) { return Entire("user.name") } // Email extracts git user email from `$HOME/.gitconfig` file or `$GIT_CONFIG`. // This is same as Global("user.email") func Email() (string, error) { return Entire("user.email") } // OriginURL extract remote origin url from current project repository. // This is same as Local("remote.origin.url") func OriginURL() (string, error) { return Local("remote.origin.url") } // Repository extract repository name of current project repository. func Repository() (string, error) { url, err := OriginURL() if err != nil { return "", err } repo := retrieveRepoName(url) return repo, nil } // Github extracts github token from `Entire gitconfig`. // This is same as Entire("github.token") func GithubToken() (string, error) { return Entire("github.token") } func execGitConfig(args ...string) (string, error) { gitArgs := append([]string{"config", "--get", "--null"}, args...) var stdout bytes.Buffer cmd := exec.Command("git", gitArgs...) cmd.Stdout = &stdout cmd.Stderr = ioutil.Discard err := cmd.Run() if exitError, ok := err.(*exec.ExitError); ok { if waitStatus, ok := exitError.Sys().(syscall.WaitStatus); ok { if waitStatus.ExitStatus() == 1 { return "", fmt.Errorf("the key `%s` is not found", args[len(args)-1]) } } return "", err } return strings.TrimRight(stdout.String(), "\000"), nil } var RepoNameRegexp = regexp.MustCompile(`.+/([^/]+)(\.git)?$`) func retrieveRepoName(url string) string { matched := RepoNameRegexp.FindStringSubmatch(url) return strings.TrimSuffix(matched[1], ".git") } golang-github-tcnksm-go-gitconfig-0.1.2/gitconfig_test.go000066400000000000000000000072621250540654300234620ustar00rootroot00000000000000package gitconfig import ( "fmt" . "github.com/onsi/gomega" "testing" ) func TestGlobal(t *testing.T) { RegisterTestingT(t) reset := withGlobalGitConfigFile(` [user] name = deeeet email = deeeet@example.com [github] user = ghdeeeet `) defer reset() var err error username, err := Global("user.name") Expect(err).NotTo(HaveOccurred()) Expect(username).To(Equal("deeeet")) email, err := Global("user.email") Expect(err).NotTo(HaveOccurred()) Expect(email).To(Equal("deeeet@example.com")) githubuser, err := Global("github.user") Expect(err).NotTo(HaveOccurred()) Expect(githubuser).To(Equal("ghdeeeet")) nothing, err := Local("nothing.return") Expect(err).To(HaveOccurred()) Expect(nothing).To(Equal("")) } func TestEntire(t *testing.T) { RegisterTestingT(t) includeFilePath := includeGitConfigFile(` [user] name = deeeet email = deeeet@example.com `) content := fmt.Sprintf(` [include] path = %s `, includeFilePath) reset := withGlobalGitConfigFile(content) defer reset() var err error username, err := Entire("user.name") Expect(err).NotTo(HaveOccurred()) Expect(username).To(Equal("deeeet")) email, err := Entire("user.email") Expect(err).NotTo(HaveOccurred()) Expect(email).To(Equal("deeeet@example.com")) nothing, err := Local("nothing.return") Expect(err).To(HaveOccurred()) Expect(nothing).To(Equal("")) } func TestLocal(t *testing.T) { RegisterTestingT(t) reset := withLocalGitConfigFile("remote.origin.url", "git@github.com:tcnksm/go-test-gitconfig.git") defer reset() var err error url, err := Local("remote.origin.url") Expect(err).NotTo(HaveOccurred()) Expect(url).To(Equal("git@github.com:tcnksm/go-test-gitconfig.git")) nothing, err := Local("nothing.return") Expect(err).To(HaveOccurred()) Expect(err.Error()).To(Equal("the key `nothing.return` is not found")) Expect(nothing).To(Equal("")) } func TestUsername(t *testing.T) { RegisterTestingT(t) reset := withGlobalGitConfigFile(` [user] name = taichi email = taichi@example.com `) defer reset() var err error username, err := Username() Expect(err).NotTo(HaveOccurred()) Expect(username).To(Equal("taichi")) } func TestEmail(t *testing.T) { RegisterTestingT(t) reset := withGlobalGitConfigFile(` [user] name = taichi email = taichi@example.com `) defer reset() var err error username, err := Email() Expect(err).NotTo(HaveOccurred()) Expect(username).To(Equal("taichi@example.com")) } func TestGithubToken(t *testing.T) { RegisterTestingT(t) reset := withGlobalGitConfigFile(` [github] token = 16c999e8c71134401a78d4d46435517b2271d6ac `) defer reset() var err error token, err := GithubToken() Expect(err).NotTo(HaveOccurred()) Expect(token).To(Equal("16c999e8c71134401a78d4d46435517b2271d6ac")) } func TestOriginURL(t *testing.T) { RegisterTestingT(t) reset := withLocalGitConfigFile("remote.origin.url", "git@github.com:taichi/gitconfig.git") defer reset() var err error url, err := OriginURL() Expect(err).NotTo(HaveOccurred()) Expect(url).To(Equal("git@github.com:taichi/gitconfig.git")) } func TestRepository(t *testing.T) { RegisterTestingT(t) reset := withLocalGitConfigFile("remote.origin.url", "git@github.com:taichi/gitconfig.git") defer reset() var err error repository, err := Repository() Expect(err).NotTo(HaveOccurred()) Expect(repository).To(Equal("gitconfig")) } func TestRetrieveRepoName(t *testing.T) { RegisterTestingT(t) repo := retrieveRepoName("https://github.com/tcnksm/ghr.git") Expect(repo).To(Equal("ghr")) repo = retrieveRepoName("https://github.com/tcnksm/ghr") Expect(repo).To(Equal("ghr")) repo = retrieveRepoName("git@github.com:taichi/gitconfig.git") Expect(repo).To(Equal("gitconfig")) } golang-github-tcnksm-go-gitconfig-0.1.2/helpers_test.go000066400000000000000000000026131250540654300231460ustar00rootroot00000000000000package gitconfig import ( "io/ioutil" "os" "os/exec" "path/filepath" ) func withGlobalGitConfigFile(content string) func() { tmpdir, err := ioutil.TempDir("", "go-gitconfig-test") if err != nil { panic(err) } tmpGitConfigFile := filepath.Join(tmpdir, ".gitconfig") ioutil.WriteFile( tmpGitConfigFile, []byte(content), 0777, ) prevGitConfigEnv := os.Getenv("HOME") os.Setenv("HOME", tmpdir) return func() { os.Setenv("HOME", prevGitConfigEnv) } } func includeGitConfigFile(content string) string { tmpdir, err := ioutil.TempDir("", "go-gitconfig-test") if err != nil { panic(err) } tmpGitIncludeConfigFile := filepath.Join(tmpdir, ".gitconfig.local") ioutil.WriteFile( tmpGitIncludeConfigFile, []byte(content), 0777, ) return tmpGitIncludeConfigFile } func withLocalGitConfigFile(key string, value string) func() { var err error tmpdir, err := ioutil.TempDir(".", "go-gitconfig-test") if err != nil { panic(err) } prevDir, err := filepath.Abs(".") if err != nil { panic(err) } os.Chdir(tmpdir) gitInit := exec.Command("git", "init") gitInit.Stderr = ioutil.Discard if err = gitInit.Run(); err != nil { panic(err) } gitAddConfig := exec.Command("git", "config", "--local", key, value) gitAddConfig.Stderr = ioutil.Discard if err = gitAddConfig.Run(); err != nil { panic(err) } return func() { os.Chdir(prevDir) os.RemoveAll(tmpdir) } } golang-github-tcnksm-go-gitconfig-0.1.2/wercker.yml000066400000000000000000000010161250540654300222770ustar00rootroot00000000000000box: tcnksm/gox build: steps: - setup-go-workspace - script: name: install latest git code: | sudo add-apt-repository -y ppa:git-core/ppa sudo apt-get -y update sudo apt-get -y install git - script: name: git version code: | git version - script: name: go get code: | go get -t ./... - tcnksm/goveralls: token: $COVERALLS_REPO_TOKEN