Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/setting/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func DefaultOpenWithEditorApps() OpenWithEditorAppsType {

type RepositoryStruct struct {
OpenWithEditorApps *config.Value[OpenWithEditorAppsType]
GitGuideRemoteName *config.Value[string]
}

type ConfigStruct struct {
Expand All @@ -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"),
},
}
}
Expand Down
6 changes: 5 additions & 1 deletion modules/setting/config/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3426,6 +3426,8 @@ 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
config.git_max_diff_lines = Max Diff Lines (for a single file)
Expand Down
62 changes: 46 additions & 16 deletions routers/web/admin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,6 @@ 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
Expand All @@ -206,6 +204,20 @@ func ChangeConfig(ctx *context.Context) {
}
return "false", nil
}

marshalString := func(emptyDefault string) func(v string) (string, error) {
return func(v string) (string, error) {
if v == "" {
v = emptyDefault
}
b, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(b), nil
}
}

marshalOpenWithApps := func(value string) (string, error) {
lines := strings.Split(value, "\n")
var openWithEditorApps setting.OpenWithEditorAppsType
Expand Down Expand Up @@ -234,22 +246,40 @@ func ChangeConfig(ctx *context.Context) {
cfg.Picture.DisableGravatar.DynKey(): marshalBool,
cfg.Picture.EnableFederatedAvatar.DynKey(): marshalBool,
cfg.Repository.OpenWithEditorApps.DynKey(): marshalOpenWithApps,
cfg.Repository.GitGuideRemoteName.DynKey(): marshalString(cfg.Repository.GitGuideRemoteName.DefaultValue()),
}
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

_ = ctx.Req.ParseForm()
queryKeys := ctx.Req.Form["key"]
queryValues := ctx.Req.Form["value"]
loop:
for i, key := range queryKeys {
if i >= len(queryValues) {
ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
break loop
}
value := queryValues[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
}

if err = system_model.SetSettings(ctx, map[string]string{key: marshaledValue}); err != nil {
ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
break loop
}
}

config.GetDynGetter().InvalidateCache()
ctx.JSONOK()
if !ctx.Written() {
ctx.JSONOK()
}
}
13 changes: 11 additions & 2 deletions templates/admin/config_settings.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,24 @@
{{ctx.Locale.Tr "repository"}}
</h4>
<div class="ui attached segment">
<form class="ui form form-fetch-action" method="post" action="{{AppSubUrl}}/-/admin/config?key={{.SystemConfig.Repository.OpenWithEditorApps.DynKey}}">
<form class="ui form form-fetch-action" method="post" action="{{AppSubUrl}}/-/admin/config">
<div class="field">
<details>
<summary>{{ctx.Locale.Tr "admin.config.open_with_editor_app_help"}}</summary>
<pre class="tw-px-4">{{.DefaultOpenWithEditorAppsString}}</pre>
</details>
</div>
<div class="field">
<textarea name="value">{{(.SystemConfig.Repository.OpenWithEditorApps.Value ctx).ToTextareaString}}</textarea>
{{$cfg := .SystemConfig.Repository.OpenWithEditorApps}}
<input type="hidden" name="key" value="{{$cfg.DynKey}}">
<textarea name="value">{{($cfg.Value ctx).ToTextareaString}}</textarea>
</div>

<div class="field">
<label>{{ctx.Locale.Tr "admin.config.git_guide_remote_name"}}</label>
{{$cfg = .SystemConfig.Repository.GitGuideRemoteName}}
<input type="hidden" name="key" value="{{$cfg.DynKey}}">
<input name="value" value="{{$cfg.Value ctx}}" placeholder="{{$cfg.DefaultValue}}" maxlength="100" dir="auto" required pattern="^[A-Za-z0-9][\-_A-Za-z0-9]*$">
</div>
<div class="field">
<button class="ui primary button">{{ctx.Locale.Tr "save"}}</button>
Expand Down
9 changes: 5 additions & 4 deletions templates/repo/empty.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,23 @@
<div class="item">
<h3>{{ctx.Locale.Tr "repo.create_new_repo_command"}}</h3>
<div class="markup">
{{$gitRemoteName := $.SystemConfig.Repository.GitGuideRemoteName.Value ctx}}
<pre><code>touch README.md
git init{{if ne .Repository.ObjectFormatName "sha1"}} --object-format={{.Repository.ObjectFormatName}}{{end}}{{/* for sha256 repo, it needs to set "object-format" explicitly*/}}
{{if ne .Repository.DefaultBranch "master"}}git checkout -b {{.Repository.DefaultBranch}}{{end}}
git add README.md
git commit -m "first commit"
git remote add origin <span class="js-clone-url">{{$.CloneButtonOriginLink.HTTPS}}</span>
git push -u origin {{.Repository.DefaultBranch}}</code></pre>
git remote add {{$gitRemoteName}} <span class="js-clone-url">{{$.CloneButtonOriginLink.HTTPS}}</span>
git push -u {{$gitRemoteName}} {{.Repository.DefaultBranch}}</code></pre>
</div>
</div>
<div class="divider"></div>

<div class="item">
<h3>{{ctx.Locale.Tr "repo.push_exist_repo"}}</h3>
<div class="markup">
<pre><code>git remote add origin <span class="js-clone-url">{{$.CloneButtonOriginLink.HTTPS}}</span>
git push -u origin {{.Repository.DefaultBranch}}</code></pre>
<pre><code>git remote add {{$gitRemoteName}} <span class="js-clone-url">{{$.CloneButtonOriginLink.HTTPS}}</span>
git push -u {{$gitRemoteName}} {{.Repository.DefaultBranch}}</code></pre>
</div>
</div>
{{end}}
Expand Down
7 changes: 4 additions & 3 deletions templates/repo/issue/view_content/pull_merge_instruction.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
{{$localBranch = print .PullRequest.HeadRepo.OwnerName "-" .PullRequest.HeadBranch}}
{{end}}
<div class="ui secondary segment tw-font-mono">
{{$gitRemoteName := ctx.RootData.SystemConfig.Repository.GitGuideRemoteName.Value ctx}}
{{if eq .PullRequest.Flow 0}}
<div>git fetch -u {{if ne .PullRequest.HeadRepo.ID .PullRequest.BaseRepo.ID}}<origin-url data-url="{{.PullRequest.HeadRepo.Link}}"></origin-url>{{else}}origin{{end}} {{.PullRequest.HeadBranch}}:{{$localBranch}}</div>
<div>git fetch -u {{if ne .PullRequest.HeadRepo.ID .PullRequest.BaseRepo.ID}}<origin-url data-url="{{.PullRequest.HeadRepo.Link}}"></origin-url>{{else}}{{$gitRemoteName}}{{end}} {{.PullRequest.HeadBranch}}:{{$localBranch}}</div>
{{else}}
<div>git fetch -u origin {{.PullRequest.GetGitHeadRefName}}:{{$localBranch}}</div>
<div>git fetch -u {{$gitRemoteName}} {{.PullRequest.GetGitHeadRefName}}:{{$localBranch}}</div>
{{end}}
<div>git checkout {{$localBranch}}</div>
</div>
Expand Down Expand Up @@ -50,7 +51,7 @@
<div>git checkout {{.PullRequest.BaseBranch}}</div>
<div>git merge {{$localBranch}}</div>
</div>
<div>git push origin {{.PullRequest.BaseBranch}}</div>
<div>git push {{$gitRemoteName}} {{.PullRequest.BaseBranch}}</div>
</div>
{{end}}
</div>
Expand Down