checkout

package
v0.0.0-...-2b89969 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2021 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Command = &cli.Command{
		Name:      "checkout",
		UsageText: "checkout [options] <changeID>",
		Usage:     "Checkout an available change",
		Action: func(ctx *cli.Context) error {
			gerritAPI := common.GerritAPI(ctx.Context)
			repo := common.GitRepository(ctx.Context)

			wt, err := repo.Worktree()
			if err != nil {
				return fmt.Errorf("failed to obtain worktree: %v", err)
			}

			changeID := ctx.Args().Get(0)

			change, err := gerritAPI.Client.GetChange(ctx.Context, changeID, gerrit.QueryChangesOpt{
				Fields: []string{"CURRENT_REVISION"},
			})
			if err != nil {
				return err
			}

			branchName := fmt.Sprintf("gerrit/%d", change.ChangeNumber)
			branchRefName := plumbing.NewBranchReferenceName(branchName)

			revision := change.Revisions[change.CurrentRevision]

			err = repo.Fetch(&git.FetchOptions{
				RemoteName: "gerrit",
				RefSpecs: []config.RefSpec{
					config.RefSpec(revision.Ref + ":" + string(branchRefName)),
				},
				Force: true,
			})
			if err != nil {
				return fmt.Errorf("failed to fetch updates for branch: %v", err)
			}

			err = wt.Checkout(&git.CheckoutOptions{
				Branch: branchRefName,
			})
			if err != nil {
				return fmt.Errorf("failed to checkout branch: %v", err)
			}

			return nil
		},
	}
)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL