Brak opisu

quit.go 3.5KB

    package main import ( "fmt" "html/template" "log" "os" "strings" "time" "github.com/libgit2/git2go" ) func main() { repo, err := git.OpenRepository("/home/lu/t/libgit2") if err != nil { log.Fatal("opening repository: ", err) } head, err := repo.Head() if err != nil { log.Fatal("getting HEAD: ", err) } commit, err := repo.LookupCommit(head.Target()) if err != nil { log.Fatal("resolving HEAD: ", err) } fmt.Printf("Commit: %s\nAuthor: %s\nDate: %s\n\n%s", commit.TreeId(), commit.Author().Name, commit.Author().When, commit.Message()) tree, err := commit.Tree() if err != nil { log.Fatal("getting tree: ", err) } err = tree.Walk(func(name string, entry *git.TreeEntry) int { obj, err := repo.Lookup(entry.Id) if err != nil { log.Fatal("lookup: ", err) } fmt.Printf("%s (%s)\n", entry.Name, obj.Type()) return 1 }) if err != nil { log.Fatal("walking tree: ", err) } fmt.Printf("\n\n\n") err = repoTmpl.Execute(os.Stdout, map[string]interface{}{ "RepoPath": "~/t/libgit2", "Repo": NewFancyRepo(repo), }) if err != nil { log.Fatal("rendering repo: ", err) } } type FancyRepo struct { repo *git.Repository commit *FancyCommit } func NewFancyRepo(repo *git.Repository) *FancyRepo { return &FancyRepo{repo: repo} } func (fr *FancyRepo) Commit() (*FancyCommit, error) { if fr.commit == nil { head, err := fr.repo.Head() if err != nil { return nil, err } commit, err := fr.repo.LookupCommit(head.Target()) if err != nil { return nil, err } fr.commit = &FancyCommit{commit: commit} } return fr.commit, nil } type FancyCommit struct { commit *git.Commit files []*FancyFile } func (fc *FancyCommit) Author() string { return fc.commit.Author().Name } func (fc *FancyCommit) Date() time.Time { return fc.commit.Author().When } func (fc *FancyCommit) Id(n int) string { if n == 0 { return fc.commit.TreeId().String() } return fc.commit.TreeId().String()[:n] } func (fc *FancyCommit) Summary() string { return fc.commit.Summary() } func (fc *FancyCommit) Files() ([]*FancyFile, error) { if fc.files == nil { tree, err := fc.commit.Tree() if err != nil { return nil, err } err = tree.Walk(func(s string, entry *git.TreeEntry) int { fc.files = append(fc.files, &FancyFile{ repo: fc.commit.Owner(), entry: entry, }) return 1 }) if err != nil { return nil, err } } return fc.files, nil } type FancyFile struct { repo *git.Repository entry *git.TreeEntry commit *FancyCommit } func (fc *FancyFile) Name() string { return fc.entry.Name } func (fc *FancyFile) Commit() (*FancyCommit, error) { if fc.commit == nil { commit, err := fc.repo.LookupCommit(fc.entry.Id) if err != nil { return nil, err } fc.commit = &FancyCommit{commit: commit} } return fc.commit, nil } func (fc *FancyFile) Type() string { return strings.ToLower(fc.entry.Type.String()) } var repoTmpl = template.Must(template.New("").Parse(`<!doctype html> <html> <head> <meta charset="utf-8" /> <title>{{ .RepoPath }}</title> </head> <body> <section id="commit" class="commit-info"> <div class="commit-author">{{ .Repo.Commit.Author }}</div> <div class="commit-summary">{{ .Repo.Commit.Summary }}</div> <div class="commit-id" data-commit="{{ .Repo.Commit.Id 0 }}">{{ .Repo.Commit.Id 10 }}</div> <time class="commit-date">{{ .Repo.Commit.Date }}</time> </section> <table class="files"> {{ range $file := .Repo.Commit.Files }} <tr class="file"> <td class="file-type-{{ $file.Type }}">{{ $file.Name }}</td> </tr> {{ end }} </body> </html> `))