Skip to content

Commit 96bbf81

Browse files
committed
Merge branch 'main' into non-text-edit
# Conflicts: # routers/web/repo/editor.go
2 parents cf44789 + 62f7349 commit 96bbf81

File tree

11 files changed

+134
-34
lines changed

11 files changed

+134
-34
lines changed

options/locale/locale_en-US.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,6 @@ file_copy_permalink = Copy Permalink
13051305
view_git_blame = View Git Blame
13061306
video_not_supported_in_browser = Your browser does not support the HTML5 'video' tag.
13071307
audio_not_supported_in_browser = Your browser does not support the HTML5 'audio' tag.
1308-
stored_lfs = Stored with Git LFS
13091308
symbolic_link = Symbolic link
13101309
executable_file = Executable File
13111310
vendored = Vendored

package-lock.json

Lines changed: 13 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"@citation-js/plugin-csl": "0.7.18",
1010
"@citation-js/plugin-software-formats": "0.6.1",
1111
"@github/markdown-toolbar-element": "2.2.3",
12-
"@github/relative-time-element": "4.4.5",
12+
"@github/relative-time-element": "4.4.6",
1313
"@github/text-expander-element": "2.9.1",
1414
"@mcaptcha/vanilla-glue": "0.1.0-alpha-3",
1515
"@primer/octicons": "19.15.1",

