diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index f1632c6bf7aac..e1590f461ef23 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -90,6 +90,9 @@ func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string // Look for both files filesInIndex, err := gitRepo.LsTree("master", unescaped, escaped) if err != nil { + if strings.Contains(err.Error(), "Not a valid object name master") { + return false, escaped, nil + } log.Error("%v", err) return false, escaped, err } diff --git a/services/wiki/wiki_test.go b/services/wiki/wiki_test.go index a1614b509c2bc..6c861d556a7f5 100644 --- a/services/wiki/wiki_test.go +++ b/services/wiki/wiki_test.go @@ -5,11 +5,15 @@ package wiki import ( + "io/ioutil" + "os" "path/filepath" "testing" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/util" + "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) +}