This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is zig-gitweb (formerly zgit), a web frontend for git repositories implemented in Zig. It's a port of cgit to Zig 0.15.1, designed to run as a CGI application that provides a fast web interface for browsing git repositories.
# Build the project
zig build
# Run tests
zig build test
# Run the CGI directly (requires CGI environment variables)
QUERY_STRING="r=repo_name" REQUEST_METHOD=GET ./zig-out/bin/gitweb.cgi
# Test with a specific repository (example with factor.git)
QUERY_STRING="r=factor" REQUEST_METHOD=GET ./zig-out/bin/gitweb.cgi-
CGI Entry Point (
src/main.zig)- Initializes libgit2
- Processes CGI environment variables
- Routes requests through the command dispatcher
-
Context System (
src/gitweb.zig)- Central
Contextstruct holds configuration, repository info, and request state Repostruct represents repository configuration- All UI handlers receive context for rendering
- Central
-
Git Wrapper (
src/git.zig)- Wraps libgit2 C API with Zig-friendly interfaces
- Provides Repository, Commit, Tree, Blob, Diff types
- Handles OID conversions and memory management
- Important: Exports
pub const cfor shared libgit2 imports (prevents type mismatch issues)
-
Command Dispatcher (
src/cmd.zig)- Maps URL commands to UI handlers
- Commands: summary, log, tree, commit, diff, refs, blame, stats, snapshot, etc.
-
UI Handlers (
src/ui/)- Each command has a corresponding handler (e.g.,
log.zig,commit.zig) - Handlers generate HTML output directly to writer
- Use shared utilities for common elements
- Additional handlers:
tree.zig,diff.zig,refs.zig,blame.zig,stats.zig,snapshot.zig,search.zig,tag.zig,blob.zig,plain.zig,patch.zig,atom.zig,clone.zig,repolist.zig
- Each command has a corresponding handler (e.g.,
-
HTML Generation (
src/html.zig)- Provides
writeHeader()andwriteFooter()for consistent page structure - Handles navigation tabs, branch selector, and page metadata
- Includes viewport meta tag for mobile responsiveness
- Provides
-
Configuration (
src/config.zig,src/configfile.zig)- Configuration system for CGI settings
- Repository configuration parsing
- Customizable paths, URLs, and display options
-
Parsing Utilities (
src/parsing.zig)- Query string parsing
- URL encoding/decoding
- Timestamp formatting
-
C Interop Pattern
- All libgit2 imports go through
git.cto avoid type namespace conflicts - UI files use
const c = git.c;orconst c = @cImport({...})for additional headers - Extensive use of
@ptrCastand@constCastfor C pointer conversions
- All libgit2 imports go through
-
Memory Management
- Repository paths constructed dynamically from
~/git/[repo_name].git - Tag names must be duplicated to avoid use-after-free when C arrays are freed
- Defer patterns used extensively for cleanup
- Repository paths constructed dynamically from
-
HTML Generation
- Direct HTML writing to output stream
- HTML escaping through
html.htmlEscape() - Static files served from
/gitweb.css,/gitweb.js
ArrayListis now unmanaged (use.emptyinstead of.init())allocPrintZ→allocPrintSentinelwith 0 sentinel at endstd.mem.split→std.mem.splitScalarfor single character delimiters- Use
deprecatedWriter()for stdout
- If getting "unable to resolve comptime value" errors, create named struct types and use runtime initialization
- Example:
CommitStatsstruct inlog.zig
- Ensure all files import from the same
git.cexport - Don't create multiple
@cImportblocks for the same headers
Repositories are expected to be bare git repositories located at:
~/git/[repository_name].git
The CGI reads repository name from the r query parameter:
?r=factor→ looks for~/git/factor.git
To test the CGI with a git repository:
- Create a bare repository:
git init --bare ~/git/test.git - Run:
QUERY_STRING="r=test" REQUEST_METHOD=GET ./zig-out/bin/gitweb.cgi
Located in static/:
gitweb.css- Stylesheet (1583 lines)gitweb.js- JavaScript for UI interactionschart.js- Chart library for stats visualizationrobots.txt- Search engine directivesfavicon*.png- Favicon in multiple sizesgitweb.png- Logo image
- Zig 0.15.1 or later
- libgit2 (system library)
- zlib (system library)
On macOS with Homebrew:
brew install libgit2The codebase has significant duplication across UI handlers (~400-500 duplicated lines):
- Repository opening pattern - Repeated in 10+ files
- Commit/reference resolution - Complex logic duplicated 6+ times
- Branch/tag collection - 50+ lines repeated in multiple handlers
- URL generation with parameters - Duplicated URL building logic
- Path navigation in git trees - Tree traversal code repeated
The CSS file (1583 lines) has opportunities for cleanup:
- Unused classes - ~57 classes defined but never used (mostly syntax highlighting)
- Missing definitions - ~30 classes used in code but not styled
- Color repetition - Color
#f6f8faused 24+ times (could use CSS variables) - Unused dark mode - Complete dark mode styles with no toggle mechanism
-
Extract common UI functions to
shared.zig:openRepositoryOrError()- Handle repo opening with error displayresolveCommitFromQuery()- Parse and resolve commit referencescollectRefsMap()- Build branch/tag reference mappingswriteUrlBase()- Generate consistent URL structuresnavigateToPath()- Tree navigation helper
-
CSS consolidation:
- Remove unused syntax highlighting classes
- Add missing class definitions for error handling
- Introduce CSS custom properties for repeated values
- Consider removing dark mode if not planned