Skip to content

Commit e844a41

Browse files
Use configurable remote name for git commands (#35172)
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>
1 parent c0f24bd commit e844a41

File tree

7 files changed

+70
-33
lines changed

7 files changed

+70
-33
lines changed

modules/setting/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func DefaultOpenWithEditorApps() OpenWithEditorAppsType {
4949

5050
type RepositoryStruct struct {
5151
OpenWithEditorApps *config.Value[OpenWithEditorAppsType]
52+
GitGuideRemoteName *config.Value[string]
5253
}
5354

5455
type ConfigStruct struct {
@@ -70,6 +71,7 @@ func initDefaultConfig() {
7071
},
7172
Repository: &RepositoryStruct{
7273
OpenWithEditorApps: config.ValueJSON[OpenWithEditorAppsType]("repository.open-with.editor-apps"),
74+
GitGuideRemoteName: config.ValueJSON[string]("repository.git-guide-remote-name").WithDefault("origin"),
7375
},
7476
}
7577
}

modules/setting/config/value.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (value *Value[T]) Value(ctx context.Context) (v T) {
4646

4747
rev := dg.GetRevision(ctx)
4848

49-
// if the revision in database doesn't change, use the last value
49+
// if the revision in the database doesn't change, use the last value
5050
value.mu.RLock()
5151
if rev == value.revision {
5252
v = value.value
@@ -84,6 +84,10 @@ func (value *Value[T]) WithDefault(def T) *Value[T] {
8484
return value
8585
}
8686

87+
func (value *Value[T]) DefaultValue() T {
88+
return value.def
89+
}
90+
8791
func (value *Value[T]) WithFileConfig(cfgSecKey CfgSecKey) *Value[T] {
8892
value.cfgSecKey = cfgSecKey
8993
return value

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,6 +3425,7 @@ config.picture_service = Picture Service
34253425
config.disable_gravatar = Disable Gravatar
34263426
config.enable_federated_avatar = Enable Federated Avatars
34273427
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.
3428+
config.git_guide_remote_name = Repository remote name for git commands in the guide
34283429
34293430
config.git_config = Git Configuration
34303431
config.git_disable_diff_highlight = Disable Diff Syntax Highlight

routers/web/admin/config.go

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,21 @@ func ConfigSettings(ctx *context.Context) {
196196
}
197197

198198
func ChangeConfig(ctx *context.Context) {
199-
key := strings.TrimSpace(ctx.FormString("key"))
200-
value := ctx.FormString("value")
201199
cfg := setting.Config()
202200

203-
marshalBool := func(v string) (string, error) { //nolint:unparam // error is always nil
204-
if b, _ := strconv.ParseBool(v); b {
205-
return "true", nil
201+
marshalBool := func(v string) ([]byte, error) {
202+
b, _ := strconv.ParseBool(v)
203+
return json.Marshal(b)
204+
}
205+
206+
marshalString := func(emptyDefault string) func(v string) ([]byte, error) {
207+
return func(v string) ([]byte, error) {
208+
return json.Marshal(util.IfZero(v, emptyDefault))
206209
}
207-
return "false", nil
208210
}
209-
marshalOpenWithApps := func(value string) (string, error) {
211+
212+
marshalOpenWithApps := func(value string) ([]byte, error) {
213+
// TODO: move the block alongside OpenWithEditorAppsType.ToTextareaString
210214
lines := strings.Split(value, "\n")
211215
var openWithEditorApps setting.OpenWithEditorAppsType
212216
for _, line := range lines {
@@ -224,32 +228,47 @@ func ChangeConfig(ctx *context.Context) {
224228
OpenURL: strings.TrimSpace(openURL),
225229
})
226230
}
227-
b, err := json.Marshal(openWithEditorApps)
228-
if err != nil {
229-
return "", err
230-
}
231-
return string(b), nil
231+
return json.Marshal(openWithEditorApps)
232232
}
233-
marshallers := map[string]func(string) (string, error){
233+
marshallers := map[string]func(string) ([]byte, error){
234234
cfg.Picture.DisableGravatar.DynKey(): marshalBool,
235235
cfg.Picture.EnableFederatedAvatar.DynKey(): marshalBool,
236236
cfg.Repository.OpenWithEditorApps.DynKey(): marshalOpenWithApps,
237+
cfg.Repository.GitGuideRemoteName.DynKey(): marshalString(cfg.Repository.GitGuideRemoteName.DefaultValue()),
237238
}
238-
marshaller, hasMarshaller := marshallers[key]
239-
if !hasMarshaller {
240-
ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
241-
return
239+
240+
_ = ctx.Req.ParseForm()
241+
configKeys := ctx.Req.Form["key"]
242+
configValues := ctx.Req.Form["value"]
243+
configSettings := map[string]string{}
244+
loop:
245+
for i, key := range configKeys {
246+
if i >= len(configValues) {
247+
ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
248+
break loop
249+
}
250+
value := configValues[i]
251+
252+
marshaller, hasMarshaller := marshallers[key]
253+
if !hasMarshaller {
254+
ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
255+
break loop
256+
}
257+
258+
marshaledValue, err := marshaller(value)
259+
if err != nil {
260+
ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
261+
break loop
262+
}
263+
configSettings[key] = string(marshaledValue)
242264
}
243-
marshaledValue, err := marshaller(value)
244-
if err != nil {
245-
ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
265+
if ctx.Written() {
246266
return
247267
}
248-
if err = system_model.SetSettings(ctx, map[string]string{key: marshaledValue}); err != nil {
249-
ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
268+
if err := system_model.SetSettings(ctx, configSettings); err != nil {
269+
ctx.ServerError("SetSettings", err)
250270
return
251271
}
252-
253272
config.GetDynGetter().InvalidateCache()
254273
ctx.JSONOK()
255274
}

templates/admin/config_settings.tmpl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,24 @@
2424
{{ctx.Locale.Tr "repository"}}
2525
</h4>
2626
<div class="ui attached segment">
27-
<form class="ui form form-fetch-action" method="post" action="{{AppSubUrl}}/-/admin/config?key={{.SystemConfig.Repository.OpenWithEditorApps.DynKey}}">
27+
<form class="ui form form-fetch-action" method="post" action="{{AppSubUrl}}/-/admin/config">
2828
<div class="field">
2929
<details>
3030
<summary>{{ctx.Locale.Tr "admin.config.open_with_editor_app_help"}}</summary>
3131
<pre class="tw-px-4">{{.DefaultOpenWithEditorAppsString}}</pre>
3232
</details>
3333
</div>
3434
<div class="field">
35-
<textarea name="value">{{(.SystemConfig.Repository.OpenWithEditorApps.Value ctx).ToTextareaString}}</textarea>
35+
{{$cfg := .SystemConfig.Repository.OpenWithEditorApps}}
36+
<input type="hidden" name="key" value="{{$cfg.DynKey}}">
37+
<textarea name="value">{{($cfg.Value ctx).ToTextareaString}}</textarea>
38+
</div>
39+
40+
<div class="field">
41+
<label>{{ctx.Locale.Tr "admin.config.git_guide_remote_name"}}</label>
42+
{{$cfg = .SystemConfig.Repository.GitGuideRemoteName}}
43+
<input type="hidden" name="key" value="{{$cfg.DynKey}}">
44+
<input name="value" value="{{$cfg.Value ctx}}" placeholder="{{$cfg.DefaultValue}}" maxlength="100" dir="auto" required pattern="^[A-Za-z0-9][\-_A-Za-z0-9]*$">
3645
</div>
3746
<div class="field">
3847
<button class="ui primary button">{{ctx.Locale.Tr "save"}}</button>

templates/repo/empty.tmpl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,23 @@
4646
<div class="item">
4747
<h3>{{ctx.Locale.Tr "repo.create_new_repo_command"}}</h3>
4848
<div class="markup">
49+
{{$gitRemoteName := $.SystemConfig.Repository.GitGuideRemoteName.Value ctx}}
4950
<pre><code>touch README.md
5051
git init{{if ne .Repository.ObjectFormatName "sha1"}} --object-format={{.Repository.ObjectFormatName}}{{end}}{{/* for sha256 repo, it needs to set "object-format" explicitly*/}}
5152
{{if ne .Repository.DefaultBranch "master"}}git checkout -b {{.Repository.DefaultBranch}}{{end}}
5253
git add README.md
5354
git commit -m "first commit"
54-
git remote add origin <span class="js-clone-url">{{$.CloneButtonOriginLink.HTTPS}}</span>
55-
git push -u origin {{.Repository.DefaultBranch}}</code></pre>
55+
git remote add {{$gitRemoteName}} <span class="js-clone-url">{{$.CloneButtonOriginLink.HTTPS}}</span>
56+
git push -u {{$gitRemoteName}} {{.Repository.DefaultBranch}}</code></pre>
5657
</div>
5758
</div>
5859
<div class="divider"></div>
5960

6061
<div class="item">
6162
<h3>{{ctx.Locale.Tr "repo.push_exist_repo"}}</h3>
6263
<div class="markup">
63-
<pre><code>git remote add origin <span class="js-clone-url">{{$.CloneButtonOriginLink.HTTPS}}</span>
64-
git push -u origin {{.Repository.DefaultBranch}}</code></pre>
64+
<pre><code>git remote add {{$gitRemoteName}} <span class="js-clone-url">{{$.CloneButtonOriginLink.HTTPS}}</span>
65+
git push -u {{$gitRemoteName}} {{.Repository.DefaultBranch}}</code></pre>
6566
</div>
6667
</div>
6768
{{end}}

templates/repo/issue/view_content/pull_merge_instruction.tmpl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
{{$localBranch = print .PullRequest.HeadRepo.OwnerName "-" .PullRequest.HeadBranch}}
99
{{end}}
1010
<div class="ui secondary segment tw-font-mono">
11+
{{$gitRemoteName := ctx.RootData.SystemConfig.Repository.GitGuideRemoteName.Value ctx}}
1112
{{if eq .PullRequest.Flow 0}}
12-
<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>
13+
<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>
1314
{{else}}
14-
<div>git fetch -u origin {{.PullRequest.GetGitHeadRefName}}:{{$localBranch}}</div>
15+
<div>git fetch -u {{$gitRemoteName}} {{.PullRequest.GetGitHeadRefName}}:{{$localBranch}}</div>
1516
{{end}}
1617
<div>git checkout {{$localBranch}}</div>
1718
</div>
@@ -50,7 +51,7 @@
5051
<div>git checkout {{.PullRequest.BaseBranch}}</div>
5152
<div>git merge {{$localBranch}}</div>
5253
</div>
53-
<div>git push origin {{.PullRequest.BaseBranch}}</div>
54+
<div>git push {{$gitRemoteName}} {{.PullRequest.BaseBranch}}</div>
5455
</div>
5556
{{end}}
5657
</div>

0 commit comments

Comments
 (0)