X Tutup
Skip to content

Commit ddfa171

Browse files
author
Nate Smith
authored
Merge pull request cli#1479 from wilso199/pr-checkout-submodules
PR Checkout - Recurse Submodules
2 parents 88b5a3b + 16f69e9 commit ddfa171

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

pkg/cmd/pr/checkout/checkout.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ package checkout
33
import (
44
"errors"
55
"fmt"
6-
"net/http"
7-
"os"
8-
"os/exec"
9-
"strings"
10-
116
"github.com/cli/cli/api"
127
"github.com/cli/cli/context"
138
"github.com/cli/cli/git"
@@ -18,6 +13,10 @@ import (
1813
"github.com/cli/cli/pkg/cmdutil"
1914
"github.com/cli/cli/pkg/iostreams"
2015
"github.com/spf13/cobra"
16+
"net/http"
17+
"os"
18+
"os/exec"
19+
"strings"
2120
)
2221

2322
type CheckoutOptions struct {
@@ -28,7 +27,8 @@ type CheckoutOptions struct {
2827
Remotes func() (context.Remotes, error)
2928
Branch func() (string, error)
3029

31-
SelectorArg string
30+
SelectorArg string
31+
RecurseSubmodules bool
3232
}
3333

3434
func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobra.Command {
@@ -64,6 +64,8 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr
6464
},
6565
}
6666

67+
cmd.Flags().BoolVarP(&opts.RecurseSubmodules, "recurse-submodules", "", false, "Update all active submodules (recursively)")
68+
6769
return cmd
6870
}
6971

@@ -166,6 +168,11 @@ func checkoutRun(opts *CheckoutOptions) error {
166168
}
167169
}
168170

171+
if opts.RecurseSubmodules {
172+
cmdQueue = append(cmdQueue, []string{"git", "submodule", "sync", "--recursive"})
173+
cmdQueue = append(cmdQueue, []string{"git", "submodule", "update", "--init", "--recursive"})
174+
}
175+
169176
for _, args := range cmdQueue {
170177
cmd := exec.Command(args[0], args[1:]...)
171178
cmd.Stdout = os.Stdout

pkg/cmd/pr/checkout/checkout_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,45 @@ func TestPRCheckout_maintainerCanModify(t *testing.T) {
565565
eq(t, strings.Join(ranCommands[2], " "), "git config branch.feature.remote https://github.com/hubot/REPO.git")
566566
eq(t, strings.Join(ranCommands[3], " "), "git config branch.feature.merge refs/heads/feature")
567567
}
568+
569+
func TestPRCheckout_recurseSubmodules(t *testing.T) {
570+
http := &httpmock.Registry{}
571+
572+
http.Register(httpmock.GraphQL(`query PullRequestByNumber\b`), httpmock.StringResponse(`
573+
{ "data": { "repository": { "pullRequest": {
574+
"number": 123,
575+
"headRefName": "feature",
576+
"headRepositoryOwner": {
577+
"login": "hubot"
578+
},
579+
"headRepository": {
580+
"name": "REPO"
581+
},
582+
"isCrossRepository": false,
583+
"maintainerCanModify": false
584+
} } } }
585+
`))
586+
587+
ranCommands := [][]string{}
588+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
589+
switch strings.Join(cmd.Args, " ") {
590+
case "git show-ref --verify -- refs/heads/feature":
591+
return &test.OutputStub{}
592+
default:
593+
ranCommands = append(ranCommands, cmd.Args)
594+
return &test.OutputStub{}
595+
}
596+
})
597+
defer restoreCmd()
598+
599+
output, err := runCommand(http, nil, "master", `123 --recurse-submodules`)
600+
eq(t, err, nil)
601+
eq(t, output.String(), "")
602+
603+
eq(t, len(ranCommands), 5)
604+
eq(t, strings.Join(ranCommands[0], " "), "git fetch origin +refs/heads/feature:refs/remotes/origin/feature")
605+
eq(t, strings.Join(ranCommands[1], " "), "git checkout feature")
606+
eq(t, strings.Join(ranCommands[2], " "), "git merge --ff-only refs/remotes/origin/feature")
607+
eq(t, strings.Join(ranCommands[3], " "), "git submodule sync --recursive")
608+
eq(t, strings.Join(ranCommands[4], " "), "git submodule update --init --recursive")
609+
}

0 commit comments

Comments
 (0)
X Tutup