← history for repobrowser/internal/git/git.go
a87fd589repobrowser/internal/git/git.go289 lines⬡ raw↓ download
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package git

import (
    "bytes"
    "fmt"
    "html/template"
    "net/url"
    "os"
    "os/exec"
    "path/filepath"
    "repobrowser/internal/config"
    "repobrowser/internal/models"
    "strings"

    chromalib "github.com/alecthomas/chroma/v2"
    chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
    "github.com/alecthomas/chroma/v2/lexers"
    "github.com/alecthomas/chroma/v2/styles"
)

func gitAt(path string, args ...string) (string, error) {
    cmd := exec.Command("git", append([]string{"-C", path}, args...)...)
    out, err := cmd.Output()
    return strings.TrimSpace(string(out)), err
}

func gitCmdStr(args ...string) string { return strings.Join(args, " ") }

func ListRepos(cfg config.RepositoryBrowserConfiguration) []models.RepoInfo {
    entries, err := os.ReadDir(cfg.Root)
    if err != nil {
        return nil
    }
    var repos []models.RepoInfo
    for _, e := range entries {
        if !e.IsDir() {
            continue
        }
        p := filepath.Join(cfg.Root, e.Name())
        // Must be a git repo
        if _, err := os.Stat(filepath.Join(p, ".git")); err != nil {
            // Also accept bare repos
            if _, err2 := os.Stat(filepath.Join(p, "HEAD")); err2 != nil {
                continue
            }
        }
        branch, _ := gitAt(p, "rev-parse", "--abbrev-ref", "HEAD")
        if branch == "" {
            branch = "main"
        }
        last, _ := gitAt(p, "log", "-1", "--pretty=format:%ar", branch)
        desc := ""
        if d, err := os.ReadFile(filepath.Join(p, "description")); err == nil {
            s := strings.TrimSpace(string(d))
            if !strings.HasPrefix(s, "Unnamed repository") {
                desc = s
            }
        }
        repos = append(repos, models.RepoInfo{
            Name:        e.Name(),
            Description: desc,
            LastCommit:  last,
            Branch:      branch,
        })
    }
    return repos
}

func ListBranches(rp string) ([]string, string) {
    out, err := gitAt(rp, "branch", "--format=%(refname:short)")
    if err != nil {
        return nil, "HEAD"
    }
    cur, _ := gitAt(rp, "rev-parse", "--abbrev-ref", "HEAD")
    var branches []string
    for _, b := range strings.Split(out, "\n") {
        b = strings.TrimSpace(b)
        if b != "" {
            branches = append(branches, b)
        }
    }
    if cur == "" {
        cur = "HEAD"
    }
    return branches, cur
}

func BuildBranchLinks(all []string, activeBr, repoName, tab string) []models.BranchLink {
    var links []models.BranchLink
    for _, b := range all {
        var u string
        switch tab {
        case "files":
            u = fmt.Sprintf("/%s/?branch=%s", repoName, url.QueryEscape(b))
        default:
            u = fmt.Sprintf("/%s/%s?branch=%s", repoName, tab, url.QueryEscape(b))
        }
        links = append(links, models.BranchLink{Name: b, URL: u, Active: b == activeBr})
    }
    return links
}

func ListFiles(rp, branch string) ([]models.FileEntry, string, error) {
    args := []string{"ls-tree", "-r", "--name-only", branch}
    out, err := gitAt(rp, args...)
    cmd := gitCmdStr(args...)
    if err != nil {
        return nil, cmd, err
    }
    var files []models.FileEntry
    for _, l := range strings.Split(out, "\n") {
        l = strings.TrimSpace(l)
        if l == "" {
            continue
        }
        countOut, _ := gitAt(rp, "log", "--oneline", "--follow", branch, "--", l)
        count := 0
        if countOut != "" {
            count = len(strings.Split(countOut, "\n"))
        }
        files = append(files, models.FileEntry{Path: l, CommitCount: count})
    }
    return files, cmd, nil
}

func ListLog(rp, branch string, limit int) ([]models.CommitEntry, string, error) {
    args := []string{"log", "--pretty=format:%H\x1f%s\x1f%an\x1f%ad", "--date=short",
        fmt.Sprintf("-%d", limit), branch}
    out, err := gitAt(rp, args...)
    cmd := gitCmdStr("log", fmt.Sprintf("-%d", limit), "--oneline", branch)
    if err != nil {
        return nil, cmd, err
    }
    return ParseCommits(out), cmd, nil
}

