cerrado @ 998c1777afeeb44ecf6ba2e735a4d6176a9b339e

feat: Add tag and branch to log

Now it is possible to see tag and branch on the log page.
  1diff --git a/pkg/git/git.go b/pkg/git/git.go
  2index 9e7f9c9bf684606b26bc3a511e7a7c49e400cbbb..d72e56101dab92141b4dd22c55202eabc330c660 100644
  3--- a/pkg/git/git.go
  4+++ b/pkg/git/git.go
  5@@ -34,7 +34,7 @@ 		tag *object.Tag
  6 	}
  7 	CommitReference struct {
  8 		commit *object.Commit
  9-		ref    *plumbing.Reference
 10+		refs   []*plumbing.Reference
 11 	}
 12 	infoWrapper struct {
 13 		name    string
 14@@ -103,10 +103,20 @@ 	}
 15 
 16 	commitRef := &CommitReference{commit: c}
 17 	if err := iter.ForEach(func(ref *plumbing.Reference) error {
 18-		if ref.Hash() != c.Hash {
 19-			return nil
 20+		obj, err := g.repository.TagObject(ref.Hash())
 21+		switch err {
 22+		case nil:
 23+			if obj.Target == commitRef.commit.Hash {
 24+				commitRef.AddReference(ref)
 25+			}
 26+		case plumbing.ErrObjectNotFound:
 27+			if commitRef.commit.Hash == ref.Hash() {
 28+				commitRef.AddReference(ref)
 29+			}
 30+		default:
 31+			return err
 32 		}
 33-		commitRef.ref = ref
 34+
 35 		return nil
 36 	}); err != nil {
 37 		return nil, err
 38@@ -155,17 +165,26 @@ 		}
 39 	}
 40 
 41 	// new we fetch for possible tags for each commit
 42-	iter, err := g.repository.Tags()
 43+	iter, err := g.repository.References()
 44 	if err != nil {
 45 		return nil, nil, err
 46 	}
 47 
 48 	if err := iter.ForEach(func(ref *plumbing.Reference) error {
 49 		for _, c := range commitRefs {
 50-			if c.commit.Hash != ref.Hash() {
 51-				continue
 52+			obj, err := g.repository.TagObject(ref.Hash())
 53+			switch err {
 54+			case nil:
 55+				if obj.Target == c.commit.Hash {
 56+					c.AddReference(ref)
 57+				}
 58+			case plumbing.ErrObjectNotFound:
 59+				if c.commit.Hash == ref.Hash() {
 60+					c.AddReference(ref)
 61+				}
 62+			default:
 63+				return err
 64 			}
 65-			c.ref = ref
 66 		}
 67 		return nil
 68 	}); err != nil {
 69@@ -495,6 +514,18 @@ }
 70 
 71 func (c *CommitReference) Commit() *object.Commit {
 72 	return c.commit
 73+}
 74+
 75+func (c *CommitReference) HasReference() bool {
 76+	return len(c.refs) > 0
 77+}
 78+
 79+func (c *CommitReference) References() []*plumbing.Reference {
 80+	return c.refs
 81+}
 82+
 83+func (c *CommitReference) AddReference(ref *plumbing.Reference) {
 84+	c.refs = append(c.refs, ref)
 85 }
 86 
 87 func (self *tagList) Len() int {
 88diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go
 89index 33adc8d2fba2f83716533128590f7a3398e95f49..b825ea41ccf284e52a2b4ca7da91dd06670041e0 100644
 90--- a/pkg/handler/git/handler.go
 91+++ b/pkg/handler/git/handler.go
 92@@ -384,7 +384,7 @@ 	gitList := &templates.GitItemPage{
 93 		Name: name,
 94 		Ref:  ref,
 95 		GitItemBase: &templates.GitItemCommitPage{
 96-			Commit: commit.Commit(),
 97+			Commit: commit,
 98 			Diff:   code.Bytes(),
 99 		},
100 	}
101diff --git a/scss/main.scss b/scss/main.scss
102index 7a1fff2fc1aea6edaded5d02c7ce4340fa8bc9f9..51b7df79808b2f5d6be51f65d06acfbfe05f6094 100644
103--- a/scss/main.scss
104+++ b/scss/main.scss
105@@ -183,3 +183,22 @@         padding: $spacer;
106         max-width: calc(100% - calc(2 * #{$spacer}));
107     }
108 }
109+
110+.ref {
111+    padding: 2px;
112+    margin: 2px;
113+    color: white;
114+    text-decoration: none;
115+
116+    &:hover {
117+        background: #70dc70;
118+    }
119+
120+    &.branch {
121+        background: #25a525;
122+    }
123+
124+    &.tag {
125+        background: #5874e2;
126+    }
127+}
128diff --git a/templates/commit.qtpl b/templates/commit.qtpl
129index b5699f4088274a0627f56791989e82edf347fd5c..adc6f2ad6c7bcf32c6a250b5d7c0f6d060de66ac 100644
130--- a/templates/commit.qtpl
131+++ b/templates/commit.qtpl
132@@ -1,24 +1,34 @@
133+{% import "git.gabrielgio.me/cerrado/pkg/git" %}
134 {% import "git.gabrielgio.me/cerrado/pkg/humanize" %}
135-{% import "github.com/go-git/go-git/v5/plumbing/object" %}
136 
137-{% func Commit(name string, c *object.Commit, showTar bool) %}
138+{% func Commit(name string, c *git.CommitReference, showTar bool) %}
139   <div class="row event">
140     <div class="row">
141       <div class="col-md">
142-       <a title="{%s c.Hash.String() %}" href="/{%s name %}/commit/{%s c.Hash.String() %}/">{%s c.Hash.String()[0:8] %}</a> —
143-       <a title="{%s c.Committer.Email %}" href="mailto:{%s c.Author.Email %}">{%s c.Author.Name %}</a>
144+       <a title="{%s c.Commit().Hash.String() %}" href="/{%s name %}/commit/{%s c.Commit().Hash.String() %}/">{%s c.Commit().Hash.String()[0:8] %}</a> —
145+       <a title="{%s c.Commit().Committer.Email %}" href="mailto:{%s c.Commit().Author.Email %}">{%s c.Commit().Author.Name %}</a>
146+       —
147+       {% if c.HasReference() %}
148+       {% for _, r := range c.References() %}
149+       {% if r.Name().IsBranch() %}
150+       <a class="ref branch" title="{%s c.Commit().Hash.String() %}" href="/{%s name %}/tree/{%s r.Name().Short() %}/">{%s r.Name().Short() %}</a>
151+       {% else %}
152+       <a class="ref tag" title="{%s c.Commit().Hash.String() %}" href="/{%s name %}/commit/{%s c.Commit().Hash.String() %}/">{%s r.Name().Short() %}</a>
153+       {% endif %}
154+       {% endfor %}
155+       {%endif%}
156       </div>
157       {% if showTar %}
158       <div class="col-md text-md-center">
159-       <a title="tar.gz for {%s c.Hash.String() %}" href="/{%s name %}/archive/{%s c.Hash.String() %}.tar.gz">tar.gz</a>
160+       <a title="tar.gz for {%s c.Commit().Hash.String() %}" href="/{%s name %}/archive/{%s c.Commit().Hash.String() %}.tar.gz">tar.gz</a>
161       </div>
162       {% endif %}
163       <div class="col-md text-md-end">
164-        <a title="{%s c.Author.When.UTC().Format("2006-01-02 15:04:05")%} UTC">{%s humanize.Time(c.Author.When) %}</a>
165+        <a title="{%s c.Commit().Author.When.UTC().Format("2006-01-02 15:04:05")%} UTC">{%s humanize.Time(c.Commit().Author.When) %}</a>
166       </div>
167     </div>
168     <div class="code-view">
169-     <pre>{%s c.Message %}</pre>
170+     <pre>{%s c.Commit().Message %}</pre>
171     </div>
172   </div>
173 {% endfunc %}
174diff --git a/templates/commit.qtpl.go b/templates/commit.qtpl.go
175index 75ae89970d6b150a4d6cbd7801e77e7075f34520..fda993d1047ed1f973fd50fba3444633c0109c0e 100644
176--- a/templates/commit.qtpl.go
177+++ b/templates/commit.qtpl.go
178@@ -5,10 +5,10 @@ //line templates/commit.qtpl:1
179 package templates
180 
181 //line templates/commit.qtpl:1
182-import "git.gabrielgio.me/cerrado/pkg/humanize"
183+import "git.gabrielgio.me/cerrado/pkg/git"
184 
185 //line templates/commit.qtpl:2
186-import "github.com/go-git/go-git/v5/plumbing/object"
187+import "git.gabrielgio.me/cerrado/pkg/humanize"
188 
189 //line templates/commit.qtpl:4
190 import (
191@@ -24,7 +24,7 @@ 	_ = qt422016.AcquireByteBuffer
192 )
193 
194 //line templates/commit.qtpl:4
195-func StreamCommit(qw422016 *qt422016.Writer, name string, c *object.Commit, showTar bool) {
196+func StreamCommit(qw422016 *qt422016.Writer, name string, c *git.CommitReference, showTar bool) {
197 //line templates/commit.qtpl:4
198 	qw422016.N().S(`
199   <div class="row event">
200@@ -32,7 +32,7 @@     <div class="row">
201       <div class="col-md">
202        <a title="`)
203 //line templates/commit.qtpl:8
204-	qw422016.E().S(c.Hash.String())
205+	qw422016.E().S(c.Commit().Hash.String())
206 //line templates/commit.qtpl:8
207 	qw422016.N().S(`" href="/`)
208 //line templates/commit.qtpl:8
209@@ -40,98 +40,168 @@ 	qw422016.E().S(name)
210 //line templates/commit.qtpl:8
211 	qw422016.N().S(`/commit/`)
212 //line templates/commit.qtpl:8
213-	qw422016.E().S(c.Hash.String())
214+	qw422016.E().S(c.Commit().Hash.String())
215 //line templates/commit.qtpl:8
216 	qw422016.N().S(`/">`)
217 //line templates/commit.qtpl:8
218-	qw422016.E().S(c.Hash.String()[0:8])
219+	qw422016.E().S(c.Commit().Hash.String()[0:8])
220 //line templates/commit.qtpl:8
221 	qw422016.N().S(`</a> —
222        <a title="`)
223 //line templates/commit.qtpl:9
224-	qw422016.E().S(c.Committer.Email)
225+	qw422016.E().S(c.Commit().Committer.Email)
226 //line templates/commit.qtpl:9
227 	qw422016.N().S(`" href="mailto:`)
228 //line templates/commit.qtpl:9
229-	qw422016.E().S(c.Author.Email)
230+	qw422016.E().S(c.Commit().Author.Email)
231 //line templates/commit.qtpl:9
232 	qw422016.N().S(`">`)
233 //line templates/commit.qtpl:9
234-	qw422016.E().S(c.Author.Name)
235+	qw422016.E().S(c.Commit().Author.Name)
236 //line templates/commit.qtpl:9
237 	qw422016.N().S(`</a>
238+       —
239+       `)
240+//line templates/commit.qtpl:11
241+	if c.HasReference() {
242+//line templates/commit.qtpl:11
243+		qw422016.N().S(`
244+       `)
245+//line templates/commit.qtpl:12
246+		for _, r := range c.References() {
247+//line templates/commit.qtpl:12
248+			qw422016.N().S(`
249+       `)
250+//line templates/commit.qtpl:13
251+			if r.Name().IsBranch() {
252+//line templates/commit.qtpl:13
253+				qw422016.N().S(`
254+       <a class="ref branch" title="`)
255+//line templates/commit.qtpl:14
256+				qw422016.E().S(c.Commit().Hash.String())
257+//line templates/commit.qtpl:14
258+				qw422016.N().S(`" href="/`)
259+//line templates/commit.qtpl:14
260+				qw422016.E().S(name)
261+//line templates/commit.qtpl:14
262+				qw422016.N().S(`/tree/`)
263+//line templates/commit.qtpl:14
264+				qw422016.E().S(r.Name().Short())
265+//line templates/commit.qtpl:14
266+				qw422016.N().S(`/">`)
267+//line templates/commit.qtpl:14
268+				qw422016.E().S(r.Name().Short())
269+//line templates/commit.qtpl:14
270+				qw422016.N().S(`</a>
271+       `)
272+//line templates/commit.qtpl:15
273+			} else {
274+//line templates/commit.qtpl:15
275+				qw422016.N().S(`
276+       <a class="ref tag" title="`)
277+//line templates/commit.qtpl:16
278+				qw422016.E().S(c.Commit().Hash.String())
279+//line templates/commit.qtpl:16
280+				qw422016.N().S(`" href="/`)
281+//line templates/commit.qtpl:16
282+				qw422016.E().S(name)
283+//line templates/commit.qtpl:16
284+				qw422016.N().S(`/commit/`)
285+//line templates/commit.qtpl:16
286+				qw422016.E().S(c.Commit().Hash.String())
287+//line templates/commit.qtpl:16
288+				qw422016.N().S(`/">`)
289+//line templates/commit.qtpl:16
290+				qw422016.E().S(r.Name().Short())
291+//line templates/commit.qtpl:16
292+				qw422016.N().S(`</a>
293+       `)
294+//line templates/commit.qtpl:17
295+			}
296+//line templates/commit.qtpl:17
297+			qw422016.N().S(`
298+       `)
299+//line templates/commit.qtpl:18
300+		}
301+//line templates/commit.qtpl:18
302+		qw422016.N().S(`
303+       `)
304+//line templates/commit.qtpl:19
305+	}
306+//line templates/commit.qtpl:19
307+	qw422016.N().S(`
308       </div>
309       `)
310-//line templates/commit.qtpl:11
311+//line templates/commit.qtpl:21
312 	if showTar {
313-//line templates/commit.qtpl:11
314+//line templates/commit.qtpl:21
315 		qw422016.N().S(`
316       <div class="col-md text-md-center">
317        <a title="tar.gz for `)
318-//line templates/commit.qtpl:13
319-		qw422016.E().S(c.Hash.String())
320-//line templates/commit.qtpl:13
321+//line templates/commit.qtpl:23
322+		qw422016.E().S(c.Commit().Hash.String())
323+//line templates/commit.qtpl:23
324 		qw422016.N().S(`" href="/`)
325-//line templates/commit.qtpl:13
326+//line templates/commit.qtpl:23
327 		qw422016.E().S(name)
328-//line templates/commit.qtpl:13
329+//line templates/commit.qtpl:23
330 		qw422016.N().S(`/archive/`)
331-//line templates/commit.qtpl:13
332-		qw422016.E().S(c.Hash.String())
333-//line templates/commit.qtpl:13
334+//line templates/commit.qtpl:23
335+		qw422016.E().S(c.Commit().Hash.String())
336+//line templates/commit.qtpl:23
337 		qw422016.N().S(`.tar.gz">tar.gz</a>
338       </div>
339       `)
340-//line templates/commit.qtpl:15
341+//line templates/commit.qtpl:25
342 	}
343-//line templates/commit.qtpl:15
344+//line templates/commit.qtpl:25
345 	qw422016.N().S(`
346       <div class="col-md text-md-end">
347         <a title="`)
348-//line templates/commit.qtpl:17
349-	qw422016.E().S(c.Author.When.UTC().Format("2006-01-02 15:04:05"))
350-//line templates/commit.qtpl:17
351+//line templates/commit.qtpl:27
352+	qw422016.E().S(c.Commit().Author.When.UTC().Format("2006-01-02 15:04:05"))
353+//line templates/commit.qtpl:27
354 	qw422016.N().S(` UTC">`)
355-//line templates/commit.qtpl:17
356-	qw422016.E().S(humanize.Time(c.Author.When))
357-//line templates/commit.qtpl:17
358+//line templates/commit.qtpl:27
359+	qw422016.E().S(humanize.Time(c.Commit().Author.When))
360+//line templates/commit.qtpl:27
361 	qw422016.N().S(`</a>
362       </div>
363     </div>
364     <div class="code-view">
365      <pre>`)
366-//line templates/commit.qtpl:21
367-	qw422016.E().S(c.Message)
368-//line templates/commit.qtpl:21
369+//line templates/commit.qtpl:31
370+	qw422016.E().S(c.Commit().Message)
371+//line templates/commit.qtpl:31
372 	qw422016.N().S(`</pre>
373     </div>
374   </div>
375 `)
376-//line templates/commit.qtpl:24
377+//line templates/commit.qtpl:34
378 }
379 
380-//line templates/commit.qtpl:24
381-func WriteCommit(qq422016 qtio422016.Writer, name string, c *object.Commit, showTar bool) {
382-//line templates/commit.qtpl:24
383+//line templates/commit.qtpl:34
384+func WriteCommit(qq422016 qtio422016.Writer, name string, c *git.CommitReference, showTar bool) {
385+//line templates/commit.qtpl:34
386 	qw422016 := qt422016.AcquireWriter(qq422016)
387-//line templates/commit.qtpl:24
388+//line templates/commit.qtpl:34
389 	StreamCommit(qw422016, name, c, showTar)
390-//line templates/commit.qtpl:24
391+//line templates/commit.qtpl:34
392 	qt422016.ReleaseWriter(qw422016)
393-//line templates/commit.qtpl:24
394+//line templates/commit.qtpl:34
395 }
396 
397-//line templates/commit.qtpl:24
398-func Commit(name string, c *object.Commit, showTar bool) string {
399-//line templates/commit.qtpl:24
400+//line templates/commit.qtpl:34
401+func Commit(name string, c *git.CommitReference, showTar bool) string {
402+//line templates/commit.qtpl:34
403 	qb422016 := qt422016.AcquireByteBuffer()
404-//line templates/commit.qtpl:24
405+//line templates/commit.qtpl:34
406 	WriteCommit(qb422016, name, c, showTar)
407-//line templates/commit.qtpl:24
408+//line templates/commit.qtpl:34
409 	qs422016 := string(qb422016.B)
410-//line templates/commit.qtpl:24
411+//line templates/commit.qtpl:34
412 	qt422016.ReleaseByteBuffer(qb422016)
413-//line templates/commit.qtpl:24
414+//line templates/commit.qtpl:34
415 	return qs422016
416-//line templates/commit.qtpl:24
417+//line templates/commit.qtpl:34
418 }
419diff --git a/templates/gititemcommit.qtpl b/templates/gititemcommit.qtpl
420index ec67757ca324adb576499f3c9f63b29ad647f110..7de1bdbf1dc5ba9373bae07d3ad904f55792449d 100644
421--- a/templates/gititemcommit.qtpl
422+++ b/templates/gititemcommit.qtpl
423@@ -1,8 +1,8 @@
424-{% import "github.com/go-git/go-git/v5/plumbing/object" %}
425+{% import "git.gabrielgio.me/cerrado/pkg/git" %}
426 
427 {% code
428 type GitItemCommitPage struct {
429-    Commit *object.Commit
430+    Commit *git.CommitReference
431     Diff []byte
432 }
433 %}
434diff --git a/templates/gititemcommit.qtpl.go b/templates/gititemcommit.qtpl.go
435index 7e458f31feba2b674f8d16d2f06e84f4beecfa3d..b7902204c1b373b7d0b97a04f05e4092fdc7a072 100644
436--- a/templates/gititemcommit.qtpl.go
437+++ b/templates/gititemcommit.qtpl.go
438@@ -5,7 +5,7 @@ //line templates/gititemcommit.qtpl:1
439 package templates
440 
441 //line templates/gititemcommit.qtpl:1
442-import "github.com/go-git/go-git/v5/plumbing/object"
443+import "git.gabrielgio.me/cerrado/pkg/git"
444 
445 //line templates/gititemcommit.qtpl:3
446 import (
447@@ -22,7 +22,7 @@ )
448 
449 //line templates/gititemcommit.qtpl:4
450 type GitItemCommitPage struct {
451-	Commit *object.Commit
452+	Commit *git.CommitReference
453 	Diff   []byte
454 }
455 
456diff --git a/templates/gititemlog.qtpl b/templates/gititemlog.qtpl
457index 93efb24c6d716454864985c84523db4ffabd7556..391de533a99393a07da13cf95c07e09d570e0e09 100644
458--- a/templates/gititemlog.qtpl
459+++ b/templates/gititemlog.qtpl
460@@ -13,7 +13,7 @@
461 {% func (g *GitItemLogPage) GitContent(name, ref string) %}
462 <div class="event-list">
463   {% for _, c := range g.Commits %}
464-  {%= Commit(name, c.Commit(), false) %}
465+  {%= Commit(name, c, false) %}
466   {% endfor %}
467   {% if g.Next != nil %}
468   <a href="/{%s name %}/log/{%s ref %}/?from={%s g.Next.Hash.String() %}"  class="btn btn-primary">Next</a>
469diff --git a/templates/gititemlog.qtpl.go b/templates/gititemlog.qtpl.go
470index 05950ed0c7d1a33d204b17fced89a60ff5381892..2c9ce93d6c0d2afd0d24e3368f86899123c88783 100644
471--- a/templates/gititemlog.qtpl.go
472+++ b/templates/gititemlog.qtpl.go
473@@ -74,7 +74,7 @@ //line templates/gititemlog.qtpl:15
474 		qw422016.N().S(`
475   `)
476 //line templates/gititemlog.qtpl:16
477-		StreamCommit(qw422016, name, c.Commit(), false)
478+		StreamCommit(qw422016, name, c, false)
479 //line templates/gititemlog.qtpl:16
480 		qw422016.N().S(`
481   `)
482diff --git a/templates/gititemsummary.qtpl b/templates/gititemsummary.qtpl
483index 5d0768026b0ff97fd293904dbdffb9f449fb3fa5..e00c37d1d7d52dbbd1e6bbe4fb975838684ed57a 100644
484--- a/templates/gititemsummary.qtpl
485+++ b/templates/gititemsummary.qtpl
486@@ -38,7 +38,7 @@ </div>
487 <div class="row">
488   <div class="event-list">
489     {% for _, c := range g.Commits %}
490-    {%= Commit(name, c.Commit(), false) %}
491+    {%= Commit(name, c, false) %}
492     {% endfor %}
493   </div>
494 </div>
495diff --git a/templates/gititemsummary.qtpl.go b/templates/gititemsummary.qtpl.go
496index e17ec7c4defe8706cb17a0258a62f0f80fba8d9a..0a78258f8e8854a3eaa4d4d34a9e229642045383 100644
497--- a/templates/gititemsummary.qtpl.go
498+++ b/templates/gititemsummary.qtpl.go
499@@ -139,7 +139,7 @@ //line templates/gititemsummary.qtpl:40
500 		qw422016.N().S(`
501     `)
502 //line templates/gititemsummary.qtpl:41
503-		StreamCommit(qw422016, name, c.Commit(), false)
504+		StreamCommit(qw422016, name, c, false)
505 //line templates/gititemsummary.qtpl:41
506 		qw422016.N().S(`
507     `)