Skip to content

Commit

Permalink
Add log command.
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Jan 28, 2021
1 parent 8a2100c commit 9148e8a
Show file tree
Hide file tree
Showing 24 changed files with 296 additions and 59 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ COMMANDS:
commit Record changes
daemon Starts a client
init Create a repo
log Print repo history
merge Merge commits
status Print changes
tag List, create, or delete tags
Expand Down
2 changes: 1 addition & 1 deletion cmd/multi/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func branchAction(c *cli.Context) error {
}

var reply rpc.BranchReply
if err = client.Call(action, &args, &reply); err != nil {
if err := client.Call(action, &args, &reply); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/multi/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func checkoutAction(c *cli.Context) error {
}

var reply rpc.CheckoutReply
if err = client.Call("Service.Checkout", &args, &reply); err != nil {
if err := client.Call("Service.Checkout", &args, &reply); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/multi/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func cloneAction(c *cli.Context) error {
}

var reply rpc.CloneReply
if err = client.Call("Service.Clone", &args, &reply); err != nil {
if err := client.Call("Service.Clone", &args, &reply); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/multi/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func commitAction(c *cli.Context) error {
}

var reply rpc.CommitReply
if err = client.Call("Service.Commit", &args, &reply); err != nil {
if err := client.Call("Service.Commit", &args, &reply); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/multi/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func initAction(c *cli.Context) error {
}

var reply rpc.InitReply
if err = client.Call("Service.Init", &args, &reply); err != nil {
if err := client.Call("Service.Init", &args, &reply); err != nil {
return err
}

Expand Down
61 changes: 61 additions & 0 deletions cmd/multi/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"fmt"
"os"

"github.com/multiverse-vcs/go-multiverse/rpc"
"github.com/urfave/cli/v2"
)

const logDateFormat = "Mon Jan 02 15:04:05 2006 -0700"

var logCommand = &cli.Command{
Action: logAction,
Name: "log",
Usage: "Print repo history",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "limit",
Aliases: []string{"l"},
Usage: "Log limit",
Value: -1,
},
},
}

func logAction(c *cli.Context) error {
cwd, err := os.Getwd()
if err != nil {
return err
}

config, err := LoadConfig(cwd)
if err != nil {
return err
}

client, err := rpc.NewClient()
if err != nil {
return err
}

args := rpc.LogArgs{
Name: config.Name,
Branch: config.Branch,
Limit: c.Int("limit"),
}

var reply rpc.LogReply
if err := client.Call("Service.Log", &args, &reply); err != nil {
return err
}

for i, c := range reply.Commits {
fmt.Printf("commit %s\n", reply.IDs[i].String())
fmt.Printf("Date: %s\n", c.Date.Format(logDateFormat))
fmt.Printf("\n\t%s\n\n", c.Message)
}

return nil
}
1 change: 1 addition & 0 deletions cmd/multi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var app = &cli.App{
commitCommand,
daemonCommand,
initCommand,
logCommand,
mergeCommand,
statusCommand,
tagCommand,
Expand Down
2 changes: 1 addition & 1 deletion cmd/multi/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func mergeAction(c *cli.Context) error {
}

var reply rpc.MergeReply
if err = client.Call("Service.Merge", &args, &reply); err != nil {
if err := client.Call("Service.Merge", &args, &reply); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/multi/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func statusAction(c *cli.Context) error {
}

var reply rpc.StatusReply
if err = client.Call("Service.Status", &args, &reply); err != nil {
if err := client.Call("Service.Status", &args, &reply); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/multi/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func tagAction(c *cli.Context) error {
}

var reply rpc.TagReply
if err = client.Call(action, &args, &reply); err != nil {
if err := client.Call(action, &args, &reply); err != nil {
return err
}

Expand Down
24 changes: 15 additions & 9 deletions core/merge_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import (

// MergeBase returns the best common ancestor of local and remote.
func MergeBase(ctx context.Context, dag ipld.DAGService, local, remote cid.Cid) (cid.Cid, error) {
history, err := Walk(ctx, dag, local, nil)
if err != nil {
history := make(map[string]bool)
visit := func(id cid.Cid) bool {
history[id.KeyString()] = true
return true
}

if err := Walk(ctx, dag, local, visit); err != nil {
return cid.Cid{}, err
}

Expand All @@ -26,7 +31,7 @@ func MergeBase(ctx context.Context, dag ipld.DAGService, local, remote cid.Cid)
// find the least common ancestor by searching
// for commits that are in both local and remote
// and that are also independent from each other
cb := func(id cid.Cid) bool {
visit = func(id cid.Cid) bool {
if err0 != nil {
return false
}
Expand All @@ -42,7 +47,7 @@ func MergeBase(ctx context.Context, dag ipld.DAGService, local, remote cid.Cid)
return false
}

if _, err := Walk(ctx, dag, remote, cb); err != nil {
if err := Walk(ctx, dag, remote, visit); err != nil {
return cid.Cid{}, err
}

Expand All @@ -55,14 +60,15 @@ func IsAncestor(ctx context.Context, dag ipld.DAGService, parent, child cid.Cid)
return false, nil
}

cb := func(id cid.Cid) bool {
return id != child
var match bool
visit := func(id cid.Cid) bool {
match = (id == child)
return !match
}

history, err := Walk(ctx, dag, parent, cb)
if err != nil {
if err := Walk(ctx, dag, parent, visit); err != nil {
return false, err
}

return history[child.KeyString()], nil
return match, nil
}
2 changes: 0 additions & 2 deletions core/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,3 @@ func TestStatus(t *testing.T) {
t.Fatalf("unexpected change type")
}
}


21 changes: 7 additions & 14 deletions core/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ import (
"github.com/multiverse-vcs/go-multiverse/data"
)

// WalkFun is called for each commit visited by walk.
type WalkFun func(cid.Cid) bool

// Walk performs a depth first traversal of parent commits starting at the given id.
func Walk(ctx context.Context, dag ipld.DAGService, id cid.Cid, cb WalkFun) (map[string]bool, error) {
func Walk(ctx context.Context, dag ipld.DAGService, id cid.Cid, visit func(cid.Cid) bool) error {
getLinks := func(ctx context.Context, id cid.Cid) ([]*ipld.Link, error) {
commit, err := data.GetCommit(ctx, dag, id)
if err != nil {
Expand All @@ -23,19 +20,15 @@ func Walk(ctx context.Context, dag ipld.DAGService, id cid.Cid, cb WalkFun) (map
return commit.ParentLinks(), nil
}

history := make(map[string]bool)
visit := func(id cid.Cid) bool {
if history[id.KeyString()] {
seen := make(map[string]bool)
wrap := func(id cid.Cid) bool {
if seen[id.KeyString()] {
return false
}

history[id.KeyString()] = true
if cb != nil {
return cb(id)
}

return true
seen[id.KeyString()] = true
return visit(id)
}

return history, merkledag.Walk(ctx, getLinks, id, visit)
return merkledag.Walk(ctx, getLinks, id, wrap)
}
16 changes: 11 additions & 5 deletions core/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/ipfs/go-cid"
"github.com/ipfs/go-merkledag/dagutils"
"github.com/multiverse-vcs/go-multiverse/data"
"github.com/multiverse-vcs/go-multiverse/unixfs"
Expand All @@ -30,20 +31,25 @@ func TestWalk(t *testing.T) {
t.Fatalf("failed to commit")
}

history, err := Walk(ctx, dag, idB, nil)
if err != nil {
var ids []cid.Cid
cb := func(id cid.Cid) bool {
ids = append(ids, id)
return true
}

if err := Walk(ctx, dag, idB, cb); err != nil {
t.Fatalf("failed to walk")
}

if len(history) != 2 {
if len(ids) != 2 {
t.Fatalf("cids do not match")
}

if !history[idA.KeyString()] {
if ids[0] != idB {
t.Errorf("cids do not match")
}

if !history[idB.KeyString()] {
if ids[1] != idA {
t.Errorf("cids do not match")
}
}
2 changes: 1 addition & 1 deletion data/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ func (s *Store) Keys() ([]string, error) {
}

return keys, nil
}
}
32 changes: 15 additions & 17 deletions rpc/branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@ package rpc

import (
"context"
"io/ioutil"
"testing"

"github.com/ipfs/go-datastore"
"github.com/multiverse-vcs/go-multiverse/data"
"github.com/multiverse-vcs/go-multiverse/peer"
)

var repositoryJSON = []byte(`{
"name": "test",
"branches": {
"default": {"/": "bagaybqabciqeutn2u7n3zuk5b4ykgfwpkekb7ctgnlwik5zfr6bcukvknj2jtpa"}
},
"tags": {}
}`)

func TestListBranches(t *testing.T) {
ctx := context.Background()

Expand All @@ -25,26 +18,31 @@ func TestListBranches(t *testing.T) {

mock, err := peer.Mock(ctx, dstore)
if err != nil {
t.Fatalf("failed to create peer")
t.Fatal("failed to create peer")
}

json, err := ioutil.ReadFile("testdata/repository.json")
if err != nil {
t.Fatal("failed to read json")
}

repo, err := data.RepositoryFromJSON(repositoryJSON)
repo, err := data.RepositoryFromJSON(json)
if err != nil {
t.Fatalf("failed to parse repository json")
t.Fatal("failed to parse repository json")
}

id, err := data.AddRepository(ctx, mock, repo)
if err != nil {
t.Fatalf("failed to create repository %s", err)
t.Fatal("failed to create repository")
}

if err := store.PutCid(repo.Name, id); err != nil {
t.Fatalf("failed to put cid in store")
t.Fatal("failed to put cid in store")
}

client, err := connect(mock, store)
if err != nil {
t.Fatalf("failed to connect to rpc server")
t.Fatal("failed to connect to rpc server")
}

args := BranchArgs{
Expand All @@ -53,14 +51,14 @@ func TestListBranches(t *testing.T) {

var reply BranchReply
if err := client.Call("Service.ListBranches", &args, &reply); err != nil {
t.Fatalf("failed to call rpc: %s", err)
t.Fatal("failed to call rpc")
}

if len(reply.Branches) != 1 {
t.Errorf("unexpected branches")
t.Error("unexpected branches")
}

if reply.Branches["default"] != repo.Branches["default"] {
t.Errorf("unexpected branches")
t.Error("unexpected branches")
}
}
2 changes: 1 addition & 1 deletion rpc/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type InitArgs struct {
}

// InitReply contains the reply.
type InitReply struct {}
type InitReply struct{}

// Init creates a new empty repository.
func (s *Service) Init(args *InitArgs, reply *InitReply) error {
Expand Down
Loading

0 comments on commit 9148e8a

Please sign in to comment.