shroomgit

generate static pages of git repos
git clone https://git.davidvoz.net/shroomgit.git
index
logs
tree
license

logs.go
1package main
2
3import (
4 "fmt"
5 "html"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strings"
10)
11
12// overall awful
13func logTest(repo string, path string, numofCommits int) {
14 topPart := genTopPart(repo, 1)
15 logsDir := filepath.Join(path, "logs")
16
17 err := os.MkdirAll(logsDir, 0o755)
18 if err != nil {
19 panic(err)
20 }
21
22 // TODO add coloring maybe
23 for i := range numofCommits {
24 cmd, err := exec.Command(
25 "git",
26 "-C", repo,
27 "show", "-1",
28 fmt.Sprintf("--skip=%d", i),
29 ).Output()
30 if err != nil {
31 panic(err)
32 }
33
34 lines := strings.Split(string(cmd), "\n")
35
36 commitHash := strings.Fields(lines[0])[1]
37
38 outputFile := filepath.Join(logsDir, fmt.Sprintf("%s.html", commitHash))
39 file, _ := os.Create(outputFile)
40 defer file.Close()
41
42 file.WriteString(topPart)
43
44 commitPageWriter(file, lines)
45
46 }
47}
48
49func commitPageWriter(file *os.File, lines []string) {
50 file.WriteString("<pre style=\"padding:6px;border-radius:4px;font-family:monospace;\">\n")
51
52 for _, line := range lines {
53 escaped := html.EscapeString(line)
54 if strings.HasPrefix(line, "+") {
55 file.WriteString("<div style=\"color:ForestGreen\">")
56 file.WriteString(escaped)
57 file.WriteString("</div>")
58 } else if strings.HasPrefix(line, "-") {
59 file.WriteString("<div style=\"color:#e54e50\">")
60 file.WriteString(escaped)
61 file.WriteString("</div>")
62 } else {
63 file.WriteString(escaped)
64 file.WriteString("\n")
65 }
66 }
67
68 file.WriteString("</pre>\n")
69}
70
71func logPage(topPart string, repo string, path string, numofCommits int) {
72 outputFile := filepath.Join(path, "log.html")
73 file, err := os.Create(outputFile)
74 if err != nil {
75 panic(err)
76 }
77 defer file.Close()
78
79 file.WriteString(topPart)
80 file.WriteString("<table>")
81
82 for i := range numofCommits {
83 file.WriteString("<tr>")
84
85 cmd, err := exec.Command(
86 "git",
87 "-C", repo,
88 "log", "-1",
89 fmt.Sprintf("--skip=%d", i),
90 "--shortstat",
91 "--pretty=format:%ai%n%an%n%s%n%H",
92 ).Output()
93 if err != nil {
94 panic(err)
95 }
96
97 commitInfo := strings.Split(string(cmd), "\n")
98 timeofCommit := commitInfo[0]
99 authorofCommit := commitInfo[1]
100 commitMessageTest := commitInfo[2]
101 commitHash := commitInfo[3]
102 shortStatInfo := commitInfo[4]
103
104 file.WriteString("<td valign=\"top\">")
105
106 dateofCommit := strings.SplitN(timeofCommit, " ", 2)[0]
107 file.WriteString(dateofCommit) // YYYY-MM-DD
108
109 file.WriteString("</td>\n<td>\n<a href=\"logs/")
110 file.WriteString(commitHash)
111 file.WriteString(".html\">" + commitMessageTest + "</a>\n")
112 file.WriteString("</td>\n")
113
114 file.WriteString("<td style=\"white-space:nowrap\" valign=\"top\">")
115 file.WriteString(authorofCommit)
116 file.WriteString("</td>\n")
117
118 cmdOutput := strings.TrimSpace(string(shortStatInfo))
119 arr := strings.Fields(cmdOutput)
120 if len(arr) == 0 {
121 continue
122 }
123 file.WriteString("<td style=\"padding-left: 1em\" valign=\"top\" align=\"right\">")
124 file.WriteString(arr[0]) // files edited
125 file.WriteString("</td>\n")
126 file.WriteString("<td style=\"color:ForestGreen\" valign=\"top\" align=\"right\">+")
127 file.WriteString(arr[3]) // lines added
128 file.WriteString("</td>\n")
129 if len(arr) > 5 {
130 file.WriteString("<td style=\"color:Crimson\" valign=\"top\" align=\"right\">-")
131 file.WriteString(arr[5]) // lines removed
132 file.WriteString("</td>\n")
133 }
134
135 file.WriteString("\n</tr>")
136 }
137
138 file.WriteString("\n</table>\n</body>")
139}