Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,22 @@ For example, use **kitty** branch symbols [more detail](https://github.com/kovid
- 'GitGraphTimestamp'
- 'GitGraphAuthor'
- 'GitGraphBranchName'
- 'GitGraphHead'
- 'GitGraphBranchTag'
- 'GitGraphBranchMsg'

Commit hashes use the same branch highlight group as the commit lane when available. `GitGraphHash` remains as the fallback highlight group.
`HEAD -> branch_name` uses `GitGraphHead`, which defaults to Tokyo Night style red.

## branch colors

- 'GitGraphBranch1'
- 'GitGraphBranch2'
- 'GitGraphBranch3'
- 'GitGraphBranch4'
- 'GitGraphBranch5'
- 'GitGraphBranch6'
- 'GitGraphBranch7'
- 'GitGraphBranch8'
- 'GitGraphBranch9'
- 'GitGraphBranch10'
48 changes: 43 additions & 5 deletions lua/gitgraph/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,29 @@ local function graph_to_lines(options, graph, sym, fields, commits)
---@type I.Highlight[]
local highlights = {}

---@param col integer
---@return string
local function branch_hg_for_col(col)
return 'GitGraphBranch' .. tostring(col % NUM_BRANCH_COLORS + 1)
end

---@param row I.Row
---@param commit_hash string
---@return string?
local function branch_hg_for_commit(row, commit_hash)
for col, cell in ipairs(row.cells) do
if cell.commit and cell.commit.hash == commit_hash then
return branch_hg_for_col(col)
end
end
end

---@param branch_names string
---@return integer?, integer?
local function find_head_ref_range(branch_names)
return branch_names:find('HEAD %-> [^|%)]+')
end

---@param cell I.Cell
---@return string
local function commit_cell_symb(cell)
Expand Down Expand Up @@ -519,7 +542,7 @@ local function graph_to_lines(options, graph, sym, fields, commits)
offset = offset + width

if cell.commit then
local hg = 'GitGraphBranch' .. tostring(j % NUM_BRANCH_COLORS + 1)
local hg = branch_hg_for_col(j)
row_hls[#row_hls + 1] = { hg = hg, row = row_idx, start = start, stop = stop }
elseif cell.symbol == sym.GHOR then
-- take color from first right cell that attaches to this connector
Expand All @@ -530,7 +553,7 @@ local function graph_to_lines(options, graph, sym, fields, commits)
-- to figure out where our vertical branch is

if rcell.commit and vim.tbl_contains(continuation_symbols, rcell.symbol) then
local hg = 'GitGraphBranch' .. tostring(k % NUM_BRANCH_COLORS + 1)
local hg = branch_hg_for_col(k)
row_hls[#row_hls + 1] = { hg = hg, row = row_idx, start = start, stop = stop }
break
end
Expand Down Expand Up @@ -632,6 +655,7 @@ local function graph_to_lines(options, graph, sym, fields, commits)
local hash = c.hash:sub(1, 7)
local timestamp = c.author_date
local author = c.author_name
local hash_hg = branch_hg_for_commit(proper_row, c.hash) or ITEM_HGS.hash.name

local branch_names = #c.branch_names > 0 and ('(%s)'):format(table.concat(c.branch_names, ' | ')) or nil

Expand Down Expand Up @@ -669,12 +693,26 @@ local function graph_to_lines(options, graph, sym, fields, commits)
for _, name in ipairs(fields) do
local value = items[name]
if value then
local start = offset
highlights[#highlights + 1] = {
hg = ITEM_HGS[name].name,
hg = name == 'hash' and hash_hg or ITEM_HGS[name].name,
row = idx,
start = offset,
stop = offset + #value,
start = start,
stop = start + #value,
}

if name == 'branch_name' then
local head_start, head_stop = find_head_ref_range(value)
if head_start and head_stop then
highlights[#highlights + 1] = {
hg = ITEM_HGS.head.name,
row = idx,
start = start + head_start - 1,
stop = start + head_stop,
}
end
end

add_to_row(value)
end
end
Expand Down
6 changes: 6 additions & 0 deletions lua/gitgraph/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ M.ITEM_HGS = {
timestamp = { name = 'GitGraphTimestamp', fg = '#98971a' },
author = { name = 'GitGraphAuthor', fg = '#458588' },
branch_name = { name = 'GitGraphBranchName', fg = '#d5651c' },
head = { name = 'GitGraphHead', fg = '#f7768e' },
tag = { name = 'GitGraphBranchTag', fg = '#d79921' },
message = { name = 'GitGraphBranchMsg', fg = '#339921' },
}
Expand All @@ -21,6 +22,11 @@ M.BRANCH_HGS = {
{ name = 'GitGraphBranch3', fg = '#d79921' },
{ name = 'GitGraphBranch4', fg = '#98971a' },
{ name = 'GitGraphBranch5', fg = '#d5651c' },
{ name = 'GitGraphBranch6', fg = '#689d6a' },
{ name = 'GitGraphBranch7', fg = '#cc241d' },
{ name = 'GitGraphBranch8', fg = '#83a598' },
{ name = 'GitGraphBranch9', fg = '#8ec07c' },
{ name = 'GitGraphBranch10', fg = '#fabd2f' },
}

--- sets highlight groups if they are missing
Expand Down