Skip to content

Add flag to include author information in recent commits #653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ neogit.setup {
console_timeout = 2000,
-- Automatically show console if a command takes more than console_timeout milliseconds
auto_show_console = true,
-- Options for the Status buffer
status = {
-- The number of recent commits to show
recent_commit_count = 10
-- Whether or not to incldue author info (name, email) in recent commits
recent_commit_include_author_info = false
},
-- Persist the values of switches/options within and across sessions
remember_settings = true,
-- Scope persisted settings on a per-project basis
Expand Down
1 change: 1 addition & 0 deletions lua/neogit/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ M.values = {
auto_show_console = true,
status = {
recent_commit_count = 10,
recent_commit_include_author_info = false,
},
commit_editor = {
kind = "split",
Expand Down
10 changes: 9 additions & 1 deletion lua/neogit/lib/git/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,16 @@ local function update_recent(state)

state.recent.items = util.filter_map(result, function(v)
if v.oid then
local recent_commit = {
v.oid:sub(1, 7),
}
if config.values.status.recent_commit_include_author_info then
table.insert(recent_commit, string.format("[%s] <%s>", v.author_name, v.author_email))
end

table.insert(recent_commit, v.description[1] or "<empty>")
return {
name = string.format("%s %s", v.oid:sub(1, 7), v.description[1] or "<empty>"),
name = table.concat(recent_commit, " "),
oid = v.oid,
commit = v,
}
Expand Down
2 changes: 2 additions & 0 deletions lua/neogit/lib/hl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ function M.setup()
NeogitUnmergedInto = { link = "Function" },
NeogitUnpulledFrom = { link = "Function" },
NeogitObjectId = { link = "Comment" },
NeogitCommitAuthor = { fg = palette.cyan },
NeogitCommitAuthorEmail = { fg = palette.green },
NeogitStash = { link = "Comment" },
NeogitRebaseDone = { link = "Comment" },
NeogitCursorLine = { bg = palette.bg1 },
Expand Down
36 changes: 20 additions & 16 deletions syntax/NeogitStatus.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,33 @@ endif
" Support the rebase todo highlights
source $VIMRUNTIME/syntax/gitrebase.vim

syn match NeogitCommitMessage /.*/ contained
syn match NeogitBranch /\S\+/ contained nextgroup=NeogitCommitMessage
syn match NeogitRemote /\S\+/ contained nextgroup=NeogitCommitMessage
syn match NeogitDiffAdd /.*/ contained
syn match NeogitDiffDelete /.*/ contained
syn match NeogitUnmergedInto /Unmerged into/ contained
syn match NeogitUnpulledFrom /Unpulled from/ contained
syn match NeogitStash /stash@{[0-9]*}\ze/
syn match NeogitObjectId /^[a-z0-9]\{7,}\>\s/
syn match NeogitCommitMessage /.*/ contained
syn match NeogitBranch /\S\+/ contained nextgroup=NeogitCommitMessage
syn match NeogitRemote /\S\+/ contained nextgroup=NeogitCommitMessage
syn match NeogitDiffAdd /.*/ contained
syn match NeogitDiffDelete /.*/ contained
syn match NeogitUnmergedInto /Unmerged into/ contained
syn match NeogitUnpulledFrom /Unpulled from/ contained
syn match NeogitStash /stash@{[0-9]*}\ze/
syn match NeogitCommitAuthorEmail /<.\+@.\+>/ contained nextgroup=NeogitCommitMessage
syn match NeogitCommitAuthor /\[.*\]/ contained nextgroup=NeogitCommitAuthorEmail
syn match NeogitRecentCommits /^Recent commits/ contained
syn match NeogitObjectId /^[a-z0-9]\{7,}\>\s/

let b:sections = ["Untracked files", "Unstaged changes", "Unmerged changes", "Unpulled changes", "Recent commits", "Staged changes", "Stashes", "Rebasing"]
let b:sections = ["Untracked files", "Unstaged changes", "Unmerged changes", "Unpulled changes", "Staged changes", "Stashes", "Rebasing"]

for section in b:sections
let id = join(split(section, " "), "")
execute 'syn match Neogit' . id . ' /^' . section . '/ contained'
execute 'syn region Neogit' . id . 'Region start=/^' . section . '\ze.*/ end=/./ contains=Neogit' . id
endfor

syn region NeogitHeadRegion start=/^Head: \zs/ end=/$/ contains=NeogitBranch
syn region NeogitPushRegion start=/^Push: \zs/ end=/$/ contains=NeogitRemote
syn region NeogitUnmergedIntoRegion start=/^Unmerged into .*/ end=/$/ contains=NeogitRemote,NeogitUnmergedInto
syn region NeogitUnpulledFromRegion start=/^Unpulled from .*/ end=/$/ contains=NeogitRemote,NeogitUnpulledFrom
syn region NeogitDiffAddRegion start=/^+.*$/ end=/$/ contains=NeogitDiffAdd
syn region NeogitDiffDeleteRegion start=/^-.*$/ end=/$/ contains=NeogitDiffDelete
syn region NeogitHeadRegion start=/^Head: \zs/ end=/$/ contains=NeogitBranch
syn region NeogitPushRegion start=/^Push: \zs/ end=/$/ contains=NeogitRemote
syn region NeogitUnmergedIntoRegion start=/^Unmerged into .*/ end=/$/ contains=NeogitRemote,NeogitUnmergedInto
syn region NeogitUnpulledFromRegion start=/^Unpulled from .*/ end=/$/ contains=NeogitRemote,NeogitUnpulledFrom
syn region NeogitDiffAddRegion start=/^+.*$/ end=/$/ contains=NeogitDiffAdd
syn region NeogitDiffDeleteRegion start=/^-.*$/ end=/$/ contains=NeogitDiffDelete
syn region NeogitRecentCommitsRegion start=/^Recent commits.*/ end=/\n$/ contains=NeogitRecentCommits,NeogitCommitAuthorEmail,NeogitCommitAuthor,NeogitObjectId

let b:current_syntax = 1
114 changes: 110 additions & 4 deletions tests/specs/neogit/status_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local eq = assert.are.same
local status = require("neogit.status")
local harness = require("tests.util.git_harness")
local system = require("tests.util.util").system
local _ = require("tests.mocks.input")
local in_prepared_repo = harness.in_prepared_repo
local get_git_status = harness.get_git_status
Expand All @@ -17,10 +18,7 @@ local function find(text)
for index, line in ipairs(vim.api.nvim_buf_get_lines(0, 0, -1, true)) do
if line:match(text) then
vim.api.nvim_win_set_cursor(0, { index, 0 })
-- print(">" .. tostring(index) .. " " .. line)
break
-- else
-- print(tostring(index) .. " " .. line)
return unpack { line, index }
end
end
end
Expand Down Expand Up @@ -307,4 +305,112 @@ describe("status buffer", function()
end)
)
end)

describe("recent commits", function()
local recent_commit_pattern = "Recent commits %(%d+%)"

local function refresh_status_buffer()
act("<c-r>")
require("plenary.async").util.block_on(status.reset)
end

local function create_new_commits(message, number_of_commits)
system("git stash && git stash clear && git clean -ffdx")
local commit_commands =
string.format("printf 'Some Content\\n' >> a-file && git add a-file && git commit -m '%s'", message)

if number_of_commits == 1 then
system(commit_commands)
else
local loop_cmd = string.format(
[[
COUNT=1
while [ "$COUNT" -ne %s ]; do
%s
COUNT=$((COUNT + 1))
done
]],
number_of_commits,
commit_commands
)
system(loop_cmd)
end
refresh_status_buffer()
end

describe("count", function()
it(
"has the correct number of recent commits",
in_prepared_repo(function()
local line = find(recent_commit_pattern)
local recent_commit_count = tonumber(string.match(line, "%d+"))
local repo_commit_count = tonumber(system("git rev-list master --count"))
assert.are.equal(recent_commit_count, repo_commit_count)
end)
)

it(
"has the correct number when there are more commits than recent_commit_count",
in_prepared_repo(function()
create_new_commits("A commit to increase commit number", 50)
local line = find(recent_commit_pattern)
local recent_commit_count = tonumber(string.match(line, "%d+"))
local repo_commit_count = tonumber(system("git rev-list master --count"))
local config_commit_count = 10
require("neogit.config").values.status.recent_commit_count = config_commit_count

print("Recent Commit Count: " .. recent_commit_count)
print("Config Commit Count: " .. config_commit_count)
print("Repository Commit Count: " .. repo_commit_count)
-- Ensures the actual number of recent commits is less than the repo commits.
-- The total number of repo commits SHOULD be more than the recent commit count.
assert.True(recent_commit_count < repo_commit_count)
-- Ensure the number of recent commits is equal to the number specified in the config
assert.are.equal(recent_commit_count, config_commit_count)
end)
)
end)
describe("content", function()
local function get_latest_recent_commit()
-- Get the commit right under the "Recent commits" message
local _, cursor_row = find(recent_commit_pattern)
act("<tab>")
vim.api.nvim_win_set_cursor(0, { cursor_row + 1, 0 })
local commit_message_line = vim.api.nvim_buf_get_lines(0, cursor_row, cursor_row + 1, true)[1]
-- Remove the leading commit hash
local commit_message = string.gsub(commit_message_line, "^[a-z0-9]+ ", "")

return commit_message
end

it(
"has correct recent commit information with the default config",
in_prepared_repo(function()
local new_commit_message = "a commit"
create_new_commits(new_commit_message, 1)

local commit_message = get_latest_recent_commit()
print("Got commit message as: " .. commit_message)

assert.are.same(new_commit_message, commit_message)
end)
)

it(
"has correct recent commit information with extra author info",
in_prepared_repo(function()
require("neogit.config").values.status.recent_commit_include_author_info = true
local new_commit_message = "a commit"
create_new_commits(new_commit_message, 1)

local commit_message = get_latest_recent_commit()
print("Got commit message as: " .. commit_message)

new_commit_message =
string.format("[%s] <%s> %s", "Neogit Test User", "test@neogit.git", new_commit_message)
assert.are.same(new_commit_message, commit_message)
end)
)
end)
end)
end)
2 changes: 2 additions & 0 deletions tests/util/git_harness.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ function M.prepare_repository()
local working_dir = util.create_temp_dir("working-dir")
vim.api.nvim_set_current_dir(working_dir)
util.system(string.format("git clone %s %s", bare_repo_path, working_dir))
util.system("git config user.email 'test@neogit.git'")
util.system("git config user.name 'Neogit Test User'")
util.system("git reset --soft HEAD~1")
util.system("git rm --cached untracked.txt")
util.system("git restore --staged a.txt")
Expand Down