cerrado @ 60004cfb97fe0eb64d1c8310e7c9caae96b8adbe

feat: Add diff view

Adds a very simple diff view for a commit
  1diff --git a/README.md b/README.md
  2index 280172b73e70163ee5d07ee848ca0648b708de52..f6e202a381dc90562512cd33264c39478b7b90da 100644
  3--- a/README.md
  4+++ b/README.md
  5@@ -23,7 +23,7 @@ To run the project you just need to do a make run.
  6 
  7 ### TODO
  8 
  9-- Add patch to the commit page
 10+- Impove diff display
 11 - Add log pagination
 12 - Fix submodule link on tree view
 13 
 14diff --git a/pkg/git/git.go b/pkg/git/git.go
 15index a9c42ce0dd57fd28cb5824cad29dc85988209612..6221e33dc5d8e7aca9682f8d22c830225b1e306b 100644
 16--- a/pkg/git/git.go
 17+++ b/pkg/git/git.go
 18@@ -179,6 +179,45 @@
 19 	return branches, nil
 20 }
 21 
 22+func (g *GitRepository) Diff() (string, error) {
 23+	err := g.validateRef()
 24+	if err != nil {
 25+		return "", err
 26+	}
 27+
 28+	c, err := g.repository.CommitObject(g.ref)
 29+	if err != nil {
 30+		return "", err
 31+	}
 32+
 33+	commitTree, err := c.Tree()
 34+	if err != nil {
 35+		return "", err
 36+	}
 37+
 38+	patch := &object.Patch{}
 39+	parentTree := &object.Tree{}
 40+	if c.NumParents() != 0 {
 41+		parent, err := c.Parents().Next()
 42+		if err == nil {
 43+			parentTree, err = parent.Tree()
 44+			if err == nil {
 45+				patch, err = parentTree.Patch(commitTree)
 46+				if err != nil {
 47+					return "", err
 48+				}
 49+			}
 50+		}
 51+	} else {
 52+		patch, err = parentTree.Patch(commitTree)
 53+		if err != nil {
 54+			return "", err
 55+		}
 56+	}
 57+
 58+	return patch.String(), nil
 59+}
 60+
 61 func (g *GitRepository) Tree(path string) (*object.Tree, error) {
 62 	err := g.validateRef()
 63 	if err != nil {
 64diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go
 65index 2ddc5f17b701a67542417653429da090f58e7b6a..40fae24a3f06fd27812819850d7f6e118ca78ef7 100644
 66--- a/pkg/handler/git/handler.go
 67+++ b/pkg/handler/git/handler.go
 68@@ -340,11 +340,17 @@ 	if err != nil {
 69 		return err
 70 	}
 71 
 72+	diff, err := g.gitService.Diff(name, ref)
 73+	if err != nil {
 74+		return err
 75+	}
 76+
 77 	gitList := &templates.GitItemPage{
 78 		Name: name,
 79 		Ref:  ref,
 80 		GitItemBase: &templates.GitItemCommitPage{
 81 			Commit: commit,
 82+			Diff:   diff,
 83 		},
 84 	}
 85 	templates.WritePageTemplate(w, gitList)
 86diff --git a/pkg/service/git.go b/pkg/service/git.go
 87index b368f0cc321b64d593bd2399e86c5de8d43c77f7..f03ba426f3933a7bf8f1a9f1ff15e6b88613954a 100644
 88--- a/pkg/service/git.go
 89+++ b/pkg/service/git.go
 90@@ -140,6 +140,25 @@
 91 	return nil
 92 }
 93 
 94+func (g *GitService) Diff(name, ref string) (string, error) {
 95+	r := g.configRepo.GetByName(name)
 96+	if r == nil {
 97+		return "", ErrRepositoryNotFound
 98+	}
 99+
100+	repo, err := git.OpenRepository(r.Path)
101+	if err != nil {
102+		return "", err
103+	}
104+
105+	err = repo.SetRef(ref)
106+	if err != nil {
107+		return "", err
108+	}
109+
110+	return repo.Diff()
111+}
112+
113 func (g *GitService) GetTree(name, ref, path string) (*object.Tree, error) {
114 	r := g.configRepo.GetByName(name)
115 	if r == nil {
116diff --git a/templates/gititemcommit.qtpl b/templates/gititemcommit.qtpl
117index 77536f1d45da568c3317b42096a8628367beb5ac..d223315dd0c8ffafe05139e1e21fd3f121dc78c9 100644
118--- a/templates/gititemcommit.qtpl
119+++ b/templates/gititemcommit.qtpl
120@@ -3,6 +3,7 @@
121 {% code
122 type GitItemCommitPage struct {
123     Commit *object.Commit
124+    Diff string
125 }
126 %}
127 
128@@ -12,8 +13,7 @@ {% func (g *GitItemCommitPage) GitContent(name, ref string) %}
129 <div class="event-list">
130   {%= Commit(name, g.Commit, true) %}
131 </div>
132-
133-<div class="alert alert-info text-center" role="alert">
134-    This page is work in progress.
135+<div class="code-view">
136+<pre>{%s g.Diff %}</pre>
137 </div>
138 {% endfunc %}
139diff --git a/templates/gititemcommit.qtpl.go b/templates/gititemcommit.qtpl.go
140index f94a1c2847b72a3a8b92fac0139b465c364671d3..39348ee98773d0c5aa684e0b804bd24bf88f9516 100644
141--- a/templates/gititemcommit.qtpl.go
142+++ b/templates/gititemcommit.qtpl.go
143@@ -23,55 +23,59 @@
144 //line gititemcommit.qtpl:4
145 type GitItemCommitPage struct {
146 	Commit *object.Commit
147+	Diff   string
148 }
149 
150-//line gititemcommit.qtpl:9
151+//line gititemcommit.qtpl:10
152 func (g *GitItemCommitPage) StreamNav(qw422016 *qt422016.Writer, name, ref string) {
153-//line gititemcommit.qtpl:9
154+//line gititemcommit.qtpl:10
155 	StreamGitItemNav(qw422016, name, ref, Log)
156-//line gititemcommit.qtpl:9
157+//line gititemcommit.qtpl:10
158 }
159 
160-//line gititemcommit.qtpl:9
161+//line gititemcommit.qtpl:10
162 func (g *GitItemCommitPage) WriteNav(qq422016 qtio422016.Writer, name, ref string) {
163-//line gititemcommit.qtpl:9
164+//line gititemcommit.qtpl:10
165 	qw422016 := qt422016.AcquireWriter(qq422016)
166-//line gititemcommit.qtpl:9
167+//line gititemcommit.qtpl:10
168 	g.StreamNav(qw422016, name, ref)
169-//line gititemcommit.qtpl:9
170+//line gititemcommit.qtpl:10
171 	qt422016.ReleaseWriter(qw422016)
172-//line gititemcommit.qtpl:9
173+//line gititemcommit.qtpl:10
174 }
175 
176-//line gititemcommit.qtpl:9
177+//line gititemcommit.qtpl:10
178 func (g *GitItemCommitPage) Nav(name, ref string) string {
179-//line gititemcommit.qtpl:9
180+//line gititemcommit.qtpl:10
181 	qb422016 := qt422016.AcquireByteBuffer()
182-//line gititemcommit.qtpl:9
183+//line gititemcommit.qtpl:10
184 	g.WriteNav(qb422016, name, ref)
185-//line gititemcommit.qtpl:9
186+//line gititemcommit.qtpl:10
187 	qs422016 := string(qb422016.B)
188-//line gititemcommit.qtpl:9
189+//line gititemcommit.qtpl:10
190 	qt422016.ReleaseByteBuffer(qb422016)
191-//line gititemcommit.qtpl:9
192+//line gititemcommit.qtpl:10
193 	return qs422016
194-//line gititemcommit.qtpl:9
195+//line gititemcommit.qtpl:10
196 }
197 
198-//line gititemcommit.qtpl:11
199+//line gititemcommit.qtpl:12
200 func (g *GitItemCommitPage) StreamGitContent(qw422016 *qt422016.Writer, name, ref string) {
201-//line gititemcommit.qtpl:11
202+//line gititemcommit.qtpl:12
203 	qw422016.N().S(`
204 <div class="event-list">
205   `)
206-//line gititemcommit.qtpl:13
207+//line gititemcommit.qtpl:14
208 	StreamCommit(qw422016, name, g.Commit, true)
209-//line gititemcommit.qtpl:13
210+//line gititemcommit.qtpl:14
211 	qw422016.N().S(`
212 </div>
213-
214-<div class="alert alert-info text-center" role="alert">
215-    This page is work in progress.
216+<div class="code-view">
217+<pre>`)
218+//line gititemcommit.qtpl:17
219+	qw422016.E().S(g.Diff)
220+//line gititemcommit.qtpl:17
221+	qw422016.N().S(`</pre>
222 </div>
223 `)
224 //line gititemcommit.qtpl:19