Skip to content

Commit dfabd17

Browse files
committed
add log and checkout commands.
1 parent 8bb4c0d commit dfabd17

File tree

7 files changed

+162
-14
lines changed

7 files changed

+162
-14
lines changed

pkg/command/checkout.go

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

pkg/command/command.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ func NewApp() *cli.App {
2424
},
2525
Commands: []*cli.Command{
2626
NewInitCommand(),
27-
NewStatusCommand(),
2827
NewCommitCommand(),
28+
NewCheckoutCommand(),
29+
NewSwitchCommand(),
2930
NewPushCommand(),
3031
NewPullCommand(),
31-
NewSwitchCommand(),
32+
NewStatusCommand(),
33+
NewLogCommand(),
3234
branch.NewCommand(),
3335
remote.NewCommand(),
3436
repo.NewCommand(),

pkg/command/log.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package command
2+
3+
import (
4+
"fmt"
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/object"
11+
"github.com/urfave/cli/v2"
12+
)
13+
14+
// NewLogCommand returns a new cli command.
15+
func NewLogCommand() *cli.Command {
16+
return &cli.Command{
17+
Name: "log",
18+
Usage: "Print branch history",
19+
Action: func(c *cli.Context) error {
20+
cwd, err := os.Getwd()
21+
if err != nil {
22+
return err
23+
}
24+
25+
cc, err := context.New(cwd)
26+
if err != nil {
27+
return err
28+
}
29+
30+
branch := cc.Config.Branches[cc.Config.Branch]
31+
32+
visit := func(id cid.Cid) bool {
33+
commit, err := object.GetCommit(c.Context, cc.DAG, id)
34+
if err != nil {
35+
return false
36+
}
37+
38+
fmt.Printf("commit %s\n", id.String())
39+
fmt.Printf("Date: %s\n", commit.Date.Format("Mon Jan 02 15:04:05 2006 -0700"))
40+
fmt.Printf("\n\t%s\n\n", commit.Message)
41+
return true
42+
}
43+
44+
return dag.Walk(c.Context, cc.DAG, branch.Head, visit)
45+
},
46+
}
47+
}

pkg/command/pull.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ func NewPullCommand() *cli.Command {
5656
return err
5757
}
5858

59-
diffs, err := dag.Status(c.Context, cc.DAG, tree, branch.Head)
59+
status, err := dag.Status(c.Context, cc.DAG, tree, branch.Head)
6060
if err != nil {
6161
return err
6262
}
6363

64-
if len(diffs) != 0 {
64+
if len(status) != 0 {
6565
return errors.New("uncommitted changes")
6666
}
6767

pkg/command/status.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ func NewStatusCommand() *cli.Command {
3333
}
3434

3535
branch := cc.Config.Branches[cc.Config.Branch]
36-
37-
diffs, err := dag.Status(c.Context, cc.DAG, tree, branch.Head)
36+
status, err := dag.Status(c.Context, cc.DAG, tree, branch.Head)
3837
if err != nil {
3938
return err
4039
}
4140

4241
paths := make([]string, 0)
43-
for path := range diffs {
42+
for path := range status {
4443
paths = append(paths, path)
4544
}
4645
sort.Strings(paths)
@@ -50,7 +49,7 @@ func NewStatusCommand() *cli.Command {
5049
fmt.Printf(" (to stop tracking files add rules to '%s')\n", context.IgnoreFile)
5150

5251
for _, p := range paths {
53-
switch diffs[p] {
52+
switch status[p] {
5453
case dagutils.Add:
5554
fmt.Printf("\tnew file: %s\n", p)
5655
case dagutils.Remove:

pkg/rpc/file/search.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type SearchReply struct {
2929
// Entries contains directory entries.
3030
Entries []*fs.DirEntry
3131
// IsDir specifies if the file is a directory.
32-
IsDir bool
32+
IsDir bool
3333
}
3434

3535
// Search returns the contents of a file at the given remote path.
@@ -109,4 +109,4 @@ func (s *Service) Search(args *SearchArgs, reply *SearchReply) error {
109109

110110
reply.IsDir = fsnode.IsDir()
111111
return nil
112-
}
112+
}

pkg/rpc/rpc.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewClient() (*rpc.Client, error) {
5050
if err != nil {
5151
return nil, err
5252
}
53-
53+
5454
config := remote.NewConfig(filepath.Join(home, remote.DotDir))
5555
if err := config.Read(); err != nil {
5656
return nil, err
@@ -64,16 +64,15 @@ func ListenAndServe(server *remote.Server) error {
6464
rpc.RegisterName("Author", &author.Service{server})
6565
rpc.RegisterName("File", &file.Service{server})
6666
rpc.RegisterName("Repo", &repo.Service{server})
67-
6867
rpc.HandleHTTP()
69-
http.HandleFunc("/_jsonRPC_", ServeHTTP)
7068

7169
listener, err := net.Listen("tcp", server.Config.HttpAddress)
7270
if err != nil {
7371
log.Fatal(err)
7472
}
7573
defer listener.Close()
7674

75+
http.HandleFunc("/_jsonRPC_", ServeHTTP)
7776
return http.Serve(listener, nil)
7877
}
7978

@@ -87,4 +86,4 @@ func ServeHTTP(w http.ResponseWriter, req *http.Request) {
8786
if req.Method == http.MethodPost {
8887
jsonrpc.ServeConn(&HttpConn{req.Body, w})
8988
}
90-
}
89+
}

0 commit comments

Comments
 (0)