gunk-dev/the-valley

standing.go

source499d9e61dfeemain's published tip history
3.4 KiB
1// Where a change stands now, for review (review.go). Review shows a
2// change as its record read when the review began, and the operator may
3// read it for a long time. Meanwhile its owner may return it to draft,
4// ready a new version, or withdraw it, another change may land and the
5// host's restacker may report a conflict, or it may land. So review reads
6// the change again with standing before it acts on an answer, and says
7// plainly what changed rather than acting on a change that moved.
8
9package main
10
11import (
12 "fmt"
13 "io"
14 "strconv"
15 "strings"
16 "time"
17
18 "the-valley/change"
19)
20
21// runStanding prints where a change stands now: one line of fields
22// separated by tabs,
23//
24// <state> <version> <head> <since> <landed> <restack>
25//
26// with the state as the record says it: ready, draft or withdrawn; the
27// active version and its head as a full object ID; since, the time the
28// record last changed, read from the record commit's committer date, in
29// UTC, as hours and minutes on the current day and with the date before
30// them otherwise; landed, v<n> when the change landed as version n
31// (rebase.Snapshot.LandedAs), by its head on main or a verified landing
32// record, and empty otherwise; and restack, "conflict" when the change's
33// restack report is current and says rebasing it onto main conflicted,
34// and empty otherwise. It fails when origin holds no record of the
35// change.
36func runStanding(g git, args []string, w io.Writer) error {
37 if len(args) != 1 || !change.ValidID(args[0]) {
38 return failure("usage: valley-go standing <change ID>")
39 }
40 if !g.succeeds(true, "rev-parse", "--git-dir") {
41 return failure("not a git repository")
42 }
43 s, err := standingOf(g, args[0])
44 if err != nil {
45 return err
46 }
47 _, err = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n", s.state, s.version, s.head, s.since, s.landed, s.conflict)
48 return err
49}
50
51// standing is the line runStanding prints, one field each.
52type standing struct {
53 state, version, head, since, landed, conflict string
54}
55
56// standingOf reads where the change id stands now, as runStanding prints
57// it.
58func standingOf(g git, id string) (standing, error) {
59 o, n, found, err := lookupRecorded(g, id)
60 if err != nil {
61 return standing{}, err
62 }
63 if !found {
64 return standing{}, failure("origin holds no record of change " + id)
65 }
66 s, err := readRecorded(g, o, n, io.Discard)
67 if err != nil {
68 return standing{}, err
69 }
70 out, err := g.output(true, "show", "-s", "--format=%ct", n.commit)
71 if err != nil {
72 return standing{}, failure("could not read the record commit of change " + id)
73 }
74 sec, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64)
75 if err != nil {
76 return standing{}, failure("the record commit of change " + id + " has no committer date")
77 }
78 landed, conflict := "", ""
79 if s.recorded.landedAs != 0 {
80 landed = fmt.Sprintf("v%d", s.recorded.landedAs)
81 }
82 if s.restack != nil && s.restack.Current && s.restack.Report.Result == change.RestackConflict {
83 conflict = "conflict"
84 }
85 v := n.record.ActiveVersion()
86 return standing{state: n.record.State, version: strconv.Itoa(v.N), head: headOf(v),
87 since: clockTime(time.Unix(sec, 0), time.Now()), landed: landed, conflict: conflict}, nil
88}
89
90// clockTime is t in UTC as hours and minutes, with the date before them
91// unless t falls on the same UTC day as now.
92func clockTime(t, now time.Time) string {
93 t, now = t.UTC(), now.UTC()
94 if t.Format(time.DateOnly) == now.Format(time.DateOnly) {
95 return t.Format("15:04Z")
96 }
97 return t.Format("2006-01-02 15:04Z")
98}