From f4888d169e7d710c2ab688618761cd1dd7f2b6d7 Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Sun, 18 Aug 2024 18:12:59 +0200 Subject: [PATCH] better git --- modules/base/git.nix | 1 + modules/base/vim.nix | 81 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 65 insertions(+), 17 deletions(-) diff --git a/modules/base/git.nix b/modules/base/git.nix index 62500d1..a16c1cc 100644 --- a/modules/base/git.nix +++ b/modules/base/git.nix @@ -19,6 +19,7 @@ }; extraConfig = { + diff.algorithm = "histogram"; diff.colorMoved = "default"; init.defaultBranch = "master"; pull.rebase = true; diff --git a/modules/base/vim.nix b/modules/base/vim.nix index 5b77fd8..6169e8e 100644 --- a/modules/base/vim.nix +++ b/modules/base/vim.nix @@ -74,7 +74,7 @@ vim.opt.listchars = { tab="▸ ", trail="·", nbsp="␣" } -- Better diffs - vim.opt.diffopt:append({"linematch:60", "algorithm:patience"}) + vim.opt.diffopt:append({"linematch:60", "algorithm:histogram", "indent-heuristic"}) vim.opt.fillchars:append({ diff = "░" }) -- Show search-replace preview live @@ -636,21 +636,6 @@ ''; } - # Git integration for buffers. - # https://github.com/lewis6991/gitsigns.nvim - { - plugin = gitsigns-nvim; - type = "lua"; - config = - /* - lua - */ - '' - require("gitsigns").setup({}) - -- TODO: keybinds - ''; - } - # An interactive and powerful Git interface for Neovim, inspired by # Magit. # https://github.com/NeogitOrg/neogit @@ -662,11 +647,73 @@ lua */ '' - require("neogit").setup({}) + require("neogit").setup({ + graph_style = "unicode", + }) vim.keymap.set("n", "gg", vim.cmd.Neogit) ''; } + # Git integration for buffers. + # https://github.com/lewis6991/gitsigns.nvim + { + plugin = gitsigns-nvim; + type = "lua"; + config = + /* + lua + */ + '' + require("gitsigns").setup({ + -- https://github.com/lewis6991/gitsigns.nvim?tab=readme-ov-file#keymaps + on_attach = function(bufnr) + local gitsigns = require("gitsigns") + + local function map(mode, l, r, opts) + opts = opts or {} + opts.buffer = bufnr + vim.keymap.set(mode, l, r, opts) + end + + -- Navigation + map("n", "]c", function() + if vim.wo.diff then + vim.cmd.normal({"]c", bang = true}) + else + gitsigns.nav_hunk("next") + end + end) + + map("n", "[c", function() + if vim.wo.diff then + vim.cmd.normal({"[c", bang = true}) + else + gitsigns.nav_hunk("prev") + end + end) + + -- Actions + map("n", "hs", gitsigns.stage_hunk) + map("n", "hr", gitsigns.reset_hunk) + map("v", "hs", function() gitsigns.stage_hunk {vim.fn.line("."), vim.fn.line("v")} end) + map("v", "hr", function() gitsigns.reset_hunk {vim.fn.line("."), vim.fn.line("v")} end) + map("n", "hS", gitsigns.stage_buffer) + map("n", "hu", gitsigns.undo_stage_hunk) + map("n", "hR", gitsigns.reset_buffer) + map("n", "hp", gitsigns.preview_hunk) + map("n", "hb", function() gitsigns.blame_line{full=true} end) + map("n", "tb", gitsigns.toggle_current_line_blame) + map("n", "hd", gitsigns.diffthis) + map("n", "hD", function() gitsigns.diffthis("~") end) + map("n", "td", gitsigns.toggle_deleted) + + -- Text object + map({"o", "x"}, "ih", ":Gitsigns select_hunk") + end + }) + ''; + } + # Single tabpage interface for easily cycling through diffs for all # modified files for any git rev. # https://github.com/sindrets/diffview.nvim