Skip to content

Commit 51eb6c6

Browse files
committed
push & pull commands
1 parent 85ef789 commit 51eb6c6

Some content is hidden

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

57 files changed

+1525
-423
lines changed

docs/getting-started.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Getting Started
2+
3+
Multiverse uses peer-to-peer network protocols to exchange data.
4+
5+
Instead of using a centralized server, every participant runs a peer node.
6+
7+
Running a peer node is simple and requires no extra setup.
8+
9+
```bash
10+
$ multi daemon
11+
```

pkg/command/author/follow.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func NewFollowCommand() *cli.Command {
1919

2020
client, err := rpc.NewClient()
2121
if err != nil {
22-
return rpc.ErrDialRPC
22+
return cli.Exit(rpc.DialErrMsg, -1)
2323
}
2424

2525
peerID, err := peer.Decode(c.Args().Get(0))

pkg/command/author/list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func NewListCommand() *cli.Command {
1616
Action: func(c *cli.Context) error {
1717
client, err := rpc.NewClient()
1818
if err != nil {
19-
return rpc.ErrDialRPC
19+
return cli.Exit(rpc.DialErrMsg, -1)
2020
}
2121

2222
args := author.SelfArgs{}

pkg/command/author/self.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func NewSelfCommand() *cli.Command {
1616
Action: func(c *cli.Context) error {
1717
client, err := rpc.NewClient()
1818
if err != nil {
19-
return rpc.ErrDialRPC
19+
return cli.Exit(rpc.DialErrMsg, -1)
2020
}
2121

2222
args := author.SelfArgs{}

pkg/command/author/unfollow.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func NewUnfollowCommand() *cli.Command {
1919

2020
client, err := rpc.NewClient()
2121
if err != nil {
22-
return rpc.ErrDialRPC
22+
return cli.Exit(rpc.DialErrMsg, -1)
2323
}
2424

2525
peerID, err := peer.Decode(c.Args().Get(0))

pkg/command/author/view.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func NewViewCommand() *cli.Command {
1717
Action: func(c *cli.Context) error {
1818
client, err := rpc.NewClient()
1919
if err != nil {
20-
return rpc.ErrDialRPC
20+
return cli.Exit(rpc.DialErrMsg, -1)
2121
}
2222

2323
peerID, err := peer.Decode(c.Args().Get(0))

pkg/command/branch/branch.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import (
88
func NewCommand() *cli.Command {
99
return &cli.Command{
1010
Name: "branch",
11-
Usage: "Manage repository branches",
11+
Usage: "List, create, or delete branches",
1212
Subcommands: []*cli.Command{
1313
NewListCommand(),
1414
NewCreateCommand(),
1515
NewDeleteCommand(),
16+
NewSetCommand(),
17+
NewGetCommand(),
1618
},
1719
}
1820
}

pkg/command/branch/create.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,25 @@ func NewCreateCommand() *cli.Command {
2323
return err
2424
}
2525

26-
ctx, err := context.New(cwd)
26+
cc, err := context.New(cwd)
2727
if err != nil {
2828
return err
2929
}
3030

3131
name := c.Args().Get(0)
32-
if _, ok := ctx.Config.Repository.Branches[name]; ok {
32+
if _, ok := cc.Config.Branches[name]; ok {
3333
return errors.New("branch already exists")
3434
}
3535

36-
ctx.Config.Repository.Branches[name] = ctx.Config.Index
37-
return ctx.Config.Write()
36+
branch := cc.Config.Branches[cc.Config.Branch]
37+
38+
cc.Config.Branches[name] = &context.Branch{
39+
Head: branch.Head,
40+
Stash: branch.Stash,
41+
Remote: branch.Remote,
42+
}
43+
44+
return cc.Config.Write()
3845
},
3946
}
4047
}

pkg/command/branch/delete.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ func NewDeleteCommand() *cli.Command {
2323
return err
2424
}
2525

26-
ctx, err := context.New(cwd)
26+
cc, err := context.New(cwd)
2727
if err != nil {
2828
return err
2929
}
3030

3131
name := c.Args().Get(0)
32-
if _, ok := ctx.Config.Repository.Branches[name]; !ok {
32+
if _, ok := cc.Config.Branches[name]; !ok {
3333
return errors.New("branch does not exists")
3434
}
3535

36-
delete(ctx.Config.Repository.Branches, name)
37-
return ctx.Config.Write()
36+
delete(cc.Config.Branches, name)
37+
return cc.Config.Write()
3838
},
3939
}
4040
}

pkg/command/branch/get.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package branch
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
8+
"github.com/multiverse-vcs/go-multiverse/pkg/command/context"
9+
"github.com/urfave/cli/v2"
10+
)
11+
12+
// NewGetCommand returns a new command.
13+
func NewGetCommand() *cli.Command {
14+
return &cli.Command{
15+
Name: "get",
16+
Usage: "Get branch settings",
17+
Action: func(c *cli.Context) error {
18+
if c.NArg() != 1 {
19+
cli.ShowSubcommandHelpAndExit(c, 1)
20+
}
21+
22+
cwd, err := os.Getwd()
23+
if err != nil {
24+
return err
25+
}
26+
27+
cc, err := context.New(cwd)
28+
if err != nil {
29+
return err
30+
}
31+
32+
branch := cc.Config.Branches[cc.Config.Branch]
33+
switch c.Args().Get(0) {
34+
case "remote":
35+
fmt.Println(branch.Remote)
36+
default:
37+
return errors.New("invalid setting")
38+
}
39+
40+
return nil
41+
},
42+
}
43+
}

pkg/command/branch/list.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ func NewListCommand() *cli.Command {
1919
return err
2020
}
2121

22-
ctx, err := context.New(cwd)
22+
cc, err := context.New(cwd)
2323
if err != nil {
2424
return err
2525
}
2626

27-
for branch := range ctx.Config.Repository.Branches {
28-
if branch == ctx.Config.Branch {
27+
for name := range cc.Config.Branches {
28+
if name == cc.Config.Branch {
2929
fmt.Print("* ")
3030
}
3131

32-
fmt.Println(branch)
32+
fmt.Println(name)
3333
}
3434

3535
return nil

pkg/command/branch/set.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package branch
2+
3+
import (
4+
"errors"
5+
"os"
6+
7+
"github.com/multiverse-vcs/go-multiverse/pkg/command/context"
8+
"github.com/urfave/cli/v2"
9+
)
10+
11+
// NewSetCommand returns a new command.
12+
func NewSetCommand() *cli.Command {
13+
return &cli.Command{
14+
Name: "set",
15+
Usage: "Set branch settings",
16+
Action: func(c *cli.Context) error {
17+
if c.NArg() != 2 {
18+
cli.ShowSubcommandHelpAndExit(c, 1)
19+
}
20+
21+
cwd, err := os.Getwd()
22+
if err != nil {
23+
return err
24+
}
25+
26+
cc, err := context.New(cwd)
27+
if err != nil {
28+
return err
29+
}
30+
31+
branch := cc.Config.Branches[cc.Config.Branch]
32+
switch c.Args().Get(0) {
33+
case "remote":
34+
branch.Remote = c.Args().Get(1)
35+
default:
36+
return errors.New("invalid setting")
37+
}
38+
39+
return cc.Config.Write()
40+
},
41+
}
42+
}

pkg/command/command.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/multiverse-vcs/go-multiverse/pkg/command/author"
88
"github.com/multiverse-vcs/go-multiverse/pkg/command/branch"
9+
"github.com/multiverse-vcs/go-multiverse/pkg/command/remote"
910
"github.com/multiverse-vcs/go-multiverse/pkg/command/repo"
1011
"github.com/urfave/cli/v2"
1112
)
@@ -23,11 +24,13 @@ func NewApp() *cli.App {
2324
},
2425
Commands: []*cli.Command{
2526
NewInitCommand(),
27+
NewStatusCommand(),
2628
NewCommitCommand(),
27-
NewRemoteCommand(),
2829
NewPushCommand(),
29-
NewStatusCommand(),
30+
NewPullCommand(),
31+
NewSwitchCommand(),
3032
branch.NewCommand(),
33+
remote.NewCommand(),
3134
repo.NewCommand(),
3235
author.NewCommand(),
3336
NewDaemonCommand(),

pkg/command/commit.go

+13-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/multiverse-vcs/go-multiverse/pkg/command/context"
88
"github.com/multiverse-vcs/go-multiverse/pkg/dag"
9-
"github.com/multiverse-vcs/go-multiverse/pkg/fs"
109
"github.com/multiverse-vcs/go-multiverse/pkg/object"
1110
"github.com/urfave/cli/v2"
1211
)
@@ -29,47 +28,43 @@ func NewCommitCommand() *cli.Command {
2928
return err
3029
}
3130

32-
ctx, err := context.New(cwd)
31+
cc, err := context.New(cwd)
3332
if err != nil {
3433
return err
3534
}
3635

37-
head, ok := ctx.Config.Repository.Branches[ctx.Config.Branch]
38-
if ok && head != ctx.Config.Index {
39-
return errors.New("index is behind head")
40-
}
36+
branch := cc.Config.Branches[cc.Config.Branch]
4137

42-
ignore, err := ctx.Ignore()
38+
tree, err := cc.Tree(c.Context)
4339
if err != nil {
4440
return err
4541
}
4642

47-
tree, err := fs.Add(c.Context, ctx.DAG, ctx.Root, ignore)
43+
diffs, err := dag.Status(c.Context, cc.DAG, tree, branch.Head)
4844
if err != nil {
4945
return err
5046
}
5147

52-
equal, err := dag.Equal(c.Context, ctx.DAG, ctx.Config.Index, tree)
53-
if err != nil {
54-
return err
55-
}
56-
57-
if equal {
48+
if len(diffs) == 0 {
5849
return errors.New("no changes to commit")
5950
}
6051

6152
commit := object.NewCommit()
6253
commit.Tree = tree.Cid()
6354
commit.Message = c.String("message")
6455

65-
commitID, err := object.AddCommit(c.Context, ctx.DAG, commit)
56+
if branch.Head.Defined() {
57+
commit.Parents = append(commit.Parents, branch.Head)
58+
}
59+
60+
commitID, err := object.AddCommit(c.Context, cc.DAG, commit)
6661
if err != nil {
6762
return err
6863
}
6964

70-
ctx.Config.Index = commitID
71-
ctx.Config.Repository.Branches[ctx.Config.Branch] = commitID
72-
return ctx.Config.Write()
65+
branch.Head = commitID
66+
branch.Stash = tree.Cid()
67+
return cc.Config.Write()
7368
},
7469
}
7570
}

pkg/command/context/config.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"path/filepath"
77

88
cid "github.com/ipfs/go-cid"
9-
"github.com/multiverse-vcs/go-multiverse/pkg/object"
109
)
1110

1211
const (
@@ -16,26 +15,37 @@ const (
1615
DefaultBranch = "main"
1716
)
1817

18+
// Branch contains branch info.
19+
type Branch struct {
20+
// Head is the CID of the branch head.
21+
Head cid.Cid `json:"head"`
22+
// Stash is the CID of the tree stash.
23+
Stash cid.Cid `json:"stash"`
24+
// Remote is the remote branch path.
25+
Remote string `json:"remote"`
26+
}
27+
1928
// Config contains repository info.
2029
type Config struct {
2130
// Branch is the name of the current branch.
2231
Branch string `json:"branch"`
23-
// Index is the CID of the current commit.
24-
Index cid.Cid `json:"index"`
25-
// Remote is the repository remote server.
26-
Remote string `json:"remote"`
27-
// Repository contains references to branch heads.
28-
Repository *object.Repository `json:"repository"`
32+
// Branches contains named branches.
33+
Branches map[string]*Branch `json:"branches"`
34+
// Remotes contains named remotes.
35+
Remotes map[string]string `json:"remotes"`
2936

3037
path string
3138
}
3239

3340
// New returns a config with default settings.
3441
func NewConfig(root string) *Config {
3542
return &Config{
36-
Branch: DefaultBranch,
37-
Repository: object.NewRepository(),
38-
path: filepath.Join(root, ConfigFile),
43+
Branch: DefaultBranch,
44+
Branches: map[string]*Branch{
45+
DefaultBranch: {},
46+
},
47+
Remotes: make(map[string]string),
48+
path: filepath.Join(root, ConfigFile),
3949
}
4050
}
4151

0 commit comments

Comments
 (0)