forked from gopasspw/gopass
/
git.go
128 lines (105 loc) · 3.68 KB
/
git.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package action
import (
"context"
"os"
"github.com/gopasspw/gopass/pkg/backend"
"github.com/gopasspw/gopass/pkg/cui"
"github.com/gopasspw/gopass/pkg/out"
"github.com/gopasspw/gopass/pkg/termio"
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
// GitInit initializes a git repo including basic configuration
func (s *Action) GitInit(ctx context.Context, c *cli.Context) error {
store := c.String("store")
un := c.String("username")
ue := c.String("useremail")
ctx = backend.WithRCSBackendString(ctx, c.String("rcs"))
// default to git
if !backend.HasRCSBackend(ctx) {
ctx = backend.WithRCSBackend(ctx, backend.GitCLI)
}
if err := s.rcsInit(ctx, store, un, ue); err != nil {
return ExitError(ctx, ExitGit, err, "failed to initialize git: %s", err)
}
return nil
}
func (s *Action) rcsInit(ctx context.Context, store, un, ue string) error {
bn := backend.RCSBackendName(backend.GetRCSBackend(ctx))
out.Green(ctx, "Initializing git repository (%s) ...", bn)
userName, userEmail := s.getUserData(ctx, store, un, ue)
if err := s.Store.GitInit(ctx, store, userName, userEmail); err != nil {
if gtv := os.Getenv("GPG_TTY"); gtv == "" {
out.Yellow(ctx, "Git initialization failed. You may want to try to 'export GPG_TTY=$(tty)' and start over.")
}
return errors.Wrapf(err, "failed to run git init")
}
out.Green(ctx, "Git initialized")
return nil
}
func (s *Action) getUserData(ctx context.Context, store, un, ue string) (string, string) {
if un != "" && ue != "" {
return un, ue
}
// for convenience, set defaults to user-selected values from available private keys
// NB: discarding returned error since this is merely a best-effort look-up for convenience
userName, userEmail, _ := cui.AskForGitConfigUser(ctx, s.Store.Crypto(ctx, store), store)
if userName == "" {
var err error
userName, err = termio.AskForString(ctx, color.CyanString("Please enter a user name for password store git config"), userName)
if err != nil {
out.Error(ctx, "Failed to ask for user input: %s", err)
}
}
if userEmail == "" {
var err error
userEmail, err = termio.AskForString(ctx, color.CyanString("Please enter an email address for password store git config"), userEmail)
if err != nil {
out.Error(ctx, "Failed to ask for user input: %s", err)
}
}
return userName, userEmail
}
// GitAddRemote adds a new git remote
func (s *Action) GitAddRemote(ctx context.Context, c *cli.Context) error {
store := c.String("store")
remote := c.Args().Get(0)
url := c.Args().Get(1)
if remote == "" || url == "" {
return ExitError(ctx, ExitUsage, nil, "Usage: %s git remote add <REMOTE> <URL>", s.Name)
}
return s.Store.GitAddRemote(ctx, store, remote, url)
}
// GitRemoveRemote removes a git remote
func (s *Action) GitRemoveRemote(ctx context.Context, c *cli.Context) error {
store := c.String("store")
remote := c.Args().Get(0)
if remote == "" {
return ExitError(ctx, ExitUsage, nil, "Usage: %s git remote rm <REMOTE>", s.Name)
}
return s.Store.GitRemoveRemote(ctx, store, remote)
}
// GitPull pulls from a git remote
func (s *Action) GitPull(ctx context.Context, c *cli.Context) error {
store := c.String("store")
origin := c.Args().Get(0)
branch := c.Args().Get(1)
if origin == "" {
origin = "origin"
}
if branch == "" {
branch = "master"
}
return s.Store.GitPull(ctx, store, origin, branch)
}
// GitPush pushes to a git remote
func (s *Action) GitPush(ctx context.Context, c *cli.Context) error {
store := c.String("store")
origin := c.Args().Get(0)
branch := c.Args().Get(1)
if origin == "" || branch == "" {
return ExitError(ctx, ExitUsage, nil, "Usage: %s git push <ORIGIN> <BRANCH>", s.Name)
}
return s.Store.GitPush(ctx, store, origin, branch)
}