From 5156fa1a3e7617685175f3a9877f8a417dba5256 Mon Sep 17 00:00:00 2001 From: Roman Konoval Date: Mon, 23 Feb 2026 09:44:10 +0100 Subject: [PATCH] fix: prevent duplicate --no-pager in jj log command (#90) Co-authored-by: Roman Konoval --- lua/jj/cmd/log.lua | 51 +++++++++++++++++++++++++++------------------ tests/run_tests.lua | 39 ++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 20 deletions(-) diff --git a/lua/jj/cmd/log.lua b/lua/jj/cmd/log.lua index 4930096..119d24a 100644 --- a/lua/jj/cmd/log.lua +++ b/lua/jj/cmd/log.lua @@ -232,6 +232,36 @@ local function setup_selected_highlights() end end +--- Build the jj log command string from options +--- @param opts? jj.cmd.log_opts Optional command options +--- @return string The full jj log command +function M.build_log_cmd(opts) + local jj_cmd = "jj log --no-pager" + local merged_opts = vim.tbl_extend("force", default_log_opts, opts or {}) + + if merged_opts.raw_flags then + -- Strip --no-pager from raw_flags since it's already in the base command + local flags = vim.trim(merged_opts.raw_flags:gsub("%-%-no%-pager", ""):gsub("%s+", " ")) + if flags ~= "" then + return string.format("%s %s", jj_cmd, flags) + end + return jj_cmd + end + + for key, value in pairs(merged_opts) do + key = key:gsub("_", "-") + if key == "limit" and value then + jj_cmd = string.format("%s --%s %d", jj_cmd, key, value) + elseif key == "revisions" and value then + jj_cmd = string.format("%s --%s %s", jj_cmd, key, value) + elseif value then + jj_cmd = string.format("%s --%s", jj_cmd, key) + end + end + + return jj_cmd +end + --- Jujutsu log --- @param opts? jj.cmd.log_opts Optional command options function M.log(opts) @@ -247,26 +277,7 @@ function M.log(opts) vim.api.nvim_buf_clear_namespace(terminal.state.buf, log_special_mode_target_ns_id, 0, -1) end - local jj_cmd = "jj log --no-pager" - local merged_opts = vim.tbl_extend("force", default_log_opts, opts or {}) - - -- If a raw has been given simply execute it as is - if merged_opts.raw_flags then - return terminal.run(string.format("%s %s", jj_cmd, merged_opts.raw_flags), M.log_keymaps()) - end - - for key, value in pairs(merged_opts) do - key = key:gsub("_", "-") - if key == "limit" and value then - jj_cmd = string.format("%s --%s %d", jj_cmd, key, value) - elseif key == "revisions" and value then - jj_cmd = string.format("%s --%s %s", jj_cmd, key, value) - elseif value then - jj_cmd = string.format("%s --%s", jj_cmd, key) - end - end - - terminal.run(jj_cmd, M.log_keymaps()) + terminal.run(M.build_log_cmd(opts), M.log_keymaps()) end --- diff --git a/tests/run_tests.lua b/tests/run_tests.lua index 19b9027..0158f34 100755 --- a/tests/run_tests.lua +++ b/tests/run_tests.lua @@ -393,6 +393,45 @@ run_test("parse_default_cmd: returns nil for nil", function() assert_is_nil(parser.parse_default_cmd(nil)) end) +print("\n=== Running build_log_cmd tests ===\n") + +local log = require("jj.cmd.log") + +run_test("build_log_cmd: raw_flags with --no-pager does not duplicate it", function() + local cmd = log.build_log_cmd({ raw_flags = "--no-pager --limit 18" }) + -- Should contain exactly one --no-pager + local _, count = cmd:gsub("%-%-no%-pager", "") + assert_equals(1, count, "Expected exactly one --no-pager") + assert_equals("jj log --no-pager --limit 18", cmd) +end) + +run_test("build_log_cmd: raw_flags without --no-pager works normally", function() + local cmd = log.build_log_cmd({ raw_flags = "--limit 18" }) + assert_equals("jj log --no-pager --limit 18", cmd) +end) + +run_test("build_log_cmd: raw_flags that is only --no-pager", function() + local cmd = log.build_log_cmd({ raw_flags = "--no-pager" }) + assert_equals("jj log --no-pager", cmd) +end) + +run_test("build_log_cmd: structured opts with limit", function() + local cmd = log.build_log_cmd({ limit = 10 }) + assert_equals(true, cmd:find("--limit 10") ~= nil, "Expected --limit 10 in command") + assert_equals(true, cmd:find("--no%-pager") ~= nil, "Expected --no-pager in command") +end) + +run_test("build_log_cmd: structured opts with revisions", function() + local cmd = log.build_log_cmd({ revisions = "main" }) + assert_equals(true, cmd:find("--revisions main") ~= nil, "Expected --revisions main in command") +end) + +run_test("build_log_cmd: default opts produces valid command", function() + local cmd = log.build_log_cmd({}) + assert_equals(true, cmd:find("^jj log %-%-no%-pager") ~= nil, "Expected command to start with jj log --no-pager") + assert_equals(true, cmd:find("--limit 20") ~= nil, "Expected default --limit 20") +end) + -- Print summary print(string.format("\n=== Test Summary ===")) print(string.format("Passed: %d", tests_passed))