id = $id; $c->headers = Tag::parseHeaders($data, $c->message); foreach ($c->headers as [$k, $v]) { match ($k) { 'tree' => $c->tree = $v, 'parent' => $c->parents[] = $v, 'author' => $c->author = Person::parse($v), 'committer' => $c->committer = Person::parse($v), default => null, }; } return $c; } /** * 新しいコミット * * @param string[] $parents */ public static function create(string $tree, array $parents, Person $author, Person $committer, string $message): self { $c = new self(); $c->tree = $tree; $c->parents = array_values($parents); $c->author = $author; $c->committer = $committer; $c->message = $message; $c->headers[] = ['tree', $tree]; foreach ($c->parents as $p) $c->headers[] = ['parent', $p]; $c->headers[] = ['author', (string)$author]; $c->headers[] = ['committer', (string)$committer]; return $c; } public function toString(): string { return Tag::buildHeaders($this->headers).$this->message; } /** メッセージの最初の行 */ public function subject(): string { return trim(strtok($this->message, "\n") ?: ''); } /** ヘッダーの値(無ければnull) */ public function header(string $name): ?string { foreach ($this->headers as [$k, $v]) if ($k === $name) return $v; return null; } }