mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-25 01:24:13 +02:00 
			
		
		
		
	Closes #19403, and makes it possible to use any remote name in code snippets for an empty repository and pull request. This change is very helpful to me, because I always use different name for my gitea remote. Uses setting config module to store the value. Default is `origin` for backward compatibility. ### Screenshots <details> <summary>Empty repo</summary> <img width="791" height="398" alt="image" src="https://github.com/user-attachments/assets/7214053d-a8dd-4e77-8c9d-78936d9859e0" /> </details> <details> <summary>Pull Request</summary> <img width="591" height="452" alt="image" src="https://github.com/user-attachments/assets/ebc3d25c-5d6d-481d-819d-9706af3c5594" /> </details> <details> <summary>Settings page</summary> <img width="1438" height="839" alt="image" src="https://github.com/user-attachments/assets/d92bfa2c-7adc-4efe-95fa-0c55ad13b3f5" /> </details> --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
		
			
				
	
	
		
			99 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			99 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2023 The Gitea Authors. All rights reserved.
 | |
| // SPDX-License-Identifier: MIT
 | |
| 
 | |
| package config
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"sync"
 | |
| 
 | |
| 	"code.gitea.io/gitea/modules/json"
 | |
| 	"code.gitea.io/gitea/modules/log"
 | |
| 	"code.gitea.io/gitea/modules/util"
 | |
| )
 | |
| 
 | |
| type CfgSecKey struct {
 | |
| 	Sec, Key string
 | |
| }
 | |
| 
 | |
| type Value[T any] struct {
 | |
| 	mu sync.RWMutex
 | |
| 
 | |
| 	cfgSecKey CfgSecKey
 | |
| 	dynKey    string
 | |
| 
 | |
| 	def, value T
 | |
| 	revision   int
 | |
| }
 | |
| 
 | |
| func (value *Value[T]) parse(key, valStr string) (v T) {
 | |
| 	v = value.def
 | |
| 	if valStr != "" {
 | |
| 		if err := json.Unmarshal(util.UnsafeStringToBytes(valStr), &v); err != nil {
 | |
| 			log.Error("Unable to unmarshal json config for key %q, err: %v", key, err)
 | |
| 		}
 | |
| 	}
 | |
| 	return v
 | |
| }
 | |
| 
 | |
| func (value *Value[T]) Value(ctx context.Context) (v T) {
 | |
| 	dg := GetDynGetter()
 | |
| 	if dg == nil {
 | |
| 		// this is an edge case: the database is not initialized but the system setting is going to be used
 | |
| 		// it should panic to avoid inconsistent config values (from config / system setting) and fix the code
 | |
| 		panic("no config dyn value getter")
 | |
| 	}
 | |
| 
 | |
| 	rev := dg.GetRevision(ctx)
 | |
| 
 | |
| 	// if the revision in the database doesn't change, use the last value
 | |
| 	value.mu.RLock()
 | |
| 	if rev == value.revision {
 | |
| 		v = value.value
 | |
| 		value.mu.RUnlock()
 | |
| 		return v
 | |
| 	}
 | |
| 	value.mu.RUnlock()
 | |
| 
 | |
| 	// try to parse the config and cache it
 | |
| 	var valStr *string
 | |
| 	if dynVal, has := dg.GetValue(ctx, value.dynKey); has {
 | |
| 		valStr = &dynVal
 | |
| 	} else if cfgVal, has := GetCfgSecKeyGetter().GetValue(value.cfgSecKey.Sec, value.cfgSecKey.Key); has {
 | |
| 		valStr = &cfgVal
 | |
| 	}
 | |
| 	if valStr == nil {
 | |
| 		v = value.def
 | |
| 	} else {
 | |
| 		v = value.parse(value.dynKey, *valStr)
 | |
| 	}
 | |
| 
 | |
| 	value.mu.Lock()
 | |
| 	value.value = v
 | |
| 	value.revision = rev
 | |
| 	value.mu.Unlock()
 | |
| 	return v
 | |
| }
 | |
| 
 | |
| func (value *Value[T]) DynKey() string {
 | |
| 	return value.dynKey
 | |
| }
 | |
| 
 | |
| func (value *Value[T]) WithDefault(def T) *Value[T] {
 | |
| 	value.def = def
 | |
| 	return value
 | |
| }
 | |
| 
 | |
| func (value *Value[T]) DefaultValue() T {
 | |
| 	return value.def
 | |
| }
 | |
| 
 | |
| func (value *Value[T]) WithFileConfig(cfgSecKey CfgSecKey) *Value[T] {
 | |
| 	value.cfgSecKey = cfgSecKey
 | |
| 	return value
 | |
| }
 | |
| 
 | |
| func ValueJSON[T any](dynKey string) *Value[T] {
 | |
| 	return &Value[T]{dynKey: dynKey}
 | |
| }
 |