| 1 |
package models |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"slices" |
| 6 |
"strings" |
| 7 |
) |
| 8 |
|
| 9 |
const ( |
| 10 |
Untracked = "untracked" |
| 11 |
) |
| 12 |
|
| 13 |
func NewCommit(r RevisionController, id, tag string) Commit { |
| 14 |
return Commit{ |
| 15 |
ID: id, |
| 16 |
Subject: r.Subject(id), |
| 17 |
Author: r.Author(id), |
| 18 |
Date: r.Date(id), |
| 19 |
MessageId: "", |
| 20 |
Tag: tag, |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
func (c Commit) Untracked() bool { |
| 25 |
return c.Tag == Untracked |
| 26 |
} |
| 27 |
|
| 28 |
func (c Commit) Info() string { |
| 29 |
s := []string{} |
| 30 |
if c.Subject == "" { |
| 31 |
s = append(s, "(no subject)") |
| 32 |
} else { |
| 33 |
s = append(s, c.Subject) |
| 34 |
} |
| 35 |
if c.Author != "" { |
| 36 |
s = append(s, c.Author) |
| 37 |
} |
| 38 |
if c.Date != "" { |
| 39 |
s = append(s, c.Date) |
| 40 |
} |
| 41 |
if c.MessageId != "" { |
| 42 |
s = append(s, "<"+c.MessageId+">") |
| 43 |
} |
| 44 |
return strings.Join(s, ", ") |
| 45 |
} |
| 46 |
|
| 47 |
func (c Commit) String() string { |
| 48 |
return fmt.Sprintf("%-6.6s %s", c.ID, c.Info()) |
| 49 |
} |
| 50 |
|
| 51 |
type Commits []Commit |
| 52 |
|
| 53 |
func (h Commits) Tags() []string { |
| 54 |
var tags []string |
| 55 |
dedup := make(map[string]struct{}) |
| 56 |
for _, c := range h { |
| 57 |
_, ok := dedup[c.Tag] |
| 58 |
if ok { |
| 59 |
continue |
| 60 |
} |
| 61 |
tags = append(tags, c.Tag) |
| 62 |
dedup[c.Tag] = struct{}{} |
| 63 |
} |
| 64 |
return tags |
| 65 |
} |
| 66 |
|
| 67 |
func (h Commits) HasTag(t string) bool { |
| 68 |
for _, c := range h { |
| 69 |
if c.Tag == t { |
| 70 |
return true |
| 71 |
} |
| 72 |
} |
| 73 |
return false |
| 74 |
} |
| 75 |
|
| 76 |
func (h Commits) Lookup(id string) (Commit, bool) { |
| 77 |
for _, c := range h { |
| 78 |
if c.ID == id { |
| 79 |
return c, true |
| 80 |
} |
| 81 |
} |
| 82 |
return Commit{}, false |
| 83 |
} |
| 84 |
|
| 85 |
type CommitIDs []string |
| 86 |
|
| 87 |
func (c CommitIDs) Has(id string) bool { |
| 88 |
return slices.Contains(c, id) |
| 89 |
} |