Skip to content

Commit

Permalink
refactor(repos & route): use specified path for repo
Browse files Browse the repository at this point in the history
  • Loading branch information
riezebosch committed Feb 11, 2020
1 parent 63803fb commit 0c330c4
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 78 deletions.
2 changes: 1 addition & 1 deletion Main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ func main() {
fmt.Print(url)

browser.OpenURL(url)
panic(http.Serve(listener, Routes()))
panic(http.Serve(listener, Routes(".git")))
}
2 changes: 1 addition & 1 deletion id.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ func refID(id string) string {
}

func objectID(path string) string {
return path[13:15] + path[16:]
return path[len(path)-41:len(path)-39] + path[len(path)-38:]
}
5 changes: 5 additions & 0 deletions id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func TestObjectId(t *testing.T) {
assert.Equal(t, "00507eabbf76528884df48a1c9fe30434825bf57", objectID(path))
}

func TestObjectIdFromNonGitPath(t *testing.T) {
path := "test-repo.git/objects/05/a8da24ba96d54811f7ea93d527968e2dee3c41"
assert.Equal(t, "05a8da24ba96d54811f7ea93d527968e2dee3c41", objectID(path))
}

func TestRefIdWindows(t *testing.T) {
id := refID(filepath.Join("refs", "tags", "0.1"))
assert.Equal(t, "0.1", id)
Expand Down
38 changes: 19 additions & 19 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
)

