GitGraph
Key bindings
Section titled “Key bindings”| Key | Action |
|---|---|
↑ / k | Move up one commit |
↓ / j | Move down one commit |
Home / g | Jump to newest commit (top) |
End / G | Jump to oldest commit (bottom) |
PgUp | Page up |
PgDn | Page down |
Ctrl+U | Half-page up |
Ctrl+D | Half-page down |
p | Jump to first parent |
P | Jump to second parent (merge commits) |
c | Jump to first child |
Enter | Select commit (fires onSelect) |
onHighlight fires on every cursor move for driving a detail panel; onSelect fires only on Enter.
Data model
Section titled “Data model”Build GitGraphData with commits and their parent/child relationships. The renderer draws branch lines automatically based on the parent hash list.
Usage
data := components.NewGitGraphData()data.AddCommit(&components.GitCommit{ Hash: "a1b2c3d", ShortHash: "a1b2c3d", Message: "Initial commit", Author: "atterpac",})data.AddCommit(&components.GitCommit{ Hash: "e4f5g6h", ShortHash: "e4f5g6h", Message: "Add feature", Author: "atterpac", Parents: []string{"a1b2c3d"},})data.LayoutGraph()
graph := components.NewGitGraph(). SetGraph(data). SetShowRefs(true). SetShowAuthor(true)
fmt.Println(graph != nil)Methods
| Method | Signature | Description |
|---|---|---|
GetGraph | func (g *GitGraph) GetGraph() *GitGraphData | GetGraph returns the underlying graph data |
GetSelected | func (g *GitGraph) GetSelected() *GitCommit | GetSelected returns the currently selected commit |
SelectByHash | func (g *GitGraph) SelectByHash(hash string) *GitGraph | SelectByHash selects a commit by its hash |
SetColumnCap | func (g *GitGraph) SetColumnCap(cap int) *GitGraph | SetColumnCap sets the maximum number of columns/lanes to display Default is 12 columns. Set to 0 to use default. |
SetDateFormat | func (g *GitGraph) SetDateFormat(format string) *GitGraph | SetDateFormat sets the date format string |
SetGraph | func (g *GitGraph) SetGraph(data *GitGraphData) *GitGraph | SetGraph sets the commit graph data to render |
SetLaneColors | func (g *GitGraph) SetLaneColors(colors []tcell.Color) *GitGraph | SetLaneColors sets custom colors for graph lanes |
SetOnChange | func (g *GitGraph) SetOnChange(fn func(commit *GitCommit)) *GitGraph | SetOnChange sets the callback for when the selection changes |
SetOnSelect | func (g *GitGraph) SetOnSelect(fn func(commit *GitCommit)) *GitGraph | SetOnSelect sets the callback for when Enter is pressed on a commit |
SetSelectedIndex | func (g *GitGraph) SetSelectedIndex(index int) *GitGraph | SetSelectedIndex sets the selected commit by index |
SetShowAuthor | func (g *GitGraph) SetShowAuthor(show bool) *GitGraph | SetShowAuthor enables/disables showing author |
SetShowDate | func (g *GitGraph) SetShowDate(show bool) *GitGraph | SetShowDate enables/disables showing date |
SetShowHash | func (g *GitGraph) SetShowHash(show bool) *GitGraph | SetShowHash enables/disables showing commit hash |
SetShowRefs | func (g *GitGraph) SetShowRefs(show bool) *GitGraph | SetShowRefs enables/disables showing branch/tag refs |
Types
type GitCommit struct GitCommit represents a git commit with graph positioning info | Field | Type | Description |
|---|---|---|
Hash | string | |
ShortHash | string | |
Message | string | |
Author | string | |
Date | time.Time | |
Parents | []string | Parent commit hashes |
Children | []string | Child commit hashes |
Branch | string | Branch name if this is a branch tip |
Tags | []string | Tags pointing to this commit |
IsMerge | bool | True if len(Parents) > 1 |
IsStash | bool | True if this is a stash entry |
IsPseudoNode | bool | True for virtual nodes (unstaged changes, staged changes) |
PseudoType | string | "unstaged", "staged", or future types |
Column | int | Assigned column in graph layout |
Row | int | Row position in flat list |
Refs | []string | All refs (branches, tags) at this commit |
Ahead | int | Commits ahead of upstream (for branch tips) |
Behind | int | Commits behind upstream (for branch tips) |
Data | any | Custom user data |
type GitGraphData struct GitGraphData represents the commit graph with layout info | Field | Type | Description |
|---|---|---|
Commits | []*GitCommit | Commits in topological order (newest first) |
CommitMap | map[string]*GitCommit | Hash -> Commit lookup |
Branches | []string | All branch names |
CurrentBranch | string | Current/active branch name (gets column 0) |
MaxColumn | int | Maximum column used in layout |
ColumnCap | int | Maximum columns to display (0 = unlimited) |
ActiveCols | map[int]string | Column -> commit hash currently in that column |
ColumnBranch | map[int]string | Column -> branch name that owns this lane |