gtx


  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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package main

import (
	"bytes"
	"errors"
	"fmt"
	"html/template"
	"io/fs"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

// SEP is a browser generated UUID v4 used to separate out commit line items.
const SEP = "6f6c1745-e902-474a-9e99-08d0084fb011"

// Helps keep track of file extensions git thinks of as binary.
var types = make(map[string]bool)

type project struct {
	base     string
	Name     string
	repo     string
	options  *options
	template *template.Template
}

func NewProject(base string, repo string, options *options) *project {
	funcMap := template.FuncMap{
		"diffstatbodyparser": diffstatbodyparser,
		"diffbodyparser":     diffbodyparser,
	}

	t := template.Must(template.New("page").Funcs(funcMap).Parse(tpl))

	return &project{
		base:     base,
		Name:     options.Name,
		repo:     repo,
		options:  options,
		template: t,
	}
}

// Creates base directories for holding objects, branches, and commits.
func (p *project) init() error {
	dirs := []string{"branch", "commit", "object"}

	for _, dir := range dirs {
		d := filepath.Join(p.base, dir)

		// Clear existing dirs when -f true.
		if p.options.Force {
			if err := os.RemoveAll(d); err != nil {
				return fmt.Errorf("unable to remove directory: %v", err)
			}
		}

		if err := os.MkdirAll(d, 0755); err != nil {
			return fmt.Errorf("unable to create directory: %v", err)
		}
	}

	return nil
}

// Saves a local clone of `target` repo.
func (p *project) save() error {
	if _, err := os.Stat(p.repo); err != nil {
		return err
	}

	return exec.Command("git", "clone", p.options.Source, p.repo).Run()
}

func (p *project) updateBranches(branches []branch) {
	for _, b := range branches {
		ref := fmt.Sprintf("refs/heads/%s:refs/origin/%s", b, b)

		cmd := exec.Command("git", "fetch", "--force", "origin", ref)
		cmd.Dir = p.repo

		log.Printf("updating branch: %s", b)

		if _, err := cmd.Output(); err != nil {
			log.Printf("unable to fetch branch: %v", err)

			continue
		}
	}
}

func (p *project) writePages(branches []branch) {
	for _, b := range branches {
		log.Printf("processing branch: %s", b)

		go p.writeBranchPage(b)

		for i, c := range b.Commits {
			log.Printf("processing commit: %s: %d/%d", c.Abbr, i+1, len(b.Commits))

			base := filepath.Join(p.base, "commit", c.Hash)

			if err := os.MkdirAll(base, 0755); err != nil {
				if err != nil {
					log.Printf("unable to create commit directory: %v", err)
				}

				continue
			}

			for _, par := range c.Parents {
				p.writeCommitDiff(base, b, c, par)
			}

			for _, obj := range c.Tree {
				dst := filepath.Join(p.base, "object", obj.Dir())

				if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
					if err != nil {
						log.Printf("unable to create object directory: %v", err)
					}

					continue
				}

				p.writeObjectBlob(obj, dst)
				p.writeObject(obj, fmt.Sprintf("%s.html", dst), base, b, c)
			}

			p.writeCommitPage(base, b, c)
		}
	}
}

func (p *project) writeMainIndex(branches []branch) {
	// This is the main index or project home.
	f, err := os.Create(filepath.Join(p.base, "index.html"))

	defer f.Close()

	if err != nil {
		log.Fatalf("unable to create home page: %v", err)
	}

	page := page{
		Base: "./",
		Data: Data{
			"Branches": branches,
			"Source":   p.options.Source,
			"Project":  p.Name,
		},
		Title: p.Name,
	}

	if err := p.template.Execute(f, page); err != nil {
		log.Fatalf("unable to apply template: %v", err)
	}
}

func (p *project) writeCommitDiff(base string, b branch, c commit, par string) {
	cmd := exec.Command("git", "diff", "-p", fmt.Sprintf("%s..%s", par, c.Hash))
	cmd.Dir = p.repo

	out, err := cmd.Output()

	if err != nil {
		log.Printf("unable to diff against parent: %v", err)

		return
	}

	dst := filepath.Join(base, fmt.Sprintf("diff-%s.html", par))
	f, err := os.Create(dst)

	defer f.Close()

	if err != nil {
		log.Printf("unable to create commit diff to parent: %v", err)

		return
	}

	page := page{
		Base: "../../",
		Data: Data{
			"Diff": diff{
				Body:   fmt.Sprintf("%s", out),
				Commit: c,
				Parent: par,
			},
			"Path": Data{
				"Branch": b.Name,
				"Commit": par,
			},
			"Project": p.Name,
		},
		Title: strings.Join([]string{p.Name, b.Name, c.Abbr}, ": "),
	}

	if err := p.template.Execute(f, page); err != nil {
		log.Printf("unable to apply template: %v", err)

		return
	}
}

