From 3d7cb5c3b23e263536c5b8a86ab4703b70f222e7 Mon Sep 17 00:00:00 2001 From: Manuel Riezebosch Date: Thu, 12 Sep 2019 13:42:56 +0200 Subject: [PATCH] feat(id): handle path separator --- id.go | 24 +++++++++++++++--------- id_test.go | 22 +++++++++++++++++++--- repo.go | 21 +++++++++++++-------- repo_test.go | 5 +++++ 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/id.go b/id.go index 291295d..1364212 100644 --- a/id.go +++ b/id.go @@ -1,21 +1,27 @@ package main -import "strings" +import ( + "path/filepath" + "strings" +) -func refID(path string) string { - if strings.HasPrefix(path, "refs/heads/") { - return path[11:] +func refID(id string) string { + id = filepath.ToSlash(id) + id = strings.Replace(id, "ref: ", "", 1) + + if strings.HasPrefix(id, "refs/heads/") { + return id[11:] } - if strings.HasPrefix(path, "refs/remotes/") { - return path[13:] + if strings.HasPrefix(id, "refs/remotes/") { + return id[13:] } - if strings.HasPrefix(path, "refs/tags/") { - return path[10:] + if strings.HasPrefix(id, "refs/tags/") { + return id[10:] } - return path + return id } func objectID(path string) string { diff --git a/id_test.go b/id_test.go index 8aa5ef7..57b5856 100644 --- a/id_test.go +++ b/id_test.go @@ -1,6 +1,7 @@ package main import ( + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -8,20 +9,35 @@ import ( func TestRefId(t *testing.T) { id := refID("refs/heads/master") - assert.Equal(t, id, "master") + assert.Equal(t, "master", id) } func TestRefIdRemote(t *testing.T) { id := refID("refs/remotes/origin/master") - assert.Equal(t, id, "origin/master") + assert.Equal(t, "origin/master", id) } func TestRefIdTag(t *testing.T) { id := refID("refs/tags/0.1") - assert.Equal(t, id, "0.1") + assert.Equal(t, "0.1", id) } func TestObjectId(t *testing.T) { path := ".git/objects/00/507eabbf76528884df48a1c9fe30434825bf57" assert.Equal(t, "00507eabbf76528884df48a1c9fe30434825bf57", objectID(path)) } + +func TestRefIdWindows(t *testing.T) { + id := refID(filepath.Join("refs", "tags", "0.1")) + assert.Equal(t, "0.1", id) +} + +func TestRefIdWindowsRemote(t *testing.T) { + id := refID(filepath.Join("refs", "remotes", "origin", "master")) + assert.Equal(t, "origin/master", id) +} + +func TestRefSpecInRefId(t *testing.T) { + path := "ref: refs/heads/master" + assert.Equal(t, "master", refID(path)) +} diff --git a/repo.go b/repo.go index f965802..0fcd6a9 100644 --- a/repo.go +++ b/repo.go @@ -6,7 +6,6 @@ import ( "fmt" "os" "path/filepath" - "strings" "github.com/git-lfs/gitobj" ) @@ -40,7 +39,17 @@ func addEdgesFromCommit(id string, commit *gitobj.Commit) (edges []Edge) { } func visitRefs(nodes []Node, edges []Edge) ([]Node, []Edge) { - files, _ := filepath.Glob(".git/refs/**/*") + var files, _ = filepath.Glob(".git/refs/heads/*") + for _, file := range files { + nodes, edges = visitRef(file, nodes, edges) + } + + files, _ = filepath.Glob(".git/refs/remotes/*/*") + for _, file := range files { + nodes, edges = visitRef(file, nodes, edges) + } + + files, _ = filepath.Glob(".git/refs/tags/*") for _, file := range files { nodes, edges = visitRef(file, nodes, edges) } @@ -52,7 +61,7 @@ func visitRef(path string, nodes []Node, edges []Edge) ([]Node, []Edge) { id := refID(path[5:]) nodes = append(nodes, Node{ID: id, Type: "branch"}) - to := readFirstLine(path) + to := refID(readFirstLine(path)) edges = append(edges, Edge{From: id, To: to}) return nodes, edges @@ -105,10 +114,6 @@ func visitObject(repo *gitobj.ObjectDatabase, id string) (node Node, edges []Edg } func visitHead(nodes []Node, edges []Edge) ([]Node, []Edge) { - to := readFirstLine(".git/HEAD") - if strings.HasPrefix(to, "ref: ") { - to = refID(to[5:]) - } - + to := refID(readFirstLine(".git/HEAD")) return append(nodes, Node{ID: "HEAD", Type: "branch"}), append(edges, Edge{From: "HEAD", To: to}) } diff --git a/repo_test.go b/repo_test.go index 9b544ba..46211fc 100644 --- a/repo_test.go +++ b/repo_test.go @@ -30,6 +30,11 @@ func TestVisitBranches(t *testing.T) { assert.Contains(t, graph.Edges, Edge{From: "for-testing", To: "627c86822eaa47167417c2c7fc99ef42c599711a"}) } +func TestVisitRemoteTrackingBranches(t *testing.T) { + graph := Visit() + assert.Contains(t, graph.Nodes, Node{ID: "origin/master", Type: "branch"}) +} + func TestVisitTags(t *testing.T) { graph := Visit() assert.Contains(t, graph.Nodes, Node{ID: "tag-for-testing", Type: "branch"})