| 1 | package rebase |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | |
| 8 | "the-valley/change" |
| 9 | ) |
| 10 | |
| 11 | // A change's restack report, read back: the latest report the host |
| 12 | // restacker wrote about the change, at RestackPrefix<change ID> |
| 13 | // (change/report.go), and whether it is current. |
| 14 | // |
| 15 | // A report is current only while every input it observed still holds on |
| 16 | // origin: main's tip, and for each layer its record commit and the head of |
| 17 | // its branch. A branch can move or disappear while its record and main |
| 18 | // stay put, so every layer's branch is compared, not only the record. For |
| 19 | // a layer the attempt published, the head compared is the one it moved the |
| 20 | // branch to, and the record compared is the one that recorded that head, |
| 21 | // when it added a version. A report that is not current is historical: it |
| 22 | // describes inputs that have moved since. |
| 23 | |
| 24 | // Report is a change's latest restack report. |
| 25 | type Report struct { |
| 26 | Ref string // the report's ref |
| 27 | Commit string // the report commit the ref names |
| 28 | Report *change.RestackReport // what it says |
| 29 | Current bool // whether every input it observed still holds |
| 30 | } |
| 31 | |
| 32 | // ReadReport reads the report a report commit holds. The commit must have |
| 33 | // been fetched. |
| 34 | func ReadReport(ctx context.Context, g Git, commit string) (*change.RestackReport, error) { |
| 35 | data, err := g.output(ctx, true, "cat-file", "blob", commit+":report.json") |
| 36 | if err != nil { |
| 37 | return nil, fmt.Errorf("the restack report %s holds no report.json", short(commit)) |
| 38 | } |
| 39 | r, err := change.ParseRestackReport([]byte(data + "\n")) |
| 40 | if err != nil { |
| 41 | return nil, fmt.Errorf("the restack report %s does not read: %v", short(commit), err) |
| 42 | } |
| 43 | return r, nil |
| 44 | } |
| 45 | |
| 46 | // Current reports whether every input r observed still holds in o. |
| 47 | func (o Origin) Current(r *change.RestackReport) bool { |
| 48 | if r.Target != Target || o.Refs["refs/heads/"+Target] != strings.TrimPrefix(r.TargetTip, change.HeadPrefix) { |
| 49 | return false |
| 50 | } |
| 51 | for _, l := range r.Layers { |
| 52 | head, record := l.Head, l.Record |
| 53 | if p := l.Published; p != nil { |
| 54 | head = p.Head |
| 55 | if p.Record != nil { |
| 56 | record = *p.Record |
| 57 | } |
| 58 | } |
| 59 | if o.Refs[change.Ref(Target, l.ChangeID)] != strings.TrimPrefix(record, change.HeadPrefix) || |
| 60 | o.Refs["refs/heads/"+l.Branch] != strings.TrimPrefix(head, change.HeadPrefix) { |
| 61 | return false |
| 62 | } |
| 63 | } |
| 64 | return true |
| 65 | } |
| 66 | |
| 67 | // Report is the restack report origin holds for a change, read once per |
| 68 | // snapshot. found is false when origin holds none, or holds one that does |
| 69 | // not read or is about another change, which no reader shows: the hook |
| 70 | // refuses such a report, so only corrupt storage holds one. |
| 71 | func (s *Snapshot) Report(id string) (r Report, found bool) { |
| 72 | if r, ok := s.reports[id]; ok { |
| 73 | return r, r.Report != nil |
| 74 | } |
| 75 | r = Report{Ref: RestackPrefix + id, Commit: s.Refs[RestackPrefix+id]} |
| 76 | if r.Commit != "" { |
| 77 | if rep, err := ReadReport(s.ctx, s.g, r.Commit); err == nil && rep.ChangeID == id { |
| 78 | r.Report, r.Current = rep, s.Origin.Current(rep) |
| 79 | } |
| 80 | } |
| 81 | s.reports[id] = r |
| 82 | return r, r.Report != nil |
| 83 | } |