diff --git a/cmd/web.go b/cmd/web.go index 57c8f0077..264c08d73 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -247,6 +247,8 @@ func runWeb(ctx *cli.Context) { m.Get("/commits/:commitid", middleware.RepoRef(), v1.CommitByID) m.Get("/commits/head", middleware.RepoRef(), v1.HEADCommit) + m.Get("/compare/:before([a-z0-9]{40})...:after([a-z0-9]{40})", middleware.RepoRef(), v1.DiffRange) + m.Group("/releases", func() { m.Combo("").Get(v1.ListReleases). Post(bindIgnErr(api.CreateReleaseOption{}), v1.CreateRelease) diff --git a/routers/api/v1/repo_commits.go b/routers/api/v1/repo_commits.go index 9892dce5a..d4dfa1fe1 100644 --- a/routers/api/v1/repo_commits.go +++ b/routers/api/v1/repo_commits.go @@ -7,6 +7,7 @@ package v1 import ( api "github.com/gogits/go-gogs-client" + "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/git" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" @@ -24,13 +25,13 @@ func ToApiCommit(commit *git.Commit) *api.Commit { return &api.Commit{ ID: commit.Id.String(), Author: *ToApiSignature(commit.Author), - Committer: *ToApiSignature(commit.Committer), - CommitMessage: commit.CommitMessage, + Committer: *ToApiSignature(commit.Committer), + CommitMessage: commit.CommitMessage, } } func HEADCommit(ctx *middleware.Context) { - ctx.JSON(200, ToApiCommit(ctx.Repo.Commit.Id.String())) + ctx.JSON(200, ctx.Repo.Commit.Id.String()) } func CommitByID(ctx *middleware.Context) { @@ -44,4 +45,92 @@ func CommitByID(ctx *middleware.Context) { } ctx.JSON(200, ToApiCommit(commit)) +} + + +func ToApiDiff(diff *models.Diff) *api.Diff { + return &api.Diff{ + TotalAddition: diff.TotalAddition, + TotalDeletion: diff.TotalDeletion, + Files: ToApiDiffFiles(diff.Files), + } +} + +func ToApiDiffFiles(diffFiles []*models.DiffFile) []*api.DiffFile { + apiDiffFiles := make([]*api.DiffFile, len(diffFiles)) + for i := range apiDiffFiles { + apiDiffFiles[i] = ToApiDiffFile(diffFiles[i]) + } + return apiDiffFiles +} + +func ToApiDiffFile(diffFile *models.DiffFile) *api.DiffFile { + return &api.DiffFile{ + Name: diffFile.Name, + Index: diffFile.Index, + Addition: diffFile.Addition, + Deletion: diffFile.Deletion, + Type: diffFile.Type, + IsCreated: diffFile.IsCreated, + IsDeleted: diffFile.IsDeleted, + IsBin: diffFile.IsBin, + Sections: ToApiDiffSections(diffFile.Sections), + } +} + +func ToApiDiffSections(diffSections []*models.DiffSection) []*api.DiffSection { + apiDiffSections := make([]*api.DiffSection, len(diffSections)) + for i := range apiDiffSections { + apiDiffSections[i] = ToApiDiffSection(diffSections[i]) + } + return apiDiffSections +} + +func ToApiDiffSection(diffSection *models.DiffSection) *api.DiffSection { + return &api.DiffSection{ + Name: diffSection.Name, + Lines: ToApiDiffLines(diffSection.Lines), + } +} + +func ToApiDiffLines(diffLines []*models.DiffLine) []*api.DiffLine { + apiDiffLines := make([]*api.DiffLine, len(diffLines)) + for i := range apiDiffLines { + apiDiffLines[i] = ToApiDiffLine(diffLines[i]) + } + return apiDiffLines +} + +func ToApiDiffLine(diffLine *models.DiffLine) *api.DiffLine { + return &api.DiffLine{ + LeftIdx: diffLine.LeftIdx, + RightIdx: diffLine.RightIdx, + Type: diffLine.Type, + Content: diffLine.Content, + } +} + +func DiffRange(ctx *middleware.Context) { + beforeCommitID := ctx.Params(":before") + afterCommitID := ctx.Params(":after") + + _, err := ctx.Repo.GitRepo.GetCommit(beforeCommitID) + if err != nil { + ctx.Handle(404, "GetCommit", err) + return + } + _, err = ctx.Repo.GitRepo.GetCommit(afterCommitID) + if err != nil { + ctx.Handle(404, "GetCommit", err) + return + } + + diff, err := models.GetDiffRange(ctx.Repo.GitRepo.Path, beforeCommitID, afterCommitID, 10000) + if err != nil { + log.Error(4, "GetDiffRange: %v", err) + ctx.Error(500, err.Error()) + return + } + + ctx.JSON(200, ToApiDiff(diff)) } \ No newline at end of file diff --git a/routers/api/v1/repo_releases.go b/routers/api/v1/repo_releases.go index 3af75fcc3..f9d84f766 100644 --- a/routers/api/v1/repo_releases.go +++ b/routers/api/v1/repo_releases.go @@ -17,18 +17,18 @@ import ( func ToApiRelease(release *models.Release, publisher *models.User) *api.Release { return &api.Release{ - ID: release.Id, - Publisher: *ToApiUser(publisher), - TagName: release.TagName, - LowerTagName: release.LowerTagName, - Target: release.Target, - Title: release.Title, - Sha1: release.Sha1, - NumCommits: release.NumCommits, - Note: release.Note, - IsDraft: release.IsDraft, - IsPrerelease: release.IsPrerelease, - Created: release.Created, + ID: release.Id, + Publisher: *ToApiUser(publisher), + TagName: release.TagName, + LowerTagName: release.LowerTagName, + Target: release.Target, + Title: release.Title, + Sha1: release.Sha1, + NumCommits: release.NumCommits, + Note: release.Note, + IsDraft: release.IsDraft, + IsPrerelease: release.IsPrerelease, + Created: release.Created, } } @@ -42,19 +42,17 @@ func ListReleases(ctx *middleware.Context) { apiReleases := make([]*api.Release, len(rels)) for i, rel := range rels { - if models.IsUserExist(rel.PublisherId, "") { - publisher, err := models.GetUserByID(rel.PublisherId) - if err != nil { - if models.IsErrUserNotExist(err) { - publisher = models.NewFakeUser() - } else { - log.Error(4, "GetUserByID: %v", err) - ctx.Status(500) - return - } + publisher, err := models.GetUserByID(rel.PublisherId) + if err != nil { + if models.IsErrUserNotExist(err) { + publisher = models.NewFakeUser() + } else { + log.Error(4, "GetUserByID: %v", err) + ctx.Status(500) + return } - apiReleases[i] = ToApiRelease(rel, publisher) } + apiReleases[i] = ToApiRelease(rel, publisher) } ctx.JSON(200, &apiReleases) }