Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).

set default branch from KNOT_REPO_MAIN_BRANCH

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