| 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 | |
| 9 | package main |
| 10 | |
| 11 | import ( |
| 12 | "fmt" |
| 13 | "io" |
| 14 | "strconv" |
| 15 | "strings" |
| 16 | "time" |
| 17 | |
| 18 | "the-valley/change" |
| 19 | ) |
| 20 | |
| 21 | // standing is where a change stands now: its state as the record says |
| 22 | // it, ready, draft or withdrawn; the active version and its head as a |
| 23 | // full object ID; since, the time the record last changed, read from the |
| 24 | // record commit's committer date, in UTC, as hours and minutes on the |
| 25 | // current day and with the date before them otherwise; landed, v<n> when |
| 26 | // the change landed as version n (rebase.Snapshot.LandedAs), by its head |
| 27 | // on main or a verified landing record, and empty otherwise; and |
| 28 | // conflict, "conflict" when the change's restack report is current and |
| 29 | // says rebasing it onto main conflicted, and empty otherwise. |
| 30 | type standing struct { |
| 31 | state, version, head, since, landed, conflict string |
| 32 | } |
| 33 | |
| 34 | // standingOf reads where the change id stands now. It fails when origin |
| 35 | // holds no record of the change. |
| 36 | func standingOf(g git, id string) (standing, error) { |
| 37 | o, n, found, err := lookupRecorded(g, id) |
| 38 | if err != nil { |
| 39 | return standing{}, err |
| 40 | } |
| 41 | if !found { |
| 42 | return standing{}, failure("origin holds no record of change " + id) |
| 43 | } |
| 44 | s, err := readRecorded(g, o, n, io.Discard) |
| 45 | if err != nil { |
| 46 | return standing{}, err |
| 47 | } |
| 48 | out, err := g.output(true, "show", "-s", "--format=%ct", n.commit) |
| 49 | if err != nil { |
| 50 | return standing{}, failure("could not read the record commit of change " + id) |
| 51 | } |
| 52 | sec, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64) |
| 53 | if err != nil { |
| 54 | return standing{}, failure("the record commit of change " + id + " has no committer date") |
| 55 | } |
| 56 | landed, conflict := "", "" |
| 57 | if s.recorded.landedAs != 0 { |
| 58 | landed = fmt.Sprintf("v%d", s.recorded.landedAs) |
| 59 | } |
| 60 | if s.restack != nil && s.restack.Current && s.restack.Report.Result == change.RestackConflict { |
| 61 | conflict = "conflict" |
| 62 | } |
| 63 | v := n.record.ActiveVersion() |
| 64 | return standing{state: n.record.State, version: strconv.Itoa(v.N), head: headOf(v), |
| 65 | since: clockTime(time.Unix(sec, 0), time.Now()), landed: landed, conflict: conflict}, nil |
| 66 | } |
| 67 | |
| 68 | // clockTime is t in UTC as hours and minutes, with the date before them |
| 69 | // unless t falls on the same UTC day as now. |
| 70 | func clockTime(t, now time.Time) string { |
| 71 | t, now = t.UTC(), now.UTC() |
| 72 | if t.Format(time.DateOnly) == now.Format(time.DateOnly) { |
| 73 | return t.Format("15:04Z") |
| 74 | } |
| 75 | return t.Format("2006-01-02 15:04Z") |
| 76 | } |