summaryrefslogtreecommitdiff
path: root/lua/config/plugins/git.lua
blob: 5502ce7588053f46e757c64623e02f8b1d47df8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
return {
  {
    "lewis6991/gitsigns.nvim",
    config = function()
      local gitsigns = require("gitsigns")

      gitsigns.setup({
        signs_staged_enable = true,
        signcolumn = true,
        numhl = true,
        linehl = false,
        word_diff = false,
        watch_gitdir = {
          follow_files = true
        },
        auto_attach = true,
        attach_to_untracked = false,
        current_line_blame = false,
        current_line_blame_opts = {
          virt_text = true,
          virt_text_pos = "eol",
          delay = 1000,
          ignore_whitespace = false,
          virt_text_priority = 100,
          use_focus = true,
        },
        current_line_blame_formatter = "<author>, <author_time:%R> - <summary>",
        sign_priority = 6,
        update_debounce = 100,
        status_formatter = nil,
        max_file_length = 40000, -- Disable if file is longer than this (in lines)
        preview_config = {
          border = "none",
          style = "minimal",
          relative = "cursor",
          row = 0,
          col = 1,
        },

        on_attach = function(bufnr)
          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", { target = "all" })
            end
          end, { desc = "Goto next change" })

          map("n", "[c", function()
            if vim.wo.diff then
              vim.cmd.normal({ "[c", bang = true })
            else
              gitsigns.nav_hunk("prev", { target = "all" })
            end
          end, { desc = "Goto previous change" })

          -- Actions
          map("n", "<Leader>ga", gitsigns.stage_hunk, { desc = "stages the current block of changes to git" })
          map("n", "<Leader>gu", gitsigns.undo_stage_hunk, { desc = "undo the last (git) staged change" })
          map("n", "<Leader>gp", gitsigns.preview_hunk, { desc = "show the git diff for the current change block" })

          map("n", "<Leader>gb", gitsigns.blame, { desc = "git blame for the whole file" })
          map("n", "<Leader>gl", gitsigns.blame_line, { desc = "git blame for the current line" })

          map("n", "<Leader>gh", gitsigns.toggle_linehl, { desc = "toggle git diff highlight by line" })
          map("n", "<Leader>gw", gitsigns.toggle_word_diff, { desc = "toggle git diff highlight by word" })

          -- Text object
          map({ "o", "x" }, "ih", ":<C-U>Gitsigns select_hunk<CR>")
        end,
      })
    end,
  },
}