Fix 500 on first wiki page (#16586) (#16598)

Backport #16586

There is a mistake in #16319 and #16487 which means that the first time
a wiki page is created a 500 is reported because the `master` branch is
not in existence in that wiki yet.

This PR simply checks for this error and returns not found.

Fix #16584

Signed-off-by: Andrew Thornton <art27@cantab.net>
release/v1.15
zeripath 2021-08-03 23:46:08 +01:00 committed by GitHub
parent ff8fadd2be
commit a17edf446f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -90,6 +90,9 @@ func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string
// Look for both files // Look for both files
filesInIndex, err := gitRepo.LsTree("master", unescaped, escaped) filesInIndex, err := gitRepo.LsTree("master", unescaped, escaped)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "Not a valid object name master") {
return false, escaped, nil
}
log.Error("%v", err) log.Error("%v", err)
return false, escaped, err return false, escaped, err
} }

View File

@ -5,11 +5,15 @@
package wiki package wiki
import ( import (
"io/ioutil"
"os"
"path/filepath" "path/filepath"
"testing" "testing"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -261,3 +265,28 @@ func TestPrepareWikiFileName(t *testing.T) {
}) })
} }
} }
func TestPrepareWikiFileName_FirstPage(t *testing.T) {
models.PrepareTestEnv(t)
// Now create a temporaryDirectory
tmpDir, err := ioutil.TempDir("", "empty-wiki")
assert.NoError(t, err)
defer func() {
if _, err := os.Stat(tmpDir); !os.IsNotExist(err) {
_ = util.RemoveAll(tmpDir)
}
}()
err = git.InitRepository(tmpDir, true)
assert.NoError(t, err)
gitRepo, err := git.OpenRepository(tmpDir)
defer gitRepo.Close()
assert.NoError(t, err)
existence, newWikiPath, err := prepareWikiFileName(gitRepo, "Home")
assert.False(t, existence)
assert.NoError(t, err)
assert.Equal(t, "Home.md", newWikiPath)
}