diff --git a/modules/context/api.go b/modules/context/api.go index 300c672ac..8c64bb5cf 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -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" diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 4f04b2d36..00272daad 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -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 diff --git a/modules/structs/wiki.go b/modules/structs/wiki.go index 9b0df85c9..b9f0449b3 100644 --- a/modules/structs/wiki.go +++ b/modules/structs/wiki.go @@ -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 { diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 8a4856e29..f7bde6ea6 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -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, diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index c04a7f518..e9cd90a8c 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -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 {