Skip to content

Commit dad9e4a

Browse files
committed
fix parsed branch and ui
1 parent fc915fe commit dad9e4a

File tree

6 files changed

+32
-35
lines changed

6 files changed

+32
-35
lines changed

routers/web/repo/editor.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ type preparedEditorCommitForm[T any] struct {
9595
form T
9696
commonForm *forms.CommitCommonForm
9797
CommitFormOptions *context.CommitFormOptions
98-
TargetBranchName string
98+
OldBranchName string
99+
NewBranchName string
99100
GitCommitter *files_service.IdentityOptions
100101
}
101102

@@ -142,6 +143,7 @@ func prepareEditorCommitSubmittedForm[T forms.CommitCommonFormInterface](ctx *co
142143
return nil
143144
}
144145

146+
oldBranchName := ctx.Repo.BranchName
145147
fromBaseBranch := ctx.FormString("from_base_branch")
146148
if fromBaseBranch != "" {
147149
err = editorPushBranchToForkedRepository(ctx, ctx.Doer, ctx.Repo.Repository.BaseRepo, fromBaseBranch, ctx.Repo.Repository, targetBranchName)
@@ -150,13 +152,16 @@ func prepareEditorCommitSubmittedForm[T forms.CommitCommonFormInterface](ctx *co
150152
ctx.JSONError(ctx.Tr("repo.editor.fork_failed_to_push_branch", targetBranchName))
151153
return nil
152154
}
155+
// since we have pushed the branch from base branch, so now we need to commit the changes directly
156+
oldBranchName = targetBranchName
153157
}
154158

