Skip to content

Commit 64ebaea

Browse files
committed
Git importer fixes.
1 parent 3df3010 commit 64ebaea

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

cmd/multi/import.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"errors"
55
"os"
6+
"path/filepath"
67

78
"github.com/multiverse-vcs/go-multiverse/rpc"
89
"github.com/urfave/cli/v2"
@@ -49,6 +50,11 @@ func importAction(c *cli.Context) error {
4950
return errors.New("repo already exists")
5051
}
5152

53+
dir, err := filepath.Abs(c.String("dir"))
54+
if err != nil {
55+
return err
56+
}
57+
5258
client, err := rpc.NewClient()
5359
if err != nil {
5460
return err
@@ -58,7 +64,7 @@ func importAction(c *cli.Context) error {
5864
Name: name,
5965
Type: c.String("type"),
6066
URL: c.String("url"),
61-
Dir: c.String("dir"),
67+
Dir: dir,
6268
}
6369

6470
var reply rpc.ImportReply

data/repository.go

+8
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ func NewRepository(name string) *Repository {
9696
}
9797
}
9898

99+
// DefaultBranch returns the default branch of the repo.
100+
func (r *Repository) DefaultBranch() string {
101+
for branch := range r.Branches {
102+
return branch
103+
}
104+
return ""
105+
}
106+
99107
// Ref returns the cid of the given ref.
100108
func (r *Repository) Ref(ref string) (cid.Cid, error) {
101109
if id, ok := r.Branches[ref]; ok {

git/git.go

+27-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package git
44
import (
55
"context"
66
"io/ioutil"
7+
"path"
78

89
"github.com/go-git/go-git/v5"
910
"github.com/go-git/go-git/v5/plumbing"
@@ -27,6 +28,7 @@ type importer struct {
2728
dag ipld.DAGService
2829
name string
2930
repo *git.Repository
31+
objects map[string]cid.Cid
3032
branches map[string]cid.Cid
3133
tags map[string]cid.Cid
3234
}
@@ -64,6 +66,7 @@ func NewImporter(ctx context.Context, dag ipld.DAGService, repo *git.Repository,
6466
dag: dag,
6567
name: name,
6668
repo: repo,
69+
objects: make(map[string]cid.Cid),
6770
branches: make(map[string]cid.Cid),
6871
tags: make(map[string]cid.Cid),
6972
}
@@ -103,7 +106,10 @@ func (i *importer) AddBranch(ref *plumbing.Reference) error {
103106
return err
104107
}
105108

106-
i.branches[string(ref.Name())] = id
109+
name := string(ref.Name())
110+
name = path.Base(name)
111+
112+
i.branches[name] = id
107113
return nil
108114
}
109115

@@ -114,12 +120,19 @@ func (i *importer) AddTag(ref *plumbing.Reference) error {
114120
return err
115121
}
116122

117-
i.tags[string(ref.Name())] = id
123+
name := string(ref.Name())
124+
name = path.Base(name)
125+
126+
i.tags[name] = id
118127
return nil
119128
}
120129

121130
// AddCommit adds the commit with the given hash to the dag.
122131
func (i *importer) AddCommit(hash plumbing.Hash) (cid.Cid, error) {
132+
if id, ok := i.objects[hash.String()]; ok {
133+
return id, nil
134+
}
135+
123136
commit, err := i.repo.CommitObject(hash)
124137
if err != nil {
125138
return cid.Cid{}, err
@@ -149,11 +162,21 @@ func (i *importer) AddCommit(hash plumbing.Hash) (cid.Cid, error) {
149162
mcommit.Metadata["git_committer_email"] = commit.Committer.Email
150163
mcommit.Metadata["git_committer_date"] = commit.Committer.When.Format(DateFormat)
151164

152-
return data.AddCommit(i.ctx, i.dag, mcommit)
165+
id, err := data.AddCommit(i.ctx, i.dag, mcommit)
166+
if err != nil {
167+
return cid.Cid{}, err
168+
}
169+
170+
i.objects[hash.String()] = id
171+
return id, nil
153172
}
154173

155174
// AddTree adds the tree with the given hash to the dag.
156175
func (i *importer) AddTree(hash plumbing.Hash) (ipld.Node, error) {
176+
if id, ok := i.objects[hash.String()]; ok {
177+
return i.dag.Get(i.ctx, id)
178+
}
179+
157180
tree, err := i.repo.TreeObject(hash)
158181
if err != nil {
159182
return nil, err
@@ -180,6 +203,7 @@ func (i *importer) AddTree(hash plumbing.Hash) (ipld.Node, error) {
180203
return nil, err
181204
}
182205

206+
i.objects[hash.String()] = node.Cid()
183207
return node, nil
184208
}
185209

git/git_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ func TestImportFromURL(t *testing.T) {
2525
if repo.Name != "go-multiverse" {
2626
t.Error("unexpected repo name")
2727
}
28-
}
28+
}

web/tree.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ func (s *Server) Tree(w http.ResponseWriter, req *http.Request) error {
4747

4848
name := params.ByName("name")
4949
file := params.ByName("file")
50-
5150
ref := params.ByName("ref")
52-
if ref == "" {
53-
ref = "default"
54-
}
5551

5652
id, err := s.store.GetCid(name)
5753
if err != nil {
@@ -63,6 +59,10 @@ func (s *Server) Tree(w http.ResponseWriter, req *http.Request) error {
6359
return err
6460
}
6561

62+
if ref == "" {
63+
ref = repo.DefaultBranch()
64+
}
65+
6666
head, err := repo.Ref(ref)
6767
if err != nil {
6868
return err

0 commit comments

Comments
 (0)