support categories

This commit is contained in:
cel 🌸 2024-02-19 23:49:34 +00:00
parent 3c1b8007b6
commit ac0983a896
3 changed files with 105 additions and 26 deletions

View File

@ -18,7 +18,12 @@ func (d *deps) InfoRefs(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name")
name = filepath.Clean(name)
repo := filepath.Join(d.c.Repo.ScanPath, name)
repo := d.c.Repo.ScanPath
category := flow.Param(r.Context(), "category")
if category != "" {
repo = filepath.Join(repo, category)
}
repo = filepath.Join(repo, name)
w.Header().Set("content-type", "application/x-git-upload-pack-advertisement")
@ -34,9 +39,15 @@ func (d *deps) InfoRefs(w http.ResponseWriter, r *http.Request) {
srv := server.NewServer(loader)
session, err := srv.NewUploadPackSession(ep, nil)
if err != nil {
http.Error(w, err.Error(), 500)
log.Printf("git: %s", err)
return
billyfs := osfs.New(repo + ".git")
loader := server.NewFilesystemLoader(billyfs)
srv := server.NewServer(loader)
session, err = srv.NewUploadPackSession(ep, nil)
if err != nil {
http.Error(w, err.Error(), 500)
log.Printf("git: %s", err)
return
}
}
ar, err := session.AdvertisedReferencesContext(r.Context())
@ -64,7 +75,12 @@ func (d *deps) UploadPack(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name")
name = filepath.Clean(name)
repo := filepath.Join(d.c.Repo.ScanPath, name)
repo := d.c.Repo.ScanPath
category := flow.Param(r.Context(), "category")
if category != "" {
repo = filepath.Join(repo, category)
}
repo = filepath.Join(repo, name)
w.Header().Set("content-type", "application/x-git-upload-pack-result")
@ -88,9 +104,15 @@ func (d *deps) UploadPack(w http.ResponseWriter, r *http.Request) {
svr := server.NewServer(loader)
session, err := svr.NewUploadPackSession(ep, nil)
if err != nil {
http.Error(w, err.Error(), 500)
log.Printf("git: %s", err)
return
billyfs := osfs.New(repo + ".git")
loader := server.NewFilesystemLoader(billyfs)
svr := server.NewServer(loader)
session, err = svr.NewUploadPackSession(ep, nil)
if err != nil {
http.Error(w, err.Error(), 500)
log.Printf("git: %s", err)
return
}
}
res, err := session.UploadPack(r.Context(), upr)

View File

@ -39,6 +39,13 @@ func Handlers(c *config.Config) *flow.Mux {
mux.HandleFunc("/", d.Index, "GET")
mux.HandleFunc("/static/:file", d.ServeStatic, "GET")
mux.HandleFunc("/:category/:name", d.Multiplex, "GET", "POST")
mux.HandleFunc("/:category/:name/tree/:ref/...", d.RepoTree, "GET")
mux.HandleFunc("/:category/:name/blob/:ref/...", d.FileContent, "GET")
mux.HandleFunc("/:category/:name/log/:ref", d.Log, "GET")
mux.HandleFunc("/:category/:name/commit/:ref", d.Diff, "GET")
mux.HandleFunc("/:category/:name/refs", d.Refs, "GET")
mux.HandleFunc("/:category/:name/...", d.Multiplex, "GET", "POST")
mux.HandleFunc("/:name", d.Multiplex, "GET", "POST")
mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET")
mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET")

View File

@ -159,12 +159,22 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
return
}
name = filepath.Clean(name)
path := filepath.Join(d.c.Repo.ScanPath, name)
path := d.c.Repo.ScanPath
category := flow.Param(r.Context(), "category")
if category != "" {
path = filepath.Join(path, category)
log.Println(category)
}
path = filepath.Join(path, name)
log.Println(path)
gr, err := git.Open(path, "")
if err != nil {
d.Write404(w)
return
gr, err = git.Open(path+".git", "")
if err != nil {
d.Write404(w)
return
}
}
commits, err := gr.Commits()
@ -242,11 +252,19 @@ func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) {
ref := flow.Param(r.Context(), "ref")
name = filepath.Clean(name)
path := filepath.Join(d.c.Repo.ScanPath, name)
path := d.c.Repo.ScanPath
category := flow.Param(r.Context(), "category")
if category != "" {
path = filepath.Join(path, category)
}
path = filepath.Join(path, name)
gr, err := git.Open(path, ref)
if err != nil {
d.Write404(w)
return
gr, err = git.Open(path+".git", ref)
if err != nil {
d.Write404(w)
return
}
}
files, err := gr.FileTree(treePath)
@ -277,11 +295,19 @@ func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) {
ref := flow.Param(r.Context(), "ref")
name = filepath.Clean(name)
path := filepath.Join(d.c.Repo.ScanPath, name)
path := d.c.Repo.ScanPath
category := flow.Param(r.Context(), "category")
if category != "" {
path = filepath.Join(path, category)
}
path = filepath.Join(path, name)
gr, err := git.Open(path, ref)
if err != nil {
d.Write404(w)
return
gr, err = git.Open(path+".git", ref)
if err != nil {
d.Write404(w)
return
}
}
contents, err := gr.FileContent(treePath)
@ -303,11 +329,19 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
}
ref := flow.Param(r.Context(), "ref")
path := filepath.Join(d.c.Repo.ScanPath, name)
path := d.c.Repo.ScanPath
category := flow.Param(r.Context(), "category")
if category != "" {
path = filepath.Join(path, category)
}
path = filepath.Join(path, name)
gr, err := git.Open(path, ref)
if err != nil {
d.Write404(w)
return
gr, err = git.Open(path+".git", ref)
if err != nil {
d.Write404(w)
return
}
}
commits, err := gr.Commits()
@ -342,11 +376,19 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
}
ref := flow.Param(r.Context(), "ref")
path := filepath.Join(d.c.Repo.ScanPath, name)
path := d.c.Repo.ScanPath
category := flow.Param(r.Context(), "category")
if category != "" {
path = filepath.Join(path, category)
}
path = filepath.Join(path, name)
gr, err := git.Open(path, ref)
if err != nil {
d.Write404(w)
return
gr, err = git.Open(path+".git", ref)
if err != nil {
d.Write404(w)
return
}
}
diff, err := gr.Diff()
@ -382,11 +424,19 @@ func (d *deps) Refs(w http.ResponseWriter, r *http.Request) {
return
}
path := filepath.Join(d.c.Repo.ScanPath, name)
path := d.c.Repo.ScanPath
category := flow.Param(r.Context(), "category")
if category != "" {
path = filepath.Join(path, category)
}
path = filepath.Join(path, name)
gr, err := git.Open(path, "")
if err != nil {
d.Write404(w)
return
gr, err = git.Open(path+".git", "")
if err != nil {
d.Write404(w)
return
}
}
tags, err := gr.Tags()