1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-15 00:15:32 +02:00

remove go-gitconfig package

This commit is contained in:
Jesse Duffield
2020-12-21 09:38:36 +11:00
parent 09f32d4f84
commit 78867647d1
16 changed files with 107 additions and 440 deletions

1
go.mod
View File

@ -34,7 +34,6 @@ require (
github.com/sirupsen/logrus v1.4.2 github.com/sirupsen/logrus v1.4.2
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad
github.com/stretchr/testify v1.4.0 github.com/stretchr/testify v1.4.0
github.com/tcnksm/go-gitconfig v0.1.2
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 // indirect golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 // indirect
golang.org/x/net v0.0.0-20201002202402-0a1ea396d57c // indirect golang.org/x/net v0.0.0-20201002202402-0a1ea396d57c // indirect
golang.org/x/sys v0.0.0-20201005172224-997123666555 // indirect golang.org/x/sys v0.0.0-20201005172224-997123666555 // indirect

2
go.sum
View File

@ -132,8 +132,6 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tcnksm/go-gitconfig v0.1.2 h1:iiDhRitByXAEyjgBqsKi9QU4o2TNtv9kPP3RgPgXBPw=
github.com/tcnksm/go-gitconfig v0.1.2/go.mod h1:/8EhP4H7oJZdIPyT+/UIsG87kTzrzM4UsLGSItWYCpE=
github.com/urfave/cli v1.20.1-0.20180226030253-8e01ec4cd3e2/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.20.1-0.20180226030253-8e01ec4cd3e2/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70= github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70=
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=

View File

@ -43,13 +43,6 @@ func (c *GitCommand) colorArg() string {
} }
func (c *GitCommand) GetConfigValue(key string) string { func (c *GitCommand) GetConfigValue(key string) string {
value, _ := c.getLocalGitConfig(key) output, _ := c.getGitConfigValue(key)
// we get an error if the key doesn't exist which we don't care about return output
if value != "" {
return value
}
value, _ = c.getGlobalGitConfig(key)
return value
} }

View File

