Skip to content

Commit 4294ee1

Browse files
authored
revert revert 57fbe4f (cli#6474)
1 parent 2cefb9f commit 4294ee1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+458
-422
lines changed

context/context.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package context
33

44
import (
5+
"context"
56
"errors"
67
"sort"
78

@@ -138,7 +139,8 @@ func (r *ResolvedRemotes) BaseRepo(io *iostreams.IOStreams, p iprompter) (ghrepo
138139
}
139140

140141
// cache the result to git config
141-
err := git.SetRemoteResolution(remote.Name, resolution)
142+
c := &git.Client{}
143+
err := c.SetRemoteResolution(context.Background(), remote.Name, resolution)
142144
return selectedRepo, err
143145
}
144146

git/client.go

+132-51
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,16 @@ func (e *NotInstalled) Unwrap() error {
3838
}
3939

4040
type GitError struct {
41-
stderr string
42-
err error
41+
ExitCode int
42+
Stderr string
43+
err error
4344
}
4445

4546
func (ge *GitError) Error() string {
46-
stderr := ge.stderr
47-
if stderr == "" {
48-
var exitError *exec.ExitError
49-
if errors.As(ge.err, &exitError) {
50-
stderr = string(exitError.Stderr)
51-
}
52-
}
53-
if stderr == "" {
47+
if ge.Stderr == "" {
5448
return fmt.Sprintf("failed to run git: %v", ge.err)
5549
}
56-
return fmt.Sprintf("failed to run git: %s", stderr)
50+
return fmt.Sprintf("failed to run git: %s", ge.Stderr)
5751
}
5852

5953
func (ge *GitError) Unwrap() error {
@@ -64,16 +58,77 @@ type gitCommand struct {
6458
*exec.Cmd
6559
}
6660

67-
// This is a hack in order to not break the hundreds of
68-
// existing tests that rely on `run.PrepareCmd` to be invoked.
6961
func (gc *gitCommand) Run() error {
70-
return run.PrepareCmd(gc.Cmd).Run()
62+
// This is a hack in order to not break the hundreds of
63+
// existing tests that rely on `run.PrepareCmd` to be invoked.
64+
err := run.PrepareCmd(gc.Cmd).Run()
65+
if err != nil {
66+
ge := GitError{err: err}
67+
var exitError *exec.ExitError
68+
if errors.As(err, &exitError) {
69+
ge.Stderr = string(exitError.Stderr)
70+
ge.ExitCode = exitError.ExitCode()
71+
}
72+
return &ge
73+
}
74+
return nil
7175
}
7276

73-
// This is a hack in order to not break the hundreds of
74-
// existing tests that rely on `run.PrepareCmd` to be invoked.
7577
func (gc *gitCommand) Output() ([]byte, error) {
76-
return run.PrepareCmd(gc.Cmd).Output()
78+
gc.Stdout = nil
79+
gc.Stderr = nil
80+
// This is a hack in order to not break the hundreds of
81+
// existing tests that rely on `run.PrepareCmd` to be invoked.
82+
out, err := run.PrepareCmd(gc.Cmd).Output()
83+
if err != nil {
84+
ge := GitError{err: err}
85+
var exitError *exec.ExitError
86+
if errors.As(err, &exitError) {
87+
ge.Stderr = string(exitError.Stderr)
88+
ge.ExitCode = exitError.ExitCode()
89+
}
90+
err = &ge
91+
}
92+
return out, err
93+
}
94+
95+
func (gc *gitCommand) setRepoDir(repoDir string) {
96+
for i, arg := range gc.Args {
97+
if arg == "-C" {
98+
gc.Args[i+1] = repoDir
99+
return
100+
}
101+
}
102+
gc.Args = append(gc.Args[:3], gc.Args[1:]...)
103+
gc.Args[1] = "-C"
104+
gc.Args[2] = repoDir
105+
}
106+
107+
// Allow individual commands to be modified from the default client options.
108+
type CommandModifier func(*gitCommand)
109+
110+
func WithStderr(stderr io.Writer) CommandModifier {
111+
return func(gc *gitCommand) {
112+
gc.Stderr = stderr
113+
}
114+
}
115+
116+
func WithStdout(stdout io.Writer) CommandModifier {
117+
return func(gc *gitCommand) {
118+
gc.Stdout = stdout
119+
}
120+
}
121+
122+
func WithStdin(stdin io.Reader) CommandModifier {
123+
return func(gc *gitCommand) {
124+
gc.Stdin = stdin
125+
}
126+
}
127+
128+
func WithRepoDir(repoDir string) CommandModifier {
129+
return func(gc *gitCommand) {
130+
gc.setRepoDir(repoDir)
131+
}
77132
}
78133

79134
type Client struct {
@@ -133,8 +188,7 @@ func resolveGitPath() (string, error) {
133188
// AuthenticatedCommand is a wrapper around Command that included configuration to use gh
134189
// as the credential helper for git.
135190
func (c *Client) AuthenticatedCommand(ctx context.Context, args ...string) (*gitCommand, error) {
136-
preArgs := []string{}
137-
preArgs = append(preArgs, "-c", "credential.helper=")
191+
preArgs := []string{"-c", "credential.helper="}
138192
if c.GhPath == "" {
139193
// Assumes that gh is in PATH.
140194
c.GhPath = "gh"
@@ -153,7 +207,7 @@ func (c *Client) Remotes(ctx context.Context) (RemoteSet, error) {
153207
}
154208
remoteOut, remoteErr := remoteCmd.Output()
155209
if remoteErr != nil {
156-
return nil, &GitError{err: remoteErr}
210+
return nil, remoteErr
157211
}
158212

159213
configArgs := []string{"config", "--get-regexp", `^remote\..*\.gh-resolved$`}
@@ -164,9 +218,9 @@ func (c *Client) Remotes(ctx context.Context) (RemoteSet, error) {
164218
configOut, configErr := configCmd.Output()
165219
if configErr != nil {
166220
// Ignore exit code 1 as it means there are no resolved remotes.
167-
var exitErr *exec.ExitError
168-
if errors.As(configErr, &exitErr) && exitErr.ExitCode() != 1 {
169-
return nil, &GitError{err: configErr}
221+
var gitErr *GitError
222+
if ok := errors.As(configErr, &gitErr); ok && gitErr.ExitCode != 1 {
223+
return nil, gitErr
170224
}
171225
}
172226

@@ -176,18 +230,20 @@ func (c *Client) Remotes(ctx context.Context) (RemoteSet, error) {
176230
return remotes, nil
177231
}
178232

179-
func (c *Client) AddRemote(ctx context.Context, name, urlStr string, trackingBranches []string) (*Remote, error) {
233+
func (c *Client) AddRemote(ctx context.Context, name, urlStr string, trackingBranches []string, mods ...CommandModifier) (*Remote, error) {
180234
args := []string{"remote", "add"}
181235
for _, branch := range trackingBranches {
182236
args = append(args, "-t", branch)
183237
}
184238
args = append(args, "-f", name, urlStr)
185-
//TODO: Use AuthenticatedCommand
186239
cmd, err := c.Command(ctx, args...)
187240
if err != nil {
188241
return nil, err
189242
}
190-
if err := cmd.Run(); err != nil {
243+
for _, mod := range mods {
244+
mod(cmd)
245+
}
246+
if _, err := cmd.Output(); err != nil {
191247
return nil, err
192248
}
193249
var urlParsed *url.URL
@@ -216,7 +272,11 @@ func (c *Client) UpdateRemoteURL(ctx context.Context, name, url string) error {
216272
if err != nil {
217273
return err
218274
}
219-
return cmd.Run()
275+
_, err = cmd.Output()
276+
if err != nil {
277+
return err
278+
}
279+
return nil
220280
}
221281

222282
func (c *Client) SetRemoteResolution(ctx context.Context, name, resolution string) error {
@@ -225,7 +285,11 @@ func (c *Client) SetRemoteResolution(ctx context.Context, name, resolution strin
225285
if err != nil {
226286
return err
227287
}
228-
return cmd.Run()
288+
_, err = cmd.Output()
289+
if err != nil {
290+
return err
291+
}
292+
return nil
229293
}
230294

231295
// CurrentBranch reads the checked-out branch for the git repository.
@@ -235,14 +299,14 @@ func (c *Client) CurrentBranch(ctx context.Context) (string, error) {
235299
if err != nil {
236300
return "", err
237301
}
238-
errBuf := bytes.Buffer{}
239-
cmd.Stderr = &errBuf
240302
out, err := cmd.Output()
241303
if err != nil {
242-
if errBuf.Len() == 0 {
243-
return "", &GitError{err: err, stderr: "not on any branch"}
304+
var gitErr *GitError
305+
if ok := errors.As(err, &gitErr); ok && len(gitErr.Stderr) == 0 {
306+
gitErr.Stderr = "not on any branch"
307+
return "", gitErr
244308
}
245-
return "", &GitError{err: err, stderr: errBuf.String()}
309+
return "", err
246310
}
247311
branch := firstLine(out)
248312
return strings.TrimPrefix(branch, "refs/heads/"), nil
@@ -278,15 +342,14 @@ func (c *Client) Config(ctx context.Context, name string) (string, error) {
278342
if err != nil {
279343
return "", err
280344
}
281-
errBuf := bytes.Buffer{}
282-
cmd.Stderr = &errBuf
283345
out, err := cmd.Output()
284346
if err != nil {
285-
var exitError *exec.ExitError
286-
if ok := errors.As(err, &exitError); ok && exitError.Error() == "1" {
287-
return "", &GitError{err: err, stderr: fmt.Sprintf("unknown config key %s", name)}
347+
var gitErr *GitError
348+
if ok := errors.As(err, &gitErr); ok && gitErr.ExitCode == 1 {
349+
gitErr.Stderr = fmt.Sprintf("unknown config key %s", name)
350+
return "", gitErr
288351
}
289-
return "", &GitError{err: err, stderr: errBuf.String()}
352+
return "", err
290353
}
291354
return firstLine(out), nil
292355
}
@@ -299,7 +362,7 @@ func (c *Client) UncommittedChangeCount(ctx context.Context) (int, error) {
299362
}
300363
out, err := cmd.Output()
301364
if err != nil {
302-
return 0, &GitError{err: err}
365+
return 0, err
303366
}
304367
lines := strings.Split(string(out), "\n")
305368
count := 0
@@ -319,7 +382,7 @@ func (c *Client) Commits(ctx context.Context, baseRef, headRef string) ([]*Commi
319382
}
320383
out, err := cmd.Output()
321384
if err != nil {
322-
return nil, &GitError{err: err}
385+
return nil, err
323386
}
324387
commits := []*Commit{}
325388
sha := 0
@@ -348,7 +411,7 @@ func (c *Client) lookupCommit(ctx context.Context, sha, format string) ([]byte,
348411
}
349412
out, err := cmd.Output()
350413
if err != nil {
351-
return nil, &GitError{err: err}
414+
return nil, err
352415
}
353416
return out, nil
354417
}
@@ -371,13 +434,16 @@ func (c *Client) CommitBody(ctx context.Context, sha string) (string, error) {
371434
}
372435

373436
// Push publishes a git ref to a remote and sets up upstream configuration.
374-
func (c *Client) Push(ctx context.Context, remote string, ref string) error {
437+
func (c *Client) Push(ctx context.Context, remote string, ref string, mods ...CommandModifier) error {
375438
args := []string{"push", "--set-upstream", remote, ref}
376439
//TODO: Use AuthenticatedCommand
377440
cmd, err := c.Command(ctx, args...)
378441
if err != nil {
379442
return err
380443
}
444+
for _, mod := range mods {
445+
mod(cmd)
446+
}
381447
return cmd.Run()
382448
}
383449

@@ -423,7 +489,11 @@ func (c *Client) DeleteLocalBranch(ctx context.Context, branch string) error {
423489
if err != nil {
424490
return err
425491
}
426-
return cmd.Run()
492+
_, err = cmd.Output()
493+
if err != nil {
494+
return err
495+
}
496+
return nil
427497
}
428498

429499
func (c *Client) HasLocalBranch(ctx context.Context, branch string) bool {
@@ -432,7 +502,7 @@ func (c *Client) HasLocalBranch(ctx context.Context, branch string) bool {
432502
if err != nil {
433503
return false
434504
}
435-
err = cmd.Run()
505+
_, err = cmd.Output()
436506
return err == nil
437507
}
438508

@@ -442,7 +512,11 @@ func (c *Client) CheckoutBranch(ctx context.Context, branch string) error {
442512
if err != nil {
443513
return err
444514
}
445-
return cmd.Run()
515+
_, err = cmd.Output()
516+
if err != nil {
517+
return err
518+
}
519+
return nil
446520
}
447521

448522
func (c *Client) CheckoutNewBranch(ctx context.Context, remoteName, branch string) error {
@@ -452,7 +526,11 @@ func (c *Client) CheckoutNewBranch(ctx context.Context, remoteName, branch strin
452526
if err != nil {
453527
return err
454528
}
455-
return cmd.Run()
529+
_, err = cmd.Output()
530+
if err != nil {
531+
return err
532+
}
533+
return nil
456534
}
457535

458536
func (c *Client) Pull(ctx context.Context, remote, branch string) error {
@@ -465,7 +543,7 @@ func (c *Client) Pull(ctx context.Context, remote, branch string) error {
465543
return cmd.Run()
466544
}
467545

468-
func (c *Client) Clone(ctx context.Context, cloneURL string, args []string) (target string, err error) {
546+
func (c *Client) Clone(ctx context.Context, cloneURL string, args []string) (string, error) {
469547
cloneArgs, target := parseCloneArgs(args)
470548
cloneArgs = append(cloneArgs, cloneURL)
471549
// If the args contain an explicit target, pass it to clone
@@ -482,7 +560,10 @@ func (c *Client) Clone(ctx context.Context, cloneURL string, args []string) (tar
482560
return "", err
483561
}
484562
err = cmd.Run()
485-
return
563+
if err != nil {
564+
return "", err
565+
}
566+
return target, nil
486567
}
487568

488569
// ToplevelDir returns the top-level directory path of the current repository.
@@ -494,7 +575,7 @@ func (c *Client) ToplevelDir(ctx context.Context) (string, error) {
494575
}
495576
out, err := cmd.Output()
496577
if err != nil {
497-
return "", &GitError{err: err}
578+
return "", err
498579
}
499580
return firstLine(out), nil
500581
}
@@ -507,7 +588,7 @@ func (c *Client) GitDir(ctx context.Context) (string, error) {
507588
}
508589
out, err := cmd.Output()
509590
if err != nil {
510-
return "", &GitError{err: err}
591+
return "", err
511592
}
512593
return firstLine(out), nil
513594
}

git/client_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,6 @@ func initRepo(t *testing.T, dir string) {
394394
}
395395
cmd, err := client.Command(context.Background(), []string{"init", "--quiet"}...)
396396
assert.NoError(t, err)
397-
err = cmd.Run()
397+
_, err = cmd.Output()
398398
assert.NoError(t, err)
399399
}

0 commit comments

Comments
 (0)