Skip to content

Commit

Permalink
feat(id): handle path separator
Browse files Browse the repository at this point in the history
  • Loading branch information
riezebosch committed Sep 12, 2019
1 parent 94a42f6 commit 3d7cb5c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
24 changes: 15 additions & 9 deletions id.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
22 changes: 19 additions & 3 deletions id_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
package main

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

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))
}
21 changes: 13 additions & 8 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/git-lfs/gitobj"
)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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})
}
5 changes: 5 additions & 0 deletions repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down

0 comments on commit 3d7cb5c

Please sign in to comment.