git.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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:new({
  13. command = "git",
  14. args = opts.args,
  15. cwd = opts.cwd,
  16. on_stderr = function(_, data)
  17. table.insert(stderr, data)
  18. end,
  19. }):sync()
  20. if not vim.tbl_isempty(stderr) then
  21. Log:debug(stderr)
  22. end
  23. if not vim.tbl_isempty(stdout) then
  24. Log:debug(stdout)
  25. end
  26. return ret, stdout, stderr
  27. end
  28. local function safe_deep_fetch()
  29. local ret, result, error = git_cmd { args = { "rev-parse", "--is-shallow-repository" } }
  30. if ret ~= 0 then
  31. Log:error(vim.inspect(error))
  32. return
  33. end
  34. -- git fetch --unshallow will cause an error on a a complete clone
  35. local fetch_mode = result[1] == "true" and "--unshallow" or "--all"
  36. ret = git_cmd { args = { "fetch", fetch_mode } }
  37. if ret ~= 0 then
  38. Log:error("Git fetch failed! Please pull the changes manually in " .. get_lvim_base_dir())
  39. return
  40. end
  41. return true
  42. end
  43. ---pulls the latest changes from github
  44. function M.update_base_lvim()
  45. Log:info "Checking for updates"
  46. if not safe_deep_fetch() then
  47. return
  48. end
  49. local ret
  50. ret = git_cmd { args = { "diff", "--quiet", "@{upstream}" } }
  51. if ret == 0 then
  52. Log:info "LunarVim is already up-to-date"
  53. return
  54. end
  55. ret = git_cmd { args = { "merge", "--ff-only", "--progress" } }
  56. if ret ~= 0 then
  57. Log:error("Update failed! Please pull the changes manually in " .. get_lvim_base_dir())
  58. return
  59. end
  60. return true
  61. end
  62. ---Switch Lunarvim to the specified development branch
  63. ---@param branch string
  64. function M.switch_lvim_branch(branch)
  65. if not safe_deep_fetch() then
  66. return
  67. end
  68. local args = { "switch", branch }
  69. if branch:match "^[0-9]" then
  70. -- avoids producing an error for tags
  71. vim.list_extend(args, { "--detach" })
  72. end
  73. local ret = git_cmd { args = args }
  74. if ret ~= 0 then
  75. Log:error "Unable to switch branches! Check the log for further information"
  76. return
  77. end
  78. return true
  79. end
  80. ---Get the current Lunarvim development branch
  81. ---@return string|nil
  82. function M.get_lvim_branch()
  83. local _, results = git_cmd { args = { "rev-parse", "--abbrev-ref", "HEAD" } }
  84. local branch = if_nil(results[1], "")
  85. return branch
  86. end
  87. ---Get currently checked-out tag of Lunarvim
  88. ---@return string
  89. function M.get_lvim_tag()
  90. local args = { "describe", "--tags", "--abbrev=0" }
  91. local _, results = git_cmd { args = args }
  92. local tag = if_nil(results[1], "")
  93. return tag
  94. end
  95. ---Get currently running version of Lunarvim
  96. ---@return string
  97. function M.get_lvim_version()
  98. local current_branch = M.get_lvim_branch()
  99. local lvim_version
  100. if current_branch ~= "HEAD" or "" then
  101. lvim_version = current_branch .. "-" .. M.get_lvim_current_sha()
  102. else
  103. lvim_version = "v" .. M.get_lvim_tag()
  104. end
  105. return lvim_version
  106. end
  107. ---Get the commit hash of currently checked-out commit of Lunarvim
  108. ---@return string|nil
  109. function M.get_lvim_current_sha()
  110. local _, log_results = git_cmd { args = { "log", "--pretty=format:%h", "-1" } }
  111. local abbrev_version = if_nil(log_results[1], "")
  112. return abbrev_version
  113. end
  114. return M