Debian Codegraph @

lib/pama/rebase.go

raw
1 package pama
2
3 import (
4 "fmt"
5
6 "git.sr.ht/~rjarry/aerc/lib/pama/models"
7 )
8
9 // RebaseCommits fetches the commits between baseID and HEAD. The tags from the
10 // current project will be mapped onto the fetched commits based on either the
11 // commit hash or the commit subject.
12 func (m PatchManager) RebaseCommits(p models.Project, baseID string) ([]models.Commit, error) {
13 rc, err := m.rc(p.RevctrlID, p.Root)
14 if err != nil {
15 return nil, revErr(err)
16 }
17
18 if !rc.Exists(baseID) {
19 return nil, fmt.Errorf("cannot rebase on %s. "+
20 "commit does not exist", baseID)
21 }
22
23 commitIDs, err := rc.History(baseID)
24 if err != nil {
25 return nil, err
26 }
27
28 commits := make([]models.Commit, len(commitIDs))
29 for i := range commitIDs {
30 commits[i] = models.NewCommit(
31 rc,
32 commitIDs[i],
33 models.Untracked,
34 )
35 }
36
37 // map tags from the commits from the project p
38 for i, r := range commits {
39 for _, c := range p.Commits {
40 if c.ID == r.ID || c.Subject == r.Subject {
41 commits[i].MessageId = c.MessageId
42 commits[i].Tag = c.Tag
43 break
44 }
45 }
46 }
47
48 return commits, nil
49 }
50
51 // SaveRebased checks if the commits actually exist in the repo, repopulate the
52 // info fields and saves the baseID for project p.
53 func (m PatchManager) SaveRebased(p models.Project, baseID string, commits []models.Commit) error {
54 rc, err := m.rc(p.RevctrlID, p.Root)
55 if err != nil {
56 return revErr(err)
57 }
58
59 exist := make([]models.Commit, 0, len(commits))
60 for _, c := range commits {
61 if !rc.Exists(c.ID) {
62 continue
63 }
64 exist = append(exist, c)
65 }
66
67 for i, c := range exist {
68 exist[i].Subject = rc.Subject(c.ID)
69 exist[i].Author = rc.Author(c.ID)
70 exist[i].Date = rc.Date(c.ID)
71 }
72
73 p.Commits = exist
74
75 if rc.Exists(baseID) {
76 p.Base = models.NewCommit(rc, baseID, "")
77 }
78
79 err = m.store().StoreProject(p, true)
80 return storeErr(err)
81 }