gunk-dev/the-valley

standing.go

source6303fac04580main's published tip history
2.9 KiB
1// Where a change stands now, for bin/valley's review. 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 o, n, found, err := lookupRecorded(g, args[0])
44 if err != nil {
45 return err
46 }
47 if !found {
48 return failure("origin holds no record of change " + args[0])
49 }
50 s, err := readRecorded(g, o, n, io.Discard)
51 if err != nil {
52 return err
53 }
54 out, err := g.output(true, "show", "-s", "--format=%ct", n.commit)
55 if err != nil {
56 return failure("could not read the record commit of change " + args[0])
57 }
58 sec, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64)
59 if err != nil {
60 return failure("the record commit of change " + args[0] + " has no committer date")
61 }
62 landed, conflict := "", ""
63 if s.recorded.landedAs != 0 {
64 landed = fmt.Sprintf("v%d", s.recorded.landedAs)
65 }
66 if s.restack != nil && s.restack.Current && s.restack.Report.Result == change.RestackConflict {
67 conflict = "conflict"
68 }
69 v := n.record.ActiveVersion()
70 _, err = fmt.Fprintf(w, "%s\t%d\t%s\t%s\t%s\t%s\n", n.record.State, v.N, headOf(v),
71 clockTime(time.Unix(sec, 0), time.Now()), landed, conflict)
72 return err
73}
74
75// clockTime is t in UTC as hours and minutes, with the date before them
76// unless t falls on the same UTC day as now.
77func clockTime(t, now time.Time) string {
78 t, now = t.UTC(), now.UTC()
79 if t.Format(time.DateOnly) == now.Format(time.DateOnly) {
80 return t.Format("15:04Z")
81 }
82 return t.Format("2006-01-02 15:04Z")
83}