From 71a42a74db2cc763fdc6bf102aeddecda59baabc Mon Sep 17 00:00:00 2001 From: modulomedito Date: Wed, 27 May 2026 14:33:50 +0800 Subject: [PATCH 1/3] feat(gitgraph): use branch highlights for commit hashes Add helper functions to fetch correct branch highlight groups for commit lanes, update hash highlight logic to use matching branch colors when available, and document the change in the README. The fallback GitGraphHash highlight is preserved for unassigned cases. --- README.md | 2 ++ lua/gitgraph/core.lua | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4b92af0..a2aff73 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,8 @@ For example, use **kitty** branch symbols [more detail](https://github.com/kovid - 'GitGraphBranchTag' - 'GitGraphBranchMsg' +Commit hashes use the same branch highlight group as the commit lane when available. `GitGraphHash` remains as the fallback highlight group. + ## branch colors - 'GitGraphBranch1' diff --git a/lua/gitgraph/core.lua b/lua/gitgraph/core.lua index 13605b9..72ab52a 100644 --- a/lua/gitgraph/core.lua +++ b/lua/gitgraph/core.lua @@ -467,6 +467,23 @@ 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 cell I.Cell ---@return string local function commit_cell_symb(cell) @@ -519,7 +536,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 @@ -530,7 +547,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 @@ -632,6 +649,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 @@ -670,7 +688,7 @@ local function graph_to_lines(options, graph, sym, fields, commits) local value = items[name] if value then 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, From 7e4160dfabb5b6240302e503d71b2695d4a62802 Mon Sep 17 00:00:00 2001 From: modulomedito Date: Thu, 28 May 2026 09:58:53 +0800 Subject: [PATCH 2/3] feat(highlights): add branch highlight groups 6-10 add GitGraphBranch6 through GitGraphBranch10 highlight groups with their respective color codes in lua/gitgraph/highlights.lua, and update the README.md to document the new branch highlight options --- README.md | 5 +++++ lua/gitgraph/highlights.lua | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/README.md b/README.md index a2aff73..f17ec7f 100644 --- a/README.md +++ b/README.md @@ -148,3 +148,8 @@ Commit hashes use the same branch highlight group as the commit lane when availa - 'GitGraphBranch3' - 'GitGraphBranch4' - 'GitGraphBranch5' + - 'GitGraphBranch6' + - 'GitGraphBranch7' + - 'GitGraphBranch8' + - 'GitGraphBranch9' + - 'GitGraphBranch10' diff --git a/lua/gitgraph/highlights.lua b/lua/gitgraph/highlights.lua index ce5a1d1..13ecf96 100644 --- a/lua/gitgraph/highlights.lua +++ b/lua/gitgraph/highlights.lua @@ -21,6 +21,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 From 7be5434a4ff5d8b41e7c82a8ae6d23d016e88fe6 Mon Sep 17 00:00:00 2001 From: modulomedito Date: Wed, 3 Jun 2026 15:15:19 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=A8=20feat(highlights):[#]add=20HEAD?= =?UTF-8?q?=20ref=20highlight=20group=20for=20branch=20display?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - The `HEAD -> branch_name` reference was previously unhighlighted, blending into the branch name and reducing visual clarity. - Added a `GitGraphHead` highlight group (default Tokyo Night red) for the HEAD portion of branch labels. - Detects the HEAD ref pattern via regex and overlays the head highlight on top of the branch name highlight. --- README.md | 2 ++ lua/gitgraph/core.lua | 24 ++++++++++++++++++++++-- lua/gitgraph/highlights.lua | 1 + 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f17ec7f..26ed10d 100644 --- a/README.md +++ b/README.md @@ -136,10 +136,12 @@ 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 diff --git a/lua/gitgraph/core.lua b/lua/gitgraph/core.lua index 72ab52a..8def18c 100644 --- a/lua/gitgraph/core.lua +++ b/lua/gitgraph/core.lua @@ -484,6 +484,12 @@ local function graph_to_lines(options, graph, sym, fields, commits) 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) @@ -687,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 = 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 diff --git a/lua/gitgraph/highlights.lua b/lua/gitgraph/highlights.lua index 13ecf96..751d530 100644 --- a/lua/gitgraph/highlights.lua +++ b/lua/gitgraph/highlights.lua @@ -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' }, }