Artifact 16d553d1040d17b5a0d82f936ae59ca95c8859231311a9297b4386b0586930f3:
- File
cli/repo_query.go
— part of check-in
[f5744359d3]
at
2026-04-19 16:30:54
on branch trunk
— Initial open source release
libfossil is a pure-Go implementation of the Fossil SCM, reading and writing the same .fossil SQLite repository format as upstream Fossil.
This is the first public release of code that previously lived in a private repository. The module path is github.com/danmestas/libfossil and the binary is named libfossil to avoid $PATH collisions with upstream fossil.
Included: - Repo lifecycle (create, clone), checkout/checkin, timeline, merge, diff, annotate, shun, tag, branch, bisect, stash, undo/redo - Sync-over-HTTP protocol (client and handler) - Two SQLite drivers: modernc (pure Go, default) and ncruces (wasm-capable) - Observer interfaces (SyncObserver, CheckoutObserver) with an optional OpenTelemetry implementation in observer/otel (separate submodule) - Deterministic simulation testing harness with BUGGIFY fault injection - Kong-based embeddable CLI under cli/ and a standalone binary under cmd/libfossil/ (user: [email protected], size: 1276) [annotate] [blame] [check-ins using]
package cli import ( "fmt" "strings" ) // RepoQueryCmd executes raw SQL against the repository database. type RepoQueryCmd struct { SQL string `arg:"" help:"SQL query to execute"` } func (c *RepoQueryCmd) Run(g *Globals) error { r, err := g.OpenRepo() if err != nil { return err } defer r.Close() db := r.Inner().DB() sql := strings.TrimSpace(c.SQL) // SELECT/PRAGMA queries return rows; everything else returns affected count. if strings.HasPrefix(strings.ToUpper(sql), "SELECT") || strings.HasPrefix(strings.ToUpper(sql), "PRAGMA") { rows, err := db.Query(sql) if err != nil { return err } defer rows.Close() cols, err := rows.Columns() if err != nil { return err } // Print header. fmt.Println(strings.Join(cols, "|")) // Print rows. vals := make([]any, len(cols)) ptrs := make([]any, len(cols)) for i := range vals { ptrs[i] = &vals[i] } for rows.Next() { rows.Scan(ptrs...) parts := make([]string, len(cols)) for i, v := range vals { parts[i] = fmt.Sprintf("%v", v) } fmt.Println(strings.Join(parts, "|")) } return rows.Err() } result, err := db.Exec(sql) if err != nil { return err } affected, _ := result.RowsAffected() fmt.Printf("%d rows affected\n", affected) return nil }