diff options
author | Eric Gaudet <gaudet.eric@gmail.com> | 2023-01-21 20:04:08 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-21 23:04:08 -0500 |
commit | 0ca1a739d19e5ed4ea731946b9a524e8af068e6c (patch) | |
tree | e019044015574a7c6fb5a5df5926aabcd2169638 /plugins | |
parent | bc047a8a0567e682568feb0c581b970565cd5664 (diff) | |
download | lite-xl-plugins-0ca1a739d19e5ed4ea731946b9a524e8af068e6c.tar.gz lite-xl-plugins-0ca1a739d19e5ed4ea731946b9a524e8af068e6c.zip |
Add new pluging gitopen:open-from-commit (#178)
Given a git commit (default HEAD), open the files that are in the project and
part of this commit.
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/gitopen.lua | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/plugins/gitopen.lua b/plugins/gitopen.lua new file mode 100644 index 0000000..4870191 --- /dev/null +++ b/plugins/gitopen.lua @@ -0,0 +1,53 @@ +-- mod-version:3 +local core = require "core" +local command = require "core.command" +local common = require "core.common" + + +local function exec(cmd) + local proc = process.start(cmd) + while proc:running() do + coroutine.yield(0.1) + end + if proc:returncode() > 0 then + core.error("ERROR - command: " .. table.concat(cmd, " ")) + end + return proc:read_stdout() or "" +end + + +local function git_find_files_and_open(commit) + local git_root = exec({"git", "rev-parse", "--show-toplevel"}):match( "^%s*(.-)%s*$" ) + local file_list_str = exec({"git", "show", "--name-only", "--pretty=format:", commit}) + + local git_files = {} + for str in string.gmatch(file_list_str, "([^\n]+)") do + git_files[git_root .. PATHSEP .. str] = true + end + + for dir, item in core.get_project_files() do + local key = dir .. PATHSEP .. item.filename + if git_files[key] then + core.root_view:open_doc(core.open_doc(item.filename)) + end + end +end + +-- works in any context +command.add(nil, { + ["gitopen:open-from-commit"] = function(dv) + core.command_view:enter("Which commit? (default=HEAD)", { + submit = function(commit) + if commit == nil or commit == "" then + commit = "HEAD" + end + -- open the files in the background, return immediately + core.add_thread( + function () + git_find_files_and_open(commit) + end + ) + end + }) + end, +}) |