GitRoot

craft your forge, build your project, grow your community freely
 1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
 2//
 3// SPDX-License-Identifier: EUPL-1.2
 4
 5package repository
 6
 7import (
 8	"github.com/go-git/go-git/v5/plumbing"
 9	"github.com/goccy/go-yaml"
10)
11
12type repoConfiguration struct {
13	DefaultBranch string
14	NoForcePush   []string
15}
16
17type RepoConfiguration struct {
18	DefaultBranch plumbing.ReferenceName
19	NoForcePush   []plumbing.ReferenceName
20}
21
22func newRepoConfiguration(defaultBranch string) repoConfiguration {
23	return repoConfiguration{
24		DefaultBranch: defaultBranch,
25		NoForcePush:   []string{defaultBranch},
26	}
27}
28
29func (r repoConfiguration) toFileContent() []byte {
30	content, _ := yaml.Marshal(r)
31	return content
32}
33
34func readRepoConfiguration(fileContent []byte) (RepoConfiguration, error) {
35	repoConf := repoConfiguration{}
36	err := yaml.Unmarshal(fileContent, &repoConf)
37	if err != nil {
38		return RepoConfiguration{}, err
39	}
40	noForcePush := make([]plumbing.ReferenceName, len(repoConf.NoForcePush))
41	for i, rc := range repoConf.NoForcePush {
42		noForcePush[i] = plumbing.NewBranchReferenceName(rc)
43	}
44	return RepoConfiguration{
45		DefaultBranch: plumbing.NewBranchReferenceName(repoConf.DefaultBranch),
46		NoForcePush:   noForcePush,
47	}, nil
48}
49
50func (r RepoConfiguration) IsNoPushBranch(branch plumbing.ReferenceName) bool {
51	for _, b := range r.NoForcePush {
52		if b == branch {
53			return true
54		}
55	}
56	return false
57}