can now import repos and have more settings for creating new ones.

This commit is contained in:
2026-05-07 12:16:58 +02:00
parent dad82a79de
commit 39dd9ab9eb
99 changed files with 7442 additions and 131 deletions
+100 -9
View File
@@ -3,6 +3,7 @@ package handlers
import (
"encoding/json"
"net/http"
"net/url"
"path/filepath"
"strconv"
@@ -62,9 +63,12 @@ func (h *RepoHandler) Create(w http.ResponseWriter, r *http.Request) {
userID, _ := middleware.UserIDFromContext(r.Context())
var body struct {
Name string `json:"name"`
Description string `json:"description"`
IsPrivate bool `json:"isPrivate"`
Name string `json:"name"`
Description string `json:"description"`
IsPrivate bool `json:"isPrivate"`
DefaultBranch string `json:"defaultBranch"`
InitReadme string `json:"initReadme"` // "none" | "blank" | "tutorial"
InitGitignore bool `json:"initGitignore"` // true → add .gitignore
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
jsonError(w, "invalid request body", http.StatusBadRequest)
@@ -74,6 +78,93 @@ func (h *RepoHandler) Create(w http.ResponseWriter, r *http.Request) {
jsonError(w, "name is required", http.StatusBadRequest)
return
}
branch := body.DefaultBranch
if branch == "" {
branch = "main"
}
diskPath := filepath.Join(h.cfg.RepoRoot, strconv.FormatInt(userID, 10), body.Name+".git")
repo := &models.Repository{
OwnerID: userID,
Name: body.Name,
Description: body.Description,
IsPrivate: body.IsPrivate,
DefaultBranch: branch,
DiskPath: diskPath,
}
gitdomain.SetRepoRoot(h.cfg.RepoRoot)
if err := gitdomain.Init(diskPath); err != nil {
jsonError(w, "could not initialise git repository", http.StatusInternalServerError)
return
}
// Update bare repo HEAD if branch differs from the Init default (main).
if branch != "main" {
gitdomain.SetDefaultBranch(diskPath, branch)
}
if _, err := h.db.Insert(repo); err != nil {
jsonError(w, "repository name already taken", http.StatusConflict)
return
}
// Create initial commit when README or .gitignore was requested.
wantsReadme := body.InitReadme == "blank" || body.InitReadme == "tutorial"
if wantsReadme || body.InitGitignore {
var u models.User
h.db.ID(userID).Get(&u)
authorEmail := u.Email
if authorEmail == "" {
authorEmail = u.Username + "@forgebucket.local"
}
if err := gitdomain.InitWithFiles(diskPath, branch, body.Name, u.Username, authorEmail, wantsReadme, body.InitGitignore); err != nil {
// Non-fatal: repo is created, just without initial files.
_ = err
}
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(h.withOwnerName(repo))
}
func (h *RepoHandler) Import(w http.ResponseWriter, r *http.Request) {
userID, ok := middleware.UserIDFromContext(r.Context())
if !ok {
jsonError(w, "unauthorized", http.StatusUnauthorized)
return
}
var body struct {
URL string `json:"url"`
Name string `json:"name"`
Description string `json:"description"`
IsPrivate bool `json:"isPrivate"`
AuthUser string `json:"authUser"`
AuthPass string `json:"authPass"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
jsonError(w, "invalid request body", http.StatusBadRequest)
return
}
if body.URL == "" || body.Name == "" {
jsonError(w, "url and name are required", http.StatusBadRequest)
return
}
// Inject credentials into the URL if provided.
srcURL := body.URL
if body.AuthUser != "" {
u, err := url.Parse(body.URL)
if err != nil {
jsonError(w, "invalid URL", http.StatusBadRequest)
return
}
u.User = url.UserPassword(body.AuthUser, body.AuthPass)
srcURL = u.String()
}
diskPath := filepath.Join(h.cfg.RepoRoot, strconv.FormatInt(userID, 10), body.Name+".git")
@@ -86,15 +177,15 @@ func (h *RepoHandler) Create(w http.ResponseWriter, r *http.Request) {
DiskPath: diskPath,
}
// Initialise bare repo on disk before inserting to DB
gitdomain.SetRepoRoot(h.cfg.RepoRoot)
if err := gitdomain.Init(diskPath); err != nil {
jsonError(w, "could not initialise git repository", http.StatusInternalServerError)
if _, err := h.db.Insert(repo); err != nil {
jsonError(w, "repository name already taken", http.StatusConflict)
return
}
if _, err := h.db.Insert(repo); err != nil {
jsonError(w, "repository name already taken", http.StatusConflict)
gitdomain.SetRepoRoot(h.cfg.RepoRoot)
if err := gitdomain.CloneRepo(srcURL, diskPath); err != nil {
h.db.ID(repo.ID).Delete(&models.Repository{})
jsonError(w, "import failed: "+err.Error(), http.StatusBadRequest)
return
}