diff options
author | Christopher Brickley <brickley@gmail.com> | 2014-08-26 08:20:18 -0400 |
---|---|---|
committer | Christopher Brickley <brickley@gmail.com> | 2014-09-01 14:56:19 -0400 |
commit | 00a864e693434bce687f3f5145d8369583197b78 (patch) | |
tree | df25bf583ec2a0070c159bcb3d0d161d931f6801 /routers | |
parent | d55c5b9e289c0c97aa51ffe5cb5b77f703cc2a47 (diff) | |
download | gitea-00a864e693434bce687f3f5145d8369583197b78.tar.gz gitea-00a864e693434bce687f3f5145d8369583197b78.zip |
add commit compare functionality
Diffstat (limited to 'routers')
-rw-r--r-- | routers/repo/commit.go | 65 |
1 files changed, 63 insertions, 2 deletions
diff --git a/routers/repo/commit.go b/routers/repo/commit.go index 6320123b40..54acc85b3e 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -114,9 +114,9 @@ func Diff(ctx *middleware.Context) { commit := ctx.Repo.Commit - diff, err := models.GetDiff(models.RepoPath(userName, repoName), commitId) + diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName), commitId) if err != nil { - ctx.Handle(404, "GetDiff", err) + ctx.Handle(404, "GetDiffCommit", err) return } @@ -162,6 +162,67 @@ func Diff(ctx *middleware.Context) { ctx.HTML(200, DIFF) } +func CompareDiff(ctx *middleware.Context) { + ctx.Data["IsRepoToolbarCommits"] = true + ctx.Data["IsDiffCompare"] = true + userName := ctx.Repo.Owner.Name + repoName := ctx.Repo.Repository.Name + beforeCommitId := ctx.Params(":before") + afterCommitId := ctx.Params(":after") + + commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitId) + if err != nil { + ctx.Handle(404, "GetCommit", err) + return + } + + diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitId, afterCommitId) + if err != nil { + ctx.Handle(404, "GetDiffRange", err) + return + } + + isImageFile := func(name string) bool { + blob, err := commit.GetBlobByPath(name) + if err != nil { + return false + } + + dataRc, err := blob.Data() + if err != nil { + return false + } + buf := make([]byte, 1024) + n, _ := dataRc.Read(buf) + if n > 0 { + buf = buf[:n] + } + _, isImage := base.IsImageFile(buf) + return isImage + } + + commits, err := commit.CommitsBeforeUntil(beforeCommitId) + if err != nil { + ctx.Handle(500, "CommitsBeforeUntil", err) + return + } + + ctx.Data["Commits"] = commits + ctx.Data["CommitCount"] = commits.Len() + ctx.Data["BeforeCommitId"] = beforeCommitId + ctx.Data["AfterCommitId"] = afterCommitId + ctx.Data["Username"] = userName + ctx.Data["Reponame"] = repoName + ctx.Data["IsImageFile"] = isImageFile + ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitId) + "..." + base.ShortSha(afterCommitId) + " ยท " + userName + "/" + repoName + ctx.Data["Commit"] = commit + ctx.Data["Diff"] = diff + ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0 + ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", afterCommitId) + ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", afterCommitId) + ctx.HTML(200, DIFF) +} + func FileHistory(ctx *middleware.Context) { ctx.Data["IsRepoToolbarCommits"] = true |