ソースを参照

Start rendering HTML

Lucas Stadler 8 年 前
コミット
739a67552f
共有1 個のファイルを変更した136 個の追加0 個の削除を含む
  1. 136 0
      quit.go

+ 136 - 0
quit.go

@ -2,7 +2,11 @@ package main
2 2
3 3
import (
4 4
	"fmt"
5
	"html/template"
5 6
	"log"
7
	"os"
8
	"strings"
9
	"time"
6 10
7 11
	"github.com/libgit2/git2go"
8 12
)
@ -41,4 +45,136 @@ func main() {
41 45
	if err != nil {
42 46
		log.Fatal("walking tree: ", err)
43 47
	}
48
49
	fmt.Printf("\n\n\n")
50
51
	err = repoTmpl.Execute(os.Stdout, map[string]interface{}{
52
		"RepoPath": "~/t/libgit2",
53
		"Repo":     NewFancyRepo(repo),
54
	})
55
	if err != nil {
56
		log.Fatal("rendering repo: ", err)
57
	}
58
}
59
60
type FancyRepo struct {
61
	repo   *git.Repository
62
	commit *FancyCommit
63
}
64
65
func NewFancyRepo(repo *git.Repository) *FancyRepo {
66
	return &FancyRepo{repo: repo}
67
}
68
69
func (fr *FancyRepo) Commit() (*FancyCommit, error) {
70
	if fr.commit == nil {
71
		head, err := fr.repo.Head()
72
		if err != nil {
73
			return nil, err
74
		}
75
76
		commit, err := fr.repo.LookupCommit(head.Target())
77
		if err != nil {
78
			return nil, err
79
		}
80
81
		fr.commit = &FancyCommit{commit: commit}
82
	}
83
	return fr.commit, nil
84
}
85
86
type FancyCommit struct {
87
	commit *git.Commit
88
	files  []*FancyFile
89
}
90
91
func (fc *FancyCommit) Author() string {
92
	return fc.commit.Author().Name
93
}
94
95
func (fc *FancyCommit) Date() time.Time {
96
	return fc.commit.Author().When
97
}
98
99
func (fc *FancyCommit) Id(n int) string {
100
	if n == 0 {
101
		return fc.commit.TreeId().String()
102
	}
103
	return fc.commit.TreeId().String()[:n]
104
}
105
106
func (fc *FancyCommit) Summary() string {
107
	return fc.commit.Summary()
108
}
109
110
func (fc *FancyCommit) Files() ([]*FancyFile, error) {
111
	if fc.files == nil {
112
		tree, err := fc.commit.Tree()
113
		if err != nil {
114
			return nil, err
115
		}
116
117
		err = tree.Walk(func(s string, entry *git.TreeEntry) int {
118
			fc.files = append(fc.files, &FancyFile{
119
				repo:  fc.commit.Owner(),
120
				entry: entry,
121
			})
122
			return 1
123
		})
124
		if err != nil {
125
			return nil, err
126
		}
127
	}
128
	return fc.files, nil
129
}
130
131
type FancyFile struct {
132
	repo   *git.Repository
133
	entry  *git.TreeEntry
134
	commit *FancyCommit
44 135
}
136
137
func (fc *FancyFile) Name() string {
138
	return fc.entry.Name
139
}
140
141
func (fc *FancyFile) Commit() (*FancyCommit, error) {
142
	if fc.commit == nil {
143
		commit, err := fc.repo.LookupCommit(fc.entry.Id)
144
		if err != nil {
145
			return nil, err
146
		}
147
148
		fc.commit = &FancyCommit{commit: commit}
149
	}
150
	return fc.commit, nil
151
}
152
153
func (fc *FancyFile) Type() string {
154
	return strings.ToLower(fc.entry.Type.String())
155
}
156
157
var repoTmpl = template.Must(template.New("").Parse(`<!doctype html>
158
<html>
159
	<head>
160
		<meta charset="utf-8" />
161
		<title>{{ .RepoPath }}</title>
162
	</head>
163
164
	<body>
165
		<section id="commit" class="commit-info">
166
			<div class="commit-author">{{ .Repo.Commit.Author }}</div>
167
			<div class="commit-summary">{{ .Repo.Commit.Summary }}</div>
168
			<div class="commit-id" data-commit="{{ .Repo.Commit.Id 0 }}">{{ .Repo.Commit.Id 10 }}</div>
169
			<time class="commit-date">{{ .Repo.Commit.Date }}</time>
170
		</section>
171
172
		<table class="files">
173
			{{ range $file := .Repo.Commit.Files }}
174
			<tr class="file">
175
				<td class="file-type-{{ $file.Type }}">{{ $file.Name }}</td>
176
			</tr>
177
			{{ end }}
178
	</body>
179
</html>
180
`))