git.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. local M = {}
  2. local Log = require "lvim.core.log"
  3. local if_nil = vim.F.if_nil
  4. local function git_cmd(opts)
  5. local plenary_loaded, Job = pcall(require, "plenary.job")
  6. if not plenary_loaded then
  7. return 1, { "" }
  8. end
  9. opts = opts or {}
  10. opts.cwd = opts.cwd or get_lvim_base_dir()
  11. local stderr = {}
  12. local stdout, ret = Job
  13. :new({
  14. command = "git",
  15. args = opts.args,
  16. cwd = opts.cwd,
  17. on_stderr = function(_, data)
  18. table.insert(stderr, data)
  19. end,
  20. })
  21. :sync()
  22. if not vim.tbl_isempty(stderr) then
  23. Log:debug(stderr)
  24. end
  25. if not vim.tbl_isempty(stdout) then
  26. Log:debug(stdout)
  27. end
  28. return ret, stdout
  29. end
  30. local function safe_deep_fetch()
  31. local ret, result = git_cmd { args = { "rev-parse", "--is-shallow-repository" } }
  32. if ret ~= 0 then
  33. Log:error "Git fetch failed! Check the log for further information"
  34. return
  35. end
  36. -- git fetch --unshallow will cause an error on a a complete clone
  37. local fetch_mode = result[1] == "true" and "--unshallow" or "--all"
  38. ret = git_cmd { args = { "fetch", fetch_mode } }
  39. if ret ~= 0 then
  40. Log:error "Git fetch failed! Check the log for further information"
  41. return
  42. end
  43. return true
  44. end
  45. ---pulls the latest changes from github
  46. function M.update_base_lvim()
  47. Log:info "Checking for updates"
  48. local ret = git_cmd { args = { "fetch" } }
  49. if ret ~= 0 then
  50. Log:error "Update failed! Check the log for further information"
  51. return
  52. end
  53. ret = git_cmd { args = { "diff", "--quiet", "@{upstream}" } }
  54. if ret == 0 then
  55. Log:info "LunarVim is already up-to-date"
  56. return
  57. end
  58. ret = git_cmd { args = { "merge", "--ff-only", "--progress" } }
  59. if ret ~= 0 then
  60. Log:error "Update failed! Please pull the changes manually instead."
  61. return
  62. end
  63. end
  64. ---Switch Lunarvim to the specified development branch
  65. ---@param branch string
  66. function M.switch_lvim_branch(branch)
  67. if not safe_deep_fetch() then
  68. return
  69. end
  70. local ret = git_cmd { args = { "switch", branch } }
  71. if ret ~= 0 then
  72. Log:error "Unable to switch branches! Check the log for further information"
  73. return
  74. end
  75. end
  76. ---Get the current Lunarvim development branch
  77. ---@return string|nil
  78. function M.get_lvim_branch()
  79. local _, results = git_cmd { args = { "rev-parse", "--abbrev-ref", "HEAD" } }
  80. local branch = if_nil(results[1], "")
  81. return branch
  82. end
  83. ---Get currently checked-out tag of Lunarvim
  84. ---@return string
  85. function M.get_lvim_tag()
  86. local args = { "describe", "--tags", "--abbrev=0" }
  87. local _, results = git_cmd { args = args }
  88. local tag = if_nil(results[1], "")
  89. return tag
  90. end
  91. ---Get the commit hash of currently checked-out commit of Lunarvim
  92. ---@return string|nil
  93. function M.get_lvim_current_sha()
  94. local _, log_results = git_cmd { args = { "log", "--pretty=format:%h", "-1" } }
  95. local abbrev_version = if_nil(log_results[1], "")
  96. return abbrev_version
  97. end
  98. function M.generate_plugins_sha(output)
  99. local list = {}
  100. output = output or "commits.lua"
  101. local core_plugins = require "lvim.plugins"
  102. for _, plugin in pairs(core_plugins) do
  103. local name = plugin[1]:match "/(%S*)"
  104. local url = "https://github.com/" .. plugin[1]
  105. print("checking: " .. name .. ", at: " .. url)
  106. local retval, latest_sha = git_cmd { args = { "ls-remote", url, "origin", "HEAD" } }
  107. if retval == 0 then
  108. -- replace dashes, remove postfixes and use lowercase
  109. local normalize_name = (name:gsub("-", "_"):gsub("%.%S+", "")):lower()
  110. list[normalize_name] = latest_sha[1]:gsub("\tHEAD", "")
  111. end
  112. end
  113. require("lvim.utils").write_file(output, "local commit = " .. vim.inspect(list), "w")
  114. end
  115. return M