summaryrefslogtreecommitdiff
path: root/internal/generator/compare.go
blob: 0b53b92204929a9e56fc38b6e502fcf6688db28f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package generator

import (
	"bytes"
	"fmt"
	"html/template"
	"os"
	"path/filepath"
	"sort"
	"strings"

	"github.com/alecthomas/chroma/v2/formatters/html"
	"github.com/alecthomas/chroma/v2/lexers"
	"github.com/alecthomas/chroma/v2/styles"
	"github.com/bluekeyes/go-gitdiff/gitdiff"

	"mokhan.ca/xlgmokha/gitmal/internal/git"
	"mokhan.ca/xlgmokha/gitmal/internal/templates"
)

func GenerateComparePages(tags []git.Tag, branches []git.Ref, params Params) error {
	outDir := filepath.Join(params.OutputDir, "compare")
	if err := os.MkdirAll(outDir, 0o755); err != nil {
		return err
	}

	tags = filterValidTags(tags, params.RepoDir)

	if len(tags) > 0 {
		latestTag := tags[0].Name
		head := params.DefaultRef.String()
		if err := generateComparePage(latestTag, head, params); err != nil {
			Echo(fmt.Sprintf("  warning: compare %s...%s failed: %v", latestTag, head, err))
		}
	}

	if len(tags) > 1 {
		for i := 0; i < len(tags)-1; i++ {
			base := tags[i+1].Name
			head := tags[i].Name
			if err := generateComparePage(base, head, params); err != nil {
				Echo(fmt.Sprintf("  warning: compare %s...%s failed: %v", base, head, err))
			}
		}
	}

	defaultBranch := params.DefaultRef.String()
	for _, branch := range branches {
		if branch.String() == defaultBranch {
			continue
		}
		if err := generateComparePage(defaultBranch, branch.String(), params); err != nil {
			Echo(fmt.Sprintf("  warning: compare %s...%s failed: %v", defaultBranch, branch, err))
		}
	}

	return nil
}

func generateComparePage(base, head string, params Params) error {
	baseRef := git.NewRef(base)
	headRef := git.NewRef(head)

	diff, err := git.CompareDiff(base, head, params.RepoDir)
	if err != nil {
		return err
	}

	commits, err := git.CompareCommits(baseRef, headRef, params.RepoDir)
	if err != nil {
		return err
	}

	files, _, err := gitdiff.Parse(strings.NewReader(diff))
	if err != nil {
		return err
	}

	formatter := html.New(
		html.WithClasses(true),
		html.WithCSSComments(false),
	)
	lightStyle := styles.Get("github")
	lexer := lexers.Get("diff")
	if lexer == nil {
		return fmt.Errorf("failed to get lexer for diff")
	}

	fileTree := buildFileTree(files)
	fileOrder := make(map[string]int)
	{
		var idx int
		var walk func(nodes []*templates.FileTree)
		walk = func(nodes []*templates.FileTree) {
			for _, n := range nodes {
				if n.IsDir {
					walk(n.Children)
					continue
				}
				if n.Path == "" {
					continue
				}
				if _, ok := fileOrder[n.Path]; !ok {
					fileOrder[n.Path] = idx
					idx++
				}
			}
		}
		walk(fileTree)
	}

	var filesViews []templates.FileView
	for _, f := range files {
		path := f.NewName
		if f.IsDelete {
			path = f.OldName
		}
		if path == "" {
			continue
		}

		var fileDiff strings.Builder
		for _, frag := range f.TextFragments {
			fileDiff.WriteString(frag.String())
		}

		it, err := lexer.Tokenise(nil, fileDiff.String())
		if err != nil {
			return err
		}
		var buf bytes.Buffer
		if err := formatter.Format(&buf, lightStyle, it); err != nil {
			return err
		}

		filesViews = append(filesViews, templates.FileView{
			Path:       path,
			OldName:    f.OldName,
			NewName:    f.NewName,
			IsNew:      f.IsNew,
			IsDelete:   f.IsDelete,
			IsRename:   f.IsRename,
			IsBinary:   f.IsBinary,
			HasChanges: f.TextFragments != nil,
			HTML:       template.HTML(buf.String()),
		})
	}

	sort.Slice(filesViews, func(i, j int) bool {
		oi, iok := fileOrder[filesViews[i].Path]
		oj, jok := fileOrder[filesViews[j].Path]
		if iok && jok {
			return oi < oj
		}
		if iok != jok {
			return iok
		}
		return filesViews[i].Path < filesViews[j].Path
	})

	for i := range commits {
		commits[i].Href = filepath.ToSlash(filepath.Join("../../commit", commits[i].Hash+".html"))
	}

	headDirName := headRef.DirName()
	if head == "HEAD" {
		headDirName = "HEAD"
	}
	dirName := baseRef.DirName() + "..." + headDirName
	outDir := filepath.Join(params.OutputDir, "compare", dirName)
	if err := os.MkdirAll(outDir, 0o755); err != nil {
		return err
	}
	outPath := filepath.Join(outDir, "index.html")
	f, err := os.Create(outPath)
	if err != nil {
		return err
	}

	rootHref := "../../"

	err = templates.CompareTemplate.ExecuteTemplate(f, "layout.gohtml", templates.CompareParams{
		LayoutParams: templates.LayoutParams{
			Title:          fmt.Sprintf("Comparing %s...%s %s %s", base, head, Dot, params.Name),
			Name:           params.Name,
			RootHref:       rootHref,
			CurrentRefDir:  params.DefaultRef.DirName(),
			Selected:       "",
			NeedsSyntaxCSS: true,
		},
		Base:      base,
		Head:      head,
		Commits:   commits,
		FileTree:  fileTree,
		FileViews: filesViews,
	})
	if err != nil {
		_ = f.Close()
		return err
	}
	return f.Close()
}

func filterValidTags(tags []git.Tag, repoDir string) []git.Tag {
	valid := make([]git.Tag, 0, len(tags))
	for _, tag := range tags {
		if git.RefExists(tag.Name, repoDir) {
			valid = append(valid, tag)
		}
	}
	return valid
}