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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions modules/setting/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ type RepositoryStruct struct {
OpenWithEditorApps *config.Value[OpenWithEditorAppsType]
}

type TemplateStruct struct {
GitRemoteName *config.Value[string]
}

type ConfigStruct struct {
Picture *PictureStruct
Repository *RepositoryStruct
Template *TemplateStruct
}

var (
Expand All @@ -71,6 +76,9 @@ func initDefaultConfig() {
Repository: &RepositoryStruct{
OpenWithEditorApps: config.ValueJSON[OpenWithEditorAppsType]("repository.open-with.editor-apps"),
},
Template: &TemplateStruct{
GitRemoteName: config.ValueJSON[string]("template.git-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.empty_repo_page = Empty Repo Page
config.empty_repo_page.git_remote_name = Git 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.Template.GitRemoteName.DynKey(): marshalStringWithDefault(cfg.Template.GitRemoteName.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["GitRemoteName"] = setting.Config().Template.GitRemoteName.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["GitRemoteName"] = setting.Config().Template.GitRemoteName.Value(ctx)

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.empty_repo_page"}}
</h4>
<div class="ui attached segment">
<form class="ui form form-fetch-action" method="post" action="{{AppSubUrl}}/-/admin/config?key={{.SystemConfig.Template.GitRemoteName.DynKey}}">
<div class="field" data-field-patched="true">
<label for="remote_name">{{ctx.Locale.Tr "admin.config.empty_repo_page.git_remote_name"}}</label>
<input id="remote_name" name="value" value="{{.SystemConfig.Template.GitRemoteName.Value ctx}}" maxlength="100" dir="auto" placeholder="{{.SystemConfig.Template.GitRemoteName.Def}}">
</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 {{.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
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 "GitRemoteName" .GitRemoteName}}
{{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}}{{.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 +50,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