(ref Ref, repoDir string)
| 216 | } |
| 217 | |
| 218 | func Commits(ref Ref, repoDir string) ([]Commit, error) { |
| 219 | format := []string{ |
| 220 | "%H", // commit hash |
| 221 | "%h", // abbreviated commit hash |
| 222 | "%s", // subject |
| 223 | "%b", // body |
| 224 | "%an", // author name |
| 225 | "%ae", // author email |
| 226 | "%ad", // author date |
| 227 | "%P", // parent hashes |
| 228 | "%D", // ref names without the "(", ")" wrapping. |
| 229 | } |
| 230 | |
| 231 | args := []string{ |
| 232 | "log", |
| 233 | "--date=unix", |
| 234 | "--pretty=format:" + strings.Join(format, "\x1F"), |
| 235 | "-z", // Separate the commits with NULs instead of newlines |
| 236 | ref.String(), |
| 237 | } |
| 238 | |
| 239 | cmd := exec.Command("git", args...) |
| 240 | if repoDir != "" { |
| 241 | cmd.Dir = repoDir |
| 242 | } |
| 243 | |
| 244 | out, err := cmd.Output() |
| 245 | if err != nil { |
| 246 | return nil, err |
| 247 | } |
| 248 | |
| 249 | lines := strings.Split(string(out), "\x00") |
| 250 | commits := make([]Commit, 0, len(lines)) |
| 251 | for _, line := range lines { |
| 252 | if line == "" { |
| 253 | continue |
| 254 | } |
| 255 | parts := strings.Split(line, "\x1F") |
| 256 | if len(parts) != len(format) { |
| 257 | return nil, fmt.Errorf("unexpected commit format: %s", line) |
| 258 | } |
| 259 | full, short, subject, body, author, email, date, parents, refs := |
| 260 | parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], parts[6], parts[7], parts[8] |
| 261 | timestamp, err := strconv.Atoi(date) |
| 262 | if err != nil { |
| 263 | return nil, fmt.Errorf("failed to parse commit date: %w", err) |
| 264 | } |
| 265 | commits = append(commits, Commit{ |
| 266 | Hash: full, |
| 267 | ShortHash: short, |
| 268 | Subject: subject, |
| 269 | Body: body, |
| 270 | Author: author, |
| 271 | Email: email, |
| 272 | Date: time.Unix(int64(timestamp), 0), |
| 273 | Parents: strings.Fields(parents), |
| 274 | RefNames: parseRefNames(refs), |
| 275 | }) |
no test coverage detected