func (p *project) writeBranchPage(b branch) {
	dst := filepath.Join(p.base, "branch", b.Name, "index.html")

	if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
		if err != nil {
			log.Fatalf("unable to create branch directory: %v", err)
		}

		return
	}

	f, err := os.Create(dst)

	defer f.Close()

	if err != nil {
		// TODO: Remove from branches slice?
		log.Printf("unable to create branch page: %v", err)

		return
	}

	page := page{
		Base: "../../",
		Data: Data{
			"Commits": b.Commits,
			"Branch":  b,
			"Project": p.Name,
		},
		Title: strings.Join([]string{p.Name, b.Name}, ": "),
	}

	if err := p.template.Execute(f, page); err != nil {
		log.Printf("unable to apply template: %v", err)

		return
	}
}

func (p *project) writeObjectBlob(obj object, dst string) {
	if _, err := os.Stat(dst); err == nil || errors.Is(err, fs.ErrExist) {
		log.Printf("object %v already processed", obj.Hash[0:7])

		return
	}

	cmd := exec.Command("git", "cat-file", "blob", obj.Hash)
	cmd.Dir = p.repo

	out, err := cmd.Output()

	if err != nil {
		log.Printf("unable to save object: %v", err)

		return
	}

	f, err := os.Create(dst)

	defer f.Close()

	if err != nil {
		log.Printf("unable to create object: %v", err)

		return
	}

	if _, err := f.Write(out); err != nil {
		log.Printf("unable to write object blob: %v", err)

		return
	}
}

func (p *project) writeObject(obj object, dst string, base string, b branch, c commit) {
	lnk := filepath.Join(base, fmt.Sprintf("%s.html", obj.Path))

	if _, err := os.Stat(lnk); err == nil || errors.Is(err, fs.ErrExist) {
		log.Printf("object %v already processed", obj.Hash[0:7])

		return
	}

	f, err := os.Create(dst)

	defer f.Close()

	if err != nil {
		log.Printf("unable to create object: %v", err)

		return
	}

	o := &show{
		object: object{
			Hash: obj.Hash,
			Path: obj.Path,
		},
		Bin: types[filepath.Ext(obj.Path)],
	}

	if o.Bin {
		// TODO.
	} else {
		cmd := exec.Command("git", "show", "--no-notes", obj.Hash)
		cmd.Dir = p.repo

		out, err := cmd.Output()

		if err != nil {
			log.Printf("unable to show object: %v", err)

			return
		}

		sep := []byte("\n")
		var lines = make([]int, bytes.Count(out, sep))

		for i := range lines {
			lines[i] = i + 1
		}

		if bytes.LastIndex(out, sep) != len(out)-1 {
			lines = append(lines, len(lines))
		}

		o.Lines = lines
		o.Body = fmt.Sprintf("%s", out)
	}

	page := page{
		Base: "../../",
		Data: Data{
			"Object": *o,
			"Path": Data{
				"Branch": b.Name,
				"Commit": c.Hash,
			},
			"Project": p.Name,
		},
		Title: strings.Join([]string{p.Name, b.Name, c.Abbr, obj.Path}, ": "),
	}

	if err := p.template.Execute(f, page); err != nil {
		log.Printf("unable to apply template: %v", err)

		return
	}

	if err := os.MkdirAll(filepath.Dir(lnk), 0755); err != nil {
		if err != nil {
			log.Printf("unable to create hard link path: %v", err)
		}

		return
	}

	if err := os.Link(dst, lnk); err != nil {
		if os.IsExist(err) {
			return
		}

		log.Printf("unable to hard link object into commit folder: %v", err)
	}
}

func (p *project) writeCommitPage(base string, b branch, c commit) {
	dst := filepath.Join(base, "index.html")

	if _, err := os.Stat(dst); err == nil || errors.Is(err, fs.ErrExist) {
		log.Printf("commit %v already processed", c.Abbr)

		return
	}

	f, err := os.Create(dst)

	defer f.Close()

	if err != nil {
		log.Printf("unable to create commit page: %v", err)

		// TODO(spike): Handle error?
		return
	}

	page := page{
		Base: "../../",
		Data: Data{
			"Commit": c,
			"Path": Data{
				"Branch": b.Name,
			},
			"Project": p.Name,
		},
		Title: strings.Join([]string{p.Name, b.Name, c.Abbr}, ": "),
	}

	if err := p.template.Execute(f, page); err != nil {
		log.Printf("unable to apply template: %v", err)
	}
}