mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-12 04:54:56 +00:00
Use rev-parse to check if commit exists
Signed-off-by: Gary Kim <gary@garykim.dev>
This commit is contained in:
parent
582a180621
commit
5852974137
|
@ -647,6 +647,9 @@ func fullSha1PatternProcessor(ctx *postProcessCtx, node *html.Node) {
|
|||
// sha1CurrentPatternProcessor renders SHA1 strings to corresponding links that
|
||||
// are assumed to be in the same repository.
|
||||
func sha1CurrentPatternProcessor(ctx *postProcessCtx, node *html.Node) {
|
||||
if ctx.metas == nil || ctx.metas["user"] == "" || ctx.metas["repo"] == "" || ctx.metas["repoPath"] == "" {
|
||||
return
|
||||
}
|
||||
m := sha1CurrentPattern.FindStringSubmatchIndex(node.Data)
|
||||
if m == nil {
|
||||
return
|
||||
|
@ -660,12 +663,9 @@ func sha1CurrentPatternProcessor(ctx *postProcessCtx, node *html.Node) {
|
|||
// as used by git and github for linking and thus we have to do similar.
|
||||
// Because of this, we check to make sure that a matched hash is actually
|
||||
// a commit in the repository before making it a link.
|
||||
if ctx.metas["repoPath"] != "" {
|
||||
repo, err := git.OpenRepository(ctx.metas["repoPath"])
|
||||
if err != nil || !repo.IsCommitExist(hash) {
|
||||
if _, err := git.NewCommand("rev-parse", "--verify", hash).RunInDirBytes(ctx.metas["repoPath"]); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
replaceContent(node, m[2], m[3],
|
||||
createCodeLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], "commit", hash), base.ShortSha(hash)))
|
||||
|
|
Loading…
Reference in a new issue