git.lua 3.4 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
  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. return true
  64. end
  65. ---Switch Lunarvim to the specified development branch
  66. ---@param branch string
  67. function M.switch_lvim_branch(branch)
  68. if not safe_deep_fetch() then
  69. return
  70. end
  71. local args = { "switch", branch }
  72. if branch:match "^[0-9]" then
  73. -- avoids producing an error for tags
  74. vim.list_extend(args, { "--detach" })
  75. end
  76. local ret = git_cmd { args = args }
  77. if ret ~= 0 then
  78. Log:error "Unable to switch branches! Check the log for further information"
  79. return
  80. end
  81. return true
  82. end
  83. ---Get the current Lunarvim development branch
  84. ---@return string|nil
  85. function M.get_lvim_branch()
  86. local _, results = git_cmd { args = { "rev-parse", "--abbrev-ref", "HEAD" } }
  87. local branch = if_nil(results[1], "")
  88. return branch
  89. end
  90. ---Get currently checked-out tag of Lunarvim
  91. ---@return string
  92. function M.get_lvim_tag()
  93. local args = { "describe", "--tags", "--abbrev=0" }
  94. local _, results = git_cmd { args = args }
  95. local tag = if_nil(results[1], "")
  96. return tag
  97. end
  98. ---Get currently running version of Lunarvim
  99. ---@return string
  100. function M.get_lvim_version()
  101. local current_branch = M.get_lvim_branch()
  102. local lvim_version
  103. if current_branch ~= "HEAD" or "" then
  104. lvim_version = current_branch .. "-" .. M.get_lvim_current_sha()
  105. else
  106. lvim_version = "v" .. M.get_lvim_tag()
  107. end
  108. return lvim_version
  109. end
  110. ---Get the commit hash of currently checked-out commit of Lunarvim
  111. ---@return string|nil
  112. function M.get_lvim_current_sha()
  113. local _, log_results = git_cmd { args = { "log", "--pretty=format:%h", "-1" } }
  114. local abbrev_version = if_nil(log_results[1], "")
  115. return abbrev_version
  116. end
  117. return M