git.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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, stderr
  29. end
  30. local function safe_deep_fetch()
  31. local ret, result, error = git_cmd { args = { "rev-parse", "--is-shallow-repository" } }
  32. if ret ~= 0 then
  33. Log:error(vim.inspect(error))
  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! Please pull the changes manually in " .. get_lvim_base_dir())
  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. if not safe_deep_fetch() then
  49. return
  50. end
  51. local ret
  52. ret = git_cmd { args = { "diff", "--quiet", "@{upstream}" } }
  53. if ret == 0 then
  54. Log:info "LunarVim is already up-to-date"
  55. return
  56. end
  57. ret = git_cmd { args = { "merge", "--ff-only", "--progress" } }
  58. if ret ~= 0 then
  59. Log:error("Update failed! Please pull the changes manually in " .. get_lvim_base_dir())
  60. return
  61. end
  62. return true
  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 args = { "switch", branch }
  71. if branch:match "^[0-9]" then
  72. -- avoids producing an error for tags
  73. vim.list_extend(args, { "--detach" })
  74. end
  75. local ret = git_cmd { args = args }
  76. if ret ~= 0 then
  77. Log:error "Unable to switch branches! Check the log for further information"
  78. return
  79. end
  80. return true
  81. end
  82. ---Get the current Lunarvim development branch
  83. ---@return string|nil
  84. function M.get_lvim_branch()
  85. local _, results = git_cmd { args = { "rev-parse", "--abbrev-ref", "HEAD" } }
  86. local branch = if_nil(results[1], "")
  87. return branch
  88. end
  89. ---Get currently checked-out tag of Lunarvim
  90. ---@return string
  91. function M.get_lvim_tag()
  92. local args = { "describe", "--tags", "--abbrev=0" }
  93. local _, results = git_cmd { args = args }
  94. local tag = if_nil(results[1], "")
  95. return tag
  96. end
  97. ---Get currently running version of Lunarvim
  98. ---@return string
  99. function M.get_lvim_version()
  100. local current_branch = M.get_lvim_branch()
  101. local lvim_version
  102. if current_branch ~= "HEAD" or "" then
  103. lvim_version = current_branch .. "-" .. M.get_lvim_current_sha()
  104. else
  105. lvim_version = "v" .. M.get_lvim_tag()
  106. end
  107. return lvim_version
  108. end
  109. ---Get the commit hash of currently checked-out commit of Lunarvim
  110. ---@return string|nil
  111. function M.get_lvim_current_sha()
  112. local _, log_results = git_cmd { args = { "log", "--pretty=format:%h", "-1" } }
  113. local abbrev_version = if_nil(log_results[1], "")
  114. return abbrev_version
  115. end
  116. return M