sindrets / diffview.nvim

Single tabpage interface for easily cycling through diffs for all modified files for any git rev.
Other
3.92k stars 107 forks source link

[Bug] conflict_choose doesn't pick the right buffer #506

Closed fdev31 closed 4 months ago

fdev31 commented 4 months ago

Description

When using the default keybind "<leader>cb" doesn't get the "base" buffer but "ours"

When overriding the keys, another problem happens, this keymap works while it's not correct:

      require('diffview').setup {
        keymaps = {
          view = {
            { 'n', 'dl', require('diffview.actions').conflict_choose 'base', { desc = 'Get left version (ours conflict)' } },
            { 'n', 'dr', require('diffview.actions').conflict_choose 'theirs', { desc = 'Get right version (theirs conflict)' } },

Expected behavior

It is possible to copy the "base" buffer's hunk.

Actual behavior

"ours" buffer is used instead.

Steps to reproduce

Use the following .gitconfig:

[merge]
     tool = diffview # nvimdiff

[mergetool "diffview"]
    prompt = false
    keepBackup = false
    layout = "LOCAL,BASE,REMOTE / MERGED"
    cmd = nvim -n -c "DiffviewOpen" "$MERGE" -c "DiffviewToggleFiles"

Health check

Output of :checkhealth diffview ``` diffview: require("diffview.health").check() Checking plugin dependencies ~ - OK nvim-web-devicons installed. Checking VCS tools ~ - The plugin requires at least one of the supported VCS tools to be valid. - OK Git found. - OK Git is up-to-date. (2.45.1) - WARNING Configured `hg_cmd` is not executable: 'hg' ```

Log info

Relevant info from :DiffviewLog ``` [INFO 2024-05-28 23:16:01.216 +0200] ...local/share/nvim/lazy/diffview.nvim/lua/diffview/lib.lua:24: [command call] :DiffviewOpen [INFO 2024-05-28 23:16:01.289 +0200] ...iffview.nvim/lua/diffview/scene/views/diff/diff_view.lua:488: [DiffView] Completed update for 14 files successfully (41.702 ms) ```

Neovim version

NVIM v0.10.0
Build type: Release
LuaJIT 2.1.1716656478
Run "nvim -V1 -v" for more info

Operating system and version

Arch-lts: 6.6.32-1-lts #1 SMP PREEMPT_DYNAMIC Sat, 25 May 2024 20:20:51 +0000 x86_64 GNU/Linux

Minimal config

-- sorry

EDIT: I don't really get how the actions & keybind work, if I call it from a function it does nothing... the sample I provided works but I suspect something wrong with it (I would have expected to use a function instead)

fdev31 commented 4 months ago

I can reformulate the bug report, such configuration does "nothing", same as trying the code interactively using :lua... is there a catch with "actions" ?

    config = function()
      local actions = require 'diffview.actions'
      require('diffview').setup {
        keymaps = {
          view = {
            {
              'n',
              'dl',
              function()
                actions.conflict_choose 'ours'
              end,
              { desc = 'Get left version (ours conflict)' },
            },
            {
              'n',
              'dr',
              function()
                actions.conflict_choose 'theirs'
              end,
              { desc = 'Get right version (theirs conflict)' },
            },
            {
              'n',
              'db',
              function()
                actions.conflict_choose 'base'
              end,
              { desc = 'Get original version (before conflict)' },
            },
          },
        },
        view = {
          merge_tool = {
            layout = 'diff4_mixed',
          },
        },
sindrets commented 4 months ago

I realise now that this is perhaps not entirely clear in the docs, but the conflict_choose() actions operate on conflict markers directly, not on buffers. Meaning that unless you're using conflictstyle = zdiff3 in your git config the conflict markers won't have a "base" version.

If you instead wish to use actions that operate on the diff buffers, use :h diffview-actions-diffget, as well as the nvim builtin :h diffget (our variants just offer some shortcuts for getting the diff from a specific version of a file during a conflict).

When using the default keybind "cb" doesn't get the "base" buffer but "ours"

I don't think that's true? You would have to provide better reproduction steps in that case. For example provide a small shell script that creates a dummy repo and produces a merge conflict. And then use the minimal nvim config provided in the bug template.

I can reformulate the bug report, such configuration does "nothing", same as trying the code interactively using :lua... is there a catch with "actions" ? [...]

You're using the actions wrong here. conflict_choose() returns a function. By wrapping it in another function, the actual action never gets called. See :h diffview.defaults for how it's supposed to look.

fdev31 commented 4 months ago

Thank you for the very clear explanation!

So if it returns functions, it makes much more sense ;)

I'm not on the same machine today but I don't see the same struggles I had yesterday... I guess there is something wrong local to my other machine. I guess this was pebkak or so...

I'll close this one since I can't reproduce, and will open a better one later in case a real issue is found.

Thank you again, that's a great plugin :)