xonecas.io
8356
Wx

www-xonecas-io / files

www-xonecas-io / internal / project / history.go

2.2 KB
1package project
2
3import (
4	"regexp"
5	"sort"
6	"strings"
7
8	"xonecas.io/site/internal/repo"
9)
10
11// Me is every author name xonecas has committed under, lower case. Other people named Sean are not in it.
12var Me = map[string]bool{"xonecas": true, "sacenox": true, "sean": true, "sean martin": true, "sean caetano martin": true}
13
14// AIFiles are instruction files coding agents read. Any of them in the history makes a project post-AI.
15var AIFiles = map[string]bool{"AGENTS.md": true, "CLAUDE.md": true, "GEMINI.md": true, ".cursorrules": true, ".windsurfrules": true, ".clinerules": true, "copilot-instructions.md": true, ".aider.conf.yml": true}
16
17var aiTrailer = regexp.MustCompile(`(?im)^\s*(co-authored-by:.*\b(claude|copilot|aider|codex|gemini|cursor)\b|.*generated with \[?claude)`)
18
19// History is what one project's commits say, across all its sources.
20type History struct {
21	Total, Mine int
22	First, Last string         // dates of my first and last commit, YYYY-MM-DD
23	Months      map[string]int // my commits per YYYY-MM
24	AI          bool           // an AI instruction file or AI trailer anywhere in the history
25}
26
27// ReadHistory reads every commit of every source once, counting each hash once.
28func ReadHistory(sources []Source) (History, error) {
29	h := History{Months: map[string]int{}}
30	seen := map[string]bool{}
31	var dates []string
32	for _, s := range sources {
33		if s.Commits == 0 {
34			continue
35		}
36		out, err := repo.Git(s.Dir, "log", "--all", "--name-only", "--format=%x00%H%x09%an%x09%as%n%B")
37		if err != nil {
38			return h, err
39		}
40		for _, rec := range strings.Split(out, "\x00")[1:] {
41			head, rest, _ := strings.Cut(rec, "\n")
42			f := strings.SplitN(head, "\t", 3)
43			if len(f) < 3 || seen[f[0]] {
44				continue
45			}
46			seen[f[0]] = true
47			h.Total++
48			h.AI = h.AI || aiTrailer.MatchString(rest) || hasAIFile(rest)
49			if Me[strings.ToLower(f[1])] {
50				h.Mine++
51				h.Months[f[2][:7]]++
52				dates = append(dates, f[2])
53			}
54		}
55	}
56	if len(dates) > 0 {
57		sort.Strings(dates)
58		h.First, h.Last = dates[0], dates[len(dates)-1]
59	}
60	return h, nil
61}
62
63func hasAIFile(text string) bool {
64	for _, line := range strings.Split(text, "\n") {
65		if i := strings.LastIndexByte(line, '/'); AIFiles[line[i+1:]] {
66			return true
67		}
68	}
69	return false
70}