|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os" |
| 6 | + |
| 7 | + cid "github.com/ipfs/go-cid" |
| 8 | + "github.com/multiverse-vcs/go-multiverse/pkg/command/context" |
| 9 | + "github.com/multiverse-vcs/go-multiverse/pkg/dag" |
| 10 | + "github.com/multiverse-vcs/go-multiverse/pkg/fs" |
| 11 | + "github.com/multiverse-vcs/go-multiverse/pkg/object" |
| 12 | + "github.com/urfave/cli/v2" |
| 13 | +) |
| 14 | + |
| 15 | +// NewCheckoutCommand returns a new cli command. |
| 16 | +func NewCheckoutCommand() *cli.Command { |
| 17 | + return &cli.Command{ |
| 18 | + Name: "checkout", |
| 19 | + Usage: "Checkout committed files", |
| 20 | + Flags: []cli.Flag{ |
| 21 | + &cli.BoolFlag{ |
| 22 | + Name: "force", |
| 23 | + Aliases: []string{"f"}, |
| 24 | + Usage: "Force checkout", |
| 25 | + }, |
| 26 | + &cli.StringFlag{ |
| 27 | + Name: "commit", |
| 28 | + Aliases: []string{"c"}, |
| 29 | + Usage: "Checkout branch commit", |
| 30 | + }, |
| 31 | + &cli.BoolFlag{ |
| 32 | + Name: "head", |
| 33 | + Usage: "Checkout branch head", |
| 34 | + }, |
| 35 | + }, |
| 36 | + Action: func(c *cli.Context) error { |
| 37 | + cwd, err := os.Getwd() |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + cc, err := context.New(cwd) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + branch := cc.Config.Branches[cc.Config.Branch] |
| 48 | + treeID := branch.Stash |
| 49 | + |
| 50 | + switch { |
| 51 | + case c.IsSet("head"): |
| 52 | + commit, err := object.GetCommit(c.Context, cc.DAG, branch.Head) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + treeID = commit.Tree |
| 58 | + case c.IsSet("commit"): |
| 59 | + id, err := cid.Decode(c.String("commit")) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + commit, err := object.GetCommit(c.Context, cc.DAG, id) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + treeID = commit.Tree |
| 70 | + default: |
| 71 | + cli.ShowAppHelpAndExit(c, -1) |
| 72 | + } |
| 73 | + |
| 74 | + stash, err := cc.Tree(c.Context) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + status, err := dag.Status(c.Context, cc.DAG, stash, branch.Head) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + if len(status) != 0 && !c.IsSet("force") { |
| 85 | + return errors.New("uncommitted changes") |
| 86 | + } |
| 87 | + |
| 88 | + tree, err := cc.DAG.Get(c.Context, treeID) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + if err := fs.Write(c.Context, cc.DAG, cc.Root, tree); err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + branch.Stash = treeID |
| 98 | + return cc.Config.Write() |
| 99 | + }, |
| 100 | + } |
| 101 | +} |
0 commit comments