gunk-dev/the-valley

standing.go

sourceb1cafbe3a404main'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 "the-valley/rebase"
20)
21
22// runStanding prints where a change stands now: one line of fields
23// separated by tabs,
24//
25// <state> <version> <head> <since> <landed> <restack>
26//
27// with the state as the record says it: ready, draft or withdrawn; the
28// active version and its head as a full object ID; since, the time the
29// record last changed, read from the record commit's committer date, in
30// UTC, as hours and minutes on the current day and with the date before
31// them otherwise; landed, "landed" when the active version has landed on
32// main, by its head on main or a verified landing record, and empty
33// otherwise; and restack, "conflict" when the change's
34// restack report is current and says rebasing it onto main conflicted,
35// and empty otherwise. It fails when origin holds no record of the
36// change.
37func runStanding(g git, args []string, w io.Writer) error {
38 if len(args) != 1 || !change.ValidID(args[0]) {
39 return failure("usage: valley-go standing <change ID>")
40 }
41 if !g.succeeds(true, "rev-parse", "--git-dir") {
42 return failure("not a git repository")
43 }
44 o, n, found, err := lookupRecorded(g, args[0])
45 if err != nil {
46 return err
47 }
48 if !found {
49 return failure("origin holds no record of change " + args[0])
50 }
51 s, err := readRecorded(g, o, n, io.Discard)
52 if err != nil {
53 return err
54 }
55 out, err := g.output(true, "show", "-s", "--format=%ct", n.commit)
56 if err != nil {
57 return failure("could not read the record commit of change " + args[0])
58 }
59 sec, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64)
60 if err != nil {
61 return failure("the record commit of change " + args[0] + " has no committer date")
62 }
63 landed, conflict := "", ""
64 if s.recorded.landing == rebase.Landed || s.recorded.landing == rebase.LandedRecorded {
65 landed = "landed"
66 }
67 if s.restack != nil && s.restack.Current && s.restack.Report.Result == change.RestackConflict {
68 conflict = "conflict"
69 }
70 v := n.record.ActiveVersion()
71 _, err = fmt.Fprintf(w, "%s\t%d\t%s\t%s\t%s\t%s\n", n.record.State, v.N, headOf(v),
72 clockTime(time.Unix(sec, 0), time.Now()), landed, conflict)
73 return err
74}
75
76// clockTime is t in UTC as hours and minutes, with the date before them
77// unless t falls on the same UTC day as now.
78func clockTime(t, now time.Time) string {
79 t, now = t.UTC(), now.UTC()
80 if t.Format(time.DateOnly) == now.Format(time.DateOnly) {
81 return t.Format("15:04Z")
82 }
83 return t.Format("2006-01-02 15:04Z")
84}