| 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 ref it observed still holds on |
| 16 | // origin what the report says it held: |
| 17 | // |
| 18 | // - main, at the report's target_tip; |
| 19 | // - each layer's record ref, at the record commit it read; |
| 20 | // - each layer's branch, at its branch_tip, or still absent when |
| 21 | // branch_tip is null. A layer that names no branch has none to compare; |
| 22 | // - each blocking request, at the tip it read. |
| 23 | // |
| 24 | // For a layer the attempt published, the branch is compared with the head |
| 25 | // the attempt moved it to, and the record with the one that recorded that |
| 26 | // head, when it added a version. |
| 27 | // |
| 28 | // Each comparison is with what the report recorded of the ref itself, |
| 29 | // never with the version's head: a branch can be missing, or somewhere |
| 30 | // other than the head its version names. Absent stays absent, so a report |
| 31 | // that observed a missing branch is historical once the branch exists. A |
| 32 | // report that is not current is historical: it describes refs that have |
| 33 | // moved since. |
| 34 | |
| 35 | // Report is a change's latest restack report. |
| 36 | type Report struct { |
| 37 | Ref string // the report's ref |
| 38 | Commit string // the report commit the ref names |
| 39 | Report *change.RestackReport // what it says |
| 40 | Current bool // whether every input it observed still holds |
| 41 | } |
| 42 | |
| 43 | // ReadReport reads the report a report commit holds. The commit must have |
| 44 | // been fetched. |
| 45 | func ReadReport(ctx context.Context, g Git, commit string) (*change.RestackReport, error) { |
| 46 | data, err := g.output(ctx, true, "cat-file", "blob", commit+":report.json") |
| 47 | if err != nil { |
| 48 | return nil, fmt.Errorf("the restack report %s holds no report.json", short(commit)) |
| 49 | } |
| 50 | r, err := change.ParseRestackReport([]byte(data + "\n")) |
| 51 | if err != nil { |
| 52 | return nil, fmt.Errorf("the restack report %s does not read: %v", short(commit), err) |
| 53 | } |
| 54 | return r, nil |
| 55 | } |
| 56 | |
| 57 | // Current reports whether every ref r observed still holds in o what r |
| 58 | // says it held, or left it holding. |
| 59 | func (o Origin) Current(r *change.RestackReport) bool { |
| 60 | if r.Target != Target || !o.holds("refs/heads/"+Target, &r.TargetTip) { |
| 61 | return false |
| 62 | } |
| 63 | for _, q := range r.Requests { |
| 64 | if !o.holds(q.Ref, &q.Tip) { |
| 65 | return false |
| 66 | } |
| 67 | } |
| 68 | for _, l := range r.Layers { |
| 69 | record, tip := l.Record, l.BranchTip |
| 70 | if p := l.Published; p != nil { |
| 71 | tip = &p.Head |
| 72 | if p.Record != nil { |
| 73 | record = *p.Record |
| 74 | } |
| 75 | } |
| 76 | if !o.holds(change.Ref(Target, l.ChangeID), &record) { |
| 77 | return false |
| 78 | } |
| 79 | if l.Branch != "" && !o.holds("refs/heads/"+l.Branch, tip) { |
| 80 | return false |
| 81 | } |
| 82 | } |
| 83 | return true |
| 84 | } |
| 85 | |
| 86 | // holds reports whether ref is at the head want names in o, or absent from |
| 87 | // o when want is nil. |
| 88 | func (o Origin) holds(ref string, want *string) bool { |
| 89 | oid, present := o.Refs[ref] |
| 90 | if want == nil { |
| 91 | return !present |
| 92 | } |
| 93 | return present && oid == strings.TrimPrefix(*want, change.HeadPrefix) |
| 94 | } |
| 95 | |
| 96 | // Report is the restack report origin holds for a change, read once per |
| 97 | // snapshot. found is false when origin holds none, or holds one that does |
| 98 | // not read or is about another change, which no reader shows: the hook |
| 99 | // refuses such a report, so only corrupt storage holds one. |
| 100 | func (s *Snapshot) Report(id string) (r Report, found bool) { |
| 101 | if r, ok := s.reports[id]; ok { |
| 102 | return r, r.Report != nil |
| 103 | } |
| 104 | r = Report{Ref: RestackPrefix + id, Commit: s.Refs[RestackPrefix+id]} |
| 105 | if r.Commit != "" { |
| 106 | if rep, err := ReadReport(s.ctx, s.g, r.Commit); err == nil && rep.ChangeID == id { |
| 107 | r.Report, r.Current = rep, s.Origin.Current(rep) |
| 108 | } |
| 109 | } |
| 110 | s.reports[id] = r |
| 111 | return r, r.Report != nil |
| 112 | } |