fix:first wikies

This commit is contained in:
wonderful 2021-08-19 17:44:25 +08:00
parent b228c9f602
commit 3072fc7e4e
5 changed files with 64 additions and 18 deletions

View File

@ -278,7 +278,28 @@ func (ctx *APIContext) FileNameError(objs ...interface{}){
"errors": errors,
})
}
func (ctx *APIContext) SameFileNameError(objs ...interface{}){
var message = "Same name exists"
var errors []string
for _, obj := range objs {
// Ignore nil
if obj == nil {
continue
}
if err, ok := obj.(error); ok {
errors = append(errors, err.Error())
} else {
message = obj.(string)
}
}
ctx.JSON(500, map[string]interface{}{
"message": message,
"documentation_url": setting.API.SwaggerURL,
"errors": errors,
})
}
func (ctx *APIContext) FileExistError(objs ...interface{}){
var message = "file does not exist"

View File

@ -206,6 +206,20 @@ func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) {
return commits.Front().Value.(*Commit), nil
}
func (repo *Repository) GetFirstAndLastCommitByPath(revision, relpath string) (*Commit,*Commit, error) {
stdout, err := NewCommand("log", revision, prettyLogFormat, "--", relpath).RunInDirBytes(repo.Path)
if err != nil {
return nil,nil, err
}
commits, err := repo.parsePrettyFormatLogToList(stdout)
if err != nil {
return nil, nil,err
}
return commits.Front().Value.(*Commit),commits.Back().Value.(*Commit), nil
}
// CommitsRangeSize the default commits range size
var CommitsRangeSize = 50

View File

@ -22,7 +22,7 @@ type WikiCommit struct {
ID string `json:"id"`
Message string `json:"message"`
Author WikiUser `json:"author"`
Commiter WikiUser `json:"-"`
Commiter WikiUser `json:"commiter"`
}
type WikiUser struct {

View File

@ -59,7 +59,8 @@ func WikiList(ctx *context.APIContext) {
if !entry.IsRegular() {
continue
}
c, err := wikiRepo.GetCommitByPath(entry.Name())
//c, err := wikiRepo.GetCommitByPath(entry.Name())
lastCommit, firstCommit, err := wikiRepo.GetFirstAndLastCommitByPath("master", entry.Name())
if err != nil {
if wikiRepo != nil {
wikiRepo.Close()
@ -89,17 +90,17 @@ func WikiList(ctx *context.APIContext) {
Name: wikiName,
Commit: api.WikiCommit{
Author: api.WikiUser{
Name: c.Author.Name,
Email: c.Author.Email,
When: c.Author.When.Unix(),
Name: firstCommit.Author.Name,
Email: firstCommit.Author.Email,
When: firstCommit.Author.When.Unix(),
},
Commiter: api.WikiUser{
Name: c.Committer.Name,
Email: c.Committer.Email,
When: c.Author.When.Unix(),
Name: lastCommit.Committer.Name,
Email: lastCommit.Committer.Email,
When: lastCommit.Author.When.Unix(),
},
ID: c.ID.String(),
Message: c.Message(),
ID: lastCommit.ID.String(),
Message: lastCommit.Message(),
},
},
})
@ -107,7 +108,7 @@ func WikiList(ctx *context.APIContext) {
//根据创建时间,按最新的时间排序
sort.Slice(wikiesList, func(i, j int) bool {
return wikiesList[i].Commit.Commiter.When < wikiesList[j].Commit.Commiter.When
return wikiesList[i].Commit.Author.When > wikiesList[j].Commit.Author.When
})
ctx.JSON(http.StatusOK, wikiesList)
}
@ -309,23 +310,32 @@ func EditWiki(ctx *context.APIContext, form api.WikiOption) {
ctx.FileNameError()
return
}
wikiRepo, commit, _ := wikies.FindWikiRepoCommit(ctx)
//Does the same name exist
//entries, err := commit.ListEntries()
//for _, entry := range entries {
// wikiname, _ := wiki_service.FilenameToName(entry.Name())
//
// if wikiname == newWikiName{
// ctx.SameFileNameError()
// return
// }
//}
wikies.EditWikiPost(ctx, form)
OwnerName := ctx.Repo.Repository.OwnerName
RepoName := ctx.Repo.Repository.Name
wikiRepo, commit, _ := wikies.FindWikiRepoCommit(ctx)
data, entry, pageFilename, _ := wikies.WikiContentsByName(ctx, commit, form.Name) //
metas := ctx.Repo.Repository.ComposeDocumentMetas()
PageContent := markdown.RenderWiki(data, ctx.Repo.RepoLink, metas)
commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename)
data, entry, pageFilename, _ := wikies.WikiContentsByName(ctx, commit, form.Name)
c, err := wikiRepo.GetCommitByPath(entry.Name())
if err != nil {
if models.IsErrWikiInvalidFileName(err) {
return
}
}
metas := ctx.Repo.Repository.ComposeDocumentMetas()
PageContent := markdown.RenderWiki(data, ctx.Repo.RepoLink, metas)
commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename)
wiki := api.WikiResponse{
OwnerName: OwnerName,
RepoName: RepoName,

View File

@ -76,6 +76,7 @@ func CheckFile(filename string) error {
}
}
// InitWiki initializes a wiki for repository,
// it does nothing when repository already has wiki.
func InitWiki(repo *models.Repository) error {