diff --git a/modules/setting/config.go b/modules/setting/config.go index 03558574c2..4c5d2df7d8 100644 --- a/modules/setting/config.go +++ b/modules/setting/config.go @@ -49,6 +49,7 @@ func DefaultOpenWithEditorApps() OpenWithEditorAppsType { type RepositoryStruct struct { OpenWithEditorApps *config.Value[OpenWithEditorAppsType] + GitGuideRemoteName *config.Value[string] } type ConfigStruct struct { @@ -70,6 +71,7 @@ func initDefaultConfig() { }, Repository: &RepositoryStruct{ OpenWithEditorApps: config.ValueJSON[OpenWithEditorAppsType]("repository.open-with.editor-apps"), + GitGuideRemoteName: config.ValueJSON[string]("repository.git-guide-remote-name").WithDefault("origin"), }, } } diff --git a/modules/setting/config/value.go b/modules/setting/config/value.go index f0ec120544..301c60f5e8 100644 --- a/modules/setting/config/value.go +++ b/modules/setting/config/value.go @@ -46,7 +46,7 @@ func (value *Value[T]) Value(ctx context.Context) (v T) { rev := dg.GetRevision(ctx) - // if the revision in database doesn't change, use the last value + // if the revision in the database doesn't change, use the last value value.mu.RLock() if rev == value.revision { v = value.value @@ -84,6 +84,10 @@ func (value *Value[T]) WithDefault(def T) *Value[T] { 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 diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index ba4664964e..315241a417 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3425,6 +3425,7 @@ config.picture_service = Picture Service config.disable_gravatar = Disable Gravatar config.enable_federated_avatar = Enable Federated Avatars config.open_with_editor_app_help = The "Open with" editors for the clone menu. If left empty, the default will be used. Expand to see the default. +config.git_guide_remote_name = Repository remote name for git commands in the guide config.git_config = Git Configuration config.git_disable_diff_highlight = Disable Diff Syntax Highlight diff --git a/routers/web/admin/config.go b/routers/web/admin/config.go index 0e5b23db6d..310ebd3f6d 100644 --- a/routers/web/admin/config.go +++ b/routers/web/admin/config.go @@ -196,17 +196,21 @@ func ConfigSettings(ctx *context.Context) { } func ChangeConfig(ctx *context.Context) { - key := strings.TrimSpace(ctx.FormString("key")) - value := ctx.FormString("value") cfg := setting.Config() - marshalBool := func(v string) (string, error) { //nolint:unparam // error is always nil - if b, _ := strconv.ParseBool(v); b { - return "true", nil - } - return "false", nil + marshalBool := func(v string) ([]byte, error) { + b, _ := strconv.ParseBool(v) + return json.Marshal(b) } - marshalOpenWithApps := func(value string) (string, error) { + + marshalString := func(emptyDefault string) func(v string) ([]byte, error) { + return func(v string) ([]byte, error) { + return json.Marshal(util.IfZero(v, emptyDefault)) + } + } + + marshalOpenWithApps := func(value string) ([]byte, error) { + // TODO: move the block alongside OpenWithEditorAppsType.ToTextareaString lines := strings.Split(value, "\n") var openWithEditorApps setting.OpenWithEditorAppsType for _, line := range lines { @@ -224,32 +228,47 @@ func ChangeConfig(ctx *context.Context) { OpenURL: strings.TrimSpace(openURL), }) } - b, err := json.Marshal(openWithEditorApps) - if err != nil { - return "", err - } - return string(b), nil + return json.Marshal(openWithEditorApps) } - marshallers := map[string]func(string) (string, error){ + marshallers := map[string]func(string) ([]byte, error){ cfg.Picture.DisableGravatar.DynKey(): marshalBool, cfg.Picture.EnableFederatedAvatar.DynKey(): marshalBool, cfg.Repository.OpenWithEditorApps.DynKey(): marshalOpenWithApps, - } - marshaller, hasMarshaller := marshallers[key] - if !hasMarshaller { - ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key)) - return - } - marshaledValue, err := marshaller(value) - if err != nil { - ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key)) - return - } - if err = system_model.SetSettings(ctx, map[string]string{key: marshaledValue}); err != nil { - ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key)) - return + cfg.Repository.GitGuideRemoteName.DynKey(): marshalString(cfg.Repository.GitGuideRemoteName.DefaultValue()), } + _ = ctx.Req.ParseForm() + configKeys := ctx.Req.Form["key"] + configValues := ctx.Req.Form["value"] + configSettings := map[string]string{} +loop: + for i, key := range configKeys { + if i >= len(configValues) { + ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key)) + break loop + } + value := configValues[i] + + marshaller, hasMarshaller := marshallers[key] + if !hasMarshaller { + ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key)) + break loop + } + + marshaledValue, err := marshaller(value) + if err != nil { + ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key)) + break loop + } + configSettings[key] = string(marshaledValue) + } + if ctx.Written() { + return + } + if err := system_model.SetSettings(ctx, configSettings); err != nil { + ctx.ServerError("SetSettings", err) + return + } config.GetDynGetter().InvalidateCache() ctx.JSONOK() } diff --git a/templates/admin/config_settings.tmpl b/templates/admin/config_settings.tmpl index 6b9bb8275c..9bd37afa7d 100644 --- a/templates/admin/config_settings.tmpl +++ b/templates/admin/config_settings.tmpl @@ -24,7 +24,7 @@ {{ctx.Locale.Tr "repository"}}