Skip to content

Commit efde275

Browse files
committed
move fetch, push to http package.
1 parent 11d0d07 commit efde275

37 files changed

+832
-567
lines changed

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/multiverse-vcs/go-multiverse
33
go 1.16
44

55
require (
6+
github.com/alecthomas/chroma v0.8.2
67
github.com/golang/groupcache v0.0.0-20191027212112-611e8accdfc9 // indirect
78
github.com/ipfs/go-bitswap v0.3.2
89
github.com/ipfs/go-blockservice v0.1.4
@@ -17,8 +18,10 @@ require (
1718
github.com/ipfs/go-ipld-cbor v0.0.3
1819
github.com/ipfs/go-ipld-format v0.2.0
1920
github.com/ipfs/go-merkledag v0.3.2
21+
github.com/ipfs/go-path v0.0.9
2022
github.com/ipfs/go-unixfs v0.2.4
2123
github.com/ipld/go-car v0.1.0
24+
github.com/julienschmidt/httprouter v1.3.0
2225
github.com/kr/text v0.2.0 // indirect
2326
github.com/libp2p/go-libp2p v0.12.0
2427
github.com/libp2p/go-libp2p-connmgr v0.2.4

go.sum

+78
Large diffs are not rendered by default.

pkg/command/config.go

+2-3
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/remote"
109
)
1110

1211
const (
@@ -24,8 +23,8 @@ type Config struct {
2423
Branches map[string]cid.Cid `json:"branches"`
2524
// Index is the CID of the current commit.
2625
Index cid.Cid `json:"index"`
27-
// Remote is the path to the repository.
28-
Remote remote.Path `json:"remote"`
26+
// Remote is the repository remote server.
27+
Remote string `json:"remote"`
2928
// Tags is a map of names to commit CIDs.
3029
Tags map[string]cid.Cid `json:"tags"`
3130

pkg/command/daemon.go

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"os/signal"
77

8+
"github.com/multiverse-vcs/go-multiverse/pkg/http"
89
"github.com/multiverse-vcs/go-multiverse/pkg/remote"
910
"github.com/multiverse-vcs/go-multiverse/pkg/rpc"
1011
"github.com/nasdf/ulimit"
@@ -40,6 +41,7 @@ func NewDaemonCommand() *cli.Command {
4041
return err
4142
}
4243

44+
go http.ListenAndServe(server)
4345
go rpc.ListenAndServe(server)
4446

4547
fmt.Printf(Banner)

pkg/command/push.go

+32-23
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package command
22

33
import (
4+
"bytes"
5+
"errors"
46
"os"
57

6-
"github.com/multiverse-vcs/go-multiverse/pkg/remote"
7-
"github.com/multiverse-vcs/go-multiverse/pkg/rpc"
8-
"github.com/multiverse-vcs/go-multiverse/pkg/rpc/repo"
8+
cid "github.com/ipfs/go-cid"
9+
ipld "github.com/ipfs/go-ipld-format"
10+
car "github.com/ipld/go-car"
11+
"github.com/multiverse-vcs/go-multiverse/pkg/http"
912
"github.com/urfave/cli/v2"
1013
)
1114

@@ -14,6 +17,13 @@ func NewPushCommand() *cli.Command {
1417
return &cli.Command{
1518
Name: "push",
1619
Usage: "Update a remote repository",
20+
Flags: []cli.Flag{
21+
&cli.StringFlag{
22+
Name: "branch",
23+
Aliases: []string{"b"},
24+
Usage: "Branch to push",
25+
},
26+
},
1727
Action: func(c *cli.Context) error {
1828
cwd, err := os.Getwd()
1929
if err != nil {
@@ -25,38 +35,37 @@ func NewPushCommand() *cli.Command {
2535
return err
2636
}
2737

28-
client, err := rpc.NewClient()
29-
if err != nil {
30-
return rpc.ErrDialRPC
38+
branch := c.String("branch")
39+
if branch == "" {
40+
branch = ctx.Config.Branch
3141
}
3242

33-
fetchArgs := repo.FetchArgs{
34-
Remote: ctx.Config.Remote,
43+
head := ctx.Config.Branches[branch]
44+
if !head.Defined() {
45+
return errors.New("nothing to push")
3546
}
3647

37-
var fetchReply repo.FetchReply
38-
if err := client.Call("Repo.Fetch", &fetchArgs, &fetchReply); err != nil {
48+
client := http.NewClient()
49+
repo, err := client.Fetch(ctx.Config.Remote)
50+
if err != nil {
3951
return err
4052
}
4153

42-
branch := ctx.Config.Branch
43-
heads := fetchReply.Repository.Heads()
44-
old := fetchReply.Repository.Branches[branch]
45-
new := ctx.Config.Branches[branch]
54+
refs := repo.Heads()
55+
walk := func(node ipld.Node) ([]*ipld.Link, error) {
56+
if refs.Has(node.Cid()) {
57+
return nil, nil
58+
}
4659

47-
pack, err := remote.BuildPack(c.Context, ctx.DAG, heads, old, new)
48-
if err != nil {
49-
return err
60+
return node.Links(), nil
5061
}
5162

52-
pushArgs := repo.PushArgs{
53-
Branch: branch,
54-
Pack: pack,
55-
Remote: ctx.Config.Remote,
63+
var data bytes.Buffer
64+
if err := car.WriteCarWithWalker(c.Context, ctx.DAG, []cid.Cid{head}, &data, walk); err != nil {
65+
return err
5666
}
5767

58-
var pushReply repo.PushReply
59-
return client.Call("Repo.Push", &pushArgs, &pushReply)
68+
return client.Push(ctx.Config.Remote, branch, data.Bytes())
6069
},
6170
}
6271
}

pkg/command/remote.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package command
33
import (
44
"os"
55

6-
"github.com/multiverse-vcs/go-multiverse/pkg/remote"
76
"github.com/urfave/cli/v2"
87
)
98

@@ -23,12 +22,7 @@ func NewRemoteCommand() *cli.Command {
2322
return err
2423
}
2524

26-
path := remote.Path(c.Args().Get(0))
27-
if err = path.Verify(); err != nil {
28-
return err
29-
}
30-
31-
repo.Config.Remote = path
25+
repo.Config.Remote = c.Args().Get(0)
3226
return repo.Config.Write()
3327
},
3428
}

pkg/command/repo/create.go

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package repo
22

33
import (
4-
"fmt"
5-
64
"github.com/multiverse-vcs/go-multiverse/pkg/rpc"
75
"github.com/multiverse-vcs/go-multiverse/pkg/rpc/repo"
86
"github.com/urfave/cli/v2"
@@ -32,7 +30,6 @@ func NewCreateCommand() *cli.Command {
3230
return err
3331
}
3432

35-
fmt.Println(reply.Remote)
3633
return nil
3734
},
3835
}

pkg/http/client.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package http
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
"net/http"
9+
10+
"github.com/multiverse-vcs/go-multiverse/pkg/object"
11+
)
12+
13+
// Client is an HTTP client.
14+
type Client struct {
15+
*http.Client
16+
}
17+
18+
// NewClient returns a new client.
19+
func NewClient() *Client {
20+
return &Client{
21+
&http.Client{},
22+
}
23+
}
24+
25+
// Fetch returns the repository at the given remote.
26+
func (c *Client) Fetch(remote string) (*object.Repository, error) {
27+
url := fmt.Sprintf("http://%s/%s", BindAddr, remote)
28+
29+
res, err := c.Get(url)
30+
if err != nil {
31+
return nil, err
32+
}
33+
defer res.Body.Close()
34+
35+
if res.StatusCode != http.StatusOK {
36+
return nil, errors.New("fetch request failed")
37+
}
38+
39+
var repo object.Repository
40+
if err := json.NewDecoder(res.Body).Decode(&repo); err != nil {
41+
return nil, err
42+
}
43+
44+
return &repo, nil
45+
}
46+
47+
// Push updates the given branch at the remote with data.
48+
func (c *Client) Push(remote string, branch string, data []byte) error {
49+
url := fmt.Sprintf("http://%s/%s/%s", BindAddr, remote, branch)
50+
51+
res, err := c.Post(url, "application/octet-stream", bytes.NewReader(data))
52+
if err != nil {
53+
return err
54+
}
55+
defer res.Body.Close()
56+
57+
if res.StatusCode != http.StatusCreated {
58+
return errors.New("push request failed")
59+
}
60+
61+
return nil
62+
}

pkg/http/fetch.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package http
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"net/http"
7+
8+
"github.com/julienschmidt/httprouter"
9+
"github.com/libp2p/go-libp2p-core/peer"
10+
"github.com/multiverse-vcs/go-multiverse/pkg/object"
11+
)
12+
13+
// Fetch returns the repository at the given path.
14+
func (s *Service) Fetch(w http.ResponseWriter, req *http.Request) error {
15+
ctx := req.Context()
16+
17+
params := httprouter.ParamsFromContext(ctx)
18+
pname := params.ByName("peer")
19+
rname := params.ByName("repo")
20+
21+
peerID, err := peer.Decode(pname)
22+
if err != nil {
23+
return err
24+
}
25+
26+
authorID, err := s.Namesys.Resolve(ctx, peerID)
27+
if err != nil {
28+
return err
29+
}
30+
31+
author, err := object.GetAuthor(ctx, s.Peer.DAG, authorID)
32+
if err != nil {
33+
return err
34+
}
35+
36+
repoID, ok := author.Repositories[rname]
37+
if !ok {
38+
return errors.New("repository does not exist")
39+
}
40+
41+
repo, err := object.GetRepository(ctx, s.Peer.DAG, repoID)
42+
if err != nil {
43+
return err
44+
}
45+
46+
w.Header().Set("Content-Type", "application/json")
47+
w.WriteHeader(http.StatusOK)
48+
json.NewEncoder(w).Encode(repo)
49+
50+
return nil
51+
}

0 commit comments

Comments
 (0)