@ -15,12 +15,11 @@ func NewDummyGitCommand() *GitCommand {
// NewDummyGitCommandWithOSCommand creates a new dummy GitCommand for testing // NewDummyGitCommandWithOSCommand creates a new dummy GitCommand for testing
func NewDummyGitCommandWithOSCommand(osCommand *oscommands.OSCommand) *GitCommand { func NewDummyGitCommandWithOSCommand(osCommand *oscommands.OSCommand) *GitCommand {
return &GitCommand{ return &GitCommand{
Log: utils.NewDummyLog(), Log: utils.NewDummyLog(),
OSCommand: osCommand, OSCommand: osCommand,
Tr: i18n.NewTranslationSet(utils.NewDummyLog()), Tr: i18n.NewTranslationSet(utils.NewDummyLog()),
Config: config.NewDummyAppConfig(), Config: config.NewDummyAppConfig(),
getGlobalGitConfig: func(string) (string, error) { return "", nil }, getGitConfigValue: func(string) (string, error) { return "", nil },
getLocalGitConfig: func(string) (string, error) { return "", nil }, removeFile: func(string) error { return nil },
removeFile: func(string) error { return nil },
} }
} }

View File

@ -268,7 +268,7 @@ func (c *GitCommand) ResetAndClean() error {
// EditFile opens a file in a subprocess using whatever editor is available, // EditFile opens a file in a subprocess using whatever editor is available,
// falling back to core.editor, VISUAL, EDITOR, then vi // falling back to core.editor, VISUAL, EDITOR, then vi
func (c *GitCommand) EditFile(filename string) (*exec.Cmd, error) { func (c *GitCommand) EditFile(filename string) (*exec.Cmd, error) {
editor, _ := c.getGlobalGitConfig("core.editor") editor := c.GetConfigValue("core.editor")
if editor == "" { if editor == "" {
editor = c.OSCommand.Getenv("VISUAL") editor = c.OSCommand.Getenv("VISUAL")

View File

@ -16,7 +16,6 @@ import (
"github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
gitconfig "github.com/tcnksm/go-gitconfig"
) )
// this takes something like: // this takes something like:
@ -32,8 +31,7 @@ type GitCommand struct {
Repo *gogit.Repository Repo *gogit.Repository
Tr *i18n.TranslationSet Tr *i18n.TranslationSet
Config config.AppConfigurer Config config.AppConfigurer
getGlobalGitConfig func(string) (string, error) getGitConfigValue func(string) (string, error)
getLocalGitConfig func(string) (string, error)
removeFile func(string) error removeFile func(string) error
DotGitDir string DotGitDir string
onSuccessfulContinue func() error onSuccessfulContinue func() error
@ -74,16 +72,15 @@ func NewGitCommand(log *logrus.Entry, osCommand *oscommands.OSCommand, tr *i18n.
} }
gitCommand := &GitCommand{ gitCommand := &GitCommand{
Log: log, Log: log,
OSCommand: osCommand, OSCommand: osCommand,
Tr: tr, Tr: tr,
Repo: repo, Repo: repo,
Config: config, Config: config,
getGlobalGitConfig: gitconfig.Global, getGitConfigValue: getGitConfigValue,
getLocalGitConfig: gitconfig.Local, removeFile: os.RemoveAll,
removeFile: os.RemoveAll, DotGitDir: dotGitDir,
DotGitDir: dotGitDir, PushToCurrent: pushToCurrent,
PushToCurrent: pushToCurrent,
} }
gitCommand.PatchManager = patch.NewPatchManager(log, gitCommand.ApplyPatch, gitCommand.ShowFileDiff) gitCommand.PatchManager = patch.NewPatchManager(log, gitCommand.ApplyPatch, gitCommand.ShowFileDiff)

View File

@ -700,45 +700,24 @@ func TestGitCommandMerge(t *testing.T) {
// TestGitCommandUsingGpg is a function. // TestGitCommandUsingGpg is a function.
func TestGitCommandUsingGpg(t *testing.T) { func TestGitCommandUsingGpg(t *testing.T) {
type scenario struct { type scenario struct {
testName string testName string
getLocalGitConfig func(string) (string, error) getGitConfigValue func(string) (string, error)
getGlobalGitConfig func(string) (string, error) test func(bool)
test func(bool)
} }
scenarios := []scenario{ scenarios := []scenario{
{ {
"Option global and local config commit.gpgsign is not set", "Option global and local config commit.gpgsign is not set",
func(string) (string, error) { func(string) (string, error) { return "", nil },
return "", nil
},
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) { func(gpgEnabled bool) {
assert.False(t, gpgEnabled) assert.False(t, gpgEnabled)
}, },
}, },
{
"Option global config commit.gpgsign is not set, fallback on local config",
func(string) (string, error) {
return "", nil
},
func(string) (string, error) {
return "true", nil
},
func(gpgEnabled bool) {
assert.True(t, gpgEnabled)
},
},
{ {
"Option commit.gpgsign is true", "Option commit.gpgsign is true",
func(string) (string, error) { func(string) (string, error) {
return "True", nil return "True", nil
}, },
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) { func(gpgEnabled bool) {
assert.True(t, gpgEnabled) assert.True(t, gpgEnabled)
}, },
@ -748,9 +727,6 @@ func TestGitCommandUsingGpg(t *testing.T) {
func(string) (string, error) { func(string) (string, error) {
return "ON", nil return "ON", nil
}, },
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) { func(gpgEnabled bool) {
assert.True(t, gpgEnabled) assert.True(t, gpgEnabled)
}, },
@ -760,9 +736,6 @@ func TestGitCommandUsingGpg(t *testing.T) {
func(string) (string, error) { func(string) (string, error) {
return "YeS", nil return "YeS", nil
}, },
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) { func(gpgEnabled bool) {
assert.True(t, gpgEnabled) assert.True(t, gpgEnabled)
}, },
@ -772,9 +745,6 @@ func TestGitCommandUsingGpg(t *testing.T) {
func(string) (string, error) { func(string) (string, error) {
return "1", nil return "1", nil
}, },
func(string) (string, error) {
return "", nil
},
func(gpgEnabled bool) { func(gpgEnabled bool) {
assert.True(t, gpgEnabled) assert.True(t, gpgEnabled)
}, },
@ -784,8 +754,7 @@ func TestGitCommandUsingGpg(t *testing.T) {
for _, s := range scenarios { for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) { t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand() gitCmd := NewDummyGitCommand()
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig gitCmd.getGitConfigValue = s.getGitConfigValue
gitCmd.getLocalGitConfig = s.getLocalGitConfig
s.test(gitCmd.usingGpg()) s.test(gitCmd.usingGpg())
}) })
} }
@ -794,11 +763,11 @@ func TestGitCommandUsingGpg(t *testing.T) {
// TestGitCommandCommit is a function. // TestGitCommandCommit is a function.
func TestGitCommandCommit(t *testing.T) { func TestGitCommandCommit(t *testing.T) {
type scenario struct { type scenario struct {
testName string testName string
command func(string, ...string) *exec.Cmd command func(string, ...string) *exec.Cmd
getGlobalGitConfig func(string) (string, error) getGitConfigValue func(string) (string, error)
test func(*exec.Cmd, error) test func(*exec.Cmd, error)
flags string flags string
} }
scenarios := []scenario{ scenarios := []scenario{
@ -875,7 +844,7 @@ func TestGitCommandCommit(t *testing.T) {
for _, s := range scenarios { for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) { t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand() gitCmd := NewDummyGitCommand()
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig gitCmd.getGitConfigValue = s.getGitConfigValue
gitCmd.OSCommand.Command = s.command gitCmd.OSCommand.Command = s.command
s.test(gitCmd.Commit("test", s.flags)) s.test(gitCmd.Commit("test", s.flags))
}) })
@ -885,10 +854,10 @@ func TestGitCommandCommit(t *testing.T) {
// TestGitCommandAmendHead is a function. // TestGitCommandAmendHead is a function.
func TestGitCommandAmendHead(t *testing.T) { func TestGitCommandAmendHead(t *testing.T) {
type scenario struct { type scenario struct {
testName string testName string
command func(string, ...string) *exec.Cmd command func(string, ...string) *exec.Cmd
getGlobalGitConfig func(string) (string, error) getGitConfigValue func(string) (string, error)
test func(*exec.Cmd, error) test func(*exec.Cmd, error)
} }
scenarios := []scenario{ scenarios := []scenario{
@ -945,7 +914,7 @@ func TestGitCommandAmendHead(t *testing.T) {
for _, s := range scenarios { for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) { t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand() gitCmd := NewDummyGitCommand()
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig gitCmd.getGitConfigValue = s.getGitConfigValue
gitCmd.OSCommand.Command = s.command gitCmd.OSCommand.Command = s.command
s.test(gitCmd.AmendHead()) s.test(gitCmd.AmendHead())
}) })
@ -955,12 +924,11 @@ func TestGitCommandAmendHead(t *testing.T) {
// TestGitCommandPush is a function. // TestGitCommandPush is a function.
func TestGitCommandPush(t *testing.T) { func TestGitCommandPush(t *testing.T) {
type scenario struct { type scenario struct {
testName string testName string
getLocalGitConfig func(string) (string, error) getGitConfigValue func(string) (string, error)
getGlobalGitConfig func(string) (string, error) command func(string, ...string) *exec.Cmd
command func(string, ...string) *exec.Cmd forcePush bool
forcePush bool test func(error)
test func(error)
} }
scenarios := []scenario{ scenarios := []scenario{
@ -969,9 +937,6 @@ func TestGitCommandPush(t *testing.T) {
func(string) (string, error) { func(string) (string, error) {
return "", nil return "", nil
}, },
func(string) (string, error) {
return "", nil
},
func(cmd string, args ...string) *exec.Cmd { func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd) assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"push", "--follow-tags"}, args) assert.EqualValues(t, []string{"push", "--follow-tags"}, args)
@ -988,9 +953,6 @@ func TestGitCommandPush(t *testing.T) {
func(string) (string, error) { func(string) (string, error) {
return "", nil return "", nil
}, },
func(string) (string, error) {
return "", nil
},
func(cmd string, args ...string) *exec.Cmd { func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd) assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"push", "--follow-tags", "--force-with-lease"}, args) assert.EqualValues(t, []string{"push", "--follow-tags", "--force-with-lease"}, args)
@ -1003,13 +965,10 @@ func TestGitCommandPush(t *testing.T) {
}, },
}, },
{ {
"Push with force disabled, follow-tags off locally", "Push with force disabled, follow-tags off",
func(string) (string, error) { func(string) (string, error) {
return "false", nil return "false", nil
}, },
func(string) (string, error) {
return "", nil
},
func(cmd string, args ...string) *exec.Cmd { func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd) assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"push"}, args) assert.EqualValues(t, []string{"push"}, args)
@ -1021,33 +980,11 @@ func TestGitCommandPush(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
{
"Push with force enabled, follow-tags off globally",
func(string) (string, error) {
return "", nil
},
func(string) (string, error) {
return "false", nil
},
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"push", "--force-with-lease"}, args)
return secureexec.Command("echo")
},
true,
func(err error) {
assert.NoError(t, err)
},
},
{ {
"Push with an error occurring, follow-tags on", "Push with an error occurring, follow-tags on",
func(string) (string, error) { func(string) (string, error) {
return "", nil return "", nil
}, },
func(string) (string, error) {
return "", nil
},
func(cmd string, args ...string) *exec.Cmd { func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd) assert.EqualValues(t, "git", cmd)
assert.EqualValues(t, []string{"push", "--follow-tags"}, args) assert.EqualValues(t, []string{"push", "--follow-tags"}, args)
@ -1064,8 +1001,7 @@ func TestGitCommandPush(t *testing.T) {
t.Run(s.testName, func(t *testing.T) { t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand() gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.Command = s.command gitCmd.OSCommand.Command = s.command
gitCmd.getLocalGitConfig = s.getLocalGitConfig gitCmd.getGitConfigValue = s.getGitConfigValue
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig
err := gitCmd.Push("test", s.forcePush, "", "", func(passOrUname string) string { err := gitCmd.Push("test", s.forcePush, "", "", func(passOrUname string) string {
return "\n" return "\n"
}) })
@ -1790,7 +1726,7 @@ func TestGitCommandCheckoutFile(t *testing.T) {
func TestGitCommandDiscardOldFileChanges(t *testing.T) { func TestGitCommandDiscardOldFileChanges(t *testing.T) {
type scenario struct { type scenario struct {
testName string testName string
getLocalGitConfig func(string) (string, error) getGitConfigValue func(string) (string, error)
commits []*models.Commit commits []*models.Commit
commitIndex int commitIndex int
fileName string fileName string
@ -1871,7 +1807,7 @@ func TestGitCommandDiscardOldFileChanges(t *testing.T) {
for _, s := range scenarios { for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) { t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.Command = s.command gitCmd.OSCommand.Command = s.command
gitCmd.getLocalGitConfig = s.getLocalGitConfig gitCmd.getGitConfigValue = s.getGitConfigValue
s.test(gitCmd.DiscardOldFileChanges(s.commits, s.commitIndex, s.fileName)) s.test(gitCmd.DiscardOldFileChanges(s.commits, s.commitIndex, s.fileName))
}) })
} }
@ -2164,11 +2100,11 @@ func TestFindDotGitDir(t *testing.T) {
// TestEditFile is a function. // TestEditFile is a function.
func TestEditFile(t *testing.T) { func TestEditFile(t *testing.T) {
type scenario struct { type scenario struct {
filename string filename string
command func(string, ...string) *exec.Cmd command func(string, ...string) *exec.Cmd
getenv func(string) string getenv func(string) string
getGlobalGitConfig func(string) (string, error) getGitConfigValue func(string) (string, error)
test func(*exec.Cmd, error) test func(*exec.Cmd, error)
} }
scenarios := []scenario{ scenarios := []scenario{
@ -2307,7 +2243,7 @@ func TestEditFile(t *testing.T) {
gitCmd := NewDummyGitCommand() gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.Command = s.command gitCmd.OSCommand.Command = s.command
gitCmd.OSCommand.Getenv = s.getenv gitCmd.OSCommand.Getenv = s.getenv
gitCmd.getGlobalGitConfig = s.getGlobalGitConfig gitCmd.getGitConfigValue = s.getGitConfigValue
s.test(gitCmd.EditFile(s.filename)) s.test(gitCmd.EditFile(s.filename))
} }
} }

56
pkg/commands/gitconfig.go Normal file
View File

@ -0,0 +1,56 @@
package commands
import (
"bytes"
"fmt"
"io/ioutil"
"os/exec"
"strings"
"syscall"
"github.com/jesseduffield/lazygit/pkg/secureexec"
)
// including license from https://github.com/tcnksm/go-gitconfig because this file is an adaptation of that repo's code
// Copyright (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.
func getGitConfigValue(key string) (string, error) {
gitArgs := append([]string{"config", "--get", "--null", key})
var stdout bytes.Buffer
cmd := secureexec.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", key)
}
}
return "", err
}
return strings.TrimRight(stdout.String(), "\000"), nil
}

View File

@ -162,14 +162,10 @@ func TestCreatePullRequest(t *testing.T) {
"invalid.work.com": "noservice:invalid.work.com", "invalid.work.com": "noservice:invalid.work.com",
"noservice.work.com": "noservice.work.com", "noservice.work.com": "noservice.work.com",
} }
gitCommand.getLocalGitConfig = func(path string) (string, error) { gitCommand.getGitConfigValue = func(path string) (string, error) {
assert.Equal(t, path, "remote.origin.url") assert.Equal(t, path, "remote.origin.url")
return s.remoteUrl, nil return s.remoteUrl, nil
} }
gitCommand.getGlobalGitConfig = func(path string) (string, error) {
assert.Equal(t, path, "remote.origin.url")
return "", nil
}
dummyPullRequest := NewPullRequest(gitCommand) dummyPullRequest := NewPullRequest(gitCommand)
s.test(dummyPullRequest.Create(s.branch)) s.test(dummyPullRequest.Create(s.branch))
}) })

View File

@ -1 +0,0 @@
*.test

View File

@ -1,64 +0,0 @@
## 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

View File

@ -1,22 +0,0 @@
Copyright (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.

View File

@ -1,84 +0,0 @@
go-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)

View File

@ -1,113 +0,0 @@
// 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")
}

View File

@ -1,20 +0,0 @@
box: 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

3
vendor/modules.txt vendored
View File

@ -161,9 +161,6 @@ github.com/spkg/bom
# github.com/stretchr/testify v1.4.0 # github.com/stretchr/testify v1.4.0
## explicit ## explicit
github.com/stretchr/testify/assert github.com/stretchr/testify/assert
# github.com/tcnksm/go-gitconfig v0.1.2
## explicit
github.com/tcnksm/go-gitconfig
# github.com/xanzy/ssh-agent v0.2.1 # github.com/xanzy/ssh-agent v0.2.1
github.com/xanzy/ssh-agent github.com/xanzy/ssh-agent
# golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 # golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0