0
0

git.lua 4.9 KB

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