99 "strings"
1010
1111 "code.gitea.io/gitea/modules/log"
12+ "code.gitea.io/gitea/modules/setting"
1213)
1314
1415// UnitType is Unit's Type
@@ -78,13 +79,89 @@ var (
7879 UnitTypeWiki ,
7980 }
8081
82+ // NotAllowedDefaultRepoUnits contains units that can't be default
83+ NotAllowedDefaultRepoUnits = []UnitType {
84+ UnitTypeExternalWiki ,
85+ UnitTypeExternalTracker ,
86+ }
87+
8188 // MustRepoUnits contains the units could not be disabled currently
8289 MustRepoUnits = []UnitType {
8390 UnitTypeCode ,
8491 UnitTypeReleases ,
8592 }
93+
94+ // DisabledRepoUnits contains the units that have been globally disabled
95+ DisabledRepoUnits = []UnitType {}
8696)
8797
98+ func loadUnitConfig () {
99+ setDefaultRepoUnits := FindUnitTypes (setting .Repository .DefaultRepoUnits ... )
100+ // Default repo units set if setting is not empty
101+ if len (setDefaultRepoUnits ) > 0 {
102+ // MustRepoUnits required as default
103+ DefaultRepoUnits = make ([]UnitType , len (MustRepoUnits ))
104+ copy (DefaultRepoUnits , MustRepoUnits )
105+ for _ , defaultU := range setDefaultRepoUnits {
106+ if ! defaultU .CanBeDefault () {
107+ log .Warn ("Not allowed as default unit: %s" , defaultU .String ())
108+ continue
109+ }
110+ // MustRepoUnits already added
111+ if defaultU .CanDisable () {
112+ DefaultRepoUnits = append (DefaultRepoUnits , defaultU )
113+ }
114+ }
115+ }
116+
117+ DisabledRepoUnits = FindUnitTypes (setting .Repository .DisabledRepoUnits ... )
118+ // Check that must units are not disabled
119+ for i , disabledU := range DisabledRepoUnits {
120+ if ! disabledU .CanDisable () {
121+ log .Warn ("Not allowed to global disable unit %s" , disabledU .String ())
122+ DisabledRepoUnits = append (DisabledRepoUnits [:i ], DisabledRepoUnits [i + 1 :]... )
123+ }
124+ }
125+ // Remove disabled units from default units
126+ for _ , disabledU := range DisabledRepoUnits {
127+ for i , defaultU := range DefaultRepoUnits {
128+ if defaultU == disabledU {
129+ DefaultRepoUnits = append (DefaultRepoUnits [:i ], DefaultRepoUnits [i + 1 :]... )
130+ }
131+ }
132+ }
133+ }
134+
135+ // UnitGlobalDisabled checks if unit type is global disabled
136+ func (u UnitType ) UnitGlobalDisabled () bool {
137+ for _ , ud := range DisabledRepoUnits {
138+ if u == ud {
139+ return true
140+ }
141+ }
142+ return false
143+ }
144+
145+ // CanDisable checks if this unit type can be disabled.
146+ func (u * UnitType ) CanDisable () bool {
147+ for _ , mu := range MustRepoUnits {
148+ if * u == mu {
149+ return false
150+ }
151+ }
152+ return true
153+ }
154+
155+ // CanBeDefault checks if the unit type can be a default repo unit
156+ func (u * UnitType ) CanBeDefault () bool {
157+ for _ , nadU := range NotAllowedDefaultRepoUnits {
158+ if * u == nadU {
159+ return false
160+ }
161+ }
162+ return true
163+ }
164+
88165// Unit is a section of one repository
89166type Unit struct {
90167 Type UnitType
@@ -96,7 +173,7 @@ type Unit struct {
96173
97174// CanDisable returns if this unit could be disabled.
98175func (u * Unit ) CanDisable () bool {
99- return true
176+ return u . Type . CanDisable ()
100177}
101178
102179// IsLessThan compares order of two units
0 commit comments