phase 3 bug repositories fixes

This commit is contained in:
2026-05-07 01:15:32 +02:00
parent 200c4f43ea
commit 44359c1bb0
29 changed files with 1223 additions and 51 deletions
+29 -5
View File
@@ -15,6 +15,24 @@ import (
"github.com/forgeo/forgebucket/internal/models"
)
// repoResponse enriches a Repository with derived fields the frontend needs.
type repoResponse struct {
models.Repository
OwnerName string `json:"ownerName"`
IsEmpty bool `json:"isEmpty"`
}
func (h *RepoHandler) withOwnerName(repo *models.Repository) repoResponse {
var owner models.User
h.db.ID(repo.OwnerID).Get(&owner)
gitdomain.SetRepoRoot(h.cfg.RepoRoot)
return repoResponse{
Repository: *repo,
OwnerName: owner.Username,
IsEmpty: gitdomain.IsEmpty(repo.DiskPath),
}
}
type RepoHandler struct {
db *xorm.Engine
cfg *config.Config
@@ -32,10 +50,16 @@ func (h *RepoHandler) List(w http.ResponseWriter, r *http.Request) {
jsonError(w, "could not list repositories", http.StatusInternalServerError)
return
}
if repos == nil {
repos = []models.Repository{}
// Fetch owner username once (all repos belong to the same user in this query)
var owner models.User
h.db.ID(userID).Get(&owner)
result := make([]repoResponse, len(repos))
for i, repo := range repos {
result[i] = repoResponse{Repository: repo, OwnerName: owner.Username}
}
jsonOK(w, repos)
jsonOK(w, result)
}
func (h *RepoHandler) Create(w http.ResponseWriter, r *http.Request) {
@@ -80,7 +104,7 @@ func (h *RepoHandler) Create(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(repo)
json.NewEncoder(w).Encode(h.withOwnerName(repo))
}
func (h *RepoHandler) Get(w http.ResponseWriter, r *http.Request) {
@@ -88,7 +112,7 @@ func (h *RepoHandler) Get(w http.ResponseWriter, r *http.Request) {
if !ok {
return
}
jsonOK(w, repo)
jsonOK(w, h.withOwnerName(repo))
}
func (h *RepoHandler) Tree(w http.ResponseWriter, r *http.Request) {
+12
View File
@@ -45,6 +45,14 @@ func run(repoPath string, args ...string) ([]byte, error) {
return out, nil
}
// IsEmpty returns true when the repo has no commits yet.
// Must use --verify: without it, rev-parse exits 0 in a bare empty repo
// because HEAD is a valid symbolic ref even before the first commit.
func IsEmpty(repoPath string) bool {
_, err := run(repoPath, "rev-parse", "--verify", "HEAD")
return err != nil
}
func Init(path string) error {
// git init --bare works even if the directory doesn't exist yet
cmd := exec.Command("git", "init", "--bare", path)
@@ -101,6 +109,10 @@ type TreeEntry struct {
}
func TreeLS(repoPath, ref, subPath string) ([]TreeEntry, error) {
// Short-circuit for repos with no commits yet.
if IsEmpty(repoPath) {
return nil, nil
}
treeRef := ref
if subPath != "" {
treeRef = ref + ":" + subPath