package history // information.go // Things related to gathering existing information. import ( "fmt" "github.com/bouncepaw/mycorrhiza/cfg" "log" "regexp" "strconv" "strings" "time" "github.com/gorilla/feeds" ) func recentChangesFeed() *feeds.Feed { feed := &feeds.Feed{ Title: "Recent changes", Link: &feeds.Link{Href: cfg.URL}, Description: "List of 30 recent changes on the wiki", Author: &feeds.Author{Name: "Wikimind", Email: "wikimind@mycorrhiza"}, Updated: time.Now(), } var ( out, err = silentGitsh( "log", "--oneline", "--no-merges", "--pretty=format:\"%h\t%ae\t%at\t%s\"", "--max-count=30", ) revs []Revision ) if err == nil { for _, line := range strings.Split(out.String(), "\n") { revs = append(revs, parseRevisionLine(line)) } } log.Printf("Found %d recent changes", len(revs)) for _, rev := range revs { feed.Add(&feeds.Item{ Title: rev.Message, Author: &feeds.Author{Name: rev.Username}, Id: rev.Hash, Description: rev.descriptionForFeed(), Created: rev.Time, Updated: rev.Time, Link: &feeds.Link{Href: cfg.URL + rev.bestLink()}, }) } return feed } func RecentChangesRSS() (string, error) { return recentChangesFeed().ToRss() } func RecentChangesAtom() (string, error) { return recentChangesFeed().ToAtom() } func RecentChangesJSON() (string, error) { return recentChangesFeed().ToJSON() } func RecentChanges(n int) []Revision { var ( out, err = silentGitsh( "log", "--oneline", "--no-merges", "--pretty=format:\"%h\t%ae\t%at\t%s\"", "--max-count="+strconv.Itoa(n), ) revs []Revision ) if err == nil { for _, line := range strings.Split(out.String(), "\n") { revs = append(revs, parseRevisionLine(line)) } } log.Printf("Found %d recent changes", len(revs)) return revs } // FileChanged tells you if the file has been changed. func FileChanged(path string) bool { _, err := gitsh("diff", "--exit-code", path) return err != nil } // Revisions returns slice of revisions for the given hypha name. func Revisions(hyphaName string) ([]Revision, error) { var ( out, err = silentGitsh( "log", "--oneline", "--no-merges", // Hash, author email, author time, commit msg separated by tab "--pretty=format:\"%h\t%ae\t%at\t%s\"", "--", hyphaName+".*", ) revs []Revision ) if err == nil { for _, line := range strings.Split(out.String(), "\n") { if line != "" { revs = append(revs, parseRevisionLine(line)) } } } log.Printf("Found %d revisions for ā€˜%sā€™\n", len(revs), hyphaName) return revs, err } // HistoryWithRevisions returns an html representation of `revs` that is meant to be inserted in a history page. func HistoryWithRevisions(hyphaName string, revs []Revision) (html string) { var ( currentYear int currentMonth time.Month ) for i, rev := range revs { if rev.Time.Month() != currentMonth || rev.Time.Year() != currentYear { currentYear = rev.Time.Year() currentMonth = rev.Time.Month() if i != 0 { html += ` ` } html += fmt.Sprintf(`

%[3]s