155159
return &preparedEditorCommitForm[T]{
156160
form: form,
157161
commonForm: commonForm,
158162
CommitFormOptions: commitFormOptions,
159-
TargetBranchName: targetBranchName,
163+
OldBranchName: oldBranchName,
164+
NewBranchName: targetBranchName,
160165
GitCommitter: gitCommitter,
161166
}
162167
}
@@ -166,7 +171,7 @@ func redirectForCommitChoice[T any](ctx *context.Context, parsed *preparedEditor
166171
if parsed.commonForm.CommitChoice == editorCommitChoiceNewBranch {
167172
// Redirect to a pull request when possible
168173
redirectToPullRequest := false
169-
repo, baseBranch, headBranch := ctx.Repo.Repository, ctx.Repo.BranchName, parsed.TargetBranchName
174+
repo, baseBranch, headBranch := ctx.Repo.Repository, parsed.OldBranchName, parsed.NewBranchName
170175
if ctx.Repo.Repository.IsFork && parsed.CommitFormOptions.CanCreateBasePullRequest {
171176
redirectToPullRequest = true
172177
baseBranch = repo.BaseRepo.DefaultBranch
@@ -183,7 +188,7 @@ func redirectForCommitChoice[T any](ctx *context.Context, parsed *preparedEditor
183188

184189
returnURI := ctx.FormString("return_uri")
185190
if returnURI == "" || !httplib.IsCurrentGiteaSiteURL(ctx, returnURI) {
186-
returnURI = util.URLJoin(ctx.Repo.RepoLink, "src/branch", util.PathEscapeSegments(parsed.TargetBranchName), util.PathEscapeSegments(treePath))
191+
returnURI = util.URLJoin(ctx.Repo.RepoLink, "src/branch", util.PathEscapeSegments(parsed.NewBranchName), util.PathEscapeSegments(treePath))
187192
}
188193
ctx.JSONRedirect(returnURI)
189194
}
@@ -319,8 +324,8 @@ func EditFilePost(ctx *context.Context) {
319324

320325
_, err := files_service.ChangeRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, &files_service.ChangeRepoFilesOptions{
321326
LastCommitID: parsed.form.LastCommit,
322-
OldBranch: ctx.Repo.BranchName,
323-
NewBranch: parsed.TargetBranchName,
327+
OldBranch: parsed.OldBranchName,
328+
NewBranch: parsed.NewBranchName,
324329
Message: parsed.GetCommitMessage(defaultCommitMessage),
325330
Files: []*files_service.ChangeRepoFile{
326331
{
@@ -335,7 +340,7 @@ func EditFilePost(ctx *context.Context) {
335340
Committer: parsed.GitCommitter,
336341
})
337342
if err != nil {
338-
editorHandleFileOperationError(ctx, parsed.TargetBranchName, err)
343+
editorHandleFileOperationError(ctx, parsed.NewBranchName, err)
339344
return
340345
}
341346

@@ -362,8 +367,8 @@ func DeleteFilePost(ctx *context.Context) {
362367
treePath := ctx.Repo.TreePath
363368
_, err := files_service.ChangeRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, &files_service.ChangeRepoFilesOptions{
364369
LastCommitID: parsed.form.LastCommit,
365-
OldBranch: ctx.Repo.BranchName,
366-
NewBranch: parsed.TargetBranchName,
370+
OldBranch: parsed.OldBranchName,
371+
NewBranch: parsed.NewBranchName,
367372
Files: []*files_service.ChangeRepoFile{
368373
{
369374
Operation: "delete",
@@ -376,12 +381,12 @@ func DeleteFilePost(ctx *context.Context) {
376381
Committer: parsed.GitCommitter,
377382
})
378383
if err != nil {
379-
editorHandleFileOperationError(ctx, parsed.TargetBranchName, err)
384+
editorHandleFileOperationError(ctx, parsed.NewBranchName, err)
380385
return
381386
}
382387

383388
ctx.Flash.Success(ctx.Tr("repo.editor.file_delete_success", treePath))
384-
redirectTreePath := getClosestParentWithFiles(ctx.Repo.GitRepo, parsed.TargetBranchName, treePath)
389+
redirectTreePath := getClosestParentWithFiles(ctx.Repo.GitRepo, parsed.NewBranchName, treePath)
385390
redirectForCommitChoice(ctx, parsed, redirectTreePath)
386391
}
387392

@@ -406,8 +411,8 @@ func UploadFilePost(ctx *context.Context) {
406411
defaultCommitMessage := ctx.Locale.TrString("repo.editor.upload_files_to_dir", util.IfZero(parsed.form.TreePath, "/"))
407412
err := files_service.UploadRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, &files_service.UploadRepoFileOptions{
408413
LastCommitID: parsed.form.LastCommit,
409-
OldBranch: ctx.Repo.BranchName,
410-
NewBranch: parsed.TargetBranchName,
414+
OldBranch: parsed.OldBranchName,
415+
NewBranch: parsed.NewBranchName,
411416
TreePath: parsed.form.TreePath,
412417
Message: parsed.GetCommitMessage(defaultCommitMessage),
413418
Files: parsed.form.Files,
@@ -416,7 +421,7 @@ func UploadFilePost(ctx *context.Context) {
416421
Committer: parsed.GitCommitter,
417422
})
418423
if err != nil {
419-
editorHandleFileOperationError(ctx, parsed.TargetBranchName, err)
424+
editorHandleFileOperationError(ctx, parsed.NewBranchName, err)
420425
return
421426
}
422427
redirectForCommitChoice(ctx, parsed, parsed.form.TreePath)

routers/web/repo/editor_apply_patch.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ func NewDiffPatchPost(ctx *context.Context) {
3333
defaultCommitMessage := ctx.Locale.TrString("repo.editor.patch")
3434
_, err := files.ApplyDiffPatch(ctx, ctx.Repo.Repository, ctx.Doer, &files.ApplyDiffPatchOptions{
3535
LastCommitID: parsed.form.LastCommit,
36-
OldBranch: ctx.Repo.BranchName,
37-
NewBranch: parsed.TargetBranchName,
36+
OldBranch: parsed.OldBranchName,
37+
NewBranch: parsed.NewBranchName,
3838
Message: parsed.GetCommitMessage(defaultCommitMessage),
3939
Content: strings.ReplaceAll(parsed.form.Content.Value(), "\r\n", "\n"),
4040
Author: parsed.GitCommitter,
@@ -44,7 +44,7 @@ func NewDiffPatchPost(ctx *context.Context) {
4444
err = util.ErrorWrapLocale(err, "repo.editor.fail_to_apply_patch")
4545
}
4646
if err != nil {
47-
editorHandleFileOperationError(ctx, parsed.TargetBranchName, err)
47+
editorHandleFileOperationError(ctx, parsed.NewBranchName, err)
4848
return
4949
}
5050
redirectForCommitChoice(ctx, parsed, parsed.form.TreePath)

routers/web/repo/editor_cherry_pick.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func CherryPickPost(ctx *context.Context) {
5353
defaultCommitMessage := util.Iif(parsed.form.Revert, ctx.Locale.TrString("repo.commit.revert-header", fromCommitID), ctx.Locale.TrString("repo.commit.cherry-pick-header", fromCommitID))
5454
opts := &files.ApplyDiffPatchOptions{
5555
LastCommitID: parsed.form.LastCommit,
56-
OldBranch: ctx.Repo.BranchName,
57-
NewBranch: parsed.TargetBranchName,
56+
OldBranch: parsed.OldBranchName,
57+
NewBranch: parsed.NewBranchName,
5858
Message: parsed.GetCommitMessage(defaultCommitMessage),
5959
Author: parsed.GitCommitter,
6060
Committer: parsed.GitCommitter,
@@ -78,7 +78,7 @@ func CherryPickPost(ctx *context.Context) {
7878
}
7979
}
8080
if err != nil {
81-
editorHandleFileOperationError(ctx, parsed.TargetBranchName, err)
81+
editorHandleFileOperationError(ctx, parsed.NewBranchName, err)
8282
return
8383
}
8484
}

routers/web/repo/view_file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
290290

291291
func prepareToRenderButtons(ctx *context.Context, lfsLock *git_model.LFSLock) {
292292
// archived or mirror repository, the buttons should not be shown
293-
if ctx.Repo.Repository.IsArchived || !ctx.Repo.Repository.CanEnableEditor() {
293+
if !ctx.Repo.Repository.CanEnableEditor() {
294294
return
295295
}
296296

services/context/repo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
155155
canCreateBasePullRequest := targetRepo.BaseRepo != nil && targetRepo.BaseRepo.UnitEnabled(ctx, unit_model.TypePullRequests)
156156
canCreatePullRequest := targetRepo.UnitEnabled(ctx, unit_model.TypePullRequests) || canCreateBasePullRequest
157157

158-
cfb := &CommitFormOptions{
158+
opts := &CommitFormOptions{
159159
TargetRepo: targetRepo,
160160
WillSubmitToFork: submitToForkedRepo,
161161
CanCommitToBranch: canCommitToBranch,
@@ -175,11 +175,11 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
175175
editorPathParamRemaining = util.PathEscapeSegments(targetRepo.DefaultBranch) + "/" + util.PathEscapeSegments(ctx.Repo.TreePath) + "?from_base_branch=" + url.QueryEscape(branchName)
176176
}
177177
if editorAction == "_cherrypick" {
178-
cfb.TargetFormAction = targetRepo.Link() + "/" + editorAction + "/" + ctx.PathParam("sha") + "/" + editorPathParamRemaining
178+
opts.TargetFormAction = targetRepo.Link() + "/" + editorAction + "/" + ctx.PathParam("sha") + "/" + editorPathParamRemaining
179179
} else {
180-
cfb.TargetFormAction = targetRepo.Link() + "/" + editorAction + "/" + editorPathParamRemaining
180+
opts.TargetFormAction = targetRepo.Link() + "/" + editorAction + "/" + editorPathParamRemaining
181181
}
182-
return cfb, nil
182+
return opts, nil
183183
}
184184

185185
// CanUseTimetracker returns whether a user can use the timetracker.

templates/repo/view_file.tmpl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,8 @@
6262
</a>
6363
{{end}}
6464
{{if .Repository.CanEnableEditor}}
65-
{{if .CanEditFile}}
66-
<a class="btn-octicon" data-tooltip-content="{{.EditFileTooltip}}" href="{{.RepoLink}}/_edit/{{PathEscapeSegments .BranchName}}/{{PathEscapeSegments .TreePath}}">{{svg "octicon-pencil"}}</a>
67-
{{else}}
68-
<span class="btn-octicon disabled" data-tooltip-content="{{.EditFileTooltip}}">{{svg "octicon-pencil"}}</span>
69-
{{end}}
70-
{{if .CanDeleteFile}}
71-
<a class="btn-octicon btn-octicon-danger" data-tooltip-content="{{.DeleteFileTooltip}}" href="{{.RepoLink}}/_delete/{{PathEscapeSegments .BranchName}}/{{PathEscapeSegments .TreePath}}">{{svg "octicon-trash"}}</a>
72-
{{else}}
73-
<span class="btn-octicon disabled" data-tooltip-content="{{.DeleteFileTooltip}}">{{svg "octicon-trash"}}</span>
74-
{{end}}
65+
<a class="btn-octicon" data-tooltip-content="{{.EditFileTooltip}}" href="{{.RepoLink}}/_edit/{{PathEscapeSegments .BranchName}}/{{PathEscapeSegments .TreePath}}">{{svg "octicon-pencil"}}</a>
66+
<a class="btn-octicon btn-octicon-danger" data-tooltip-content="{{.DeleteFileTooltip}}" href="{{.RepoLink}}/_delete/{{PathEscapeSegments .BranchName}}/{{PathEscapeSegments .TreePath}}">{{svg "octicon-trash"}}</a>
7567
{{end}}
7668
{{else if .EscapeStatus.Escaped}}
7769
<button class="ui mini basic button unescape-button tw-mr-1 tw-hidden">{{ctx.Locale.Tr "repo.unescape_control_characters"}}</button>

0 commit comments

Comments
 (0)