Decode transforms a plumbing.EncodedObject into a Commit struct.
(o plumbing.EncodedObject)
| 184 | |
| 185 | // Decode transforms a plumbing.EncodedObject into a Commit struct. |
| 186 | func (c *Commit) Decode(o plumbing.EncodedObject) (err error) { |
| 187 | if o.Type() != plumbing.CommitObject { |
| 188 | return ErrUnsupportedObject |
| 189 | } |
| 190 | |
| 191 | c.Hash = o.Hash() |
| 192 | c.Encoding = defaultUtf8CommitMessageEncoding |
| 193 | |
| 194 | reader, err := o.Reader() |
| 195 | if err != nil { |
| 196 | return err |
| 197 | } |
| 198 | defer ioutil.CheckClose(reader, &err) |
| 199 | |
| 200 | r := sync.GetBufioReader(reader) |
| 201 | defer sync.PutBufioReader(r) |
| 202 | |
| 203 | var message bool |
| 204 | var mergetag bool |
| 205 | var pgpsig bool |
| 206 | var msgbuf bytes.Buffer |
| 207 | for { |
| 208 | line, err := r.ReadBytes('\n') |
| 209 | if err != nil && err != io.EOF { |
| 210 | return err |
| 211 | } |
| 212 | |
| 213 | if mergetag { |
| 214 | if len(line) > 0 && line[0] == ' ' { |
| 215 | line = bytes.TrimLeft(line, " ") |
| 216 | c.MergeTag += string(line) |
| 217 | continue |
| 218 | } else { |
| 219 | mergetag = false |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | if pgpsig { |
| 224 | if len(line) > 0 && line[0] == ' ' { |
| 225 | line = bytes.TrimLeft(line, " ") |
| 226 | c.PGPSignature += string(line) |
| 227 | continue |
| 228 | } else { |
| 229 | pgpsig = false |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if !message { |
| 234 | line = bytes.TrimSpace(line) |
| 235 | if len(line) == 0 { |
| 236 | message = true |
| 237 | continue |
| 238 | } |
| 239 | |
| 240 | split := bytes.SplitN(line, []byte{' '}, 2) |
| 241 | |
| 242 | var data []byte |
| 243 | if len(split) == 2 { |