| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "context" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "net/http" |
| 11 | "net/url" |
| 12 | "strconv" |
| 13 | "strings" |
| 14 | "unicode" |
| 15 | "unicode/utf8" |
| 16 | |
| 17 | "the-valley/webui/export" |
| 18 | ) |
| 19 | |
| 20 | // treeURL is the directory at path in commit. It ends in a slash. |
| 21 | func treeURL(project, commit string, path []string) string { |
| 22 | u := "/source/" + url.PathEscape(project) + "/tree/" + commit + "/" |
| 23 | if len(path) > 0 { |
| 24 | u += escapePath(path) + "/" |
| 25 | } |
| 26 | return u |
| 27 | } |
| 28 | |
| 29 | // blobURL is the file at path in commit. |
| 30 | func blobURL(project, commit string, path []string) string { |
| 31 | return "/source/" + url.PathEscape(project) + "/blob/" + commit + "/" + escapePath(path) |
| 32 | } |
| 33 | |
| 34 | // logURL is the history that ends at commit. |
| 35 | func logURL(project, commit string) string { |
| 36 | return "/source/" + url.PathEscape(project) + "/log/" + commit |
| 37 | } |
| 38 | |
| 39 | func escapePath(path []string) string { |
| 40 | parts := make([]string, len(path)) |
| 41 | for i, s := range path { |
| 42 | parts[i] = url.PathEscape(s) |
| 43 | } |
| 44 | return strings.Join(parts, "/") |
| 45 | } |
| 46 | |
| 47 | // source serves the routes under /source/<project>/. The project is |
| 48 | // checked first, so everything under a project the export does not hold |
| 49 | // as public answers as a missing project does. |
| 50 | func (q *request) source(project string, rest []string) { |
| 51 | p, ok := q.project(project) |
| 52 | if !ok { |
| 53 | q.notFound() |
| 54 | return |
| 55 | } |
| 56 | repo, ok := q.g.Repo(p.Name) |
| 57 | if !ok { |
| 58 | q.notFound() |
| 59 | return |
| 60 | } |
| 61 | var view func(ctx context.Context) error |
| 62 | switch { |
| 63 | case len(rest) == 0 || len(rest) == 1 && rest[0] == "": |
| 64 | http.Redirect(q.w, q.r, treeURL(p.Name, p.SourceTip, nil), http.StatusFound) |
| 65 | return |
| 66 | case len(rest) >= 2 && rest[0] == "tree": |
| 67 | path := rest[2:] |
| 68 | if n := len(path); n > 0 && path[n-1] == "" { |
| 69 | path = path[:n-1] |
| 70 | } |
| 71 | if !validPath(path) { |
| 72 | q.notFound() |
| 73 | return |
| 74 | } |
| 75 | view = func(ctx context.Context) error { return q.dir(ctx, p, repo, rest[1], path) } |
| 76 | case len(rest) >= 3 && rest[0] == "blob": |
| 77 | path := rest[2:] |
| 78 | if !validPath(path) { |
| 79 | q.notFound() |
| 80 | return |
| 81 | } |
| 82 | view = func(ctx context.Context) error { return q.file(ctx, p, repo, rest[1], path) } |
| 83 | case len(rest) == 2 && rest[0] == "log": |
| 84 | view = func(ctx context.Context) error { return q.log(ctx, p, repo, rest[1]) } |
| 85 | default: |
| 86 | q.notFound() |
| 87 | return |
| 88 | } |
| 89 | ctx, done, err := q.gitContext() |
| 90 | if err != nil { |
| 91 | return // the reader went away |
| 92 | } |
| 93 | defer done() |
| 94 | if err := view(ctx); err != nil { |
| 95 | q.fail(err) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // validPath is a path a source route may name: at most MaxDepth |
| 100 | // segments, none of them empty, a dot segment, or holding a slash or a |
| 101 | // NUL. |
| 102 | func validPath(path []string) bool { |
| 103 | if len(path) > MaxDepth { |
| 104 | return false |
| 105 | } |
| 106 | for _, s := range path { |
| 107 | if s == "" || s == "." || s == ".." || strings.ContainsAny(s, "/\x00") { |
| 108 | return false |
| 109 | } |
| 110 | } |
| 111 | return true |
| 112 | } |
| 113 | |
| 114 | // commitTree is the tree of commit, when commit is a commit the |
| 115 | // project's published source tip reaches. ok is false for anything else: |
| 116 | // an unreachable commit, another kind of object, or text that is no full |
| 117 | // object ID. |
| 118 | func commitTree(ctx context.Context, p *export.Project, repo, commit string) (tree string, ok bool, err error) { |
| 119 | reachable, err := export.Reachable(ctx, repo, p.SourceTip, commit) |
| 120 | if err != nil || !reachable { |
| 121 | return "", false, err |
| 122 | } |
| 123 | out, err := export.Git(ctx, repo, maxLine, "rev-parse", "--verify", commit+"^{tree}") |
| 124 | if err != nil { |
| 125 | return "", false, err |
| 126 | } |
| 127 | tree = strings.TrimSpace(string(out)) |
| 128 | if !export.ValidObjectID(tree) { |
| 129 | return "", false, fmt.Errorf("the tree of %s is %q", commit, tree) |
| 130 | } |
| 131 | return tree, true, nil |
| 132 | } |
| 133 | |
| 134 | // The bounds on what a page keeps of one record of git's output. |
| 135 | const ( |
| 136 | // maxLine bounds what git prints for one object ID. |
| 137 | maxLine = 1 << 10 |
| 138 | // maxEntryRecord bounds one entry of git ls-tree: its mode, type, |
| 139 | // object ID and size, and its name. git fsck refuses a name longer |
| 140 | // than 4,096 bytes, so the receiver accepts no entry this cuts. |
| 141 | maxEntryRecord = 8 << 10 |
| 142 | // maxLogRecord bounds one commit of git log: its ID, date, author |
| 143 | // and subject. A page shows maxLogText of the author and subject. |
| 144 | maxLogRecord = 16 << 10 |
| 145 | maxLogText = 1 << 10 |
| 146 | ) |
| 147 | |
| 148 | // records reads the records of git's output that end in sep, and keeps |
| 149 | // at most max bytes of each. |
| 150 | type records struct { |
| 151 | r *bufio.Reader |
| 152 | sep byte |
| 153 | buf []byte |
| 154 | } |
| 155 | |
| 156 | func newRecords(r io.Reader, sep byte, max int) *records { |
| 157 | return &records{r: bufio.NewReaderSize(r, max), sep: sep} |
| 158 | } |
| 159 | |
| 160 | // next is the next record, without its sep. A record longer than the |
| 161 | // bound is cut to it, with cut set, and the rest of it is read and |
| 162 | // dropped. At the end of the output next fails with io.EOF. On any other |
| 163 | // error, rec is the start of the record the error cut short. |
| 164 | func (rs *records) next() (rec []byte, cut bool, err error) { |
| 165 | line, err := rs.r.ReadSlice(rs.sep) |
| 166 | rs.buf = append(rs.buf[:0], line...) |
| 167 | for err == bufio.ErrBufferFull { |
| 168 | cut = true |
| 169 | _, err = rs.r.ReadSlice(rs.sep) |
| 170 | } |
| 171 | switch { |
| 172 | case err == io.EOF && len(rs.buf) > 0: |
| 173 | return rs.buf, cut, nil // a last record git did not end |
| 174 | case err != nil: |
| 175 | return rs.buf, cut, err |
| 176 | case !cut: |
| 177 | return rs.buf[:len(rs.buf)-1], false, nil |
| 178 | } |
| 179 | return rs.buf, true, nil |
| 180 | } |
| 181 | |
| 182 | // entry is one entry of a tree, as git ls-tree --long gives it. Size is |
| 183 | // -1 for an entry that is not a blob. Cut is set when the entry's name is |
| 184 | // longer than a page keeps, and Name is then its start alone. |
| 185 | type entry struct { |
| 186 | Mode string |
| 187 | Type string |
| 188 | OID string |
| 189 | Size int64 |
| 190 | Name string |
| 191 | Cut bool |
| 192 | } |
| 193 | |
| 194 | // readTree calls each with the entries of tree, in git's order, until |
| 195 | // each returns false. It reads at most MaxRead bytes of git ls-tree's |
| 196 | // output: complete is false when the listing is longer and was cut there. |
| 197 | func readTree(ctx context.Context, repo, tree string, each func(entry) bool) (complete bool, err error) { |
| 198 | complete = true |
| 199 | err = export.GitStream(ctx, repo, MaxRead, func(r io.Reader) error { |
| 200 | recs := newRecords(r, 0, maxEntryRecord) |
| 201 | for { |
| 202 | rec, cut, err := recs.next() |
| 203 | switch { |
| 204 | case err == io.EOF: |
| 205 | return nil |
| 206 | case errors.Is(err, export.ErrOutputLimit): |
| 207 | complete = false |
| 208 | return nil |
| 209 | case err != nil: |
| 210 | return err |
| 211 | } |
| 212 | e, err := parseEntry(rec, cut) |
| 213 | if err != nil { |
| 214 | return err |
| 215 | } |
| 216 | if !each(e) { |
| 217 | return nil |
| 218 | } |
| 219 | } |
| 220 | }, "ls-tree", "-z", "--long", tree) |
| 221 | return complete, err |
| 222 | } |
| 223 | |
| 224 | func parseEntry(rec []byte, cut bool) (entry, error) { |
| 225 | meta, name, ok := bytes.Cut(rec, []byte{'\t'}) |
| 226 | f := strings.Fields(string(meta)) |
| 227 | if !ok || len(f) != 4 || !export.ValidObjectID(f[2]) { |
| 228 | return entry{}, fmt.Errorf("git ls-tree gave %q", rec) |
| 229 | } |
| 230 | e := entry{Mode: f[0], Type: f[1], OID: f[2], Size: -1, Name: string(name), Cut: cut} |
| 231 | if f[3] != "-" { |
| 232 | n, err := strconv.ParseInt(f[3], 10, 64) |
| 233 | if err != nil || n < 0 { |
| 234 | return entry{}, fmt.Errorf("git ls-tree gave %q", rec) |
| 235 | } |
| 236 | e.Size = n |
| 237 | } |
| 238 | return e, nil |
| 239 | } |
| 240 | |
| 241 | // resolve walks path down from the tree root, one tree entry at a time, |
| 242 | // and gives the entry it ends at. ok is false when a segment names no |
| 243 | // entry, or a segment other than the last names something that is not a |
| 244 | // tree. Each directory is read only until the segment's entry, and no |
| 245 | // further than readTree reads, so an entry past MaxRead is not found. |
| 246 | func resolve(ctx context.Context, repo, root string, path []string) (e entry, ok bool, err error) { |
| 247 | e = entry{Mode: "040000", Type: "tree", OID: root, Size: -1} |
| 248 | for _, name := range path { |
| 249 | if e.Type != "tree" { |
| 250 | return entry{}, false, nil |
| 251 | } |
| 252 | found := false |
| 253 | _, err := readTree(ctx, repo, e.OID, func(c entry) bool { |
| 254 | if !c.Cut && c.Name == name { |
| 255 | e, found = c, true |
| 256 | } |
| 257 | return !found |
| 258 | }) |
| 259 | if err != nil || !found { |
| 260 | return entry{}, false, err |
| 261 | } |
| 262 | } |
| 263 | return e, true, nil |
| 264 | } |
| 265 | |
| 266 | // Entry modes git gives for a symbolic link and a submodule. |
| 267 | const ( |
| 268 | modeSymlink = "120000" |
| 269 | modeSubmodule = "160000" |
| 270 | ) |
| 271 | |
| 272 | // shown is how a page shows a blob: its text, or a notice that says why |
| 273 | // it is not shown. A symbolic link is shown as its target and is never |
| 274 | // followed. |
| 275 | func shown(ctx context.Context, repo string, e entry) (text, notice string, err error) { |
| 276 | if e.Size > MaxFile { |
| 277 | return "", fmt.Sprintf("This file is %s, larger than the %s a page shows.", size(e.Size), size(MaxFile)), nil |
| 278 | } |
| 279 | data, err := export.Git(ctx, repo, MaxFile, "cat-file", "blob", e.OID) |
| 280 | if err != nil { |
| 281 | return "", "", err |
| 282 | } |
| 283 | if e.Mode == modeSymlink { |
| 284 | return "", "A symbolic link to " + oneLine(string(data)) + ". It is not followed.", nil |
| 285 | } |
| 286 | if bytes.IndexByte(data, 0) >= 0 || !utf8.Valid(data) { |
| 287 | return "", fmt.Sprintf("This file is binary, %s, and is not shown.", size(e.Size)), nil |
| 288 | } |
| 289 | return string(data), "", nil |
| 290 | } |
| 291 | |
| 292 | // dirPage is one directory of a commit. |
| 293 | type dirPage struct { |
| 294 | Project string |
| 295 | Commit string |
| 296 | Short string |
| 297 | AtTip bool |
| 298 | TipURL string |
| 299 | LogURL string |
| 300 | Crumbs []link |
| 301 | Entries []dirEntry |
| 302 | Omitted int |
| 303 | // Partial is set when the directory is larger than a page reads, |
| 304 | // and only its start is listed. |
| 305 | Partial bool |
| 306 | Readme *readme |
| 307 | } |
| 308 | |
| 309 | type dirEntry struct { |
| 310 | Name string |
| 311 | URL string |
| 312 | Kind string |
| 313 | Size string |
| 314 | } |
| 315 | |
| 316 | type readme struct { |
| 317 | Name string |
| 318 | URL string |
| 319 | Text string |
| 320 | Notice string |
| 321 | // Partial is set when the README is longer than a page shows. |
| 322 | Partial bool |
| 323 | } |
| 324 | |
| 325 | func (q *request) dir(ctx context.Context, p *export.Project, repo, commit string, path []string) error { |
| 326 | root, ok, err := commitTree(ctx, p, repo, commit) |
| 327 | if err != nil || !ok { |
| 328 | q.missing(err) |
| 329 | return nil |
| 330 | } |
| 331 | e, ok, err := resolve(ctx, repo, root, path) |
| 332 | if err != nil || !ok || e.Type != "tree" { |
| 333 | q.missing(err) |
| 334 | return nil |
| 335 | } |
| 336 | // Directories come first, then everything else, each in git's |
| 337 | // order. Keeping MaxEntries of each keeps the first MaxEntries of |
| 338 | // that order, however many entries the tree holds. |
| 339 | var dirs, others []entry |
| 340 | read, rank := 0, -1 |
| 341 | var rm entry |
| 342 | complete, err := readTree(ctx, repo, e.OID, func(c entry) bool { |
| 343 | read++ |
| 344 | switch { |
| 345 | case c.Type == "tree" && len(dirs) < MaxEntries: |
| 346 | dirs = append(dirs, c) |
| 347 | case c.Type != "tree" && len(others) < MaxEntries: |
| 348 | others = append(others, c) |
| 349 | } |
| 350 | if r := readmeRank(c); r >= 0 && (rank < 0 || r < rank) { |
| 351 | rm, rank = c, r |
| 352 | } |
| 353 | return true |
| 354 | }) |
| 355 | if err != nil { |
| 356 | return err |
| 357 | } |
| 358 | entries := append(dirs, others...) |
| 359 | if len(entries) > MaxEntries { |
| 360 | entries = entries[:MaxEntries] |
| 361 | } |
| 362 | page := dirPage{Project: p.Name, Commit: commit, Short: commit[:12], AtTip: commit == p.SourceTip, |
| 363 | TipURL: treeURL(p.Name, p.SourceTip, nil), LogURL: logURL(p.Name, commit), |
| 364 | Crumbs: crumbs(p.Name, commit, path), Omitted: read - len(entries), Partial: !complete} |
| 365 | for _, c := range entries { |
| 366 | at := append(append([]string{}, path...), c.Name) |
| 367 | d := dirEntry{Name: oneLine(c.Name)} |
| 368 | switch { |
| 369 | case c.Type == "tree": |
| 370 | d.Kind, d.URL = "directory", treeURL(p.Name, commit, at) |
| 371 | case c.Mode == modeSubmodule || c.Type == "commit": |
| 372 | d.Kind = "submodule at " + c.OID[:12] |
| 373 | case c.Mode == modeSymlink: |
| 374 | d.Kind, d.URL = "symbolic link", blobURL(p.Name, commit, at) |
| 375 | default: |
| 376 | d.Kind, d.URL, d.Size = "file", blobURL(p.Name, commit, at), size(c.Size) |
| 377 | } |
| 378 | if c.Cut { |
| 379 | d.Name += "…" |
| 380 | } |
| 381 | if c.Cut || !validPath(at) { |
| 382 | d.URL = "" // a name no route takes is listed and not linked |
| 383 | } |
| 384 | page.Entries = append(page.Entries, d) |
| 385 | } |
| 386 | if rank >= 0 { |
| 387 | text, notice, err := shown(ctx, repo, rm) |
| 388 | if err != nil { |
| 389 | return err |
| 390 | } |
| 391 | lines, _, partial := excerpt(trimEnd(text), 1) |
| 392 | at := append(append([]string{}, path...), rm.Name) |
| 393 | page.Readme = &readme{Name: oneLine(rm.Name), URL: blobURL(p.Name, commit, at), |
| 394 | Text: strings.Join(lines, "\n"), Notice: notice, Partial: partial} |
| 395 | } |
| 396 | title := p.Name + " at " + commit[:12] |
| 397 | if len(path) > 0 { |
| 398 | title = strings.Join(path, "/") + " · " + title |
| 399 | } |
| 400 | q.page(http.StatusOK, "tree", title, page) |
| 401 | return nil |
| 402 | } |
| 403 | |
| 404 | // readmeRank is how a directory's README is chosen from its entries: |
| 405 | // README.md, README or README.txt, in that order, and otherwise the first |
| 406 | // file whose name is README in any case, with any extension. A lower rank |
| 407 | // is chosen first. It is -1 for an entry that is no README. |
| 408 | func readmeRank(e entry) int { |
| 409 | if e.Type != "blob" || e.Mode == modeSymlink || e.Cut { |
| 410 | return -1 |
| 411 | } |
| 412 | switch e.Name { |
| 413 | case "README.md": |
| 414 | return 0 |
| 415 | case "README": |
| 416 | return 1 |
| 417 | case "README.txt": |
| 418 | return 2 |
| 419 | } |
| 420 | if base, _, _ := strings.Cut(strings.ToLower(e.Name), "."); base == "readme" { |
| 421 | return 3 |
| 422 | } |
| 423 | return -1 |
| 424 | } |
| 425 | |
| 426 | // filePage is one file of a commit. |
| 427 | type filePage struct { |
| 428 | Project string |
| 429 | Commit string |
| 430 | Short string |
| 431 | AtTip bool |
| 432 | TipURL string |
| 433 | LogURL string |
| 434 | Crumbs []link |
| 435 | Name string |
| 436 | Size string |
| 437 | Lines []string |
| 438 | // Partial says where the page stops, when the file is longer than a |
| 439 | // page shows. |
| 440 | Partial string |
| 441 | Notice string |
| 442 | } |
| 443 | |
| 444 | func (q *request) file(ctx context.Context, p *export.Project, repo, commit string, path []string) error { |
| 445 | root, ok, err := commitTree(ctx, p, repo, commit) |
| 446 | if err != nil || !ok { |
| 447 | q.missing(err) |
| 448 | return nil |
| 449 | } |
| 450 | e, ok, err := resolve(ctx, repo, root, path) |
| 451 | if err != nil || !ok || e.Type != "blob" { |
| 452 | q.missing(err) |
| 453 | return nil |
| 454 | } |
| 455 | text, notice, err := shown(ctx, repo, e) |
| 456 | if err != nil { |
| 457 | return err |
| 458 | } |
| 459 | page := filePage{Project: p.Name, Commit: commit, Short: commit[:12], AtTip: commit == p.SourceTip, |
| 460 | TipURL: treeURL(p.Name, p.SourceTip, nil), LogURL: logURL(p.Name, commit), |
| 461 | Crumbs: crumbs(p.Name, commit, path), Name: oneLine(path[len(path)-1]), |
| 462 | Size: size(e.Size), Notice: notice} |
| 463 | if notice == "" { |
| 464 | lines, total, partial := excerpt(text, rowCost) |
| 465 | page.Lines = lines |
| 466 | if partial { |
| 467 | page.Partial = fmt.Sprintf("This file is longer than a page shows. The page stops at line %d of %d.", len(lines), total) |
| 468 | } |
| 469 | } |
| 470 | q.page(http.StatusOK, "blob", strings.Join(path, "/")+" · "+p.Name+" at "+commit[:12], page) |
| 471 | return nil |
| 472 | } |
| 473 | |
| 474 | // crumbs are the links from the project's root down to path. The last is |
| 475 | // the page itself and has no link. |
| 476 | func crumbs(project, commit string, path []string) []link { |
| 477 | out := []link{{project, treeURL(project, commit, nil)}} |
| 478 | for i, name := range path { |
| 479 | out = append(out, link{oneLine(name), treeURL(project, commit, path[:i+1])}) |
| 480 | } |
| 481 | out[len(out)-1].URL = "" |
| 482 | return out |
| 483 | } |
| 484 | |
| 485 | // rowCost is what a file page's markup takes for each line it shows, |
| 486 | // besides the line's text: a table row, an anchor and the line number. |
| 487 | // It is an upper bound for line numbers of up to seven digits. |
| 488 | const rowCost = 100 |
| 489 | |
| 490 | // excerpt is the start of text that a page shows: whole lines, at most |
| 491 | // MaxLines of them, while the bytes they take once escaped, with cost |
| 492 | // more for each line, stay within MaxShown. A line that would pass |
| 493 | // MaxShown is cut to fit, and ends the excerpt. total is how many lines |
| 494 | // text holds, and partial is set when the excerpt holds less than all of |
| 495 | // it. |
| 496 | func excerpt(text string, cost int) (lines []string, total int, partial bool) { |
| 497 | text = strings.TrimSuffix(text, "\n") |
| 498 | total = strings.Count(text, "\n") + 1 |
| 499 | budget := MaxShown |
| 500 | for rest := text; len(lines) < MaxLines; { |
| 501 | line, more, found := strings.Cut(rest, "\n") |
| 502 | n := cost + escapedSize(line) |
| 503 | if n > budget { |
| 504 | if room := budget - cost; room > 0 { |
| 505 | lines = append(lines, cutEscaped(line, room)) |
| 506 | } |
| 507 | return lines, total, true |
| 508 | } |
| 509 | budget -= n |
| 510 | lines = append(lines, line) |
| 511 | if !found { |
| 512 | return lines, total, false |
| 513 | } |
| 514 | rest = more |
| 515 | } |
| 516 | return lines, total, true |
| 517 | } |
| 518 | |
| 519 | // escapedSize is an upper bound on the bytes html/template writes for s |
| 520 | // as text: five for each character it escapes, and one for any other |
| 521 | // byte. |
| 522 | func escapedSize(s string) int { |
| 523 | n := len(s) |
| 524 | for i := 0; i < len(s); i++ { |
| 525 | switch s[i] { |
| 526 | case '"', '\'', '&', '+', '<', '>', 0: |
| 527 | n += 4 |
| 528 | } |
| 529 | } |
| 530 | return n |
| 531 | } |
| 532 | |
| 533 | // cutEscaped is the longest start of s that ends on a whole character |
| 534 | // and that escapedSize counts at no more than room bytes. |
| 535 | func cutEscaped(s string, room int) string { |
| 536 | n := 0 |
| 537 | for i := 0; i < len(s); i++ { |
| 538 | if n += escapedSize(s[i : i+1]); n > room { |
| 539 | for i > 0 && !utf8.RuneStart(s[i]) { |
| 540 | i-- |
| 541 | } |
| 542 | return s[:i] |
| 543 | } |
| 544 | } |
| 545 | return s |
| 546 | } |
| 547 | |
| 548 | // clip is s cut to at most n bytes, on a whole character, and marked as |
| 549 | // cut when it is. |
| 550 | func clip(s string, n int) string { |
| 551 | if len(s) <= n { |
| 552 | return s |
| 553 | } |
| 554 | for n > 0 && !utf8.RuneStart(s[n]) { |
| 555 | n-- |
| 556 | } |
| 557 | return s[:n] + "…" |
| 558 | } |
| 559 | |
| 560 | // logPage is one page of the history that ends at a commit. |
| 561 | type logPage struct { |
| 562 | Project string |
| 563 | Commit string |
| 564 | Short string |
| 565 | AtTip bool |
| 566 | Commits []logEntry |
| 567 | // Partial is set when the page's commits are larger than a page |
| 568 | // reads, and the page ends early. |
| 569 | Partial bool |
| 570 | Newer string |
| 571 | Older string |
| 572 | } |
| 573 | |
| 574 | type logEntry struct { |
| 575 | Short string |
| 576 | URL string |
| 577 | Subject string |
| 578 | Author string |
| 579 | Date string |
| 580 | } |
| 581 | |
| 582 | func (q *request) log(ctx context.Context, p *export.Project, repo, commit string) error { |
| 583 | skip := 0 |
| 584 | if s := q.r.URL.Query().Get("skip"); s != "" { |
| 585 | n, err := strconv.Atoi(s) |
| 586 | if err != nil || n < 0 || n > 1_000_000 || strconv.Itoa(n) != s { |
| 587 | q.notFound() |
| 588 | return nil |
| 589 | } |
| 590 | skip = n |
| 591 | } |
| 592 | if _, ok, err := commitTree(ctx, p, repo, commit); err != nil || !ok { |
| 593 | q.missing(err) |
| 594 | return nil |
| 595 | } |
| 596 | // One commit past the page says whether there is an older page. A |
| 597 | // listing cut at MaxRead ends the page at the commit it cut, shown |
| 598 | // as far as it was read, and the older page starts after it. |
| 599 | page := logPage{Project: p.Name, Commit: commit, Short: commit[:12], AtTip: commit == p.SourceTip} |
| 600 | err := export.GitStream(ctx, repo, MaxRead, func(r io.Reader) error { |
| 601 | recs := newRecords(r, 0, maxLogRecord) |
| 602 | for len(page.Commits) <= LogPage { |
| 603 | rec, _, err := recs.next() |
| 604 | cut := errors.Is(err, export.ErrOutputLimit) |
| 605 | if err == io.EOF { |
| 606 | return nil |
| 607 | } |
| 608 | if err != nil && !cut { |
| 609 | return err |
| 610 | } |
| 611 | if e, ok := logEntryOf(p.Name, string(rec)); ok { |
| 612 | page.Commits = append(page.Commits, e) |
| 613 | } |
| 614 | if cut { |
| 615 | page.Partial = true |
| 616 | return nil |
| 617 | } |
| 618 | } |
| 619 | return nil |
| 620 | }, "log", "--no-mailmap", "--no-notes", "--no-show-signature", |
| 621 | "--no-decorate", "--no-color", "-z", "--format=%H%n%aI%n%an%n%s", |
| 622 | "--max-count="+strconv.Itoa(LogPage+1), "--skip="+strconv.Itoa(skip), commit, "--") |
| 623 | if err != nil { |
| 624 | return err |
| 625 | } |
| 626 | switch { |
| 627 | case len(page.Commits) > LogPage: |
| 628 | page.Commits = page.Commits[:LogPage] |
| 629 | page.Older = logURL(p.Name, commit) + "?skip=" + strconv.Itoa(skip+LogPage) |
| 630 | case page.Partial && len(page.Commits) > 0: |
| 631 | page.Older = logURL(p.Name, commit) + "?skip=" + strconv.Itoa(skip+len(page.Commits)) |
| 632 | } |
| 633 | if skip > 0 { |
| 634 | page.Newer = logURL(p.Name, commit) |
| 635 | if skip > LogPage { |
| 636 | page.Newer += "?skip=" + strconv.Itoa(skip-LogPage) |
| 637 | } |
| 638 | } |
| 639 | q.page(http.StatusOK, "log", "History of "+p.Name+" at "+commit[:12], page) |
| 640 | return nil |
| 641 | } |
| 642 | |
| 643 | // logEntryOf is one record of git log: a commit's ID, author date, |
| 644 | // author and subject, each on a line of its own. A record cut short is |
| 645 | // shown as far as it goes. ok is false for a record that names no commit. |
| 646 | func logEntryOf(project, rec string) (e logEntry, ok bool) { |
| 647 | f := strings.SplitN(rec, "\n", 4) |
| 648 | if !export.ValidObjectID(f[0]) { |
| 649 | return logEntry{}, false |
| 650 | } |
| 651 | for len(f) < 4 { |
| 652 | f = append(f, "") |
| 653 | } |
| 654 | return logEntry{Short: f[0][:12], URL: treeURL(project, f[0], nil), Date: stampOf(f[1]), |
| 655 | Author: clip(oneLine(f[2]), maxLogText), Subject: clip(oneLine(f[3]), maxLogText)}, true |
| 656 | } |
| 657 | |
| 658 | // missing answers a source route whose commit or path names nothing it |
| 659 | // serves: the one 404, or the one 500 when git failed. |
| 660 | func (q *request) missing(err error) { |
| 661 | if err != nil { |
| 662 | q.fail(err) |
| 663 | return |
| 664 | } |
| 665 | q.notFound() |
| 666 | } |
| 667 | |
| 668 | // oneLine is text from a repository as a page shows it on one line: |
| 669 | // valid UTF-8, with control and bidirectional formatting characters |
| 670 | // replaced, so a name cannot reorder or hide the text around it. |
| 671 | func oneLine(s string) string { |
| 672 | return strings.Map(func(r rune) rune { |
| 673 | if unicode.IsControl(r) || unicode.Is(unicode.Bidi_Control, r) { |
| 674 | return '�' |
| 675 | } |
| 676 | return r |
| 677 | }, strings.ToValidUTF8(s, "�")) |
| 678 | } |
| 679 | |
| 680 | // size is a byte count in words. |
| 681 | func size(n int64) string { |
| 682 | switch { |
| 683 | case n < 0: |
| 684 | return "" |
| 685 | case n < 1<<10: |
| 686 | return count(int(n), "byte") |
| 687 | case n < 1<<20: |
| 688 | return fmt.Sprintf("%.1f KiB", float64(n)/(1<<10)) |
| 689 | case n < 1<<30: |
| 690 | return fmt.Sprintf("%.1f MiB", float64(n)/(1<<20)) |
| 691 | } |
| 692 | return fmt.Sprintf("%.1f GiB", float64(n)/(1<<30)) |
| 693 | } |