Personal Git forge.

Code git clone ssh://git.poy.ooo:6969/~poyo/farga ZIP
app/repo-new.go / 1KiB #be8fce98
Raw
package appimport (	md "farga/app/middleware"	"farga/db/types"	"farga/lib"	errs "farga/lib/errors"	"farga/templating"	"fmt"	"net/http"	"github.com/go-git/go-git/v6")type CreateRepoForm struct {	Name        string `form:"name" validate:"required,min=1,max=40"`	Description string `form:"description" validate:"max=150"`	Visibility  string `form:"visibility" validate:"required,oneof=public private"`}func (a *App) NewRepo(w http.ResponseWriter, r *http.Request) {	switch r.Method {	case http.MethodGet:		templating.Render(w, r, "base", "repo/new")	case http.MethodPost:		form, ok := md.Form[CreateRepoForm](r)		if !ok {			http.Error(w, "Something went wrong on our end.", http.StatusInternalServerError)			return		}		if !lib.GitNameRegex.MatchString(form.Name) {			http.Redirect(w, r, "/new?err=GitInvalidBranchName", http.StatusSeeOther)			return		}		user, ok := r.Context().Value(lib.UserKey).(*types.User)		if !ok {			http.Redirect(w, r, "/?err=UserNotFound", http.StatusSeeOther)			return		}		repo := types.Repo{			Name:        form.Name,			Description: form.Description,			UserID:      &user.ID,			BaseBranch:  user.Config.GitDefaultBranch,			IsPrivate:   form.Visibility == "private",		}		gitRepo, err := a.Store.NewRepo(*user, repo)		if err != nil {			switch err {			case errs.ErrRepoAlreadyExists:				http.Redirect(w, r, "/new?err=RepoNameTaken", http.StatusSeeOther)			case git.ErrDestinationExists:				http.Redirect(w, r, "/new?err=RepoNameTaken", http.StatusSeeOther)			default:				http.Error(w, fmt.Sprintf("Uncaught error: %s", err.Error()), 500)			}			return		}		gitRepo.Close() // mandatory!!		http.Redirect(w, r, fmt.Sprintf("/~%s/%s", user.Handle, repo.Name), http.StatusFound)	}}