diff options
author | Koni Marti <koni.marti@gmail.com> | 2023-11-24 16:02:57 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-12-30 15:42:09 +0100 |
commit | 724d3a22cdaae3299df23195788ccfad7e332db8 (patch) | |
tree | 842a74f0b2a9b875b5c73b41cb03754c4b92b6ab /lib/pama/models/commit.go | |
parent | 60c1c7347ad88ad00fc0809acd10ddd31243acd3 (diff) | |
download | aerc-724d3a22cdaae3299df23195788ccfad7e332db8.tar.gz |
pama: define the entity models
Define the entity models for the patch management. Add a Project and
Commit struct and implement the Stringer interface for both.
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/pama/models/commit.go')
-rw-r--r-- | lib/pama/models/commit.go | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/lib/pama/models/commit.go b/lib/pama/models/commit.go new file mode 100644 index 00000000..4cd06b4b --- /dev/null +++ b/lib/pama/models/commit.go @@ -0,0 +1,93 @@ +package models + +import ( + "fmt" + "strings" +) + +const ( + Untracked = "untracked" +) + +func NewCommit(r RevisionController, id, tag string) Commit { + return Commit{ + ID: id, + Subject: r.Subject(id), + Author: r.Author(id), + Date: r.Date(id), + MessageId: "", + Tag: tag, + } +} + +func (c Commit) Untracked() bool { + return c.Tag == Untracked +} + +func (c Commit) Info() string { + s := []string{} + if c.Subject == "" { + s = append(s, "(no subject)") + } else { + s = append(s, c.Subject) + } + if c.Author != "" { + s = append(s, c.Author) + } + if c.Date != "" { + s = append(s, c.Date) + } + if c.MessageId != "" { + s = append(s, "<"+c.MessageId+">") + } + return strings.Join(s, ", ") +} + +func (c Commit) String() string { + return fmt.Sprintf("%-6.6s %s", c.ID, c.Info()) +} + +type Commits []Commit + +func (h Commits) Tags() []string { + var tags []string + dedup := make(map[string]struct{}) + for _, c := range h { + _, ok := dedup[c.Tag] + if ok { + continue + } + tags = append(tags, c.Tag) + dedup[c.Tag] = struct{}{} + } + return tags +} + +func (h Commits) HasTag(t string) bool { + for _, c := range h { + if c.Tag == t { + return true + } + } + return false +} + +func (h Commits) Lookup(id string) (Commit, bool) { + for _, c := range h { + if c.ID == id { + return c, true + } + } + return Commit{}, false +} + +type CommitIDs []string + +func (c CommitIDs) Has(id string) bool { + for _, cid := range c { + if cid == id { + return true + } + } + return false +} |