gunk-dev/the-valley

restack.go

source2886163c0514main's published tip history
2.7 KiB
1// A change's restack report, as status, changes and pending show it: the
2// latest report the host restacker wrote about the change, and whether it
3// is current (../rebase/report.go). A report is current only while main's
4// tip, and every layer's record and branch head, are what it observed, or
5// what it published. Otherwise it is historical.
6
7package main
8
9import (
10 "fmt"
11 "strings"
12
13 "the-valley/change"
14 "the-valley/rebase"
15)
16
17// jsonRestack is a change's restack report as valley-status/v1,
18// valley-changes/v1 and valley-pending/v1 write it: its ref, the report
19// commit, whether it is current, and the report as it reads.
20type jsonRestack struct {
21 Ref string `json:"ref"`
22 Commit string `json:"commit"`
23 Current bool `json:"current"`
24 Report *change.RestackReport `json:"report"`
25}
26
27// restackView is the JSON of a report, or nil for none.
28func restackView(r rebase.Report, found bool) *jsonRestack {
29 if !found {
30 return nil
31 }
32 return &jsonRestack{Ref: r.Ref, Commit: r.Commit, Current: r.Current, Report: r.Report}
33}
34
35// report is the restack report origin holds for a change.
36func (s *snapshot) report(id string) (rebase.Report, bool) { return s.land.Report(id) }
37
38// restackSays is what a report says came of the attempt, as a phrase:
39// what happened, onto which main, and where it stopped. name gives a
40// change's handle, or "" for a change it has none for, which is named by
41// its ID.
42func restackSays(r *change.RestackReport, name func(id string) string) string {
43 onto := "onto main at " + short(strings.TrimPrefix(r.TargetTip, change.HeadPrefix))
44 switch r.Result {
45 case change.RestackConflict:
46 failing := name(*r.Failing)
47 if failing == "" {
48 failing = *r.Failing
49 }
50 says := fmt.Sprintf("a conflict rebasing %s %s", failing, onto)
51 if len(r.Conflicts) > 0 {
52 says += ", in " + firstFew(r.Conflicts)
53 }
54 return says
55 case change.RestackBlocked:
56 says := "held before rebasing " + onto
57 if len(r.Requests) > 0 {
58 var names []string
59 for _, ref := range r.Requests {
60 names = append(names, strings.TrimPrefix(ref, rebase.RequestPrefix))
61 }
62 says += " by the request"
63 if len(names) > 1 {
64 says += "s"
65 }
66 says += " " + firstFew(names)
67 }
68 if r.Refusal != nil {
69 says += ": " + *r.Refusal
70 }
71 return says
72 case change.RestackPartial:
73 return "rebased " + onto + ", and not every version was added"
74 case change.RestackComplete:
75 return "rebased " + onto
76 default:
77 return "given up rebasing " + onto + ": its inputs moved while it ran"
78 }
79}
80
81// firstFew is up to three names, and how many more there are.
82func firstFew(names []string) string {
83 if len(names) <= 3 {
84 return strings.Join(names, ", ")
85 }
86 return fmt.Sprintf("%s and %d more", strings.Join(names[:3], ", "), len(names)-3)
87}