Skip to content

Use configurable remote name in snippets #35172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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]
SnippetRemoteName *config.Value[string]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Snippet a proper name?

IMO it is not a "snippet", it is for "git guide for users"

Copy link
Author

@ilya-nurullin ilya-nurullin Aug 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What name would you use here? Can it be just RemoteName?

}

type ConfigStruct struct {
Expand All @@ -70,6 +71,7 @@ func initDefaultConfig() {
},
Repository: &RepositoryStruct{
OpenWithEditorApps: config.ValueJSON[OpenWithEditorAppsType]("repository.open-with.editor-apps"),
SnippetRemoteName: config.ValueJSON[string]("repository.snippet-remote-name").WithDefault("origin"),
},
}
}
Expand Down
4 changes: 4 additions & 0 deletions modules/setting/config/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (value *Value[T]) DynKey() string {
return value.dynKey
}

func (value *Value[T]) Def() T {
return value.def
}

func (value *Value[T]) WithDefault(def T) *Value[T] {
value.def = def
return value
Expand Down
3 changes: 3 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3419,6 +3419,9 @@ 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.repository_snippets = Repository Snippets
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also an unclear name ....

Repository Snippets reads like "some parts of the repository", and GitLab users have been familiar with its Code Snippets https://docs.gitlab.com/user/snippets/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you suggest a better name?

config.repository_snippets.remote_name = Remote name (default: origin)

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
15 changes: 15 additions & 0 deletions routers/web/admin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,20 @@ func ChangeConfig(ctx *context.Context) {
}
return "false", nil
}

marshalStringWithDefault := func(def string) func(v string) (string, error) {
return func(v string) (string, error) {
if strings.TrimSpace(v) == "" {
v = def
}
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,6 +248,7 @@ func ChangeConfig(ctx *context.Context) {
cfg.Picture.DisableGravatar.DynKey(): marshalBool,
cfg.Picture.EnableFederatedAvatar.DynKey(): marshalBool,
cfg.Repository.OpenWithEditorApps.DynKey(): marshalOpenWithApps,
cfg.Repository.SnippetRemoteName.DynKey(): marshalStringWithDefault(cfg.Repository.SnippetRemoteName.Def()),
}
marshaller, hasMarshaller := marshallers[key]
if !hasMarshaller {
Expand Down
1 change: 1 addition & 0 deletions routers/web/repo/issue_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ func ViewPullMergeBox(ctx *context.Context) {

// TODO: it should use a dedicated struct to render the pull merge box, to make sure all data is prepared correctly
ctx.Data["IsIssuePoster"] = ctx.IsSigned && issue.IsPoster(ctx.Doer.ID)
ctx.Data["SnippetRemoteName"] = setting.Config().Repository.SnippetRemoteName.Value(ctx)
ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)
ctx.HTML(http.StatusOK, tplPullMergeBox)
}
Expand Down
1 change: 1 addition & 0 deletions services/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ func RepoAssignment(ctx *Context) {
ctx.Data["CanWriteIssues"] = ctx.Repo.CanWrite(unit_model.TypeIssues)
ctx.Data["CanWritePulls"] = ctx.Repo.CanWrite(unit_model.TypePullRequests)
ctx.Data["CanWriteActions"] = ctx.Repo.CanWrite(unit_model.TypeActions)
ctx.Data["SnippetRemoteName"] = setting.Config().Repository.SnippetRemoteName.Value(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and above: we can remove this SnippetRemoteName from template data, and get its value directly from the templates that need it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is used in several places in the template, I think it makes sense to extract it into a variable, which is what I did.


canSignedUserFork, err := repo_module.CanUserForkRepo(ctx, ctx.Doer, ctx.Repo.Repository)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions templates/admin/config_settings.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,19 @@
</div>
</form>
</div>

<h4 class="ui top attached header">
{{ctx.Locale.Tr "admin.config.repository_snippets"}}
</h4>
<div class="ui attached segment">
<form class="ui form form-fetch-action" method="post" action="{{AppSubUrl}}/-/admin/config?key={{.SystemConfig.Repository.SnippetRemoteName.DynKey}}">
<div class="field" data-field-patched="true">
<label for="remote_name">{{ctx.Locale.Tr "admin.config.repository_snippets.remote_name"}}</label>
<input id="remote_name" name="value" value="{{.SystemConfig.Repository.SnippetRemoteName.Value ctx}}" maxlength="100" dir="auto" placeholder="{{.SystemConfig.Repository.SnippetRemoteName.Def}}">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the Def here is useful.

The value is always filled by Value when a user opens this page.

So I think we should remove the Def function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main reason for Def is being able to get the default value when user removes value on settings page.
It is used here: https://github.com/go-gitea/gitea/pull/35172/files#diff-b3b66aef8cc83304cd5cfc661233e6c1c1be656fbd6cd1e09b114348f224ff32R251

In current architecture func (value *Value[T]) Value(ctx context.Context) (v T) function uses a default value only if it is not present in the database. So null in db, nil in go values are valid ones, and therefore the default value won't be used, and as a result there won't be any remote name on the page in snippets.

</div>
<div class="field">
<button class="ui primary button">{{ctx.Locale.Tr "save"}}</button>
</div>
</form>
</div>
{{template "admin/layout_footer" .}}
8 changes: 4 additions & 4 deletions templates/repo/empty.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ git init{{if ne .Repository.ObjectFormatName "sha1"}} --object-format={{.Reposit
{{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 {{.SnippetRemoteName}} <span class="js-clone-url">{{$.CloneButtonOriginLink.HTTPS}}</span>
git push -u {{.SnippetRemoteName}} {{.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 {{.SnippetRemoteName}} <span class="js-clone-url">{{$.CloneButtonOriginLink.HTTPS}}</span>
git push -u {{.SnippetRemoteName}} {{.Repository.DefaultBranch}}</code></pre>
</div>
</div>
{{end}}
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/issue/view_content/pull_merge_box.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@
{{end}}

{{if and .Issue.PullRequest.HeadRepo (not .Issue.PullRequest.HasMerged) (not .Issue.IsClosed)}}
{{template "repo/issue/view_content/pull_merge_instruction" dict "PullRequest" .Issue.PullRequest "ShowMergeInstructions" .ShowMergeInstructions}}
{{template "repo/issue/view_content/pull_merge_instruction" dict "PullRequest" .Issue.PullRequest "ShowMergeInstructions" .ShowMergeInstructions "SnippetRemoteName" .SnippetRemoteName}}
{{end}}
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions templates/repo/issue/view_content/pull_merge_instruction.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
{{end}}
<div class="ui secondary segment tw-font-mono">
{{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}}{{.SnippetRemoteName}}{{end}} {{.PullRequest.HeadBranch}}:{{$localBranch}}</div>
{{else}}
<div>git fetch -u origin {{.PullRequest.GetGitHeadRefName}}:{{$localBranch}}</div>
<div>git fetch -u {{.SnippetRemoteName}} {{.PullRequest.GetGitHeadRefName}}:{{$localBranch}}</div>
{{end}}
<div>git checkout {{$localBranch}}</div>
</div>
Expand Down Expand Up @@ -50,7 +50,7 @@
<div>git checkout {{.PullRequest.BaseBranch}}</div>
<div>git merge {{$localBranch}}</div>
</div>
<div>git push origin {{.PullRequest.BaseBranch}}</div>
<div>git push {{.SnippetRemoteName}} {{.PullRequest.BaseBranch}}</div>
</div>
{{end}}
</div>
Expand Down