package generator import ( "encoding/json" "os" "path/filepath" "mokhan.ca/xlgmokha/gitmal/internal/git" ) type CommitJSON struct { SHA string `json:"sha"` Commit CommitDetail `json:"commit"` Parents []ParentRef `json:"parents"` } type CommitDetail struct { Author PersonInfo `json:"author"` Committer PersonInfo `json:"committer"` Message string `json:"message"` } type PersonInfo struct { Name string `json:"name"` Email string `json:"email"` Date string `json:"date"` } type ParentRef struct { SHA string `json:"sha"` } func toCommitJSON(c git.Commit) CommitJSON { message := c.Subject if c.Body != "" { message = c.Subject + "\n\n" + c.Body } parents := make([]ParentRef, len(c.Parents)) for i, p := range c.Parents { parents[i] = ParentRef{SHA: p} } return CommitJSON{ SHA: c.Hash, Commit: CommitDetail{ Author: PersonInfo{Name: c.Author, Email: c.Email, Date: c.Date.Format("2006-01-02T15:04:05Z")}, Committer: PersonInfo{Name: c.CommitterName, Email: c.CommitterEmail, Date: c.CommitterDate.Format("2006-01-02T15:04:05Z")}, Message: message, }, Parents: parents, } } func GenerateCommitsJSON(commits []git.Commit, params Params) error { outDir := filepath.Join(params.OutputDir, "commits") if err := os.MkdirAll(outDir, 0o755); err != nil { return err } list := make([]CommitJSON, len(commits)) for i, c := range commits { list[i] = toCommitJSON(c) } outPath := filepath.Join(outDir, params.Ref.DirName()+".json") f, err := os.Create(outPath) if err != nil { return err } defer f.Close() encoder := json.NewEncoder(f) encoder.SetIndent("", " ") return encoder.Encode(list) }