From 60004cfb97fe0eb64d1c8310e7c9caae96b8adbe Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sun, 4 Aug 2024 14:44:36 +0200 Subject: feat: Add diff view Adds a very simple diff view for a commit --- README.md | 2 +- pkg/git/git.go | 39 +++++++++++++++++++++++++++++++++ pkg/handler/git/handler.go | 6 ++++++ pkg/service/git.go | 19 ++++++++++++++++ templates/gititemcommit.qtpl | 6 +++--- templates/gititemcommit.qtpl.go | 48 ++++++++++++++++++++++------------------- 6 files changed, 94 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 280172b..f6e202a 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ To run the project you just need to do a make run. ### TODO -- Add patch to the commit page +- Impove diff display - Add log pagination - Fix submodule link on tree view diff --git a/pkg/git/git.go b/pkg/git/git.go index a9c42ce..6221e33 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -179,6 +179,45 @@ func (g *GitRepository) Branches() ([]*plumbing.Reference, error) { return branches, nil } +func (g *GitRepository) Diff() (string, error) { + err := g.validateRef() + if err != nil { + return "", err + } + + c, err := g.repository.CommitObject(g.ref) + if err != nil { + return "", err + } + + commitTree, err := c.Tree() + if err != nil { + return "", err + } + + patch := &object.Patch{} + parentTree := &object.Tree{} + if c.NumParents() != 0 { + parent, err := c.Parents().Next() + if err == nil { + parentTree, err = parent.Tree() + if err == nil { + patch, err = parentTree.Patch(commitTree) + if err != nil { + return "", err + } + } + } + } else { + patch, err = parentTree.Patch(commitTree) + if err != nil { + return "", err + } + } + + return patch.String(), nil +} + func (g *GitRepository) Tree(path string) (*object.Tree, error) { err := g.validateRef() if err != nil { diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index 2ddc5f1..40fae24 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -340,11 +340,17 @@ func (g *GitHandler) Commit(w http.ResponseWriter, r *http.Request) error { return err } + diff, err := g.gitService.Diff(name, ref) + if err != nil { + return err + } + gitList := &templates.GitItemPage{ Name: name, Ref: ref, GitItemBase: &templates.GitItemCommitPage{ Commit: commit, + Diff: diff, }, } templates.WritePageTemplate(w, gitList) diff --git a/pkg/service/git.go b/pkg/service/git.go index b368f0c..f03ba42 100644 --- a/pkg/service/git.go +++ b/pkg/service/git.go @@ -140,6 +140,25 @@ func (g *GitService) WriteTarGZip(w io.Writer, name, ref string, prefix string) return nil } +func (g *GitService) Diff(name, ref string) (string, error) { + r := g.configRepo.GetByName(name) + if r == nil { + return "", ErrRepositoryNotFound + } + + repo, err := git.OpenRepository(r.Path) + if err != nil { + return "", err + } + + err = repo.SetRef(ref) + if err != nil { + return "", err + } + + return repo.Diff() +} + func (g *GitService) GetTree(name, ref, path string) (*object.Tree, error) { r := g.configRepo.GetByName(name) if r == nil { diff --git a/templates/gititemcommit.qtpl b/templates/gititemcommit.qtpl index 77536f1..d223315 100644 --- a/templates/gititemcommit.qtpl +++ b/templates/gititemcommit.qtpl @@ -3,6 +3,7 @@ {% code type GitItemCommitPage struct { Commit *object.Commit + Diff string } %} @@ -12,8 +13,7 @@ type GitItemCommitPage struct {
{%= Commit(name, g.Commit, true) %}
- -