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")
@ -33,11 +38,17 @@ func (d *deps) InfoRefs(w http.ResponseWriter, r *http.Request) {
loader := server.NewFilesystemLoader(billyfs)
srv := server.NewServer(loader)
session, err := srv.NewUploadPackSession(ep, nil)
if err != nil {
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())
if errors.Is(err, transport.ErrRepositoryNotFound) {
@ -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")
@ -87,11 +103,17 @@ func (d *deps) UploadPack(w http.ResponseWriter, r *http.Request) {
loader := server.NewFilesystemLoader(billyfs)
svr := server.NewServer(loader)
session, err := svr.NewUploadPackSession(ep, nil)
if err != nil {
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)
if err != nil {

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,13 +159,23 @@ 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 {
gr, err = git.Open(path+".git", "")
if err != nil {
d.Write404(w)
return
}
}
commits, err := gr.Commits()
if err != nil {
@ -242,12 +252,20 @@ 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 {
gr, err = git.Open(path+".git", ref)
if err != nil {
d.Write404(w)
return
}
}
files, err := gr.FileTree(treePath)
if err != nil {
@ -277,12 +295,20 @@ 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 {
gr, err = git.Open(path+".git", ref)
if err != nil {
d.Write404(w)
return
}
}
contents, err := gr.FileContent(treePath)
data := make(map[string]any)
@ -303,12 +329,20 @@ 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 {
gr, err = git.Open(path+".git", ref)
if err != nil {
d.Write404(w)
return
}
}
commits, err := gr.Commits()
if err != nil {
@ -342,12 +376,20 @@ 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 {
gr, err = git.Open(path+".git", ref)
if err != nil {
d.Write404(w)
return
}
}
diff, err := gr.Diff()
if err != nil {
@ -382,12 +424,20 @@ 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 {
gr, err = git.Open(path+".git", "")
if err != nil {
d.Write404(w)
return
}
}
tags, err := gr.Tags()
if err != nil {