| 1 | package knot |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "regexp" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // OpTrailer is the trailer every graph commit carries: the ID of the |
| 14 | // operation it records. The valley command picks the ID once for an |
| 15 | // operation and keeps it across every retry, so after a push whose outcome |
| 16 | // it cannot tell, it can look on the ref for the operation before trying |
| 17 | // it again. |
| 18 | const OpTrailer = "Valley-Op" |
| 19 | |
| 20 | // Commit is what a graph commit says: its tree, its parents in order, the |
| 21 | // name and the time its committer line gives, and the operation its |
| 22 | // trailer names. |
| 23 | type Commit struct { |
| 24 | Tree string |
| 25 | Parents []string |
| 26 | Committer string |
| 27 | // Time is when the commit was committed, in UTC. |
| 28 | Time time.Time |
| 29 | Op string |
| 30 | } |
| 31 | |
| 32 | // ParseCommit reads a commit object as git stores it, with object IDs of |
| 33 | // hashSize bytes. The tree line comes first and the parent lines directly |
| 34 | // after it, as git reads them; a parent line anywhere later is one git |
| 35 | // ignores, and a commit that holds one is refused rather than read two |
| 36 | // ways. The message's last line is the Valley-Op trailer, and no other |
| 37 | // line names it. |
| 38 | func ParseCommit(body []byte, hashSize int) (Commit, error) { |
| 39 | header, message, ok := bytes.Cut(body, []byte("\n\n")) |
| 40 | if !ok { |
| 41 | return Commit{}, errors.New("it is a commit with no message, and a graph commit's message carries its Valley-Op trailer") |
| 42 | } |
| 43 | lines := strings.Split(string(header), "\n") |
| 44 | var c Commit |
| 45 | tree, found := strings.CutPrefix(lines[0], "tree ") |
| 46 | if !found || !isHex(tree, hashSize) { |
| 47 | return Commit{}, errors.New("it is a commit git could not have written: it does not open with its tree") |
| 48 | } |
| 49 | c.Tree = tree |
| 50 | rest := lines[1:] |
| 51 | for len(rest) > 0 && strings.HasPrefix(rest[0], "parent ") { |
| 52 | parent := strings.TrimPrefix(rest[0], "parent ") |
| 53 | if !isHex(parent, hashSize) { |
| 54 | return Commit{}, errors.New("it is a commit git could not have written: a parent is not an object ID") |
| 55 | } |
| 56 | c.Parents = append(c.Parents, parent) |
| 57 | rest = rest[1:] |
| 58 | } |
| 59 | committers := 0 |
| 60 | for _, line := range rest { |
| 61 | switch { |
| 62 | case strings.HasPrefix(line, "parent "): |
| 63 | return Commit{}, errors.New("it names a parent after its other headers, which git does not read as a parent") |
| 64 | case strings.HasPrefix(line, "committer "): |
| 65 | committers++ |
| 66 | who, rest, ok := strings.Cut(strings.TrimPrefix(line, "committer "), " <") |
| 67 | end := strings.LastIndex(rest, "> ") |
| 68 | if !ok || end < 0 { |
| 69 | return Commit{}, errors.New("its committer line names no email, as git writes one") |
| 70 | } |
| 71 | c.Committer = who |
| 72 | when, err := commitTime(rest[end+2:]) |
| 73 | if err != nil { |
| 74 | return Commit{}, err |
| 75 | } |
| 76 | c.Time = when |
| 77 | } |
| 78 | } |
| 79 | if committers != 1 { |
| 80 | return Commit{}, fmt.Errorf("it has %d committer lines, and a commit has one", committers) |
| 81 | } |
| 82 | op, err := OpOf(string(message)) |
| 83 | if err != nil { |
| 84 | return Commit{}, err |
| 85 | } |
| 86 | c.Op = op |
| 87 | return c, nil |
| 88 | } |
| 89 | |
| 90 | // commitTime reads the time a committer line ends with, as git writes it: |
| 91 | // seconds since the epoch, and the committer's offset from UTC. |
| 92 | func commitTime(s string) (time.Time, error) { |
| 93 | seconds, zone, ok := strings.Cut(s, " ") |
| 94 | n, err := strconv.ParseInt(seconds, 10, 64) |
| 95 | if !ok || err != nil || !zoneShape.MatchString(zone) { |
| 96 | return time.Time{}, errors.New("its committer line ends with no time, as git writes one: seconds since the epoch and a zone, +HHMM") |
| 97 | } |
| 98 | return time.Unix(n, 0).UTC(), nil |
| 99 | } |
| 100 | |
| 101 | // zoneShape is a commit time's offset from UTC. |
| 102 | var zoneShape = regexp.MustCompile(`^[+-][0-9]{4}$`) |
| 103 | |
| 104 | // OpOf is the operation a graph commit's message names in its trailer: |
| 105 | // the message's last line is "Valley-Op: <16 hex digits>", and no other |
| 106 | // line opens with Valley-Op. |
| 107 | func OpOf(message string) (string, error) { |
| 108 | lines := strings.Split(strings.TrimRight(message, "\n"), "\n") |
| 109 | last := lines[len(lines)-1] |
| 110 | op, ok := strings.CutPrefix(last, OpTrailer+": ") |
| 111 | if !ok || !IsOpID(op) { |
| 112 | return "", fmt.Errorf("its message does not end with a %s: <16 hex digits> trailer, which every graph commit carries", OpTrailer) |
| 113 | } |
| 114 | for _, line := range lines[:len(lines)-1] { |
| 115 | if strings.HasPrefix(line, OpTrailer) { |
| 116 | return "", fmt.Errorf("its message names %s on more than its last line", OpTrailer) |
| 117 | } |
| 118 | } |
| 119 | return op, nil |
| 120 | } |
| 121 | |
| 122 | // Message is the commit message of one operation: a subject, and the |
| 123 | // trailer naming the operation. |
| 124 | func Message(subject, op string) string { |
| 125 | return subject + "\n\n" + OpTrailer + ": " + op + "\n" |
| 126 | } |
| 127 | |
| 128 | func isHex(s string, size int) bool { |
| 129 | if len(s) != 2*size { |
| 130 | return false |
| 131 | } |
| 132 | for _, c := range []byte(s) { |
| 133 | if (c < '0' || c > '9') && (c < 'a' || c > 'f') { |
| 134 | return false |
| 135 | } |
| 136 | } |
| 137 | return true |
| 138 | } |