package history import ( "bytes" "fmt" "log" "os/exec" "strconv" "strings" "time" "github.com/bouncepaw/mycorrhiza/user" "github.com/bouncepaw/mycorrhiza/util" ) // Start initializes git credentials. func Start(wikiDir string) { _, err := gitsh("config", "user.name", "wikimind") if err != nil { log.Fatal(err) } _, err = gitsh("config", "user.email", "wikimind@mycorrhiza") if err != nil { log.Fatal(err) } } // Revision represents a revision, duh. Hash is usually short. Username is extracted from email. type Revision struct { Hash string Username string Time time.Time Message string } // TimeString returns a human readable time representation. func (rev Revision) TimeString() string { return rev.Time.Format(time.RFC822) } // HyphaeLinks returns a comma-separated list of hyphae that were affected by this revision as HTML string. func (rev Revision) HyphaeLinks() (html string) { // diff-tree --no-commit-id --name-only -r var ( // List of files affected by this revision, one per line. out, err = gitsh("diff-tree", "--no-commit-id", "--name-only", "-r", rev.Hash) // set is used to determine if a certain hypha has been already noted (hyphae are stored in 2 files at most). set = make(map[string]bool) isNewName = func(hyphaName string) bool { if _, present := set[hyphaName]; present { return false } else { set[hyphaName] = true return true } } ) if err != nil { return "" } for _, filename := range strings.Split(out.String(), "\n") { // If filename has an ampersand: if strings.IndexRune(filename, '.') >= 0 { // Remove ampersanded suffix from filename: ampersandPos := strings.LastIndexByte(filename, '.') hyphaName := string([]byte(filename)[0:ampersandPos]) // is it safe? if isNewName(hyphaName) { // Entries are separated by commas if len(set) > 1 { html += `%[1]s`, hyphaName) } } } return html } func (rev Revision) RecentChangesEntry() (html string) { if user.AuthUsed && rev.Username != "anon" { return fmt.Sprintf(`
` } html += fmt.Sprintf(`