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