Monorepo for Tangled tangled.org

set default branch from KNOT_REPO_MAIN_BRANCH

Changed files
+29 -19
knotserver
+1 -1
knotserver/config/config.go
··· 9 type Repo struct { 10 ScanPath string `env:"SCAN_PATH, default=/home/git"` 11 Readme []string `env:"README"` 12 - MainBranch []string `env:"MAIN_BRANCH"` 13 } 14 15 type Server struct {
··· 9 type Repo struct { 10 ScanPath string `env:"SCAN_PATH, default=/home/git"` 11 Readme []string `env:"README"` 12 + MainBranch string `env:"MAIN_BRANCH, default=main"` 13 } 14 15 type Server struct {
+5 -12
knotserver/git/git.go
··· 225 return branches, nil 226 } 227 228 - func (g *GitRepo) FindMainBranch(branches []string) (string, error) { 229 - branches = append(branches, []string{ 230 - "main", 231 - "master", 232 - "trunk", 233 - }...) 234 - for _, b := range branches { 235 - _, err := g.r.ResolveRevision(plumbing.Revision(b)) 236 - if err == nil { 237 - return b, nil 238 - } 239 } 240 - return "", fmt.Errorf("unable to find main branch") 241 } 242 243 // WriteTar writes itself from a tree into a binary tar file format.
··· 225 return branches, nil 226 } 227 228 + func (g *GitRepo) FindMainBranch() (string, error) { 229 + ref, err := g.r.Head() 230 + if err != nil { 231 + return "", fmt.Errorf("unable to find main branch: %w", err) 232 } 233 + return string(ref.Name()), err 234 } 235 236 // WriteTar writes itself from a tree into a binary tar file format.
+11 -2
knotserver/git/repo.go
··· 8 9 gogit "github.com/go-git/go-git/v5" 10 "github.com/go-git/go-git/v5/config" 11 ) 12 13 - func InitBare(path string) error { 14 parent := filepath.Dir(path) 15 16 if err := os.MkdirAll(parent, 0755); errors.Is(err, os.ErrExist) { ··· 22 return err 23 } 24 25 err = repository.CreateBranch(&config.Branch{ 26 - Name: "main", 27 }) 28 if err != nil { 29 return fmt.Errorf("creating branch: %w", err) 30 } 31 32 return nil
··· 8 9 gogit "github.com/go-git/go-git/v5" 10 "github.com/go-git/go-git/v5/config" 11 + "github.com/go-git/go-git/v5/plumbing" 12 ) 13 14 + func InitBare(path, defaultBranch string) error { 15 parent := filepath.Dir(path) 16 17 if err := os.MkdirAll(parent, 0755); errors.Is(err, os.ErrExist) { ··· 23 return err 24 } 25 26 + // set up default branch 27 err = repository.CreateBranch(&config.Branch{ 28 + Name: defaultBranch, 29 }) 30 if err != nil { 31 return fmt.Errorf("creating branch: %w", err) 32 + } 33 + 34 + defaultReference := plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", defaultBranch)) 35 + 36 + ref := plumbing.NewSymbolicReference(plumbing.HEAD, defaultReference) 37 + if err = repository.Storer.SetReference(ref); err != nil { 38 + return fmt.Errorf("creating symbolic reference: %w", err) 39 } 40 41 return nil
+12 -4
knotserver/routes.go
··· 9 "errors" 10 "fmt" 11 "html/template" 12 "net/http" 13 "path/filepath" 14 "strconv" ··· 37 38 gr, err := git.Open(path, ref) 39 if err != nil { 40 if errors.Is(err, plumbing.ErrReferenceNotFound) { 41 resp := types.RepoIndexResponse{ 42 IsEmpty: true, ··· 135 } 136 137 if ref == "" { 138 - mainBranch, err := gr.FindMainBranch(h.c.Repo.MainBranch) 139 if err != nil { 140 writeError(w, err.Error(), http.StatusInternalServerError) 141 l.Error("finding main branch", "error", err.Error()) ··· 493 l := h.l.With("handler", "NewRepo") 494 495 data := struct { 496 - Did string `json:"did"` 497 - Name string `json:"name"` 498 }{} 499 500 if err := json.NewDecoder(r.Body).Decode(&data); err != nil { ··· 502 return 503 } 504 505 did := data.Did 506 name := data.Name 507 508 relativeRepoPath := filepath.Join(did, name) 509 repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, relativeRepoPath) 510 - err := git.InitBare(repoPath) 511 if err != nil { 512 l.Error("initializing bare repo", "error", err.Error()) 513 if errors.Is(err, gogit.ErrRepositoryAlreadyExists) {
··· 9 "errors" 10 "fmt" 11 "html/template" 12 + "log" 13 "net/http" 14 "path/filepath" 15 "strconv" ··· 38 39 gr, err := git.Open(path, ref) 40 if err != nil { 41 + log.Println(err) 42 if errors.Is(err, plumbing.ErrReferenceNotFound) { 43 resp := types.RepoIndexResponse{ 44 IsEmpty: true, ··· 137 } 138 139 if ref == "" { 140 + mainBranch, err := gr.FindMainBranch() 141 if err != nil { 142 writeError(w, err.Error(), http.StatusInternalServerError) 143 l.Error("finding main branch", "error", err.Error()) ··· 495 l := h.l.With("handler", "NewRepo") 496 497 data := struct { 498 + Did string `json:"did"` 499 + Name string `json:"name"` 500 + DefaultBranch string `json:"default_branch,omitempty"` 501 }{} 502 503 if err := json.NewDecoder(r.Body).Decode(&data); err != nil { ··· 505 return 506 } 507 508 + if data.DefaultBranch == "" { 509 + data.DefaultBranch = "main" 510 + } 511 + 512 did := data.Did 513 name := data.Name 514 + defaultBranch := data.DefaultBranch 515 516 relativeRepoPath := filepath.Join(did, name) 517 repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, relativeRepoPath) 518 + err := git.InitBare(repoPath, defaultBranch) 519 if err != nil { 520 l.Error("initializing bare repo", "error", err.Error()) 521 if errors.Is(err, gogit.ErrRepositoryAlreadyExists) {