Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix git invocation #140

Merged
merged 1 commit into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,30 @@ func New(v string) *Action {
name = filepath.Base(os.Args[0])
}

debug := false
if gdb := os.Getenv("GOPASS_DEBUG"); gdb == "true" {
gpg.Debug = true
debug = true
}
noColor := false
// need this override for our integration tests
if nc := os.Getenv("GOPASS_NOCOLOR"); nc == "true" {
noColor = true
}
// only emit color codes when stdout is a terminal
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
noColor = true
}

// try to read config (if it exists)
if cfg, err := config.Load(); err == nil && cfg != nil {
cfg.ImportFunc = askForKeyImport
cfg.FsckFunc = askForConfirmation
cfg.Version = v
cfg.Debug = debug
color.NoColor = cfg.NoColor
// need this override for our integration tests
if nc := os.Getenv("GOPASS_NOCOLOR"); nc == "true" {
color.NoColor = true
}
// only emit color codes when stdout is a terminal
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
color.NoColor = true
if noColor {
color.NoColor = noColor
}
store, err := root.New(cfg)
if err != nil {
Expand All @@ -59,18 +66,12 @@ func New(v string) *Action {
cfg.Path = pwStoreDir("")
cfg.ImportFunc = askForKeyImport
cfg.FsckFunc = askForConfirmation
cfg.Debug = debug
rs, err := root.New(cfg)
if err != nil {
panic(err)
}

// need this override for our integration tests
if nc := os.Getenv("GOPASS_NOCOLOR"); nc == "true" {
color.NoColor = true
}
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
color.NoColor = true
}
color.NoColor = noColor

return &Action{
Name: name,
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func Load() (*Config, error) {
for _, l := range configLocations() {
if cfg, err := load(l); err == nil {
if gdb := os.Getenv("GOPASS_DEBUG"); gdb == "true" {
fmt.Printf("Loaded config from %s: %+v\n", l, cfg)
fmt.Printf("[DEBUG] Loaded config from %s: %+v\n", l, cfg)
}
return cfg, err
}
Expand Down
14 changes: 7 additions & 7 deletions gpg/gpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func listKeys(typ string, search ...string) (KeyList, error) {
args = append(args, search...)
cmd := exec.Command(GPGBin, args...)
if Debug {
fmt.Printf("gpg.listKeys: %s %+v\n", cmd.Path, cmd.Args)
fmt.Printf("[DEBUG] gpg.listKeys: %s %+v\n", cmd.Path, cmd.Args)
}
out, err := cmd.CombinedOutput()
if err != nil {
Expand Down Expand Up @@ -257,7 +257,7 @@ func GetRecipients(file string) ([]string, error) {
args := []string{"--batch", "--list-only", "--list-packets", "--no-default-keyring", "--secret-keyring", "/dev/null", file}
cmd := exec.Command(GPGBin, args...)
if Debug {
fmt.Printf("gpg.GetRecipients: %s %+v\n", cmd.Path, cmd.Args)
fmt.Printf("[DEBUG] gpg.GetRecipients: %s %+v\n", cmd.Path, cmd.Args)
}
out, err := cmd.CombinedOutput()
if err != nil {
Expand All @@ -268,7 +268,7 @@ func GetRecipients(file string) ([]string, error) {
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if Debug {
fmt.Printf("gpg Out: %s\n", line)
fmt.Printf("[DEBUG] gpg Output: %s\n", line)
}
if !strings.HasPrefix(line, ":pubkey enc packet:") {
continue
Expand Down Expand Up @@ -315,7 +315,7 @@ func Encrypt(path string, content []byte, recipients []string, alwaysTrust bool)

cmd := exec.Command(GPGBin, args...)
if Debug {
fmt.Printf("gpg.Encrypt: %s %+v\n", cmd.Path, cmd.Args)
fmt.Printf("[DEBUG] gpg.Encrypt: %s %+v\n", cmd.Path, cmd.Args)
}
cmd.Stdin = bytes.NewReader(content)
cmd.Stdout = os.Stdout
Expand All @@ -329,7 +329,7 @@ func Decrypt(path string) ([]byte, error) {
args := append(GPGArgs, "--decrypt", path)
cmd := exec.Command(GPGBin, args...)
if Debug {
fmt.Printf("gpg.Decrypt: %s %+v\n", cmd.Path, cmd.Args)
fmt.Printf("[DEBUG] gpg.Decrypt: %s %+v\n", cmd.Path, cmd.Args)
}
return cmd.Output()
}
Expand All @@ -339,7 +339,7 @@ func ExportPublicKey(id, filename string) error {
args := append(GPGArgs, "--armor", "--export", id)
cmd := exec.Command(GPGBin, args...)
if Debug {
fmt.Printf("gpg.ExportPublicKey: %s %+v\n", cmd.Path, cmd.Args)
fmt.Printf("[DEBUG] gpg.ExportPublicKey: %s %+v\n", cmd.Path, cmd.Args)
}
out, err := cmd.Output()
if err != nil {
Expand All @@ -359,7 +359,7 @@ func ImportPublicKey(filename string) error {
args := append(GPGArgs, "--import")
cmd := exec.Command(GPGBin, args...)
if Debug {
fmt.Printf("gpg.ImportPublicKey: %s %+v\n", cmd.Path, cmd.Args)
fmt.Printf("[DEBUG] gpg.ImportPublicKey: %s %+v\n", cmd.Path, cmd.Args)
}
cmd.Stdin = bytes.NewReader(buf)
cmd.Stdout = os.Stdout
Expand Down
2 changes: 2 additions & 0 deletions store/root/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func (s *Store) Config() *config.Config {
AutoPull: s.autoPull,
AutoPush: s.autoPush,
ClipTimeout: s.clipTimeout,
Debug: s.debug,
LoadKeys: s.loadKeys,
Mounts: make(map[string]string, len(s.mounts)),
NoColor: s.noColor,
Expand All @@ -41,6 +42,7 @@ func (s *Store) UpdateConfig(cfg *config.Config) error {
s.autoImport = cfg.AutoImport
s.autoPull = cfg.AutoPull
s.autoPush = cfg.AutoPush
s.debug = cfg.Debug
s.clipTimeout = cfg.ClipTimeout
s.loadKeys = cfg.LoadKeys
s.noColor = cfg.NoColor
Expand Down
2 changes: 1 addition & 1 deletion store/root/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ func (r *Store) GitInit(name, sk string) error {
// Git runs arbitrary git commands on this store and all substores
func (r *Store) Git(name string, args ...string) error {
store := r.getStore(name)
return store.Git(store.Alias(), args...)
return store.Git(args...)
}
1 change: 1 addition & 0 deletions store/root/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func New(cfg *config.Config) (*Store, error) {
safeContent: cfg.SafeContent,
}

// TODO(dschulz) this should be passed down from main, not set here
if d := os.Getenv("GOPASS_DEBUG"); d == "true" {
r.debug = true
}
Expand Down
80 changes: 15 additions & 65 deletions store/sub/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
"github.com/justwatchcom/gopass/store"
)

func (s *Store) gitCmd(args ...string) error {
cmd := exec.Command(args[0], args[1:]...)
func (s *Store) gitCmd(name string, args ...string) error {
cmd := exec.Command("git", args[0:]...)
cmd.Dir = s.path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if s.debug {
fmt.Printf("store.GitInit: %s %+v\n", cmd.Path, cmd.Args)
fmt.Printf("[DEBUG] store.%s: %s %+v\n", name, cmd.Path, cmd.Args)
}
return cmd.Run()
}
Expand All @@ -32,7 +32,7 @@ func (s *Store) GitInit(alias, signKey string) error {
// the git repo may be empty (i.e. no branches, cloned from a fresh remote)
// or already initialized. Only run git init if the folder is completely empty
if !s.isGit() {
if err := s.gitCmd("git", "init"); err != nil {
if err := s.gitCmd("GitInit", "init"); err != nil {
return fmt.Errorf("Failed to initialize git: %s", err)
}
}
Expand All @@ -55,10 +55,10 @@ func (s *Store) GitInit(alias, signKey string) error {
}

// setup for proper diffs
if err := s.gitCmd("git", "config", "--local", "diff.gpg.binary", "true"); err != nil {
if err := s.gitCmd("GitInit", "config", "--local", "diff.gpg.binary", "true"); err != nil {
color.Yellow("Error while initializing git: %s\n", err)
}
if err := s.gitCmd("git", "config", "--local", "diff.gpg.textconv", "gpg --no-tty --decrypt"); err != nil {
if err := s.gitCmd("GitInit", "config", "--local", "diff.gpg.textconv", "gpg --no-tty --decrypt"); err != nil {
color.Yellow("Error while initializing git: %s\n", err)
}

Expand All @@ -75,49 +75,22 @@ func (s *Store) gitSetSignKey(sk string) error {
return fmt.Errorf("SignKey not set")
}

cmd := exec.Command("git", "config", "--local", "user.signingkey", sk)
cmd.Dir = s.path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if s.debug {
fmt.Printf("store.gitSetSignKey: %s %+v\n", cmd.Path, cmd.Args)
}
if err := cmd.Run(); err != nil {
if err := s.gitCmd("gitSetSignKey", "config", "--local", "user.signingkey", sk); err != nil {
return err
}

cmd = exec.Command("git", "config", "--local", "commit.gpgsign", "true")
cmd.Dir = s.path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if s.debug {
fmt.Printf("store.gitSetSignKey: %s %+v\n", cmd.Path, cmd.Args)
}
return cmd.Run()
return s.gitCmd("gitSetSignKey", "config", "--local", "commit.gpgsign", "true")
}

// Git runs arbitrary git commands on this store and all substores
func (s *Store) Git(alias string, args ...string) error {
cmd := exec.Command("git", args...)
cmd.Dir = s.path
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr

if s.debug {
fmt.Printf("store.Git: %s %+v\n", cmd.Path, cmd.Args)
}
if err := cmd.Run(); err != nil {
return err
}

return nil
// Git runs arbitrary git commands on this store
func (s *Store) Git(args ...string) error {
return s.gitCmd("Git", args...)
}

// isGit returns true if this stores has a .git folder
func (s *Store) isGit() bool {
// TODO(dschulz) we may want to check if the folder actually contains
// an initialized git setup
return fsutil.IsDir(filepath.Join(s.path, ".git"))
}

Expand All @@ -132,19 +105,8 @@ func (s *Store) gitAdd(files ...string) error {

args := []string{"add", "--all"}
args = append(args, files...)
cmd := exec.Command("git", args...)
cmd.Dir = s.path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if s.debug {
fmt.Printf("store.gitAdd: %s %+v\n", cmd.Path, cmd.Args)
}
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to add files to git: %v", err)
}

return nil
return s.gitCmd("gitAdd", args...)
}

// gitCommit creates a new git commit with the given commit message
Expand All @@ -153,19 +115,7 @@ func (s *Store) gitCommit(msg string) error {
return store.ErrGitNotInit
}

cmd := exec.Command("git", "commit", "-m", msg)
cmd.Dir = s.path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if s.debug {
fmt.Printf("store.gitCommit: %s %+v\n", cmd.Path, cmd.Args)
}
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to commit files to git: %v", err)
}

return nil
return s.gitCmd("gitCommit", "commit", "-m", msg)
}

func (s *Store) gitConfigValue(key string) (string, error) {
Expand Down