routers/api/v1/repo/pull.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1632,7 +1632,9 @@ func GetPullRequestFiles(ctx *context.APIContext) {
16321632

16331633
apiFiles := make([]*api.ChangedFile, 0, limit)
16341634
for i := start; i < start+limit; i++ {
1635-
apiFiles = append(apiFiles, convert.ToChangedFile(diff.Files[i], pr.HeadRepo, endCommitID))
1635+
// refs/pull/1/head stores the HEAD commit ID, allowing all related commits to be found in the base repository.
1636+
// The head repository might have been deleted, so we should not rely on it here.
1637+
apiFiles = append(apiFiles, convert.ToChangedFile(diff.Files[i], pr.BaseRepo, endCommitID))
16361638
}
16371639

16381640
ctx.SetLinkHeader(totalNumberOfFiles, listOptions.PageSize)

routers/web/repo/compare.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,11 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
402402
ci.HeadRepo = ctx.Repo.Repository
403403
ci.HeadGitRepo = ctx.Repo.GitRepo
404404
} else if has {
405-
ci.HeadGitRepo, err = gitrepo.OpenRepository(ctx, ci.HeadRepo)
405+
ci.HeadGitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ci.HeadRepo)
406406
if err != nil {
407-
ctx.ServerError("OpenRepository", err)
407+
ctx.ServerError("RepositoryFromRequestContextOrOpen", err)
408408
return nil
409409
}
410-
defer ci.HeadGitRepo.Close()
411410
} else {
412411
ctx.NotFound(nil)
413412
return nil
@@ -726,11 +725,6 @@ func getBranchesAndTagsForRepo(ctx gocontext.Context, repo *repo_model.Repositor
726725
// CompareDiff show different from one commit to another commit
727726
func CompareDiff(ctx *context.Context) {
728727
ci := ParseCompareInfo(ctx)
729-
defer func() {
730-
if ci != nil && ci.HeadGitRepo != nil {
731-
ci.HeadGitRepo.Close()
732-
}
733-
}()
734728
if ctx.Written() {
735729
return
736730
}

routers/web/repo/editor.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"code.gitea.io/gitea/modules/markup"
2121
"code.gitea.io/gitea/modules/setting"
2222
"code.gitea.io/gitea/modules/templates"
23-
"code.gitea.io/gitea/modules/typesniffer"
2423
"code.gitea.io/gitea/modules/util"
2524
"code.gitea.io/gitea/modules/web"
2625
"code.gitea.io/gitea/routers/utils"
@@ -146,22 +145,27 @@ func editFile(ctx *context.Context, isNewFile bool) {
146145
}
147146

148147
blob := entry.Blob()
149-
dataRc, err := blob.DataAsync()
150-
if err != nil {
148+
if blob.Size() >= setting.UI.MaxDisplayFileSize {
151149
ctx.NotFound(err)
152150
return
153151
}
154152

153+
buf, dataRc, fInfo, err := getFileReader(ctx, ctx.Repo.Repository.ID, blob)
154+
if err != nil {
155+
if git.IsErrNotExist(err) {
156+
ctx.NotFound(err)
157+
} else {
158+
ctx.ServerError("getFileReader", err)
159+
}
160+
return
161+
}
162+
155163
defer dataRc.Close()
156164

157165
ctx.Data["FileSize"] = blob.Size()
158166

159-
buf := make([]byte, 1024)
160-
n, _ := util.ReadAtMost(dataRc, buf)
161-
buf = buf[:n]
162-
163167
// Only some file types are editable online as text.
164-
ctx.Data["IsFileEditable"] = typesniffer.DetectContentType(buf).IsRepresentableAsText()
168+
ctx.Data["IsFileEditable"] = fInfo.isTextFile && !fInfo.isLFSFile
165169

166170
if blob.Size() >= setting.UI.MaxDisplayFileSize {
167171
ctx.Data["IsFileTooLarge"] = true

routers/web/repo/pull.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,11 +1296,6 @@ func CompareAndPullRequestPost(ctx *context.Context) {
12961296
)
12971297

12981298
ci := ParseCompareInfo(ctx)
1299-
defer func() {
1300-
if ci != nil && ci.HeadGitRepo != nil {
1301-
ci.HeadGitRepo.Close()
1302-
}
1303-
}()
13041299
if ctx.Written() {
13051300
return
13061301
}

templates/repo/diff/box.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@
101101
{{end}}
102102
</div>
103103
<span class="file tw-flex tw-items-center tw-font-mono tw-flex-1"><a class="muted file-link" title="{{if $file.IsRenamed}}{{$file.OldName}} → {{end}}{{$file.Name}}" href="#diff-{{$file.NameHash}}">{{if $file.IsRenamed}}{{$file.OldName}} → {{end}}{{$file.Name}}</a>
104-
{{if .IsLFSFile}} ({{ctx.Locale.Tr "repo.stored_lfs"}}){{end}}
105104
<button class="btn interact-fg tw-p-2" data-clipboard-text="{{$file.Name}}" data-tooltip-content="{{ctx.Locale.Tr "copy_path"}}">{{svg "octicon-copy" 14}}</button>
105+
{{if .IsLFSFile}}<span class="ui label">LFS</span>{{end}}
106106
{{if $file.IsGenerated}}
107107
<span class="ui label">{{ctx.Locale.Tr "repo.diff.generated"}}</span>
108108
{{end}}

templates/repo/file_info.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{{end}}
1212
{{if ne .FileSize nil}}
1313
<div class="file-info-entry">
14-
{{FileSize .FileSize}}{{if .IsLFSFile}} ({{ctx.Locale.Tr "repo.stored_lfs"}}){{end}}
14+
{{FileSize .FileSize}}{{if .IsLFSFile}}<span class="ui label">LFS</span>{{end}}
1515
</div>
1616
{{end}}
1717
{{if .LFSLock}}

tests/integration/api_pull_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import (
88
"fmt"
99
"io"
1010
"net/http"
11+
"net/url"
12+
"strings"
1113
"testing"
14+
"time"
1215

1316
auth_model "code.gitea.io/gitea/models/auth"
1417
"code.gitea.io/gitea/models/db"
@@ -17,11 +20,15 @@ import (
1720
repo_model "code.gitea.io/gitea/models/repo"
1821
"code.gitea.io/gitea/models/unittest"
1922
user_model "code.gitea.io/gitea/models/user"
23+
"code.gitea.io/gitea/modules/git"
2024
"code.gitea.io/gitea/modules/setting"
2125
api "code.gitea.io/gitea/modules/structs"
26+
"code.gitea.io/gitea/services/convert"
2227
"code.gitea.io/gitea/services/forms"
2328
"code.gitea.io/gitea/services/gitdiff"
2429
issue_service "code.gitea.io/gitea/services/issue"
30+
pull_service "code.gitea.io/gitea/services/pull"
31+
files_service "code.gitea.io/gitea/services/repository/files"
2532
"code.gitea.io/gitea/tests"
2633

2734
"github.com/stretchr/testify/assert"
@@ -424,3 +431,94 @@ func TestAPICommitPullRequest(t *testing.T) {
424431
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/commits/%s/pull", owner.Name, repo.Name, invalidCommitSHA).AddTokenAuth(ctx.Token)
425432
ctx.Session.MakeRequest(t, req, http.StatusNotFound)
426433
}
434+
435+
func TestAPIViewPullFilesWithHeadRepoDeleted(t *testing.T) {
436+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
437+
baseRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
438+
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
439+
440+
ctx := NewAPITestContext(t, "user1", baseRepo.Name, auth_model.AccessTokenScopeAll)
441+
442+
doAPIForkRepository(ctx, "user2")(t)
443+
444+
forkedRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ForkID: baseRepo.ID, OwnerName: "user1"})
445+
446+
// add a new file to the forked repo
447+
addFileToForkedResp, err := files_service.ChangeRepoFiles(git.DefaultContext, forkedRepo, user1, &files_service.ChangeRepoFilesOptions{
448+
Files: []*files_service.ChangeRepoFile{
449+
{
450+
Operation: "create",
451+
TreePath: "file_1.txt",
452+
ContentReader: strings.NewReader("file1"),
453+
},
454+
},
455+
Message: "add file1",
456+
OldBranch: "master",
457+
NewBranch: "fork-branch-1",
458+
Author: &files_service.IdentityOptions{
459+
GitUserName: user1.Name,
460+
GitUserEmail: user1.Email,
461+
},
462+
Committer: &files_service.IdentityOptions{
463+
GitUserName: user1.Name,
464+
GitUserEmail: user1.Email,
465+
},
466+
Dates: &files_service.CommitDateOptions{
467+
Author: time.Now(),
468+
Committer: time.Now(),
469+
},
470+
})
471+
assert.NoError(t, err)
472+
assert.NotEmpty(t, addFileToForkedResp)
473+
474+
// create Pull
475+
pullIssue := &issues_model.Issue{
476+
RepoID: baseRepo.ID,
477+
Title: "Test pull-request-target-event",
478+
PosterID: user1.ID,
479+
Poster: user1,
480+
IsPull: true,
481+
}
482+
pullRequest := &issues_model.PullRequest{
483+
HeadRepoID: forkedRepo.ID,
484+
BaseRepoID: baseRepo.ID,
485+
HeadBranch: "fork-branch-1",
486+
BaseBranch: "master",
487+
HeadRepo: forkedRepo,
488+
BaseRepo: baseRepo,
489+
Type: issues_model.PullRequestGitea,
490+
}
491+
492+
prOpts := &pull_service.NewPullRequestOptions{Repo: baseRepo, Issue: pullIssue, PullRequest: pullRequest}
493+
err = pull_service.NewPullRequest(git.DefaultContext, prOpts)
494+
assert.NoError(t, err)
495+
pr := convert.ToAPIPullRequest(t.Context(), pullRequest, user1)
496+
497+
ctx = NewAPITestContext(t, "user2", baseRepo.Name, auth_model.AccessTokenScopeAll)
498+
doAPIGetPullFiles(ctx, pr, func(t *testing.T, files []*api.ChangedFile) {
499+
if assert.Len(t, files, 1) {
500+
assert.Equal(t, "file_1.txt", files[0].Filename)
501+
assert.Empty(t, files[0].PreviousFilename)
502+
assert.Equal(t, 1, files[0].Additions)
503+
assert.Equal(t, 1, files[0].Changes)
504+
assert.Equal(t, 0, files[0].Deletions)
505+
assert.Equal(t, "added", files[0].Status)
506+
}
507+
})(t)
508+
509+
// delete the head repository of the pull request
510+
forkCtx := NewAPITestContext(t, "user1", forkedRepo.Name, auth_model.AccessTokenScopeAll)
511+
doAPIDeleteRepository(forkCtx)(t)
512+
513+
doAPIGetPullFiles(ctx, pr, func(t *testing.T, files []*api.ChangedFile) {
514+
if assert.Len(t, files, 1) {
515+
assert.Equal(t, "file_1.txt", files[0].Filename)
516+
assert.Empty(t, files[0].PreviousFilename)
517+
assert.Equal(t, 1, files[0].Additions)
518+
assert.Equal(t, 1, files[0].Changes)
519+
assert.Equal(t, 0, files[0].Deletions)
520+
assert.Equal(t, "added", files[0].Status)
521+
}
522+
})(t)
523+
})
524+
}

0 commit comments

Comments
 (0)