summaryrefslogtreecommitdiff
path: root/docs/handbook/cgit/ui-modules.md
blob: b03a437a350198ff681e9dcbb91aafa0e722e9cb (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# cgit — UI Modules

## Overview

cgit's user interface is implemented as a collection of `ui-*.c` modules,
each responsible for rendering a specific page type.  All modules share
common infrastructure from `ui-shared.c` and `html.c`.

## Module Map

| Module | Page | Entry Function |
|--------|------|---------------|
| `ui-repolist.c` | Repository index | `cgit_print_repolist()` |
| `ui-summary.c` | Repository summary | `cgit_print_summary()` |
| `ui-log.c` | Commit log | `cgit_print_log()` |
| `ui-tree.c` | File/directory tree | `cgit_print_tree()` |
| `ui-blob.c` | File content | `cgit_print_blob()` |
| `ui-commit.c` | Commit details | `cgit_print_commit()` |
| `ui-diff.c` | Diff view | `cgit_print_diff()` |
| `ui-ssdiff.c` | Side-by-side diff | `cgit_ssdiff_*()` |
| `ui-patch.c` | Patch output | `cgit_print_patch()` |
| `ui-refs.c` | Branch/tag listing | `cgit_print_refs()` |
| `ui-tag.c` | Tag details | `cgit_print_tag()` |
| `ui-stats.c` | Statistics | `cgit_print_stats()` |
| `ui-atom.c` | Atom feed | `cgit_print_atom()` |
| `ui-plain.c` | Raw file serving | `cgit_print_plain()` |
| `ui-blame.c` | Blame view | `cgit_print_blame()` |
| `ui-clone.c` | HTTP clone | `cgit_clone_info/objects/head()` |
| `ui-snapshot.c` | Archive download | `cgit_print_snapshot()` |
| `ui-shared.c` | Common layout | (shared functions) |

## `ui-repolist.c` — Repository Index

Renders the main page listing all configured repositories.

### Functions

```c
void cgit_print_repolist(void)
```

### Features

- Sortable columns: Name, Description, Owner, Idle (age)
- Section grouping (based on `repo.section` or `section-from-path`)
- Pagination with configurable `max-repo-count`
- Age calculation via `read_agefile()` or ref modification time
- Optional filter by search query

### Sorting

```c
static int cmp_name(const void *a, const void *b);
static int cmp_section(const void *a, const void *b);
static int cmp_idle(const void *a, const void *b);
```

Sort field is selected by the `s` query parameter or `repository-sort`
directive.

### Age File Resolution

```c
static time_t read_agefile(const char *path)
{
    /* Try reading date from agefile content */
    /* Fall back to file mtime */
    /* Fall back to refs/ dir mtime */
}
```

### Pagination

```c
static void print_pager(int items, int pagelen, char *search, char *sort)
{
    /* Render page navigation links */
    /* [prev] 1 2 3 4 5 [next] */
}
```

## `ui-summary.c` — Repository Summary

Renders the overview page for a single repository.

### Functions

```c
void cgit_print_summary(void)
```

### Content

- Repository metadata table (description, owner, homepage, clone URLs)
- SPDX license detection from `LICENSES/` directory
- CODEOWNERS and MAINTAINERS file detection
- Badges display
- Branch listing (limited by `summary-branches`)
- Tag listing (limited by `summary-tags`)
- Recent commits (limited by `summary-log`)
- Snapshot download links
- README rendering (via about-filter)

### License Detection

```c
/* Scan for SPDX license identifiers */
/* Check LICENSES/ directory for .txt files */
/* Extract license names from filenames */
```

### README Priority

README files are tried in order of `repo.readme` entries:

1. `ref:README.md` — tracked file in a specific ref
2. `:README.md` — tracked file in HEAD
3. `/path/to/README.md` — file on disk

## `ui-log.c` — Commit Log

Renders a paginated list of commits.

### Functions

```c
void cgit_print_log(const char *tip, int ofs, int cnt,
                    char *grep, char *pattern, char *path,
                    int pager, int commit_graph, int commit_sort)
```

### Features

- Commit graph visualization (ASCII art)
- File change count per commit (when `enable-log-filecount=1`)
- Line count per commit (when `enable-log-linecount=1`)
- Grep/search within commit messages
- Path filtering (show commits affecting a specific path)
- Follow renames (when `enable-follow-links=1`)
- Pagination with next/prev links

### Commit Graph Colors

```c
static const char *column_colors_html[] = {
    "<span class='column1'>",
    "<span class='column2'>",
    "<span class='column3'>",
    "<span class='column4'>",
    "<span class='column5'>",
    "<span class='column6'>",
};
```

### Decorations

```c
static void show_commit_decorations(struct commit *commit)
{
    /* Display branch/tag labels next to commits */
    /* Uses git's decoration API */
}
```

## `ui-tree.c` — Tree View

Renders directory listings and file contents.

### Functions

```c
void cgit_print_tree(const char *rev, char *path)
```

### Directory Listing

For each entry in a tree object:

```c
/* For each tree entry */
switch (entry->mode) {
    case S_IFDIR:   /* directory → link to subtree */
    case S_IFREG:   /* regular file → link to blob */
    case S_IFLNK:   /* symlink → show target */
    case S_IFGITLINK: /* submodule → link to submodule */
}
```

### File Display

```c
static void print_text_buffer(const char *name, char *buf,
                               unsigned long size)
{
    /* Show file content with line numbers */
    /* Apply source filter if configured */
}

static void print_binary_buffer(char *buf, unsigned long size)
{
    /* Show "Binary file (N bytes)" message */
}
```

### Walk Tree Context

```c
struct walk_tree_context {
    char *curr_rev;
    char *match_path;
    int state;     /* 0=searching, 1=found, 2=printed */
};
```

The tree walker recursively descends into subdirectories to find the
requested path.

## `ui-blob.c` — Blob View

Displays individual file content or serves raw file data.

### Functions

```c
void cgit_print_blob(const char *hex, char *path,
                     const char *head, int file_only)
int cgit_ref_path_exists(const char *path, const char *ref, int file_only)
char *cgit_ref_read_file(const char *path, const char *ref,
                         unsigned long *size)
```

### MIME Detection

When serving raw content, MIME types are detected from:
1. The `mimetype.<ext>` configuration directives
2. The `mimetype-file` (Apache-style mime.types)
3. Default: `application/octet-stream`

## `ui-commit.c` — Commit View

Displays full commit details.

### Functions

```c
void cgit_print_commit(const char *rev, const char *prefix)
```

### Content

- Author and committer info (name, email, date)
- Commit subject and full message
- Parent commit links
- Git notes
- Commit decorations (branches, tags)
- Diffstat
- Full diff (unified or side-by-side)

### Notes Display

```c
/* Check for git notes */
struct strbuf notes = STRBUF_INIT;
format_display_notes(&commit->object.oid, &notes, ...);
if (notes.len) {
    html("<div class='notes-header'>Notes</div>");
    html("<div class='notes'>");
    html_txt(notes.buf);
    html("</div>");
}
```

## `ui-diff.c` — Diff View

Renders diffs between commits or trees.

### Functions

```c
void cgit_print_diff(const char *new_rev, const char *old_rev,
                     const char *prefix, int show_ctrls, int raw)
void cgit_print_diffstat(const struct object_id *old,
                         const struct object_id *new,
                         const char *prefix)
```

See [diff-engine.md](diff-engine.md) for detailed documentation.

## `ui-ssdiff.c` — Side-by-Side Diff

Renders two-column diff view with character-level highlighting.

### Functions

```c
void cgit_ssdiff_header_begin(void)
void cgit_ssdiff_header_end(void)
void cgit_ssdiff_footer(void)
```

See [diff-engine.md](diff-engine.md) for LCS algorithm details.

## `ui-patch.c` — Patch Output

Generates a downloadable patch file.

### Functions

```c
void cgit_print_patch(const char *new_rev, const char *old_rev,
                      const char *prefix)
```

Output is `text/plain` content suitable for `git apply`.  Uses Git's
`rev_info` and `log_tree_commit` to generate the patch.

## `ui-refs.c` — References View

Displays branches and tags with sorting.

### Functions

```c
void cgit_print_refs(void)
void cgit_print_branches(int max)
void cgit_print_tags(int max)
```

### Branch Display

Each branch row shows:
- Branch name (link to log)
- Idle time
- Author of last commit

### Tag Display

Each tag row shows:
- Tag name (link to tag)
- Idle time
- Author/tagger
- Download links (if snapshots enabled)

### Sorting

```c
static int cmp_branch_age(const void *a, const void *b);
static int cmp_tag_age(const void *a, const void *b);
static int cmp_branch_name(const void *a, const void *b);
static int cmp_tag_name(const void *a, const void *b);
```

Sort order is controlled by `branch-sort` (0=name, 1=age).

## `ui-tag.c` — Tag View

Displays details of a specific tag.

### Functions

```c
void cgit_print_tag(const char *revname)
```

### Content

For annotated tags:
- Tagger name and date
- Tag message
- Tagged object link

For lightweight tags:
- Redirects to the tagged object (commit, tree, or blob)

## `ui-stats.c` — Statistics View

Displays contributor statistics by period.

### Functions

```c
void cgit_print_stats(void)
```

### Periods

```c
struct cgit_period {
    const char *name;     /* "week", "month", "quarter", "year" */
    int max_periods;
    int count;
    /* accessor functions for period boundaries */
};
```

### Data Collection

```c
static void collect_stats(struct cgit_period *period)
{
    /* Walk commit log */
    /* Group commits by author and period */
    /* Count additions/deletions per period */
}
```

### Output

- Bar chart showing commits per period
- Author ranking table
- Sortable by commit count

## `ui-atom.c` — Atom Feed

Generates an Atom XML feed.

### Functions

```c
void cgit_print_atom(char *tip, char *path, int max)
```

### Output

```xml
<?xml version='1.0' encoding='utf-8'?>
<feed xmlns='http://www.w3.org/2005/Atom'>
  <title>repo - log</title>
  <updated>2024-01-01T00:00:00Z</updated>
  <entry>
    <title>commit subject</title>
    <updated>2024-01-01T00:00:00Z</updated>
    <author><name>Alice</name><email>alice@example.com</email></author>
    <id>urn:sha1:abc123</id>
    <link href='commit URL'/>
    <content type='text'>commit message</content>
  </entry>
</feed>
```

Limited by `max-atom-items` (default 10).

## `ui-plain.c` — Raw File Serving

Serves file content with proper MIME types.

### Functions

```c
void cgit_print_plain(void)
```

### Features

- MIME type detection by file extension
- Directory listing (HTML) when path is a tree
- Binary file serving with correct Content-Type
- Security: HTML serving gated by `enable-html-serving`

### Security

When `enable-html-serving=0` (default), HTML files are served as
`text/plain` to prevent XSS.

## `ui-blame.c` — Blame View

Displays line-by-line blame information.

### Functions

```c
void cgit_print_blame(void)
```

### Implementation

Uses Git's `blame_scoreboard` API:

```c
/* Set up blame scoreboard */
/* Walk file history */
/* For each line, emit: commit hash, author, line content */
```

### Output

Each line shows:
- Abbreviated commit hash (linked to commit view)
- Line number
- File content

Requires `enable-blame=1`.

## `ui-clone.c` — HTTP Clone Endpoints

Serves the smart HTTP clone protocol.

### Functions

```c
void cgit_clone_info(void)     /* GET info/refs */
void cgit_clone_objects(void)  /* GET objects/* */
void cgit_clone_head(void)     /* GET HEAD */
```

### `cgit_clone_info()`

Enumerates all refs and their SHA-1 hashes:

```c
static void print_ref_info(const char *refname,
                           const struct object_id *oid, ...)
{
    /* Output: sha1\trefname\n */
}
```

### `cgit_clone_objects()`

Serves loose objects and pack files from the object store.

### `cgit_clone_head()`

Returns the symbolic HEAD reference.

Requires `enable-http-clone=1` (default).

## `ui-snapshot.c` — Archive Downloads

See [snapshot-system.md](snapshot-system.md) for detailed documentation.

## `ui-shared.c` — Common Infrastructure

Provides shared layout and link generation used by all modules.

See [html-rendering.md](html-rendering.md) for detailed documentation.

### Key Functions

- Page skeleton: `cgit_print_docstart()`, `cgit_print_pageheader()`,
  `cgit_print_docend()`
- Links: `cgit_commit_link()`, `cgit_tree_link()`, `cgit_log_link()`, etc.
- URLs: `cgit_repourl()`, `cgit_fileurl()`, `cgit_pageurl()`
- Errors: `cgit_print_error_page()`