summaryrefslogtreecommitdiff
path: root/lua/blink/cmp/fuzzy/download/git.lua
diff options
context:
space:
mode:
authorMike Vink <mike@pionative.com>2025-01-19 13:52:52 +0100
committerMike Vink <mike@pionative.com>2025-01-19 13:52:52 +0100
commitb77413ff8f59f380612074f0c9bd49093d8db695 (patch)
tree32c39a811ba96ed4ab0a1c81cce9f8d518ed7e31 /lua/blink/cmp/fuzzy/download/git.lua
Squashed 'mut/neovim/pack/plugins/start/blink.cmp/' content from commit 1cc3b1a
git-subtree-dir: mut/neovim/pack/plugins/start/blink.cmp git-subtree-split: 1cc3b1a908fbcfd15451c4772759549724f38524
Diffstat (limited to 'lua/blink/cmp/fuzzy/download/git.lua')
-rw-r--r--lua/blink/cmp/fuzzy/download/git.lua70
1 files changed, 70 insertions, 0 deletions
diff --git a/lua/blink/cmp/fuzzy/download/git.lua b/lua/blink/cmp/fuzzy/download/git.lua
new file mode 100644
index 0000000..63a7646
--- /dev/null
+++ b/lua/blink/cmp/fuzzy/download/git.lua
@@ -0,0 +1,70 @@
+local async = require('blink.cmp.lib.async')
+local files = require('blink.cmp.fuzzy.download.files')
+local git = {}
+
+function git.get_version()
+ return async.task.await_all({ git.get_tag(), git.get_sha() }):map(
+ function(results)
+ return {
+ tag = results[1],
+ sha = results[2],
+ }
+ end
+ )
+end
+
+function git.get_tag()
+ return async.task.new(function(resolve, reject)
+ -- If repo_dir is nil, no git reposiory is found, similar to `out.code == 128`
+ local repo_dir = vim.fs.root(files.root_dir, '.git')
+ if not repo_dir then resolve() end
+
+ vim.system({
+ 'git',
+ '--git-dir',
+ vim.fs.joinpath(repo_dir, '.git'),
+ '--work-tree',
+ repo_dir,
+ 'describe',
+ '--tags',
+ '--exact-match',
+ }, { cwd = files.root_dir }, function(out)
+ if out.code == 128 then return resolve() end
+ if out.code ~= 0 then
+ return reject('While getting git tag, git exited with code ' .. out.code .. ': ' .. out.stderr)
+ end
+
+ local lines = vim.split(out.stdout, '\n')
+ if not lines[1] then return reject('Expected atleast 1 line of output from git describe') end
+ return resolve(lines[1])
+ end)
+ end)
+end
+
+function git.get_sha()
+ return async.task.new(function(resolve, reject)
+ -- If repo_dir is nil, no git reposiory is found, similar to `out.code == 128`
+ local repo_dir = vim.fs.root(files.root_dir, '.git')
+ if not repo_dir then resolve() end
+
+ vim.system({
+ 'git',
+ '--git-dir',
+ vim.fs.joinpath(repo_dir, '.git'),
+ '--work-tree',
+ repo_dir,
+ 'rev-parse',
+ 'HEAD',
+ }, { cwd = files.root_dir }, function(out)
+ if out.code == 128 then return resolve() end
+ if out.code ~= 0 then
+ return reject('While getting git sha, git exited with code ' .. out.code .. ': ' .. out.stderr)
+ end
+
+ local sha = vim.split(out.stdout, '\n')[1]
+ return resolve(sha)
+ end)
+ end)
+end
+
+return git