aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2024-08-04 14:44:36 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2024-08-04 14:44:36 +0200
commit60004cfb97fe0eb64d1c8310e7c9caae96b8adbe (patch)
tree2267cf703f6870b05d68c73be7af1fc57190e770
parentaa559681fcf8df993029370f28e0011315ca8761 (diff)
downloadcerrado-60004cfb97fe0eb64d1c8310e7c9caae96b8adbe.tar.gz
cerrado-60004cfb97fe0eb64d1c8310e7c9caae96b8adbe.tar.bz2
cerrado-60004cfb97fe0eb64d1c8310e7c9caae96b8adbe.zip
feat: Add diff view
Adds a very simple diff view for a commit
-rw-r--r--README.md2
-rw-r--r--pkg/git/git.go39
-rw-r--r--pkg/handler/git/handler.go6
-rw-r--r--pkg/service/git.go19
-rw-r--r--templates/gititemcommit.qtpl6
-rw-r--r--templates/gititemcommit.qtpl.go48
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 {
<div class="event-list">
{%= Commit(name, g.Commit, true) %}
</div>
-
-<div class="alert alert-info text-center" role="alert">
- This page is work in progress.
+<div class="code-view">
+<pre>{%s g.Diff %}</pre>
</div>
{% endfunc %}
diff --git a/templates/gititemcommit.qtpl.go b/templates/gititemcommit.qtpl.go
index f94a1c2..39348ee 100644
--- a/templates/gititemcommit.qtpl.go
+++ b/templates/gititemcommit.qtpl.go
@@ -23,55 +23,59 @@ var (
//line gititemcommit.qtpl:4
type GitItemCommitPage struct {
Commit *object.Commit
+ Diff string
}
-//line gititemcommit.qtpl:9
+//line gititemcommit.qtpl:10
func (g *GitItemCommitPage) StreamNav(qw422016 *qt422016.Writer, name, ref string) {
-//line gititemcommit.qtpl:9
+//line gititemcommit.qtpl:10
StreamGitItemNav(qw422016, name, ref, Log)
-//line gititemcommit.qtpl:9
+//line gititemcommit.qtpl:10
}
-//line gititemcommit.qtpl:9
+//line gititemcommit.qtpl:10
func (g *GitItemCommitPage) WriteNav(qq422016 qtio422016.Writer, name, ref string) {
-//line gititemcommit.qtpl:9
+//line gititemcommit.qtpl:10
qw422016 := qt422016.AcquireWriter(qq422016)
-//line gititemcommit.qtpl:9
+//line gititemcommit.qtpl:10
g.StreamNav(qw422016, name, ref)
-//line gititemcommit.qtpl:9
+//line gititemcommit.qtpl:10
qt422016.ReleaseWriter(qw422016)
-//line gititemcommit.qtpl:9
+//line gititemcommit.qtpl:10
}
-//line gititemcommit.qtpl:9
+//line gititemcommit.qtpl:10
func (g *GitItemCommitPage) Nav(name, ref string) string {
-//line gititemcommit.qtpl:9
+//line gititemcommit.qtpl:10
qb422016 := qt422016.AcquireByteBuffer()
-//line gititemcommit.qtpl:9
+//line gititemcommit.qtpl:10
g.WriteNav(qb422016, name, ref)
-//line gititemcommit.qtpl:9
+//line gititemcommit.qtpl:10
qs422016 := string(qb422016.B)
-//line gititemcommit.qtpl:9
+//line gititemcommit.qtpl:10
qt422016.ReleaseByteBuffer(qb422016)
-//line gititemcommit.qtpl:9
+//line gititemcommit.qtpl:10
return qs422016
-//line gititemcommit.qtpl:9
+//line gititemcommit.qtpl:10
}
-//line gititemcommit.qtpl:11
+//line gititemcommit.qtpl:12
func (g *GitItemCommitPage) StreamGitContent(qw422016 *qt422016.Writer, name, ref string) {
-//line gititemcommit.qtpl:11
+//line gititemcommit.qtpl:12
qw422016.N().S(`
<div class="event-list">
`)
-//line gititemcommit.qtpl:13
+//line gititemcommit.qtpl:14
StreamCommit(qw422016, name, g.Commit, true)
-//line gititemcommit.qtpl:13
+//line gititemcommit.qtpl:14
qw422016.N().S(`
</div>
-
-<div class="alert alert-info text-center" role="alert">
- This page is work in progress.
+<div class="code-view">
+<pre>`)
+//line gititemcommit.qtpl:17
+ qw422016.E().S(g.Diff)
+//line gititemcommit.qtpl:17
+ qw422016.N().S(`</pre>
</div>
`)
//line gititemcommit.qtpl:19