2020-08-29 17:54:57 +00:00
|
|
|
// information.go
|
|
|
|
// Things related to gathering existing information.
|
2020-08-19 18:54:23 +00:00
|
|
|
package history
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2020-10-03 12:59:53 +00:00
|
|
|
"strconv"
|
2020-08-19 18:54:23 +00:00
|
|
|
"strings"
|
2020-09-26 18:19:17 +00:00
|
|
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/templates"
|
2020-08-19 18:54:23 +00:00
|
|
|
)
|
|
|
|
|
2020-09-26 18:19:17 +00:00
|
|
|
func RecentChanges(n int) string {
|
|
|
|
var (
|
|
|
|
out, err = gitsh(
|
|
|
|
"log", "--oneline", "--no-merges",
|
|
|
|
"--pretty=format:\"%h\t%ce\t%ct\t%s\"",
|
2020-10-03 12:59:53 +00:00
|
|
|
"--max-count="+strconv.Itoa(n),
|
2020-09-26 18:19:17 +00:00
|
|
|
)
|
|
|
|
revs []Revision
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
for _, line := range strings.Split(out.String(), "\n") {
|
|
|
|
revs = append(revs, parseRevisionLine(line))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entries := make([]string, len(revs))
|
|
|
|
for i, rev := range revs {
|
|
|
|
entries[i] = rev.RecentChangesEntry()
|
|
|
|
}
|
|
|
|
return templates.RecentChangesHTML(entries, n)
|
|
|
|
}
|
|
|
|
|
2020-08-29 17:54:57 +00:00
|
|
|
// Revisions returns slice of revisions for the given hypha name.
|
2020-08-28 19:10:46 +00:00
|
|
|
func Revisions(hyphaName string) ([]Revision, error) {
|
2020-08-19 18:54:23 +00:00
|
|
|
var (
|
|
|
|
out, err = gitsh(
|
|
|
|
"log", "--oneline", "--no-merges",
|
|
|
|
// Hash, Commiter email, Commiter time, Commit msg separated by tab
|
|
|
|
"--pretty=format:\"%h\t%ce\t%ct\t%s\"",
|
2020-10-25 13:50:14 +00:00
|
|
|
"--", hyphaName+".*",
|
2020-08-19 18:54:23 +00:00
|
|
|
)
|
|
|
|
revs []Revision
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
for _, line := range strings.Split(out.String(), "\n") {
|
2020-09-29 15:04:22 +00:00
|
|
|
if line != "" {
|
|
|
|
revs = append(revs, parseRevisionLine(line))
|
|
|
|
}
|
2020-08-19 18:54:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return revs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This regex is wrapped in "". For some reason, these quotes appear at some time and we have to get rid of them.
|
|
|
|
var revisionLinePattern = regexp.MustCompile("\"(.*)\t(.*)@.*\t(.*)\t(.*)\"")
|
|
|
|
|
2020-08-29 17:54:57 +00:00
|
|
|
func parseRevisionLine(line string) Revision {
|
|
|
|
results := revisionLinePattern.FindStringSubmatch(line)
|
|
|
|
return Revision{
|
|
|
|
Hash: results[1],
|
|
|
|
Username: results[2],
|
|
|
|
Time: *unixTimestampAsTime(results[3]),
|
|
|
|
Message: results[4],
|
|
|
|
}
|
2020-08-19 18:54:23 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 17:54:57 +00:00
|
|
|
// Represent revision as a table row.
|
2020-08-20 17:20:13 +00:00
|
|
|
func (rev *Revision) AsHtmlTableRow(hyphaName string) string {
|
2020-08-19 18:54:23 +00:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
<tr>
|
2020-08-29 17:54:57 +00:00
|
|
|
<td><time>%s</time></td>
|
2020-08-20 17:20:13 +00:00
|
|
|
<td><a href="/rev/%s/%s">%s</a></td>
|
2020-08-19 18:54:23 +00:00
|
|
|
<td>%s</td>
|
2020-09-26 18:19:17 +00:00
|
|
|
</tr>`, rev.TimeString(), rev.Hash, hyphaName, rev.Hash, rev.Message)
|
2020-08-20 17:20:13 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 17:54:57 +00:00
|
|
|
// See how the file with `filepath` looked at commit with `hash`.
|
2020-08-20 17:20:13 +00:00
|
|
|
func FileAtRevision(filepath, hash string) (string, error) {
|
|
|
|
out, err := gitsh("show", hash+":"+filepath)
|
|
|
|
return out.String(), err
|
2020-08-19 18:54:23 +00:00
|
|
|
}
|