func ListRefs(rp string) ([]models.RefEntry, string, error) {
    args := []string{"for-each-ref",
        "--format=%(refname:short)\x1f%(objecttype)\x1f%(objectname:short)\x1f%(objectname)",
        "refs/heads", "refs/tags"}
    out, err := gitAt(rp, args...)
    cmd := gitCmdStr("for-each-ref", "refs/heads", "refs/tags")
    if err != nil {
        return nil, cmd, err
    }
    var refs []models.RefEntry
    for _, line := range strings.Split(out, "\n") {
        line = strings.TrimSpace(line)
        if line == "" {
            continue
        }
        parts := strings.SplitN(line, "\x1f", 4)
        if len(parts) < 4 {
            continue
        }
        kind := "branch"
        if parts[1] == "tag" {
            kind = "tag"
        }
        refs = append(refs, models.RefEntry{Kind: kind, Name: parts[0], Short: parts[2], Hash: parts[3]})
    }
    return refs, cmd, nil
}

func FileHistory(rp, path, branch string) ([]models.CommitEntry, string, error) {
    args := []string{"log", "--all", "--follow",
        "--pretty=format:%H\x1f%s\x1f%an\x1f%ad", "--date=short", branch, "--", path}
    out, err := gitAt(rp, args...)
    cmd := gitCmdStr("log", "--all", "--follow", "--oneline", branch, "--", path)
    if err != nil {
        return nil, cmd, err
    }
    return ParseCommits(out), cmd, nil
}

func FileAtCommit(rp, hash, path string) (string, string, error) {
    ref := fmt.Sprintf("%s:%s", hash, path)
    args := []string{"show", ref}
    out, err := gitAt(rp, args...)
    return out, gitCmdStr(args...), err
}

func CommitMeta(rp, hash string) (string, string) {
    meta, _ := gitAt(rp, "show", "--no-patch",
        "--pretty=format:%s\x1f%an\x1f%ae\x1f%ad", "--date=format:%Y-%m-%d %H:%M", hash)
    return meta, gitCmdStr("show", "--stat", hash)
}

func CommitFiles(rp, hash string) string {
    out, _ := gitAt(rp, "diff-tree", "--no-commit-id", "-r", "--name-status", hash)
    return out
}

func ParseCommits(out string) []models.CommitEntry {
    var commits []models.CommitEntry
    for _, line := range strings.Split(out, "\n") {
        line = strings.TrimSpace(line)
        if line == "" {
            continue
        }
        parts := strings.SplitN(line, "\x1f", 4)
        if len(parts) < 4 {
            continue
        }
        short := parts[0]
        if len(short) > 8 {
            short = short[:8]
        }
        commits = append(commits, models.CommitEntry{
            Hash: parts[0], Short: short,
            Message: parts[1], Author: parts[2], Date: parts[3],
        })
    }
    return commits
}

func FileIcon(name string) string {
    ext := ""
    if i := strings.LastIndex(name, "."); i >= 0 {
        ext = name[i+1:]
    }
    switch ext {
    case "go":
        return "🔵"
    case "js", "ts", "jsx", "tsx":
        return "🟡"
    case "py":
        return "🟢"
    case "md":
        return "📝"
    case "sh", "bash":
        return "⚙️"
    case "json", "yaml", "yml", "toml":
        return "🔧"
    case "html", "css":
        return "🌐"
    case "rs":
        return "🦀"
    case "c", "cpp", "h":
        return "🔩"
    }
    return "📄"
}

func HighlightCode(content, filename string) (string, string) {
    lexer := lexers.Match(filename)
    if lexer == nil {
        lexer = lexers.Analyse(content)
    }
    if lexer == nil {
        lexer = lexers.Fallback
    }
    lexer = chromalib.Coalesce(lexer)

    style := styles.Get("onedark")
    if style == nil {
        style = styles.Fallback
    }

    formatter := chromahtml.New(
        chromahtml.WithClasses(true),
        chromahtml.TabWidth(4),
        chromahtml.WithLineNumbers(true),
        chromahtml.LineNumbersInTable(true),
    )

    var cssBuf bytes.Buffer
    if err := formatter.WriteCSS(&cssBuf, style); err != nil {
        return plainFallback(content), ""
    }
    chromaCSS := cssBuf.String()

    iterator, err := lexer.Tokenise(nil, content)
    if err != nil {
        return plainFallback(content), chromaCSS
    }

    var hlBuf bytes.Buffer
    if err := formatter.Format(&hlBuf, style, iterator); err != nil {
        return plainFallback(content), chromaCSS
    }

    return hlBuf.String(), chromaCSS
}

func plainFallback(content string) string {
    return `<pre style="padding:1rem;overflow:auto;font-size:0.82rem;line-height:1.65">` +
        template.HTMLEscapeString(content) + `</pre>`
}