-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Replace repo.namedBlob
by git.TreeEntry
.
#22898
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
947604d
Replace `repo.namedBlob` by `git.TreeEntry`.
kousu e9e3518
Forgotten line
kousu d6dba37
fixup test
kousu d108c2f
Merge branch 'main' into treeentry-findReadmeFile
kousu 580c379
Merge branch 'main' into treeentry-findReadmeFile
kousu 6e162dc
Merge branch 'main' into treeentry-findReadmeFile
kousu 014a753
Merge branch 'main' into treeentry-findReadmeFile
kousu 4e4aadb
Merge branch 'main' into treeentry-findReadmeFile
kousu f5703d4
Merge branch 'main' into treeentry-findReadmeFile
kousu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -50,12 +50,6 @@ const ( | |||||
tplMigrating base.TplName = "repo/migrate/migrating" | ||||||
) | ||||||
|
||||||
type namedBlob struct { | ||||||
name string | ||||||
isSymlink bool | ||||||
blob *git.Blob | ||||||
} | ||||||
|
||||||
// locate a README for a tree in one of the supported paths. | ||||||
// | ||||||
// entries is passed to reduce calls to ListEntries(), so | ||||||
|
@@ -64,14 +58,14 @@ type namedBlob struct { | |||||
// entries == ctx.Repo.Commit.SubTree(ctx.Repo.TreePath).ListEntries() | ||||||
// | ||||||
// FIXME: There has to be a more efficient way of doing this | ||||||
func findReadmeFileInEntries(ctx *context.Context, entries []*git.TreeEntry) (*namedBlob, error) { | ||||||
func findReadmeFileInEntries(ctx *context.Context, entries []*git.TreeEntry) (string, *git.TreeEntry, error) { | ||||||
// Create a list of extensions in priority order | ||||||
// 1. Markdown files - with and without localisation - e.g. README.en-us.md or README.md | ||||||
// 2. Txt files - e.g. README.txt | ||||||
// 3. No extension - e.g. README | ||||||
exts := append(localizedExtensions(".md", ctx.Language()), ".txt", "") // sorted by priority | ||||||
extCount := len(exts) | ||||||
readmeFiles := make([]*namedBlob, extCount+1) | ||||||
readmeFiles := make([]*git.TreeEntry, extCount+1) | ||||||
|
||||||
docsEntries := make([]*git.TreeEntry, 3) // (one of docs/, .gitea/ or .github/) | ||||||
for _, entry := range entries { | ||||||
|
@@ -98,28 +92,21 @@ func findReadmeFileInEntries(ctx *context.Context, entries []*git.TreeEntry) (*n | |||||
} | ||||||
if i, ok := util.IsReadmeFileExtension(entry.Name(), exts...); ok { | ||||||
log.Debug("Potential readme file: %s", entry.Name()) | ||||||
if readmeFiles[i] == nil || base.NaturalSortLess(readmeFiles[i].name, entry.Blob().Name()) { | ||||||
name := entry.Name() | ||||||
isSymlink := entry.IsLink() | ||||||
target := entry | ||||||
if isSymlink { | ||||||
var err error | ||||||
target, err = entry.FollowLinks() | ||||||
if readmeFiles[i] == nil || base.NaturalSortLess(readmeFiles[i].Name(), entry.Blob().Name()) { | ||||||
if entry.IsLink() { | ||||||
target, err := entry.FollowLinks() | ||||||
if err != nil && !git.IsErrBadLink(err) { | ||||||
return nil, err | ||||||
} | ||||||
} | ||||||
if target != nil && (target.IsExecutable() || target.IsRegular()) { | ||||||
readmeFiles[i] = &namedBlob{ | ||||||
name, | ||||||
isSymlink, | ||||||
target.Blob(), | ||||||
return "", nil, err | ||||||
} else if target != nil && (target.IsExecutable() || target.IsRegular()) { | ||||||
readmeFiles[i] = entry | ||||||
} | ||||||
} else { | ||||||
readmeFiles[i] = entry | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
var readmeFile *namedBlob | ||||||
var readmeFile *git.TreeEntry | ||||||
for _, f := range readmeFiles { | ||||||
if f != nil { | ||||||
readmeFile = f | ||||||
|
@@ -140,20 +127,20 @@ func findReadmeFileInEntries(ctx *context.Context, entries []*git.TreeEntry) (*n | |||||
var err error | ||||||
childEntries, err := subTree.ListEntries() | ||||||
if err != nil { | ||||||
return nil, err | ||||||
return "", nil, err | ||||||
} | ||||||
readmeFile, err = findReadmeFileInEntries(ctx, childEntries) | ||||||
|
||||||
subfolder, readmeFile, err := findReadmeFileInEntries(ctx, childEntries) | ||||||
if err != nil && !git.IsErrNotExist(err) { | ||||||
return nil, err | ||||||
return "", nil, err | ||||||
} | ||||||
if readmeFile != nil { | ||||||
readmeFile.name = subTreeEntry.Name() + "/" + readmeFile.name | ||||||
break | ||||||
return path.Join(subTreeEntry.Name(), subfolder), readmeFile, nil | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return readmeFile, nil | ||||||
return "", readmeFile, nil | ||||||
} | ||||||
|
||||||
func renderDirectory(ctx *context.Context, treeLink string) { | ||||||
|
@@ -177,16 +164,13 @@ func renderDirectory(ctx *context.Context, treeLink string) { | |||||
return | ||||||
} | ||||||
|
||||||
readmeFile, err := findReadmeFileInEntries(ctx, entries) | ||||||
subfolder, readmeFile, err := findReadmeFileInEntries(ctx, entries) | ||||||
if err != nil { | ||||||
ctx.ServerError("findReadmeFileInEntries", err) | ||||||
return | ||||||
} | ||||||
if readmeFile == nil { | ||||||
return | ||||||
} | ||||||
|
||||||
renderReadmeFile(ctx, readmeFile, fmt.Sprintf("%s/%s", treeLink, readmeFile.name)) | ||||||
renderReadmeFile(ctx, subfolder, readmeFile, treeLink) | ||||||
} | ||||||
|
||||||
// localizedExtensions prepends the provided language code with and without a | ||||||
|
@@ -270,25 +254,35 @@ func getFileReader(repoID int64, blob *git.Blob) ([]byte, io.ReadCloser, *fileIn | |||||
return buf, dataRc, &fileInfo{st.IsText(), true, meta.Size, &meta.Pointer, st}, nil | ||||||
} | ||||||
|
||||||
func renderReadmeFile(ctx *context.Context, readmeFile *namedBlob, readmeTreelink string) { | ||||||
func renderReadmeFile(ctx *context.Context, subfolder string, readmeFile *git.TreeEntry, readmeTreelink string) { | ||||||
target := readmeFile | ||||||
if readmeFile != nil && readmeFile.IsLink() { | ||||||
target, _ = readmeFile.FollowLinks() | ||||||
} | ||||||
if target == nil { | ||||||
// if findReadmeFile() failed and/or gave us a broken symlink (which it shouldn't) | ||||||
// simply skip rendering the README | ||||||
return | ||||||
} | ||||||
|
||||||
ctx.Data["RawFileLink"] = "" | ||||||
ctx.Data["ReadmeInList"] = true | ||||||
ctx.Data["ReadmeExist"] = true | ||||||
ctx.Data["FileIsSymlink"] = readmeFile.isSymlink | ||||||
ctx.Data["FileIsSymlink"] = readmeFile.IsLink() | ||||||
|
||||||
buf, dataRc, fInfo, err := getFileReader(ctx.Repo.Repository.ID, readmeFile.blob) | ||||||
buf, dataRc, fInfo, err := getFileReader(ctx.Repo.Repository.ID, target.Blob()) | ||||||
if err != nil { | ||||||
ctx.ServerError("getFileReader", err) | ||||||
return | ||||||
} | ||||||
defer dataRc.Close() | ||||||
|
||||||
ctx.Data["FileIsText"] = fInfo.isTextFile | ||||||
ctx.Data["FileName"] = readmeFile.name | ||||||
ctx.Data["FileName"] = path.Join(subfolder, readmeFile.Name()) | ||||||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
ctx.Data["IsLFSFile"] = fInfo.isLFSFile | ||||||
|
||||||
if fInfo.isLFSFile { | ||||||
filenameBase64 := base64.RawURLEncoding.EncodeToString([]byte(readmeFile.name)) | ||||||
filenameBase64 := base64.RawURLEncoding.EncodeToString([]byte(readmeFile.Name())) | ||||||
ctx.Data["RawFileLink"] = fmt.Sprintf("%s.git/info/lfs/objects/%s/%s", ctx.Repo.Repository.Link(), url.PathEscape(fInfo.lfsMeta.Oid), url.PathEscape(filenameBase64)) | ||||||
} | ||||||
|
||||||
|
@@ -306,19 +300,19 @@ func renderReadmeFile(ctx *context.Context, readmeFile *namedBlob, readmeTreelin | |||||
|
||||||
rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc)) | ||||||
|
||||||
if markupType := markup.Type(readmeFile.name); markupType != "" { | ||||||
if markupType := markup.Type(readmeFile.Name()); markupType != "" { | ||||||
ctx.Data["IsMarkup"] = true | ||||||
ctx.Data["MarkupType"] = markupType | ||||||
|
||||||
ctx.Data["EscapeStatus"], ctx.Data["FileContent"], err = markupRender(ctx, &markup.RenderContext{ | ||||||
Ctx: ctx, | ||||||
RelativePath: path.Join(ctx.Repo.TreePath, readmeFile.name), // ctx.Repo.TreePath is the directory not the Readme so we must append the Readme filename (and path). | ||||||
URLPrefix: path.Dir(readmeTreelink), | ||||||
RelativePath: path.Join(ctx.Repo.TreePath, readmeFile.Name()), // ctx.Repo.TreePath is the directory not the Readme so we must append the Readme filename (and path). | ||||||
URLPrefix: path.Join(readmeTreelink, subfolder), | ||||||
Metas: ctx.Repo.Repository.ComposeDocumentMetas(), | ||||||
GitRepo: ctx.Repo.GitRepo, | ||||||
}, rd) | ||||||
if err != nil { | ||||||
log.Error("Render failed for %s in %-v: %v Falling back to rendering source", readmeFile.name, ctx.Repo.Repository, err) | ||||||
log.Error("Render failed for %s in %-v: %v Falling back to rendering source", readmeFile.Name(), ctx.Repo.Repository, err) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps
Suggested change
? |
||||||
buf := &bytes.Buffer{} | ||||||
ctx.Data["EscapeStatus"], _ = charset.EscapeControlStringReader(rd, buf, ctx.Locale) | ||||||
ctx.Data["FileContent"] = buf.String() | ||||||
|
Binary file added
BIN
+47 Bytes
...gitea-repositories-meta/user2/repo1.git/objects/16/633238d370a441f98dca532e4296a619c4c85f
Binary file not shown.
4 changes: 4 additions & 0 deletions
4
...gitea-repositories-meta/user2/repo1.git/objects/46/49299398e4d39a5c09eb4f534df6f1e1eb87cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
tests/gitea-repositories-meta/user2/repo1.git/refs/heads/sub-home-md-img-check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4649299398e4d39a5c09eb4f534df6f1e1eb87cc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This had to change due to the test additions I did. I used the same test repo (
user2/repo1.git
) that #23152 did, but I don't know why that PR didn't force changing this number.