gunk-dev/the-valley

restack.go

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