diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/git/git.go | 39 | ||||
-rw-r--r-- | pkg/handler/git/handler.go | 6 | ||||
-rw-r--r-- | pkg/service/git.go | 19 |
3 files changed, 64 insertions, 0 deletions
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 { |