diff --git a/knotserver/internal.go b/knotserver/internal.go index 625a72e8..bb08faf9 100644 --- a/knotserver/internal.go +++ b/knotserver/internal.go @@ -300,6 +300,11 @@ func (h *InternalHandle) insertRefUpdate(line git.PostReceiveLine, gitUserDid, o return fmt.Errorf("failed to open git repo at ref %s: %w", line.Ref, err) } + err = h.ensureDefaultBranchIsSet(gr, line) + if err != nil { + h.l.Error("attempted to ensure default branch is set on repo", "error", err) + } + meta, err := gr.RefUpdateMeta(line) if err != nil { return fmt.Errorf("failed to get ref update metadata: %w", err) @@ -323,6 +328,28 @@ func (h *InternalHandle) insertRefUpdate(line git.PostReceiveLine, gitUserDid, o return h.db.InsertEvent(event, h.n) } +func (h *InternalHandle) ensureDefaultBranchIsSet(gr *git.GitRepo, line git.PostReceiveLine) error { + mainBranch, err := gr.FindMainBranch() + if err != nil { + return err + } + + // if main branch is HEAD it means that this is the first push to the repo and the configured default + // branch for the knot doesn't match the branch that was just pushed + if mainBranch != "HEAD" { + return nil + } + + defaultBranch := strings.ReplaceAll(line.Ref, "refs/heads/", "") + err = gr.SetDefaultBranch(defaultBranch) + if err != nil { + return fmt.Errorf("setting detault branch: %w", err) + } + h.l.Info("set default branch to " + defaultBranch) + + return nil +} + func (h *InternalHandle) triggerPipeline( clientMsgs *[]string, line git.PostReceiveLine,