Skip to content

Commit

Permalink
feat(main): specify repository path on command line
Browse files Browse the repository at this point in the history
  • Loading branch information
riezebosch committed Feb 12, 2020
1 parent 0c330c4 commit ad9fe82
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 30 deletions.
17 changes: 15 additions & 2 deletions Main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package main

import (
"flag"
"fmt"
"net"
"net/http"
"os"

"github.com/pkg/browser"
)

func main() {
listener, err := net.Listen("tcp", ":0")
port := flag.Int("port", 0, "")
flag.Usage = func() {
fmt.Println(fmt.Sprintf("%s [flags] [repository]", os.Args[0]))
fmt.Println()
fmt.Println("flags:")
flag.PrintDefaults()
}
flag.Parse()

listener, err := net.Listen("tcp", fmt.Sprintf(":%v", *port))
if err != nil {
panic(err)
}
Expand All @@ -18,5 +29,7 @@ func main() {
fmt.Print(url)

browser.OpenURL(url)
panic(http.Serve(listener, Routes(".git")))
path := flag.Arg(0)
fmt.Print(path)
panic(http.Serve(listener, Routes(path)))
}
41 changes: 26 additions & 15 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/git-lfs/gitobj"
"github.com/go-chi/chi"
Expand All @@ -14,7 +16,7 @@ import (
)

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

if wd == "" {
wd, _ = os.Getwd()
}

repo := repo(wd)

r.Route("/api", func(r chi.Router) {
r.Get("/info", get(info, path))
r.Get("/graph", get(graph, path))
r.Get("/info", get(info, wd))
r.Get("/graph", get(graph, repo))
r.Route("/refs", func(r chi.Router) {
r.Get("/{type}/{name}", get(ref, path))
r.Get("/remotes/{remote}/{name}", get(remote, path))
r.Get("/HEAD", get(head, path))
r.Get("/{type}/{name}", get(ref, repo))
r.Get("/remotes/{remote}/{name}", get(remote, repo))
r.Get("/HEAD", get(head, repo))
})
r.Route("/objects", func(r chi.Router) {
r.Get("/blob/{id}", get(blob, path))
r.Get("/tree/{id}", get(tree, path))
r.Get("/commit/{id}", get(commit, path))
r.Get("/blob/{id}", get(blob, repo))
r.Get("/tree/{id}", get(tree, repo))
r.Get("/commit/{id}", get(commit, repo))
})
})

Expand All @@ -56,12 +64,7 @@ type Info struct {
Directory string `json:"directory"`
}

func info(w http.ResponseWriter, r *http.Request, path string) {
wd, err := filepath.Abs(filepath.Join(path, ".."))
if err != nil {
panic(err)
}

func info(w http.ResponseWriter, r *http.Request, wd string) {
render.JSON(w, r, Info{Directory: filepath.Base(wd)})
}

Expand Down Expand Up @@ -148,3 +151,11 @@ func get(f func(http.ResponseWriter, *http.Request, string), path string) func(h
f(w, r, path)
}
}

func repo(wd string) string {
if strings.HasSuffix(wd, ".git") != true {
return filepath.Join(wd, ".git")
}

return wd
}
34 changes: 21 additions & 13 deletions routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,58 @@ import (
)

func TestInfo(t *testing.T) {
assert.Contains(t, request(t, "/api/info"), "\"gitviz\"")
assert.Contains(t, request(t, "test-repo.git", "/api/info"), "\"test-repo.git\"")
}

func TestInfoCurrentDir(t *testing.T) {
assert.Contains(t, request(t, "", "/api/info"), "\"gitviz\"")
}

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

func TestGraphCurrentDir(t *testing.T) {
assert.Contains(t, request(t, "", "/api/graph"), "\"from\":\"HEAD\"")
}

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

func TestObjectBlob(t *testing.T) {
assert.Contains(t, request(t, "/api/objects/blob/f07c09068550591ffd7efda7814ec1dfda4a0da8"), "<html>")
assert.Contains(t, request(t, "test-repo.git", "/api/objects/blob/f07c09068550591ffd7efda7814ec1dfda4a0da8"), "<html>")
}

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

func TestRefTag(t *testing.T) {
assert.Contains(t, request(t, "/api/refs/tags/R0.1"), "07870fcf1cae67fcee108e7e0bac81a4c69842d0")
assert.Contains(t, request(t, "test-repo.git", "/api/refs/tags/R0.1"), "07870fcf1cae67fcee108e7e0bac81a4c69842d0")
}

func TestRefRemoteTrackingBranch(t *testing.T) {
assert.NotEmpty(t, request(t, "/api/refs/remotes/github/master"))
assert.NotEmpty(t, request(t, "test-repo.git", "/api/refs/remotes/github/master"))
}

func TestRefHead(t *testing.T) {
assert.Contains(t, request(t, "/api/refs/HEAD"), "ref: refs/heads/master")
assert.Contains(t, request(t, "test-repo.git", "/api/refs/HEAD"), "ref: refs/heads/master")
}

func TestIndex(t *testing.T) {
assert.Contains(t, request(t, ""), "<html>")
assert.Contains(t, request(t, "test-repo.git", ""), "<html>")
}

func TestFavicon(t *testing.T) {
assert.NotContains(t, request(t, "/favicon.ico"), "404")
assert.NotContains(t, request(t, "test-repo.git", "/favicon.ico"), "404")
}
func TestObjectTree(t *testing.T) {
assert.Contains(t, request(t, "/api/objects/tree/4e84516b47b89c12f2f9bf41f34725ef6ddce099"), "Main.go")
assert.Contains(t, request(t, "test-repo.git", "/api/objects/tree/4e84516b47b89c12f2f9bf41f34725ef6ddce099"), "Main.go")
}

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

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

0 comments on commit ad9fe82

Please sign in to comment.