// Visit repository and return directed acyclic graph
func Visit() Graph {
func Visit(path string) Graph {
var nodes = []Node{}
var edges = []Edge{}
nodes, edges = visitObjects(nodes, edges)
nodes, edges = visitRefs(nodes, edges)
nodes, edges = visitHead(nodes, edges)
nodes, edges = visitObjects(path, nodes, edges)
nodes, edges = visitRefs(path, nodes, edges)
nodes, edges = visitHead(path, nodes, edges)

return Graph{Nodes: nodes, Edges: edges}
}
Expand All @@ -38,30 +38,30 @@ func addEdgesFromCommit(id string, commit *gitobj.Commit) (edges []Edge) {
return
}

func visitRefs(nodes []Node, edges []Edge) ([]Node, []Edge) {
var files, _ = filepath.Glob(".git/refs/heads/*")
func visitRefs(path string, nodes []Node, edges []Edge) ([]Node, []Edge) {
var files, _ = filepath.Glob(path + "/refs/heads/*")
for _, file := range files {
nodes, edges = visitRef(file, nodes, edges, "head")
nodes, edges = visitRef(path, file, nodes, edges, "head")
}

files, _ = filepath.Glob(".git/refs/remotes/*/*")
files, _ = filepath.Glob(path + "/refs/remotes/*/*")
for _, file := range files {
nodes, edges = visitRef(file, nodes, edges, "remote")
nodes, edges = visitRef(path, file, nodes, edges, "remote")
}

files, _ = filepath.Glob(".git/refs/tags/*")
files, _ = filepath.Glob(path + "/refs/tags/*")
for _, file := range files {
nodes, edges = visitRef(file, nodes, edges, "tag")
nodes, edges = visitRef(path, file, nodes, edges, "tag")
}

return nodes, edges
}

func visitRef(path string, nodes []Node, edges []Edge, t string) ([]Node, []Edge) {
id := refID(path[5:])
func visitRef(path string, file string, nodes []Node, edges []Edge, t string) ([]Node, []Edge) {
id := refID(file[len(path)+1:])
nodes = append(nodes, Node{ID: id, Type: t})

to := refID(readFirstLine(path))
to := refID(readFirstLine(file))
edges = append(edges, Edge{From: id, To: to})

return nodes, edges
Expand All @@ -77,11 +77,11 @@ func readFirstLine(path string) string {
return scanner.Text()
}

func visitObjects(nodes []Node, edges []Edge) ([]Node, []Edge) {
repo, _ := gitobj.FromFilesystem(".git/objects", "")
func visitObjects(path string, nodes []Node, edges []Edge) ([]Node, []Edge) {
repo, _ := gitobj.FromFilesystem(filepath.Join(path, "objects"), "")
defer repo.Close()

objects, _ := filepath.Glob(".git/objects/??/*")
objects, _ := filepath.Glob(path + "/objects/??/*")
for _, object := range objects {
node, e, _ := visitObject(repo, objectID(object))
nodes = append(nodes, node)
Expand Down Expand Up @@ -113,7 +113,7 @@ func visitObject(repo *gitobj.ObjectDatabase, id string) (node Node, edges []Edg
return Node{}, edges, fmt.Errorf("Unkown object type for sha-ish %s", id)
}

func visitHead(nodes []Node, edges []Edge) ([]Node, []Edge) {
to := refID(readFirstLine(".git/HEAD"))
func visitHead(path string, nodes []Node, edges []Edge) ([]Node, []Edge) {
to := refID(readFirstLine(filepath.Join(path, "HEAD")))
return append(nodes, Node{ID: "HEAD", Type: "HEAD"}), append(edges, Edge{From: "HEAD", To: to})
}
42 changes: 21 additions & 21 deletions repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,48 @@ import (
)

func TestVisitObjectTree(t *testing.T) {
graph := Visit()
assert.Contains(t, graph.Nodes, Node{Type: "tree", ID: "4e84516b47b89c12f2f9bf41f34725ef6ddce099"})
assert.Contains(t, graph.Edges, Edge{From: "4e84516b47b89c12f2f9bf41f34725ef6ddce099", To: "eea118847928ac06875446004228e11658bcb789"})
graph := Visit("test-repo.git")
assert.Contains(t, graph.Nodes, Node{Type: "tree", ID: "9a96926442f477a2b03ae06cfffcab8393748218"})
assert.Contains(t, graph.Edges, Edge{From: "9a96926442f477a2b03ae06cfffcab8393748218", To: "f07c09068550591ffd7efda7814ec1dfda4a0da8"})
}

func TestVisitObjectCommit(t *testing.T) {
graph := Visit()
assert.Contains(t, graph.Nodes, Node{Type: "commit", ID: "bb4840c0b5dc29bcb7d4e0e2e5d1b9e9dec721e5"})
assert.Contains(t, graph.Edges, Edge{From: "bb4840c0b5dc29bcb7d4e0e2e5d1b9e9dec721e5", To: "c932e6ae3a32d3cdea9dd8043c165e52495ea4c9"})
assert.Contains(t, graph.Edges, Edge{From: "bb4840c0b5dc29bcb7d4e0e2e5d1b9e9dec721e5", To: "3dda9e9c4e40b7e1b743793075850eadf5817ab5"})
graph := Visit("test-repo.git")
assert.Contains(t, graph.Nodes, Node{Type: "commit", ID: "5b4ddc33ef3da7a248025cc228bc9ef7e860740a"})
assert.Contains(t, graph.Edges, Edge{From: "5b4ddc33ef3da7a248025cc228bc9ef7e860740a", To: "9a96926442f477a2b03ae06cfffcab8393748218"})
assert.Contains(t, graph.Edges, Edge{From: "5b4ddc33ef3da7a248025cc228bc9ef7e860740a", To: "408a3337cc79ba20939e223697fb6276628992a4"})
}

func TestVisitObjectBlob(t *testing.T) {
graph := Visit()
assert.Contains(t, graph.Nodes, Node{ID: "eea118847928ac06875446004228e11658bcb789", Type: "blob"})
graph := Visit("test-repo.git")
assert.Contains(t, graph.Nodes, Node{ID: "f07c09068550591ffd7efda7814ec1dfda4a0da8", Type: "blob"})
}

func TestVisitBranches(t *testing.T) {
graph := Visit()
assert.Contains(t, graph.Nodes, Node{ID: "for-testing", Type: "head"})
assert.Contains(t, graph.Edges, Edge{From: "for-testing", To: "627c86822eaa47167417c2c7fc99ef42c599711a"})
graph := Visit("test-repo.git")
assert.Contains(t, graph.Nodes, Node{ID: "simple-merge", Type: "head"})
assert.Contains(t, graph.Edges, Edge{From: "simple-merge", To: "6de25b8c5cd0cd49dc40d91e96f8e1cc9c2d07d8"})
}

func TestVisitRemoteTrackingBranches(t *testing.T) {
graph := Visit()
assert.Contains(t, graph.Nodes, Node{ID: "origin/master", Type: "remote"})
graph := Visit("test-repo.git")
assert.Contains(t, graph.Nodes, Node{ID: "github/master", Type: "remote"})
}

func TestVisitTags(t *testing.T) {
graph := Visit()
assert.Contains(t, graph.Nodes, Node{ID: "tag-for-testing", Type: "tag"})
assert.Contains(t, graph.Edges, Edge{From: "tag-for-testing", To: "5e0ddee0751a036f9f51585aa7fb7bde5afe5000"})
graph := Visit("test-repo.git")
assert.Contains(t, graph.Nodes, Node{ID: "R0.1", Type: "tag"})
assert.Contains(t, graph.Edges, Edge{From: "R0.1", To: "07870fcf1cae67fcee108e7e0bac81a4c69842d0"})
}

func TestVisitHead(t *testing.T) {
graph := Visit()
graph := Visit("test-repo.git")
assert.Contains(t, graph.Nodes, Node{ID: "HEAD", Type: "HEAD"})
assert.Contains(t, graph.Edges, Edge{From: "HEAD", To: "master"})
}

func TestVisitAll(t *testing.T) {
graph := Visit()
assert.Contains(t, graph.Nodes, Node{ID: "for-testing", Type: "head"})
assert.Contains(t, graph.Nodes, Node{ID: "tag-for-testing", Type: "tag"})
graph := Visit("test-repo.git")
assert.Contains(t, graph.Nodes, Node{ID: "simple-merge", Type: "head"})
assert.Contains(t, graph.Nodes, Node{ID: "R0.1", Type: "tag"})
}
65 changes: 36 additions & 29 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"

"github.com/go-chi/render"

"github.com/git-lfs/gitobj"
"github.com/go-chi/chi"
"github.com/go-chi/cors"
"github.com/go-chi/render"
)

// Routes for graph and content
func Routes() *chi.Mux {
func Routes(path string) *chi.Mux {
r := chi.NewRouter()
cors := cors.New(cors.Options{
AllowedOrigins: []string{"https://riezebosch.github.io"},
Expand All @@ -37,17 +34,17 @@ func Routes() *chi.Mux {
})

r.Route("/api", func(r chi.Router) {
r.Get("/info", getInfo)
r.Get("/graph", getGraph)
r.Get("/info", get(info, path))
r.Get("/graph", get(graph, path))
r.Route("/refs", func(r chi.Router) {
r.Get("/{type}/{name}", getRef)
r.Get("/remotes/{remote}/{name}", getRefRemote)
r.Get("/HEAD", getHead)
r.Get("/{type}/{name}", get(ref, path))
r.Get("/remotes/{remote}/{name}", get(remote, path))
r.Get("/HEAD", get(head, path))
})
r.Route("/objects", func(r chi.Router) {
r.Get("/blob/{id}", getBlob)
r.Get("/tree/{id}", getTree)
r.Get("/commit/{id}", getCommit)
r.Get("/blob/{id}", get(blob, path))
r.Get("/tree/{id}", get(tree, path))
r.Get("/commit/{id}", get(commit, path))
})
})

Expand All @@ -59,33 +56,37 @@ type Info struct {
Directory string `json:"directory"`
}

func getInfo(w http.ResponseWriter, r *http.Request) {
directory, _ := os.Getwd()
render.JSON(w, r, Info{Directory: filepath.Base(directory)})
func info(w http.ResponseWriter, r *http.Request, path string) {
wd, err := filepath.Abs(filepath.Join(path, ".."))
if err != nil {
panic(err)
}

render.JSON(w, r, Info{Directory: filepath.Base(wd)})
}

func getGraph(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, Visit())
func graph(w http.ResponseWriter, r *http.Request, path string) {
render.JSON(w, r, Visit(path))
}

func getRef(w http.ResponseWriter, r *http.Request) {
func ref(w http.ResponseWriter, r *http.Request, path string) {
t := chi.URLParam(r, "type")
n := chi.URLParam(r, "name")
w.Write([]byte(readFirstLine(path.Join(".git", "refs", t, n))))
w.Write([]byte(readFirstLine(filepath.Join(path, "refs", t, n))))
}

func getRefRemote(w http.ResponseWriter, r *http.Request) {
func remote(w http.ResponseWriter, r *http.Request, path string) {
t := chi.URLParam(r, "remote")
n := chi.URLParam(r, "name")
w.Write([]byte(readFirstLine(path.Join(".git", "refs", "remotes", t, n))))
w.Write([]byte(readFirstLine(filepath.Join(path, "refs", "remotes", t, n))))
}

func getHead(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(readFirstLine(path.Join(".git", "HEAD"))))
func head(w http.ResponseWriter, r *http.Request, path string) {
w.Write([]byte(readFirstLine(filepath.Join(path, "HEAD"))))
}

func getBlob(w http.ResponseWriter, r *http.Request) {
repo, _ := gitobj.FromFilesystem(".git/objects", "")
func blob(w http.ResponseWriter, r *http.Request, path string) {
repo, _ := gitobj.FromFilesystem(filepath.Join(path, "objects"), "")
defer repo.Close()

id, err := hex.DecodeString(chi.URLParam(r, "id"))
Expand All @@ -106,7 +107,7 @@ func getBlob(w http.ResponseWriter, r *http.Request) {
w.Write(content)
}

func getTree(w http.ResponseWriter, r *http.Request) {
func tree(w http.ResponseWriter, r *http.Request, path string) {
repo, _ := gitobj.FromFilesystem(".git/objects", "")
defer repo.Close()

Expand All @@ -125,8 +126,8 @@ func getTree(w http.ResponseWriter, r *http.Request) {
}
}

func getCommit(w http.ResponseWriter, r *http.Request) {
repo, _ := gitobj.FromFilesystem(".git/objects", "")
func commit(w http.ResponseWriter, r *http.Request, path string) {
repo, _ := gitobj.FromFilesystem(filepath.Join(path, "objects"), "")
defer repo.Close()

id, err := hex.DecodeString(chi.URLParam(r, "id"))
Expand All @@ -141,3 +142,9 @@ func getCommit(w http.ResponseWriter, r *http.Request) {

o.Encode(w)
}

func get(f func(http.ResponseWriter, *http.Request, string), path string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
f(w, r, path)
}
}
14 changes: 7 additions & 7 deletions routers_test.go → routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ func TestInfo(t *testing.T) {
}

func TestGraph(t *testing.T) {
assert.Contains(t, request(t, "/api/graph"), "nodes")
assert.Contains(t, request(t, "/api/graph"), "f07c09068550591ffd7efda7814ec1dfda4a0da8")
}

func TestObjectCommit(t *testing.T) {
assert.Contains(t, request(t, "/api/objects/commit/627c86822eaa47167417c2c7fc99ef42c599711a"), "author Manuel Riezebosch <[email protected]>")
assert.Contains(t, request(t, "/api/objects/commit/5b4ddc33ef3da7a248025cc228bc9ef7e860740a"), "author Manuel Riezebosch <[email protected]>")
}

func TestObjectBlob(t *testing.T) {
assert.Contains(t, request(t, "/api/objects/blob/eea118847928ac06875446004228e11658bcb789"), "package main")
assert.Contains(t, request(t, "/api/objects/blob/f07c09068550591ffd7efda7814ec1dfda4a0da8"), "<html>")
}

func TestRef(t *testing.T) {
assert.Contains(t, request(t, "/api/refs/heads/for-testing"), "627c86822eaa47167417c2c7fc99ef42c599711a")
assert.Contains(t, request(t, "/api/refs/heads/simple-merge"), "6de25b8c5cd0cd49dc40d91e96f8e1cc9c2d07d8")
}

func TestRefTag(t *testing.T) {
assert.Contains(t, request(t, "/api/refs/tags/v0.1"), "6362adfa2c2ce3adddae2ea82db7b54f6d60da34")
assert.Contains(t, request(t, "/api/refs/tags/R0.1"), "07870fcf1cae67fcee108e7e0bac81a4c69842d0")
}

func TestRefRemoteTrackingBranch(t *testing.T) {
assert.Contains(t, request(t, "/api/refs/remotes/origin/for-testing"), "627c86822eaa47167417c2c7fc99ef42c599711a")
assert.NotEmpty(t, request(t, "/api/refs/remotes/github/master"))
}

func TestRefHead(t *testing.T) {
Expand All @@ -53,7 +53,7 @@ func TestObjectTree(t *testing.T) {
}

func request(t *testing.T, path string) string {
ts := httptest.NewServer(Routes())
ts := httptest.NewServer(Routes("test-repo.git"))
defer ts.Close()

req, err := http.NewRequest("GET", ts.URL+path, nil)
Expand Down

0 comments on commit 0c330c4

Please sign in to comment.