0
0

git.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. return {
  2. {
  3. "lewis6991/gitsigns.nvim",
  4. enabled = true,
  5. lazy = false,
  6. dependencies = { "nvim-telescope/telescope.nvim", { "command-palette" } },
  7. config = function(_, opts)
  8. local gitsigns = require("gitsigns")
  9. gitsigns.setup {
  10. diff_opts = {
  11. internal = true,
  12. linematch = 1,
  13. algorithm = "minimal"
  14. },
  15. signcolumn = true,
  16. numhl = false,
  17. linehl = false,
  18. max_file_length = 50000,
  19. word_diff = false,
  20. auto_attach = true,
  21. attach_to_untracked = true
  22. }
  23. local pickers = require("telescope.pickers")
  24. local actions = require("telescope.actions")
  25. local action_state = require("telescope.actions.state")
  26. local finders = require("telescope.finders")
  27. local conf = require("telescope.config").values
  28. local entry_display = require("telescope.pickers.entry_display")
  29. local palette = require("command-palette")
  30. local open_picker = function(title, command)
  31. local displayer = entry_display.create({
  32. separator = " ▏",
  33. items = {
  34. { width = 16 }, { width = 64 }, { width = 24 },
  35. { remaining = true }
  36. }
  37. })
  38. local branches = vim.split(vim.fn.system(
  39. "git branch --list --sort committerdate --format '%(refname) ||!|| %(committerdate:relative) ||!|| branch: %(refname:short) ||!|| %(authorname) ||!|| %(subject)' | tac"),
  40. "\n")
  41. local commits = vim.split(vim.fn.system(
  42. "git log --branches=\\* --pretty='format:%H ||!|| %cr ||!|| %(decorate:prefix=,suffix=,separator= ) ||!|| %an ||!|| %s'"),
  43. "\n")
  44. local results = {}
  45. for _, v in pairs(branches) do
  46. if string.len(v) > 10 then
  47. table.insert(results, v)
  48. end
  49. end
  50. for _, v in pairs(commits) do
  51. if string.len(v) > 10 then
  52. table.insert(results, v)
  53. end
  54. end
  55. pickers.new(opts, {
  56. prompt_title = title,
  57. finder = finders.new_table({
  58. results = results,
  59. entry_maker = function(entry)
  60. local s = vim.split(entry, "||!||")
  61. return {
  62. value = s[1],
  63. display = function()
  64. return displayer({
  65. { s[2] }, { s[3] }, { s[4] }, { s[5] }
  66. })
  67. end,
  68. ordinal = string.format("%s %s", s[3], s[5])
  69. }
  70. end
  71. }),
  72. sorter = conf.generic_sorter(),
  73. attach_mappings = function(prompt_bufnr, _)
  74. actions.select_default:replace(function()
  75. actions.close(prompt_bufnr)
  76. local hash = action_state.get_selected_entry().value
  77. if command == "base" then
  78. vim.api.nvim_exec(
  79. ":Gitsigns change_base " .. hash .. " true",
  80. true)
  81. elseif command == "diff" then
  82. vim.api.nvim_exec(":Gitsigns diffthis " .. hash,
  83. true)
  84. end
  85. end)
  86. return true
  87. end
  88. }):find()
  89. end
  90. local open_diff_picker =
  91. function()
  92. open_picker("Gitsigns Diff", "diff")
  93. end
  94. local open_base_picker = function()
  95. open_picker("Gitsigns Change Base", "base")
  96. end
  97. vim.keymap.set("n", "Sgd", open_diff_picker)
  98. vim.keymap.set("n", "Sgb", open_base_picker)
  99. palette.add({
  100. {
  101. "Gitsigns", "", {
  102. {
  103. "Diff file",
  104. "Open the Gitsigns differ for this file",
  105. open_diff_picker
  106. },
  107. {
  108. "Change diff base",
  109. "Change base branch for Gitsigns to diff again",
  110. open_base_picker
  111. }
  112. }
  113. }
  114. })
  115. end
  116. }
  117. }