diff --git a/README.md b/README.md index 803117b..43eff2e 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,8 @@ fix that). ## Known Issues -There are a few known issues. I'll try to be actively filing them in the issues. If you experience something, and it's not an issue, feel free to make an issue! Even if it's a dupe I am just happy for the contribution. + +There are a few known issues. I'll try to be actively filing them in the issues. If you experience something, and it's not an issue, feel free to make an issue! Even if it's a dupe I am just happy for the contribution. ## Dependencies @@ -44,7 +45,7 @@ installed. ## Repository -This repository does work best with a bare repo. To clone a bare repo, do the following. +This repository does work best with a bare repo. To clone a bare repo, do the following. ```shell git clone --bare @@ -53,18 +54,23 @@ git clone --bare If you do not use a bare repo, using telescope create command will be more helpful in the process of creating a branch. ### Debugging + git-worktree writes logs to a `git-worktree-nvim.log` file that resides in Neovim's cache path. (`:echo stdpath("cache")` to find where that is for you.) By default, logging is enabled for warnings and above. This can be changed by setting `vim.g.git_worktree_log_level` variable to one of the following log levels: `trace`, `debug`, `info`, `warn`, `error`, or `fatal`. Note that this would have to be done **before** git-worktree's `setup` call. Alternatively, it can be more convenient to launch Neovim with an environment variable, e.g. `> GIT_WORKTREE_NVIM_LOG=trace nvim`. In case both, `vim.g` and an environment variable are used, the log level set by the environment variable overrules. Supplying an invalid log level defaults back to warnings. ### Troubleshooting + If the upstream is not setup correctly when trying to pull or push, make sure the following command returns what is shown below. This seems to happen with the gitHub cli. + ``` git config --get remote.origin.fetch +refs/heads/*:refs/remotes/origin/* ``` + if it does not run the following + ``` git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" ``` @@ -74,7 +80,7 @@ git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" `change_directory_command`: The vim command used to change to the new worktree directory. Set this to `tcd` if you want to only change the `pwd` for the current vim Tab. -`update_on_change`: Updates the current buffer to point to the new work tree if +`update_on_change`: Updates the current buffer to point to the new work tree if the file is found in the new project. Otherwise, the following command will be run. `update_on_change_command`: The vim command to run during the `update_on_change` event. @@ -104,8 +110,12 @@ Three primary functions should cover your day-to-day. The path can be either relative from the git root dir or absoulut path to the worktree. ```lua --- Creates a worktree. Requires the path, branch name, and the upstream --- Example: +-- Creates a worktree. Requires the path, branch name, and the upstream, +-- Optionally the base branch from which to create the new worktree and branch can be added +-- Examples: +-- Creating new worktree and branch `feat-69` based on develop branch to path `feat-69` +:lua require("git-worktree").create_worktree("feat-69", "feat-69", "origin", "develop") +-- Creating new worktree `master` based on master to path `feat-69` :lua require("git-worktree").create_worktree("feat-69", "master", "origin") -- switches to an existing worktree. Requires the path name @@ -126,6 +136,7 @@ require("telescope").load_extension("git_worktree") ``` ### Switch and Delete a worktrees + To bring up the telescope window listing your workspaces run the following ```lua @@ -136,11 +147,13 @@ To bring up the telescope window listing your workspaces run the following ``` ### Create a worktree + To bring up the telescope window to create a new worktree run the following ```lua :lua require('telescope').extensions.git_worktree.create_git_worktree() ``` + First a telescope git branch window will appear. Presing enter will choose the selected branch for the branch name. If no branch is selected, then the prompt will be used as the branch name. After the git branch window, a prompt will be presented to enter the path name to write the worktree to. @@ -149,7 +162,7 @@ As of now you can not specify the upstream in the telescope create workflow, how ## Hooks -Yes! The best part about `git-worktree` is that it emits information so that you +Yes! The best part about `git-worktree` is that it emits information so that you can act on it. ```lua @@ -181,4 +194,4 @@ charging your development experience! ## Made with fury All plugins are made live on [Twitch](https://twitch.tv/ThePrimeagen) with love -and fury. Come and join! +and fury. Come and join! diff --git a/lua/git-worktree/enum.lua b/lua/git-worktree/enum.lua index a98b6f7..d58150a 100644 --- a/lua/git-worktree/enum.lua +++ b/lua/git-worktree/enum.lua @@ -15,8 +15,5 @@ return { Create = "create", Switch = "switch", Delete = "delete", - }) + }), } - - - diff --git a/lua/git-worktree/init.lua b/lua/git-worktree/init.lua index 0fb5295..b504036 100644 --- a/lua/git-worktree/init.lua +++ b/lua/git-worktree/init.lua @@ -16,7 +16,9 @@ M.setup_git_info = function() local is_in_worktree = false local inside_worktree_job = Job:new({ - 'git', 'rev-parse', '--is-inside-work-tree', + "git", + "rev-parse", + "--is-inside-work-tree", cwd = cwd, }) @@ -27,7 +29,9 @@ M.setup_git_info = function() end local find_git_dir_job = Job:new({ - 'git', 'rev-parse', '--absolute-git-dir', + "git", + "rev-parse", + "--absolute-git-dir", cwd = cwd, }) @@ -50,7 +54,7 @@ M.setup_git_info = function() git_worktree_root = cwd else local start = stdout:find("%.git") - git_worktree_root = stdout:sub(1,start - 2) + git_worktree_root = stdout:sub(1, start - 2) end else local start = stdout:find("/worktrees/") @@ -67,12 +71,16 @@ M.setup_git_info = function() end local find_toplevel_job = Job:new({ - 'git', 'rev-parse', '--show-toplevel', + "git", + "rev-parse", + "--show-toplevel", cwd = cwd, }) local find_toplevel_bare_job = Job:new({ - 'git', 'rev-parse', '--is-bare-repository', + "git", + "rev-parse", + "--is-bare-repository", cwd = cwd, }) @@ -121,7 +129,6 @@ M.setup_git_info = function() return end end - end local function on_tree_change_handler(op, metadata) @@ -129,7 +136,9 @@ local function on_tree_change_handler(op, metadata) if op == Enum.Operations.Switch then local changed = M.update_current_buffer(metadata["prev_path"]) if not changed then - status:log().debug("Could not change to the file in the new worktree, running the `update_on_change_command`") + status:log().debug( + "Could not change to the file in the new worktree, running the `update_on_change_command`" + ) vim.cmd(M._config.update_on_change_command) end end @@ -152,12 +161,16 @@ local function change_dirs(path) -- vim.loop.chdir(worktree_path) if Path:new(worktree_path):exists() then - local cmd = string.format("%s %s", M._config.change_directory_command, worktree_path) + local cmd = string.format( + "%s %s", + M._config.change_directory_command, + worktree_path + ) status:log().debug("Changing to directory " .. worktree_path) vim.cmd(cmd) current_worktree_path = worktree_path else - status:error('Could not chang to directory: ' ..worktree_path) + status:error("Could not chang to directory: " .. worktree_path) end if M._config.clearjumps_on_change then @@ -168,15 +181,16 @@ local function change_dirs(path) return previous_worktree end -local function create_worktree_job(path, branch, found_branch) - - local worktree_add_cmd = 'git' - local worktree_add_args = {'worktree', 'add'} +local function create_worktree_job(path, branch, found_branch, base_branch) + local worktree_add_cmd = "git" + local worktree_add_args = { "worktree", "add" } if not found_branch then - table.insert(worktree_add_args, '-b') + table.insert(worktree_add_args, "-b") table.insert(worktree_add_args, branch) table.insert(worktree_add_args, path) + + table.insert(worktree_add_args, base_branch) else table.insert(worktree_add_args, path) table.insert(worktree_add_args, branch) @@ -187,8 +201,10 @@ local function create_worktree_job(path, branch, found_branch) args = worktree_add_args, cwd = git_worktree_root, on_start = function() - status:next_status(worktree_add_cmd .. " " .. table.concat(worktree_add_args, " ")) - end + status:next_status( + worktree_add_cmd .. " " .. table.concat(worktree_add_args, " ") + ) + end, }) end @@ -199,8 +215,10 @@ local function has_worktree(path, cb) local plenary_path = Path:new(path) local job = Job:new({ - 'git', 'worktree', 'list', on_stdout = function(_, data) - + "git", + "worktree", + "list", + on_stdout = function(_, data) local list_data = {} for section in data:gmatch("%S+") do table.insert(list_data, section) @@ -213,17 +231,26 @@ local function has_worktree(path, cb) start = data == path else local worktree_path = Path:new( - string.format("%s" .. Path.path.sep .. "%s", git_worktree_root, path) + string.format( + "%s" .. Path.path.sep .. "%s", + git_worktree_root, + path + ) ) worktree_path = worktree_path:absolute() start = data == worktree_path end -- TODO: This is clearly a hack (do not think we need this anymore?) - local start_with_head = string.find(data, string.format("[heads/%s]", path), 1, true) + local start_with_head = string.find( + data, + string.format("[heads/%s]", path), + 1, + true + ) found = found or start or start_with_head end, - cwd = git_worktree_root + cwd = git_worktree_root, }) job:after(function() @@ -243,7 +270,8 @@ local function failure(from, cmd, path, soft_error) path, vim.inspect(cmd), vim.inspect(e:result()), - vim.inspect(e:stderr_result())) + vim.inspect(e:stderr_result()) + ) if soft_error then status:status(error_message) @@ -256,18 +284,22 @@ end local function has_origin() local found = false local job = Job:new({ - 'git', 'remote', 'show', + "git", + "remote", + "show", on_stdout = function(_, data) data = vim.trim(data) - found = found or data == 'origin' + found = found or data == "origin" end, cwd = git_worktree_root, }) -- TODO: I really don't want status's spread everywhere... seems bad - job:after(function() - status:status("found origin: " .. tostring(found)) - end):sync() + job + :after(function() + status:status("found origin: " .. tostring(found)) + end) + :sync() return found end @@ -275,9 +307,11 @@ end local function has_branch(branch, cb) local found = false local job = Job:new({ - 'git', 'branch', on_stdout = function(_, data) + "git", + "branch", + on_stdout = function(_, data) -- remove markere on current branch - data = data:gsub("*","") + data = data:gsub("*", "") data = vim.trim(data) found = found or data == branch end, @@ -286,14 +320,60 @@ local function has_branch(branch, cb) -- TODO: I really don't want status's spread everywhere... seems bad status:next_status(string.format("Checking for branch %s", branch)) - job:after(function() - status:status("found branch: " .. tostring(found)) - cb(found) - end):start() + job + :after(function() + status:status("found branch: " .. tostring(found)) + cb(found) + end) + :start() end -local function create_worktree(path, branch, upstream, found_branch) - local create = create_worktree_job(path, branch, found_branch) +-- Has branch function to use outside of this file +-- Using the existing one did not work for some reason, got weird erros :) +M.has_branch = function(branch) + local found = false + Job + :new({ + "git", + "branch", + on_stdout = function(_, data) + -- remove marker on current branch + data = data:gsub("*", "") + data = vim.trim(data) + found = found or data == branch + end, + cwd = git_worktree_root, + }) + :sync() + + return found +end + +local function create_worktree( + path, + branch, + upstream, + found_branch, + base_branch +) + has_branch(base_branch, function(found) + if not found then + status:status( + "Valid base branch was not defined, using current worktree" + ) + base_branch = nil + end + end) + + local current_branch_job = Job:new({ + "git", + "branch", + "--show-current", + cwd = vim.loop.cwd(), + on_stdout = function(_, data) + base_branch = base_branch or data + end, + }) local worktree_path if Path:new(path):is_absolute() then @@ -303,88 +383,127 @@ local function create_worktree(path, branch, upstream, found_branch) end local fetch = Job:new({ - 'git', 'fetch', '--all', + "git", + "fetch", + "--all", cwd = worktree_path, on_start = function() status:next_status("git fetch --all (This may take a moment)") - end + end, }) - local set_branch_cmd = 'git' - local set_branch_args= {'branch', string.format('--set-upstream-to=%s/%s', upstream, branch)} + local set_branch_cmd = "git" + local set_branch_args = { + "branch", + string.format("--set-upstream-to=%s/%s", upstream, branch), + } local set_branch = Job:new({ command = set_branch_cmd, args = set_branch_args, cwd = worktree_path, on_start = function() - status:next_status(set_branch_cmd .. " " .. table.concat(set_branch_args, " ")) - end + status:next_status( + set_branch_cmd .. " " .. table.concat(set_branch_args, " ") + ) + end, }) -- TODO: How to configure origin??? Should upstream ever be the push -- destination? - local set_push_cmd = 'git' - local set_push_args = {'push', "--set-upstream", upstream, branch, path} - local set_push = Job:new({ + local set_push_cmd = "git" + local set_push_args = { "push", "--set-upstream", upstream, branch, path } + local set_push = Job:new({ command = set_push_cmd, args = set_push_args, cwd = worktree_path, on_start = function() - status:next_status(set_push_cmd .. " " .. table.concat(set_push_args, " ")) - end + status:next_status( + set_push_cmd .. " " .. table.concat(set_push_args, " ") + ) + end, }) local rebase = Job:new({ - 'git', 'rebase', + "git", + "rebase", cwd = worktree_path, on_start = function() status:next_status("git rebase") - end + end, }) - if upstream ~= nil then - create:and_then_on_success(fetch) - fetch:and_then_on_success(set_branch) - - if M._config.autopush then - -- These are "optional" operations. - -- We have to figure out how we want to handle these... - set_branch:and_then(set_push) - set_push:and_then(rebase) - set_push:after_failure(failure("create_worktree", set_branch.args, worktree_path, true)) - else - set_branch:and_then(rebase) - end - - create:after_failure(failure("create_worktree", create.args, git_worktree_root)) - fetch:after_failure(failure("create_worktree", fetch.args, worktree_path)) + current_branch_job:add_on_exit_callback(function() + local create = create_worktree_job( + path, + branch, + found_branch, + base_branch + ) + + if upstream ~= nil then + create:and_then_on_success(fetch) + fetch:and_then_on_success(set_branch) + + if M._config.autopush then + -- These are "optional" operations. + -- We have to figure out how we want to handle these... + set_branch:and_then(set_push) + set_push:and_then(rebase) + set_push:after_failure( + failure( + "create_worktree", + set_branch.args, + worktree_path, + true + ) + ) + else + set_branch:and_then(rebase) + end - set_branch:after_failure(failure("create_worktree", set_branch.args, worktree_path, true)) + create:after_failure( + failure("create_worktree", create.args, git_worktree_root) + ) + fetch:after_failure( + failure("create_worktree", fetch.args, worktree_path) + ) - rebase:after(function() + set_branch:after_failure( + failure("create_worktree", set_branch.args, worktree_path, true) + ) - if rebase.code ~= 0 then - status:status("Rebase failed, but that's ok.") - end + rebase:after(function() + if rebase.code ~= 0 then + status:status("Rebase failed, but that's ok.") + end - vim.schedule(function() - emit_on_change(Enum.Operations.Create, {path = path, branch = branch, upstream = upstream}) - M.switch_worktree(path) + vim.schedule(function() + emit_on_change( + Enum.Operations.Create, + { path = path, branch = branch, upstream = upstream } + ) + M.switch_worktree(path) + end) end) - end) - else - create:after(function() - vim.schedule(function() - emit_on_change(Enum.Operations.Create, {path = path, branch = branch, upstream = upstream}) - M.switch_worktree(path) + else + create:after(function() + vim.schedule(function() + emit_on_change( + Enum.Operations.Create, + { path = path, branch = branch, upstream = upstream } + ) + M.switch_worktree(path) + end) end) - end) - end + end + + create:start() + end) - create:start() + current_branch_job:start() end -M.create_worktree = function(path, branch, upstream) +M.create_worktree = function(path, branch, upstream, base_branch) status:reset(8) if upstream == nil then @@ -401,26 +520,28 @@ M.create_worktree = function(path, branch, upstream) end has_branch(branch, function(found_branch) - create_worktree(path, branch, upstream, found_branch) + create_worktree(path, branch, upstream, found_branch, base_branch) end) end) - end M.switch_worktree = function(path) status:reset(2) M.setup_git_info() has_worktree(path, function(found) - if not found then - status:error("worktree does not exists, please create it first " .. path) + status:error( + "worktree does not exists, please create it first " .. path + ) end vim.schedule(function() local prev_path = change_dirs(path) - emit_on_change(Enum.Operations.Switch, { path = path, prev_path = prev_path }) + emit_on_change( + Enum.Operations.Switch, + { path = path, prev_path = prev_path } + ) end) - end) end @@ -437,7 +558,10 @@ M.delete_worktree = function(path, force, opts) end local cmd = { - "git", "worktree", "remove", path + "git", + "worktree", + "remove", + path, } if force then @@ -485,7 +609,7 @@ M.update_current_buffer = function(prev_path) end local name = Path:new(current_buf_name):absolute() - local start, fin = string.find(name, cwd..Path.path.sep, 1, true) + local start, fin = string.find(name, cwd .. Path.path.sep, 1, true) if start ~= nil then return true end @@ -497,7 +621,7 @@ M.update_current_buffer = function(prev_path) local local_name = name:sub(fin + 2) - local final_path = Path:new({cwd, local_name}):absolute() + local final_path = Path:new({ cwd, local_name }):absolute() if not Path:new(final_path):exists() then return false diff --git a/lua/git-worktree/status.lua b/lua/git-worktree/status.lua index e1e5825..e1ea0ca 100644 --- a/lua/git-worktree/status.lua +++ b/lua/git-worktree/status.lua @@ -2,7 +2,8 @@ local Status = {} local function set_log_level() local log_levels = { "trace", "debug", "info", "warn", "error", "fatal" } - local log_level = vim.env.GIT_WORKTREE_NVIM_LOG or vim.g.git_worktree_log_level + local log_level = vim.env.GIT_WORKTREE_NVIM_LOG + or vim.g.git_worktree_log_level for _, level in pairs(log_levels) do if level == log_level then @@ -13,14 +14,13 @@ local function set_log_level() return "warn" -- default, if user hasn't set to one from log_levels end - function Status:new(options) - local obj = vim.tbl_extend('force', { + local obj = vim.tbl_extend("force", { -- What to do here? logger = require("plenary.log").new({ plugin = "git-worktree-nvim", level = set_log_level(), - }) + }), }, options or {}) setmetatable(obj, self) diff --git a/lua/git-worktree/test.lua b/lua/git-worktree/test.lua index 3906c9a..3db2700 100644 --- a/lua/git-worktree/test.lua +++ b/lua/git-worktree/test.lua @@ -1,8 +1,4 @@ - local Path = require("plenary.path") local path = Path:new(vim.loop.cwd(), "foo", "..", "..") - print(path:absolute()) - - diff --git a/lua/telescope/_extensions/git_worktree.lua b/lua/telescope/_extensions/git_worktree.lua index d2494fe..85d11e4 100644 --- a/lua/telescope/_extensions/git_worktree.lua +++ b/lua/telescope/_extensions/git_worktree.lua @@ -57,72 +57,85 @@ delete_worktree = function(prompt_bufnr, force) end end -local create_input_prompt = function(cb) +local create_input_prompt = function() + return vim.fn.input("Path to subtree > ") +end - --[[ - local window = Window.centered({ - width = 30, - height = 1 - }) - vim.api.nvim_buf_set_option(window.bufnr, "buftype", "prompt") - vim.fn.prompt_setprompt(window.bufnr, "Worktree Location: ") - vim.fn.prompt_setcallback(window.bufnr, function(text) - vim.api.nvim_win_close(window.win_id, true) - vim.api.nvim_buf_delete(window.bufnr, {force = true}) - cb(text) - end) - - vim.api.nvim_set_current_win(window.win_id) - vim.fn.schedule(function() - vim.nvim_command("startinsert") - end) - --]] - -- - - local subtree = vim.fn.input("Path to subtree > ") - cb(subtree) +local use_current_worktree_as_base_prompt = function() + return vim.fn.confirm("Use current worktree as base?", "&Yes\n&No", 1) == 1 +end + +local get_base_branch = function(opts, name, branch) + local base_branch_selection_opts = opts or {} + base_branch_selection_opts.attach_mappings = function() + actions.select_default:replace(function(prompt_bufnr, _) + local selected_entry = action_state.get_selected_entry() + local current_line = action_state.get_current_line() + + actions.close(prompt_bufnr) + + local base_branch = selected_entry ~= nil and selected_entry.value + or current_line + + git_worktree.create_worktree(name, branch, nil, base_branch) + end) + + -- do we need to replace other default maps? + + return true + end + require("telescope.builtin").git_branches(base_branch_selection_opts) end local create_worktree = function(opts) - opts = opts or {} - opts.attach_mappings = function() - actions.select_default:replace( - function(prompt_bufnr, _) - local selected_entry = action_state.get_selected_entry() - local current_line = action_state.get_current_line() + local branch_selection_opts = opts or {} + branch_selection_opts.attach_mappings = function() + actions.select_default:replace(function(prompt_bufnr, _) + local selected_entry = action_state.get_selected_entry() + local current_line = action_state.get_current_line() - actions.close(prompt_bufnr) + actions.close(prompt_bufnr) - local branch = selected_entry ~= nil and - selected_entry.value or current_line + local branch = selected_entry ~= nil and selected_entry.value + or current_line - if branch == nil then - return - end + if branch == nil then + return + end - create_input_prompt(function(name) - if name == "" then - name = branch - end + local name = create_input_prompt() + if name == "" then + name = branch + end + + local has_branch = git_worktree.has_branch(branch) + + if not has_branch then + if use_current_worktree_as_base_prompt() then git_worktree.create_worktree(name, branch) - end) - end) + else + get_base_branch(opts, name, branch) + end + else + git_worktree.create_worktree(name, branch) + end + end) -- do we need to replace other default maps? return true end - require("telescope.builtin").git_branches(opts) + require("telescope.builtin").git_branches(branch_selection_opts) end local telescope_git_worktree = function(opts) opts = opts or {} - local output = utils.get_os_command_output({"git", "worktree", "list"}) + local output = utils.get_os_command_output({ "git", "worktree", "list" }) local results = {} local widths = { path = 0, sha = 0, - branch = 0 + branch = 0, } local parse_line = function(line) @@ -141,7 +154,10 @@ local telescope_git_worktree = function(opts) local path_len = strings.strdisplaywidth(new_path or "") widths[key] = math.max(val, path_len) else - widths[key] = math.max(val, strings.strdisplaywidth(entry[key] or "")) + widths[key] = math.max( + val, + strings.strdisplaywidth(entry[key] or "") + ) end end @@ -157,26 +173,26 @@ local telescope_git_worktree = function(opts) return end - local displayer = require("telescope.pickers.entry_display").create { + local displayer = require("telescope.pickers.entry_display").create({ separator = " ", items = { { width = widths.branch }, { width = widths.path }, { width = widths.sha }, }, - } + }) local make_display = function(entry) - return displayer { + return displayer({ { entry.branch, "TelescopeResultsIdentifier" }, { utils.transform_path(opts, entry.path) }, { entry.sha }, - } + }) end pickers.new(opts or {}, { prompt_title = "Git Worktrees", - finder = finders.new_table { + finder = finders.new_table({ results = results, entry_maker = function(entry) entry.value = entry.branch @@ -184,7 +200,7 @@ local telescope_git_worktree = function(opts) entry.display = make_display return entry end, - }, + }), sorter = conf.generic_sorter(opts), attach_mappings = function(_, map) action_set.select:replace(switch_worktree) @@ -197,14 +213,13 @@ local telescope_git_worktree = function(opts) end) return true - end + end, }):find() end -return require("telescope").register_extension( - { - exports = { - git_worktrees = telescope_git_worktree, - create_git_worktree = create_worktree - } - }) +return require("telescope").register_extension({ + exports = { + git_worktrees = telescope_git_worktree, + create_git_worktree = create_worktree, + }, +}) diff --git a/stylua.toml b/stylua.toml new file mode 100644 index 0000000..4318ca8 --- /dev/null +++ b/stylua.toml @@ -0,0 +1,3 @@ +column_width = 80 +indent_type = "Spaces" +indent_width = 4 diff --git a/tests/git_harness.lua b/tests/git_harness.lua index afa216b..fbe6515 100644 --- a/tests/git_harness.lua +++ b/tests/git_harness.lua @@ -1,5 +1,5 @@ -local git_worktree = require('git-worktree') -local Job = require('plenary.job') +local git_worktree = require("git-worktree") +local Job = require("plenary.job") local Path = require("plenary.path") local M = {} @@ -7,61 +7,75 @@ local M = {} local get_os_command_output = function(cmd) local command = table.remove(cmd, 1) local stderr = {} - local stdout, ret = Job:new({ - command = command, - args = cmd, - cwd = git_worktree.get_root(), - on_stderr = function(_, data) - table.insert(stderr, data) - end - }):sync() + local stdout, ret = Job + :new({ + command = command, + args = cmd, + cwd = git_worktree.get_root(), + on_stderr = function(_, data) + table.insert(stderr, data) + end, + }) + :sync() return stdout, ret, stderr end local prepare_origin_repo = function(dir) - vim.api.nvim_exec('!cp -r tests/repo_origin/ /tmp/' .. dir, true) - vim.api.nvim_exec('!mv /tmp/'..dir..'/.git-orig /tmp/'..dir..'/.git', true) + vim.api.nvim_exec("!cp -r tests/repo_origin/ /tmp/" .. dir, true) + vim.api.nvim_exec( + "!mv /tmp/" .. dir .. "/.git-orig /tmp/" .. dir .. "/.git", + true + ) end local prepare_bare_repo = function(dir, origin_dir) - vim.api.nvim_exec('!git clone --bare /tmp/'..origin_dir..' /tmp/'..dir, true) + vim.api.nvim_exec( + "!git clone --bare /tmp/" .. origin_dir .. " /tmp/" .. dir, + true + ) end local fix_fetch_all = function() - vim.api.nvim_exec('!git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"', true) + vim.api.nvim_exec( + '!git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"', + true + ) end local prepare_repo = function(dir, origin_dir) - vim.api.nvim_exec('!git clone /tmp/'..origin_dir..' /tmp/'..dir, true) + vim.api.nvim_exec("!git clone /tmp/" .. origin_dir .. " /tmp/" .. dir, true) end local random_string = function() - math.randomseed(os.clock()^5) + math.randomseed(os.clock() ^ 5) local ret = "" for _ = 1, 5 do - local random_char = math.random(97,122) + local random_char = math.random(97, 122) ret = ret .. string.char(random_char) end return ret end local change_dir = function(dir) - vim.api.nvim_set_current_dir('/tmp/'..dir) - git_worktree.set_worktree_root('/tmp/'..dir) + vim.api.nvim_set_current_dir("/tmp/" .. dir) + git_worktree.set_worktree_root("/tmp/" .. dir) end local cleanup_repos = function() - vim.api.nvim_exec('silent !rm -rf /tmp/git_worktree_test*', true) + vim.api.nvim_exec("silent !rm -rf /tmp/git_worktree_test*", true) end local create_worktree = function(folder_path, commitish) - vim.api.nvim_exec('!git worktree add ' .. folder_path .. ' ' .. commitish, true) + vim.api.nvim_exec( + "!git worktree add " .. folder_path .. " " .. commitish, + true + ) end -local project_dir = vim.api.nvim_exec('pwd', true) +local project_dir = vim.api.nvim_exec("pwd", true) local reset_cwd = function() - vim.cmd('cd ' .. project_dir) + vim.cmd("cd " .. project_dir) vim.api.nvim_set_current_dir(project_dir) end @@ -89,15 +103,14 @@ M.in_non_git_repo = function(cb) if err ~= nil then error(err) end - end end M.in_bare_repo_from_origin_no_worktrees = function(cb) return function() local random_id = random_string() - local origin_repo_dir = 'git_worktree_test_origin_repo_' .. random_id - local bare_repo_dir = 'git_worktree_test_repo_' .. random_id + local origin_repo_dir = "git_worktree_test_origin_repo_" .. random_id + local bare_repo_dir = "git_worktree_test_repo_" .. random_id config_git_worktree() cleanup_repos() @@ -117,15 +130,14 @@ M.in_bare_repo_from_origin_no_worktrees = function(cb) if err ~= nil then error(err) end - end end M.in_repo_from_origin_no_worktrees = function(cb) return function() local random_id = random_string() - local origin_repo_dir = 'git_worktree_test_origin_repo_' .. random_id - local repo_dir = 'git_worktree_test_repo_' .. random_id + local origin_repo_dir = "git_worktree_test_origin_repo_" .. random_id + local repo_dir = "git_worktree_test_repo_" .. random_id config_git_worktree() cleanup_repos() @@ -144,14 +156,13 @@ M.in_repo_from_origin_no_worktrees = function(cb) if err ~= nil then error(err) end - end end M.in_repo_from_local_no_worktrees = function(cb) return function() local random_id = random_string() - local local_repo_dir = 'git_worktree_test_origin_repo_' .. random_id + local local_repo_dir = "git_worktree_test_origin_repo_" .. random_id config_git_worktree() cleanup_repos() @@ -169,15 +180,14 @@ M.in_repo_from_local_no_worktrees = function(cb) if err ~= nil then error(err) end - end end M.in_bare_repo_from_origin_1_worktree = function(cb) return function() local random_id = random_string() - local origin_repo_dir = 'git_worktree_test_origin_repo_' .. random_id - local bare_repo_dir = 'git_worktree_test_repo_' .. random_id + local origin_repo_dir = "git_worktree_test_origin_repo_" .. random_id + local bare_repo_dir = "git_worktree_test_repo_" .. random_id config_git_worktree() cleanup_repos() @@ -185,7 +195,7 @@ M.in_bare_repo_from_origin_1_worktree = function(cb) prepare_origin_repo(origin_repo_dir) prepare_bare_repo(bare_repo_dir, origin_repo_dir) change_dir(bare_repo_dir) - create_worktree('master','master') + create_worktree("master", "master") local _, err = pcall(cb) @@ -196,16 +206,15 @@ M.in_bare_repo_from_origin_1_worktree = function(cb) if err ~= nil then error(err) end - end end M.in_repo_from_origin_1_worktree = function(cb) return function() local random_id = random_string() - local origin_repo_dir = 'git_worktree_test_origin_repo_' .. random_id - local repo_dir = 'git_worktree_test_repo_' .. random_id - local feat_dir = 'git_worktree_test_repo_featB_' .. random_id + local origin_repo_dir = "git_worktree_test_origin_repo_" .. random_id + local repo_dir = "git_worktree_test_repo_" .. random_id + local feat_dir = "git_worktree_test_repo_featB_" .. random_id config_git_worktree() cleanup_repos() @@ -214,7 +223,7 @@ M.in_repo_from_origin_1_worktree = function(cb) prepare_repo(repo_dir, origin_repo_dir) change_dir(repo_dir) - create_worktree('../'..feat_dir,'featB') + create_worktree("../" .. feat_dir, "featB") local _, err = pcall(cb) @@ -225,15 +234,14 @@ M.in_repo_from_origin_1_worktree = function(cb) if err ~= nil then error(err) end - end end M.in_bare_repo_from_origin_2_worktrees = function(cb) return function() local random_id = random_string() - local origin_repo_dir = 'git_worktree_test_origin_repo_' .. random_id - local bare_repo_dir = 'git_worktree_test_repo_' .. random_id + local origin_repo_dir = "git_worktree_test_origin_repo_" .. random_id + local bare_repo_dir = "git_worktree_test_repo_" .. random_id config_git_worktree() cleanup_repos() @@ -241,8 +249,8 @@ M.in_bare_repo_from_origin_2_worktrees = function(cb) prepare_origin_repo(origin_repo_dir) prepare_bare_repo(bare_repo_dir, origin_repo_dir) change_dir(bare_repo_dir) - create_worktree('featB','featB') - create_worktree('featC','featC') + create_worktree("featB", "featB") + create_worktree("featC", "featC") local _, err = pcall(cb) @@ -253,17 +261,16 @@ M.in_bare_repo_from_origin_2_worktrees = function(cb) if err ~= nil then error(err) end - end end M.in_repo_from_origin_2_worktrees = function(cb) return function() local random_id = random_string() - local origin_repo_dir = 'git_worktree_test_origin_repo_' .. random_id - local repo_dir = 'git_worktree_test_repo_' .. random_id - local featB_dir = 'git_worktree_test_repo_featB_' .. random_id - local featC_dir = 'git_worktree_test_repo_featC_' .. random_id + local origin_repo_dir = "git_worktree_test_origin_repo_" .. random_id + local repo_dir = "git_worktree_test_repo_" .. random_id + local featB_dir = "git_worktree_test_repo_featB_" .. random_id + local featC_dir = "git_worktree_test_repo_featC_" .. random_id config_git_worktree() cleanup_repos() @@ -272,8 +279,8 @@ M.in_repo_from_origin_2_worktrees = function(cb) prepare_repo(repo_dir, origin_repo_dir) change_dir(repo_dir) - create_worktree('../'..featB_dir,'featB') - create_worktree('../'..featC_dir,'featC') + create_worktree("../" .. featB_dir, "featB") + create_worktree("../" .. featC_dir, "featC") local _, err = pcall(cb) @@ -284,15 +291,14 @@ M.in_repo_from_origin_2_worktrees = function(cb) if err ~= nil then error(err) end - end end M.in_bare_repo_from_origin_2_similar_named_worktrees = function(cb) return function() local random_id = random_string() - local origin_repo_dir = 'git_worktree_test_origin_repo_' .. random_id - local bare_repo_dir = 'git_worktree_test_repo_' .. random_id + local origin_repo_dir = "git_worktree_test_origin_repo_" .. random_id + local bare_repo_dir = "git_worktree_test_repo_" .. random_id config_git_worktree() cleanup_repos() @@ -300,8 +306,8 @@ M.in_bare_repo_from_origin_2_similar_named_worktrees = function(cb) prepare_origin_repo(origin_repo_dir) prepare_bare_repo(bare_repo_dir, origin_repo_dir) change_dir(bare_repo_dir) - create_worktree('featB','featB') - create_worktree('featB-test','featC') + create_worktree("featB", "featB") + create_worktree("featB-test", "featC") local _, err = pcall(cb) @@ -312,17 +318,16 @@ M.in_bare_repo_from_origin_2_similar_named_worktrees = function(cb) if err ~= nil then error(err) end - end end M.in_repo_from_origin_2_similar_named_worktrees = function(cb) return function() local random_id = random_string() - local origin_repo_dir = 'git_worktree_test_origin_repo_' .. random_id - local repo_dir = 'git_worktree_test_repo_' .. random_id - local featB_dir = 'git_worktree_test_repo_featB_' .. random_id - local featC_dir = 'git_worktree_test_repo_featB-test_' .. random_id + local origin_repo_dir = "git_worktree_test_origin_repo_" .. random_id + local repo_dir = "git_worktree_test_repo_" .. random_id + local featB_dir = "git_worktree_test_repo_featB_" .. random_id + local featC_dir = "git_worktree_test_repo_featB-test_" .. random_id config_git_worktree() cleanup_repos() @@ -331,8 +336,8 @@ M.in_repo_from_origin_2_similar_named_worktrees = function(cb) prepare_repo(repo_dir, origin_repo_dir) change_dir(repo_dir) - create_worktree('../'..featB_dir,'featB') - create_worktree('../'..featC_dir,'featC') + create_worktree("../" .. featB_dir, "featB") + create_worktree("../" .. featC_dir, "featC") local _, err = pcall(cb) @@ -343,13 +348,16 @@ M.in_repo_from_origin_2_similar_named_worktrees = function(cb) if err ~= nil then error(err) end - end end local get_git_branches_upstreams = function() local output = get_os_command_output({ - "git", "for-each-ref", "--format", "'%(refname:short),%(upstream:short)'", "refs/heads" + "git", + "for-each-ref", + "--format", + "'%(refname:short),%(upstream:short)'", + "refs/heads", }) return output end @@ -362,21 +370,20 @@ M.check_branch_upstream = function(branch, upstream) if upstream == nil then upstream_to_check = "" else - upstream_to_check = upstream .. '/' .. branch + upstream_to_check = upstream .. "/" .. branch end local refs = get_git_branches_upstreams() for _, ref in ipairs(refs) do - ref = ref:gsub("'","") - local line = vim.split(ref, ",",true) + ref = ref:gsub("'", "") + local line = vim.split(ref, ",", true) local b = line[1] local u = line[2] if b == branch then correct_branch = true - correct_upstream = ( u == upstream_to_check ) + correct_upstream = (u == upstream_to_check) end - end return correct_branch, correct_upstream @@ -384,7 +391,9 @@ end local get_git_worktrees = function() local output = get_os_command_output({ - "git", "worktree", "list" + "git", + "worktree", + "list", }) return output end @@ -402,7 +411,6 @@ M.check_git_worktree_exists = function(worktree_path) if worktree_path == worktree_line[1] then worktree_exists = true end - end return worktree_exists diff --git a/tests/worktree_spec.lua b/tests/worktree_spec.lua index 4f5e523..d30e298 100644 --- a/tests/worktree_spec.lua +++ b/tests/worktree_spec.lua @@ -1,22 +1,27 @@ -local git_worktree = require('git-worktree') -local Path = require('plenary.path') +local git_worktree = require("git-worktree") +local Path = require("plenary.path") -local harness = require('tests.git_harness') +local harness = require("tests.git_harness") local in_non_git_repo = harness.in_non_git_repo -local in_bare_repo_from_origin_no_worktrees = harness.in_bare_repo_from_origin_no_worktrees -local in_repo_from_origin_no_worktrees = harness.in_repo_from_origin_no_worktrees -local in_bare_repo_from_origin_1_worktree = harness.in_bare_repo_from_origin_1_worktree +local in_bare_repo_from_origin_no_worktrees = + harness.in_bare_repo_from_origin_no_worktrees +local in_repo_from_origin_no_worktrees = + harness.in_repo_from_origin_no_worktrees +local in_bare_repo_from_origin_1_worktree = + harness.in_bare_repo_from_origin_1_worktree local in_repo_from_origin_1_worktree = harness.in_repo_from_origin_1_worktree local in_repo_from_local_no_worktrees = harness.in_repo_from_local_no_worktrees -local in_bare_repo_from_origin_2_worktrees = harness.in_bare_repo_from_origin_2_worktrees +local in_bare_repo_from_origin_2_worktrees = + harness.in_bare_repo_from_origin_2_worktrees local in_repo_from_origin_2_worktrees = harness.in_repo_from_origin_2_worktrees -local in_bare_repo_from_origin_2_similar_named_worktrees = harness.in_bare_repo_from_origin_2_similar_named_worktrees -local in_repo_from_origin_2_similar_named_worktrees = harness.in_repo_from_origin_2_similar_named_worktrees +local in_bare_repo_from_origin_2_similar_named_worktrees = + harness.in_bare_repo_from_origin_2_similar_named_worktrees +local in_repo_from_origin_2_similar_named_worktrees = + harness.in_repo_from_origin_2_similar_named_worktrees local check_git_worktree_exists = harness.check_git_worktree_exists local check_branch_upstream = harness.check_branch_upstream -describe('git-worktree', function() - +describe("git-worktree", function() local completed_create = false local completed_switch = false local completed_delete = false @@ -46,868 +51,877 @@ describe('git-worktree', function() git_worktree.reset() end) - describe('Create', function() - - it('can create a worktree(from origin)(relative path) from a bare repo and switch to it', + describe("Create", function() + it( + "can create a worktree(from origin)(relative path) from a bare repo and switch to it", in_bare_repo_from_origin_no_worktrees(function() - - local branch = "master" - local upstream = "origin" - local path = "master" - git_worktree.create_worktree(path, branch, upstream) - - vim.fn.wait( - 10000, - function() - return completed_create and completed_switch - end, - 1000 - ) - - local expected_path = git_worktree:get_root() .. Path.path.sep .. path - -- Check to make sure directory was switched - assert.are.same(expected_path, vim.loop.cwd()) - - -- Check to make sure it is added to git worktree list - assert.True(check_git_worktree_exists(expected_path)) - - -- check to make sure branch/upstream is correct - local correct_branch, correct_upstream = check_branch_upstream(branch, upstream) - assert.True(correct_branch) - assert.True(correct_upstream) - - end)) - - it('can create a worktree(from origin)(absolute path) from a bare repo and switch to it', + local branch = "master" + local upstream = "origin" + local path = "master" + git_worktree.create_worktree(path, branch, upstream) + + vim.fn.wait(10000, function() + return completed_create and completed_switch + end, 1000) + + local expected_path = git_worktree:get_root() + .. Path.path.sep + .. path + -- Check to make sure directory was switched + assert.are.same(expected_path, vim.loop.cwd()) + + -- Check to make sure it is added to git worktree list + assert.True(check_git_worktree_exists(expected_path)) + + -- check to make sure branch/upstream is correct + local correct_branch, correct_upstream = check_branch_upstream( + branch, + upstream + ) + assert.True(correct_branch) + assert.True(correct_upstream) + end) + ) + + it( + "can create a worktree(from origin)(absolute path) from a bare repo and switch to it", in_bare_repo_from_origin_no_worktrees(function() - - local branch = "master" - local upstream = "origin" - local path = git_worktree.get_root() .. Path.path.sep .. "master" - git_worktree.create_worktree(path, branch, upstream) - - vim.fn.wait( - 10000, - function() - return completed_create and completed_switch - end, - 1000 - ) - - -- Check to make sure directory was switched - assert.are.same(vim.loop.cwd(), path) - - -- Check to make sure it is added to git worktree list - assert.True(check_git_worktree_exists(path)) - - -- check to make sure branch/upstream is correct - local correct_branch, correct_upstream = check_branch_upstream(branch, upstream) - assert.True(correct_branch) - assert.True(correct_upstream) - - end)) - - it('can create a worktree(from origin)(relative path) from a repo and switch to it', + local branch = "master" + local upstream = "origin" + local path = git_worktree.get_root() + .. Path.path.sep + .. "master" + git_worktree.create_worktree(path, branch, upstream) + + vim.fn.wait(10000, function() + return completed_create and completed_switch + end, 1000) + + -- Check to make sure directory was switched + assert.are.same(vim.loop.cwd(), path) + + -- Check to make sure it is added to git worktree list + assert.True(check_git_worktree_exists(path)) + + -- check to make sure branch/upstream is correct + local correct_branch, correct_upstream = check_branch_upstream( + branch, + upstream + ) + assert.True(correct_branch) + assert.True(correct_upstream) + end) + ) + + it( + "can create a worktree(from origin)(relative path) from a repo and switch to it", in_repo_from_origin_no_worktrees(function() - - local random_str = git_worktree.get_root():sub(git_worktree.get_root():len()-4) - local branch = "featB" - local upstream = "origin" - local path = "../git_worktree_test_repo_" .. branch .. "_" .. random_str - git_worktree.create_worktree(path, branch, upstream) - - vim.fn.wait( - 10000, - function() - return completed_create and completed_switch - end, - 1000 - ) - - -- Check to make sure directory was switched - local expected_path = Path:new(git_worktree:get_root() .. '/' .. path):normalize() - assert.are.same(expected_path, vim.loop.cwd()) - - -- Check to make sure it is added to git worktree list - assert.True(check_git_worktree_exists(expected_path)) - - -- check to make sure branch/upstream is correct - local correct_branch, correct_upstream = check_branch_upstream(branch, upstream) - assert.True(correct_branch) - assert.True(correct_upstream) - - end)) - - it('can create a worktree(from origin)(absolute path) from a repo and switch to it', + local random_str = git_worktree.get_root():sub( + git_worktree.get_root():len() - 4 + ) + local branch = "featB" + local upstream = "origin" + local path = "../git_worktree_test_repo_" + .. branch + .. "_" + .. random_str + git_worktree.create_worktree(path, branch, upstream) + + vim.fn.wait(10000, function() + return completed_create and completed_switch + end, 1000) + + -- Check to make sure directory was switched + local expected_path = Path + :new(git_worktree:get_root() .. "/" .. path) + :normalize() + assert.are.same(expected_path, vim.loop.cwd()) + + -- Check to make sure it is added to git worktree list + assert.True(check_git_worktree_exists(expected_path)) + + -- check to make sure branch/upstream is correct + local correct_branch, correct_upstream = check_branch_upstream( + branch, + upstream + ) + assert.True(correct_branch) + assert.True(correct_upstream) + end) + ) + + it( + "can create a worktree(from origin)(absolute path) from a repo and switch to it", in_repo_from_origin_no_worktrees(function() - - local random_str = git_worktree.get_root():sub(git_worktree.get_root():len()-4) - local branch = "featB" - local upstream = "origin" - local path = "/tmp/git_worktree_test_repo_" .. branch .. "_" .. random_str - - git_worktree.create_worktree(path, branch, upstream) - - vim.fn.wait( - 10000, - function() - return completed_create and completed_switch - end, - 1000 - ) - - -- Check to make sure directory was switched - assert.are.same(path, vim.loop.cwd()) - - -- Check to make sure it is added to git worktree list - assert.True(check_git_worktree_exists(path)) - - -- check to make sure branch/upstream is correct - local correct_branch, correct_upstream = check_branch_upstream(branch, upstream) - assert.True(correct_branch) - assert.True(correct_upstream) - - end)) - - it('can create a worktree(no upstream but detect origin)(relative path) from a bare repo and switch to it', + local random_str = git_worktree.get_root():sub( + git_worktree.get_root():len() - 4 + ) + local branch = "featB" + local upstream = "origin" + local path = "/tmp/git_worktree_test_repo_" + .. branch + .. "_" + .. random_str + + git_worktree.create_worktree(path, branch, upstream) + + vim.fn.wait(10000, function() + return completed_create and completed_switch + end, 1000) + + -- Check to make sure directory was switched + assert.are.same(path, vim.loop.cwd()) + + -- Check to make sure it is added to git worktree list + assert.True(check_git_worktree_exists(path)) + + -- check to make sure branch/upstream is correct + local correct_branch, correct_upstream = check_branch_upstream( + branch, + upstream + ) + assert.True(correct_branch) + assert.True(correct_upstream) + end) + ) + + it( + "can create a worktree(no upstream but detect origin)(relative path) from a bare repo and switch to it", in_bare_repo_from_origin_no_worktrees(function() - - local branch = "master" - local upstream = "origin" - local path = "master" - git_worktree.create_worktree(path, branch) - - vim.fn.wait( - 10000, - function() - return completed_create and completed_switch - end, - 1000 - ) - - local expected_path = git_worktree:get_root() .. '/' .. path - - -- Check to make sure directory was switched - assert.are.same(expected_path, vim.loop.cwd()) - - -- Check to make sure it is added to git worktree list - assert.True(check_git_worktree_exists(expected_path)) - - -- check to make sure branch/upstream is correct - local correct_branch, correct_upstream = check_branch_upstream(branch, upstream) - assert.True(correct_branch) - assert.True(correct_upstream) - - end)) - - it('can create a worktree(no upstream but detect origin)(absolute path) from a bare repo and switch to it', + local branch = "master" + local upstream = "origin" + local path = "master" + git_worktree.create_worktree(path, branch) + + vim.fn.wait(10000, function() + return completed_create and completed_switch + end, 1000) + + local expected_path = git_worktree:get_root() .. "/" .. path + + -- Check to make sure directory was switched + assert.are.same(expected_path, vim.loop.cwd()) + + -- Check to make sure it is added to git worktree list + assert.True(check_git_worktree_exists(expected_path)) + + -- check to make sure branch/upstream is correct + local correct_branch, correct_upstream = check_branch_upstream( + branch, + upstream + ) + assert.True(correct_branch) + assert.True(correct_upstream) + end) + ) + + it( + "can create a worktree(no upstream but detect origin)(absolute path) from a bare repo and switch to it", in_bare_repo_from_origin_no_worktrees(function() - - local branch = "master" - local upstream = "origin" - local path = git_worktree:get_root() .. Path.path.sep .. "master" - - git_worktree.create_worktree(path, branch) - - vim.fn.wait( - 10000, - function() - return completed_create and completed_switch - end, - 1000 - ) - - -- Check to make sure directory was switched - assert.are.same(path, vim.loop.cwd()) - - -- Check to make sure it is added to git worktree list - assert.True(check_git_worktree_exists(path)) - - -- check to make sure branch/upstream is correct - local correct_branch, correct_upstream = check_branch_upstream(branch, upstream) - assert.True(correct_branch) - assert.True(correct_upstream) - - end)) - - it('can create a worktree(no upstream but detect origin)(relative path) from a repo and switch to it', + local branch = "master" + local upstream = "origin" + local path = git_worktree:get_root() + .. Path.path.sep + .. "master" + + git_worktree.create_worktree(path, branch) + + vim.fn.wait(10000, function() + return completed_create and completed_switch + end, 1000) + + -- Check to make sure directory was switched + assert.are.same(path, vim.loop.cwd()) + + -- Check to make sure it is added to git worktree list + assert.True(check_git_worktree_exists(path)) + + -- check to make sure branch/upstream is correct + local correct_branch, correct_upstream = check_branch_upstream( + branch, + upstream + ) + assert.True(correct_branch) + assert.True(correct_upstream) + end) + ) + + it( + "can create a worktree(no upstream but detect origin)(relative path) from a repo and switch to it", in_repo_from_origin_no_worktrees(function() - - local random_str = git_worktree.get_root():sub(git_worktree.get_root():len()-4) - local branch = "featB" - local upstream = "origin" - local path = "../git_worktree_test_repo_" .. branch .. "_" .. random_str - git_worktree.create_worktree(path, branch) - - vim.fn.wait( - 10000, - function() - return completed_create and completed_switch - end, - 1000 - ) - - -- Check to make sure directory was switched - local expected_path = Path:new(git_worktree:get_root() .. '/' .. path):normalize() - assert.are.same(expected_path, vim.loop.cwd()) - - -- Check to make sure it is added to git worktree list - assert.True(check_git_worktree_exists(expected_path)) - - -- check to make sure branch/upstream is correct - local correct_branch, correct_upstream = check_branch_upstream(branch, upstream) - assert.True(correct_branch) - assert.True(correct_upstream) - - end)) - - it('can create a worktree(no upstream but detect origin)(absolute path) from a repo and switch to it', + local random_str = git_worktree.get_root():sub( + git_worktree.get_root():len() - 4 + ) + local branch = "featB" + local upstream = "origin" + local path = "../git_worktree_test_repo_" + .. branch + .. "_" + .. random_str + git_worktree.create_worktree(path, branch) + + vim.fn.wait(10000, function() + return completed_create and completed_switch + end, 1000) + + -- Check to make sure directory was switched + local expected_path = Path + :new(git_worktree:get_root() .. "/" .. path) + :normalize() + assert.are.same(expected_path, vim.loop.cwd()) + + -- Check to make sure it is added to git worktree list + assert.True(check_git_worktree_exists(expected_path)) + + -- check to make sure branch/upstream is correct + local correct_branch, correct_upstream = check_branch_upstream( + branch, + upstream + ) + assert.True(correct_branch) + assert.True(correct_upstream) + end) + ) + + it( + "can create a worktree(no upstream but detect origin)(absolute path) from a repo and switch to it", in_repo_from_origin_no_worktrees(function() - - local random_str = git_worktree.get_root():sub(git_worktree.get_root():len()-4) - local branch = "featB" - local upstream = "origin" - local path = "/tmp/git_worktree_test_repo_" .. branch .. "_" .. random_str - - git_worktree.create_worktree(path, branch) - - vim.fn.wait( - 10000, - function() - return completed_create and completed_switch - end, - 1000 - ) - - -- Check to make sure directory was switched - assert.are.same(path, vim.loop.cwd()) - - -- Check to make sure it is added to git worktree list - assert.True(check_git_worktree_exists(path)) - - -- check to make sure branch/upstream is correct - local correct_branch, correct_upstream = check_branch_upstream(branch, upstream) - assert.True(correct_branch) - assert.True(correct_upstream) - - end)) - - it('can create a worktree(no upstream no origin)(relative path) from a repo and switch to it', + local random_str = git_worktree.get_root():sub( + git_worktree.get_root():len() - 4 + ) + local branch = "featB" + local upstream = "origin" + local path = "/tmp/git_worktree_test_repo_" + .. branch + .. "_" + .. random_str + + git_worktree.create_worktree(path, branch) + + vim.fn.wait(10000, function() + return completed_create and completed_switch + end, 1000) + + -- Check to make sure directory was switched + assert.are.same(path, vim.loop.cwd()) + + -- Check to make sure it is added to git worktree list + assert.True(check_git_worktree_exists(path)) + + -- check to make sure branch/upstream is correct + local correct_branch, correct_upstream = check_branch_upstream( + branch, + upstream + ) + assert.True(correct_branch) + assert.True(correct_upstream) + end) + ) + + it( + "can create a worktree(no upstream no origin)(relative path) from a repo and switch to it", in_repo_from_local_no_worktrees(function() - - local random_str = git_worktree.get_root():sub(git_worktree.get_root():len()-4) - local branch = "featB" - local upstream = nil - local path = "../git_worktree_test_repo_" .. branch .. "_" .. random_str - git_worktree.create_worktree(path, branch) - - vim.fn.wait( - 10000, - function() - return completed_create and completed_switch - end, - 1000 - ) - - -- Check to make sure directory was switched - local expected_path = Path:new(git_worktree:get_root() .. '/' .. path):normalize() - assert.are.same(expected_path, vim.loop.cwd()) - - -- Check to make sure it is added to git worktree list - assert.True(check_git_worktree_exists(expected_path)) - - -- check to make sure branch/upstream is correct - local correct_branch, correct_upstream = check_branch_upstream(branch, upstream) - assert.True(correct_branch) - assert.True(correct_upstream) - - end)) - - it('can create a worktree(no upstream no origin)(absolute path) from a repo and switch to it', + local random_str = git_worktree.get_root():sub( + git_worktree.get_root():len() - 4 + ) + local branch = "featB" + local upstream = nil + local path = "../git_worktree_test_repo_" + .. branch + .. "_" + .. random_str + git_worktree.create_worktree(path, branch) + + vim.fn.wait(10000, function() + return completed_create and completed_switch + end, 1000) + + -- Check to make sure directory was switched + local expected_path = Path + :new(git_worktree:get_root() .. "/" .. path) + :normalize() + assert.are.same(expected_path, vim.loop.cwd()) + + -- Check to make sure it is added to git worktree list + assert.True(check_git_worktree_exists(expected_path)) + + -- check to make sure branch/upstream is correct + local correct_branch, correct_upstream = check_branch_upstream( + branch, + upstream + ) + assert.True(correct_branch) + assert.True(correct_upstream) + end) + ) + + it( + "can create a worktree(no upstream no origin)(absolute path) from a repo and switch to it", in_repo_from_local_no_worktrees(function() - - local random_str = git_worktree.get_root():sub(git_worktree.get_root():len()-4) - local branch = "featB" - local upstream = nil - local path = "/tmp/git_worktree_test_repo_" .. branch .. "_" .. random_str - - git_worktree.create_worktree(path, branch) - - vim.fn.wait( - 10000, - function() - return completed_create and completed_switch - end, - 1000 - ) - - -- Check to make sure directory was switched - assert.are.same(path, vim.loop.cwd()) - - -- Check to make sure it is added to git worktree list - assert.True(check_git_worktree_exists(path)) - - -- check to make sure branch/upstream is correct - local correct_branch, correct_upstream = check_branch_upstream(branch, upstream) - assert.True(correct_branch) - assert.True(correct_upstream) - - end)) - - + local random_str = git_worktree.get_root():sub( + git_worktree.get_root():len() - 4 + ) + local branch = "featB" + local upstream = nil + local path = "/tmp/git_worktree_test_repo_" + .. branch + .. "_" + .. random_str + + git_worktree.create_worktree(path, branch) + + vim.fn.wait(10000, function() + return completed_create and completed_switch + end, 1000) + + -- Check to make sure directory was switched + assert.are.same(path, vim.loop.cwd()) + + -- Check to make sure it is added to git worktree list + assert.True(check_git_worktree_exists(path)) + + -- check to make sure branch/upstream is correct + local correct_branch, correct_upstream = check_branch_upstream( + branch, + upstream + ) + assert.True(correct_branch) + assert.True(correct_upstream) + end) + ) end) - describe('Switch', function() - - it('from a bare repo with one worktree, able to switch to worktree (relative path)', + describe("Switch", function() + it( + "from a bare repo with one worktree, able to switch to worktree (relative path)", in_bare_repo_from_origin_1_worktree(function() - - local path = "master" - git_worktree.switch_worktree(path) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - - -- Check to make sure directory was switched - assert.are.same(vim.loop.cwd(), git_worktree:get_root() .. Path.path.sep .. path) - - end)) - - it('from a bare repo with one worktree, able to switch to worktree (absolute path)', + local path = "master" + git_worktree.switch_worktree(path) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + + -- Check to make sure directory was switched + assert.are.same( + vim.loop.cwd(), + git_worktree:get_root() .. Path.path.sep .. path + ) + end) + ) + + it( + "from a bare repo with one worktree, able to switch to worktree (absolute path)", in_bare_repo_from_origin_1_worktree(function() - - local path = git_worktree:get_root() .. Path.path.sep .. "master" - git_worktree.switch_worktree(path) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - - -- Check to make sure directory was switched - assert.are.same(vim.loop.cwd(), path) - - end)) - - it('from a repo with one worktree, able to switch to worktree (relative path)', + local path = git_worktree:get_root() + .. Path.path.sep + .. "master" + git_worktree.switch_worktree(path) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + + -- Check to make sure directory was switched + assert.are.same(vim.loop.cwd(), path) + end) + ) + + it( + "from a repo with one worktree, able to switch to worktree (relative path)", in_repo_from_origin_1_worktree(function() - - local random_str = git_worktree.get_root():sub(git_worktree.get_root():len()-4) - local path = "../git_worktree_test_repo_featB_"..random_str - git_worktree.switch_worktree(path) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - - local expected_path = Path:new(git_worktree:get_root() .. '/'..path):normalize() - - -- Check to make sure directory was switched - assert.are.same(vim.loop.cwd(), expected_path) - - end)) - - it('from a repo with one worktree, able to switch to worktree (absolute path)', + local random_str = git_worktree.get_root():sub( + git_worktree.get_root():len() - 4 + ) + local path = "../git_worktree_test_repo_featB_" .. random_str + git_worktree.switch_worktree(path) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + + local expected_path = Path + :new(git_worktree:get_root() .. "/" .. path) + :normalize() + + -- Check to make sure directory was switched + assert.are.same(vim.loop.cwd(), expected_path) + end) + ) + + it( + "from a repo with one worktree, able to switch to worktree (absolute path)", in_repo_from_origin_1_worktree(function() + local random_str = git_worktree.get_root():sub( + git_worktree.get_root():len() - 4 + ) + local path = "/tmp/git_worktree_test_repo_featB_" .. random_str + git_worktree.switch_worktree(path) - local random_str = git_worktree.get_root():sub(git_worktree.get_root():len()-4) - local path = "/tmp/git_worktree_test_repo_featB_"..random_str - git_worktree.switch_worktree(path) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) + vim.fn.wait(10000, function() + return completed_switch + end, 1000) - -- Check to make sure directory was switched - assert.are.same(vim.loop.cwd(), path) - - end)) + -- Check to make sure directory was switched + assert.are.same(vim.loop.cwd(), path) + end) + ) local get_current_file = function() return vim.api.nvim_buf_get_name(0) end - it('in a featB worktree(bare) with file A open, switch to featC and switch to file A in other worktree', + it( + "in a featB worktree(bare) with file A open, switch to featC and switch to file A in other worktree", in_bare_repo_from_origin_2_worktrees(function() - - local featB_path = "featB" - local featB_abs_path = git_worktree:get_root() .. Path.path.sep .. featB_path - local featB_abs_A_path = featB_abs_path .. Path.path.sep .. "A.txt" - - local featC_path = "featC" - local featC_abs_path = git_worktree:get_root() .. Path.path.sep .. featC_path - local featC_abs_A_path = featC_abs_path .. Path.path.sep .. "A.txt" - - -- switch to featB worktree - git_worktree.switch_worktree(featB_path) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - - -- open A file - vim.cmd("e A.txt") - -- make sure it is opensd - assert.True(featB_abs_A_path == get_current_file()) - - -- switch to featB worktree - reset_variables() - git_worktree.switch_worktree(featC_path) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - - -- make sure it switch to file in other tree - assert.True(featC_abs_A_path == get_current_file()) - end)) - - it('in a featB worktree(non bare) with file A open, switch to featC and switch to file A in other worktree', + local featB_path = "featB" + local featB_abs_path = git_worktree:get_root() + .. Path.path.sep + .. featB_path + local featB_abs_A_path = featB_abs_path + .. Path.path.sep + .. "A.txt" + + local featC_path = "featC" + local featC_abs_path = git_worktree:get_root() + .. Path.path.sep + .. featC_path + local featC_abs_A_path = featC_abs_path + .. Path.path.sep + .. "A.txt" + + -- switch to featB worktree + git_worktree.switch_worktree(featB_path) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + + -- open A file + vim.cmd("e A.txt") + -- make sure it is opensd + assert.True(featB_abs_A_path == get_current_file()) + + -- switch to featB worktree + reset_variables() + git_worktree.switch_worktree(featC_path) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + + -- make sure it switch to file in other tree + assert.True(featC_abs_A_path == get_current_file()) + end) + ) + + it( + "in a featB worktree(non bare) with file A open, switch to featC and switch to file A in other worktree", in_repo_from_origin_2_worktrees(function() - - local random_str = git_worktree.get_root():sub(git_worktree.get_root():len()-4) - - local featB_path = "../git_worktree_test_repo_featB_"..random_str - local featB_abs_path = "/tmp/git_worktree_test_repo_featB_"..random_str - local featB_abs_A_path = featB_abs_path.."/A.txt" - - local featC_path = "../git_worktree_test_repo_featC_"..random_str - local featC_abs_path = "/tmp/git_worktree_test_repo_featC_"..random_str - local featC_abs_A_path = featC_abs_path.."/A.txt" - - -- switch to featB worktree - git_worktree.switch_worktree(featB_path) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - - -- open A file - vim.cmd("e A.txt") - -- make sure it is opensd - assert.True(featB_abs_A_path == get_current_file()) - - -- switch to featB worktree - reset_variables() - git_worktree.switch_worktree(featC_path) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - - -- make sure it switch to file in other tree - assert.True(featC_abs_A_path == get_current_file()) - end)) - - it("in a featB worktree(bare) with file B open, switch to featC and switch to worktree root in other worktree", + local random_str = git_worktree.get_root():sub( + git_worktree.get_root():len() - 4 + ) + + local featB_path = "../git_worktree_test_repo_featB_" + .. random_str + local featB_abs_path = "/tmp/git_worktree_test_repo_featB_" + .. random_str + local featB_abs_A_path = featB_abs_path .. "/A.txt" + + local featC_path = "../git_worktree_test_repo_featC_" + .. random_str + local featC_abs_path = "/tmp/git_worktree_test_repo_featC_" + .. random_str + local featC_abs_A_path = featC_abs_path .. "/A.txt" + + -- switch to featB worktree + git_worktree.switch_worktree(featB_path) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + + -- open A file + vim.cmd("e A.txt") + -- make sure it is opensd + assert.True(featB_abs_A_path == get_current_file()) + + -- switch to featB worktree + reset_variables() + git_worktree.switch_worktree(featC_path) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + + -- make sure it switch to file in other tree + assert.True(featC_abs_A_path == get_current_file()) + end) + ) + + it( + "in a featB worktree(bare) with file B open, switch to featC and switch to worktree root in other worktree", in_bare_repo_from_origin_2_worktrees(function() - - local featB_path = "featB" - local featB_abs_path = git_worktree:get_root() .. Path.path.sep .. featB_path - local featB_abs_B_path = featB_abs_path .. Path.path.sep .. "B.txt" - - local featC_path = "featC" - local featC_abs_path = git_worktree:get_root() .. Path.path.sep .. featC_path - - -- switch to featB worktree - git_worktree.switch_worktree(featB_path) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - - -- open B file - vim.cmd("e B.txt") - -- make sure it is opensd - assert.True(featB_abs_B_path == get_current_file()) - - -- switch to featB worktree - reset_variables() - git_worktree.switch_worktree(featC_path) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - - -- make sure it switch to file in other tree - assert.True(featC_abs_path == get_current_file()) - end)) - - it("in a featB worktree(non bare) with file B open, switch to featC and switch to worktree root in other worktree", + local featB_path = "featB" + local featB_abs_path = git_worktree:get_root() + .. Path.path.sep + .. featB_path + local featB_abs_B_path = featB_abs_path + .. Path.path.sep + .. "B.txt" + + local featC_path = "featC" + local featC_abs_path = git_worktree:get_root() + .. Path.path.sep + .. featC_path + + -- switch to featB worktree + git_worktree.switch_worktree(featB_path) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + + -- open B file + vim.cmd("e B.txt") + -- make sure it is opensd + assert.True(featB_abs_B_path == get_current_file()) + + -- switch to featB worktree + reset_variables() + git_worktree.switch_worktree(featC_path) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + + -- make sure it switch to file in other tree + assert.True(featC_abs_path == get_current_file()) + end) + ) + + it( + "in a featB worktree(non bare) with file B open, switch to featC and switch to worktree root in other worktree", in_repo_from_origin_2_worktrees(function() - - local random_str = git_worktree.get_root():sub(git_worktree.get_root():len()-4) - - local featB_path = "../git_worktree_test_repo_featB_"..random_str - local featB_abs_path = "/tmp/git_worktree_test_repo_featB_"..random_str - local featB_abs_B_path = featB_abs_path.."/B.txt" - - local featC_path = "../git_worktree_test_repo_featC_"..random_str - local featC_abs_path = "/tmp/git_worktree_test_repo_featC_"..random_str - - -- switch to featB worktree - git_worktree.switch_worktree(featB_path) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - - -- open A file - vim.cmd("e B.txt") - -- make sure it is opensd - assert.True(featB_abs_B_path == get_current_file()) - - -- switch to featB worktree - reset_variables() - git_worktree.switch_worktree(featC_path) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - - -- make sure it switch to file in other tree - assert.True(featC_abs_path == get_current_file()) - end)) - - it('from a bare repo with two worktrees, able to switch to worktree with similar names (relative path)', + local random_str = git_worktree.get_root():sub( + git_worktree.get_root():len() - 4 + ) + + local featB_path = "../git_worktree_test_repo_featB_" + .. random_str + local featB_abs_path = "/tmp/git_worktree_test_repo_featB_" + .. random_str + local featB_abs_B_path = featB_abs_path .. "/B.txt" + + local featC_path = "../git_worktree_test_repo_featC_" + .. random_str + local featC_abs_path = "/tmp/git_worktree_test_repo_featC_" + .. random_str + + -- switch to featB worktree + git_worktree.switch_worktree(featB_path) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + + -- open A file + vim.cmd("e B.txt") + -- make sure it is opensd + assert.True(featB_abs_B_path == get_current_file()) + + -- switch to featB worktree + reset_variables() + git_worktree.switch_worktree(featC_path) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + + -- make sure it switch to file in other tree + assert.True(featC_abs_path == get_current_file()) + end) + ) + + it( + "from a bare repo with two worktrees, able to switch to worktree with similar names (relative path)", in_bare_repo_from_origin_2_similar_named_worktrees(function() - - local path1 = "featB" - local path2 = "featB-test" - git_worktree.switch_worktree(path1) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - reset_variables() - - -- Check to make sure directory was switched - assert.are.same(vim.loop.cwd(), git_worktree:get_root() .. Path.path.sep .. path1) - - -- open A file - vim.cmd("e A.txt") - -- make sure it is opensd - assert.True(vim.loop.cwd().."/A.txt" == get_current_file()) - - git_worktree.switch_worktree(path2) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - reset_variables() - - -- Check to make sure directory was switched - assert.are.same(vim.loop.cwd(), git_worktree:get_root() .. Path.path.sep .. path2) - -- Make sure file is switched - assert.True(vim.loop.cwd().."/A.txt" == get_current_file()) - - git_worktree.switch_worktree(path1) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - - -- Check to make sure directory was switched - assert.are.same(vim.loop.cwd(), git_worktree:get_root() .. Path.path.sep .. path1) - -- Make sure file is switched - assert.True(vim.loop.cwd().."/A.txt" == get_current_file()) - - end)) - - it('from a bare repo with two worktrees, able to switch to worktree with similar names (absolute path)', + local path1 = "featB" + local path2 = "featB-test" + git_worktree.switch_worktree(path1) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + reset_variables() + + -- Check to make sure directory was switched + assert.are.same( + vim.loop.cwd(), + git_worktree:get_root() .. Path.path.sep .. path1 + ) + + -- open A file + vim.cmd("e A.txt") + -- make sure it is opensd + assert.True(vim.loop.cwd() .. "/A.txt" == get_current_file()) + + git_worktree.switch_worktree(path2) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + reset_variables() + + -- Check to make sure directory was switched + assert.are.same( + vim.loop.cwd(), + git_worktree:get_root() .. Path.path.sep .. path2 + ) + -- Make sure file is switched + assert.True(vim.loop.cwd() .. "/A.txt" == get_current_file()) + + git_worktree.switch_worktree(path1) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + + -- Check to make sure directory was switched + assert.are.same( + vim.loop.cwd(), + git_worktree:get_root() .. Path.path.sep .. path1 + ) + -- Make sure file is switched + assert.True(vim.loop.cwd() .. "/A.txt" == get_current_file()) + end) + ) + + it( + "from a bare repo with two worktrees, able to switch to worktree with similar names (absolute path)", in_bare_repo_from_origin_2_similar_named_worktrees(function() - - local path1 = git_worktree:get_root() .. Path.path.sep .. "featB" - local path2 = git_worktree:get_root() .. Path.path.sep .. "featB-test" - - git_worktree.switch_worktree(path1) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - reset_variables() - - -- Check to make sure directory was switched - assert.are.same(vim.loop.cwd(), path1) - - -- open B file - vim.cmd("e A.txt") - -- make sure it is opensd - assert.True(path1.."/A.txt" == get_current_file()) - - git_worktree.switch_worktree(path2) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - reset_variables() - - -- Check to make sure directory was switched - assert.are.same(vim.loop.cwd(), path2) - -- Make sure file is switched - assert.True(path2.."/A.txt" == get_current_file()) - - git_worktree.switch_worktree(path1) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - - -- Check to make sure directory was switched - assert.are.same(vim.loop.cwd(), path1) - -- Make sure file is switched - assert.True(path1.."/A.txt" == get_current_file()) - - end)) - + local path1 = git_worktree:get_root() + .. Path.path.sep + .. "featB" + local path2 = git_worktree:get_root() + .. Path.path.sep + .. "featB-test" + + git_worktree.switch_worktree(path1) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + reset_variables() + + -- Check to make sure directory was switched + assert.are.same(vim.loop.cwd(), path1) + + -- open B file + vim.cmd("e A.txt") + -- make sure it is opensd + assert.True(path1 .. "/A.txt" == get_current_file()) + + git_worktree.switch_worktree(path2) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + reset_variables() + + -- Check to make sure directory was switched + assert.are.same(vim.loop.cwd(), path2) + -- Make sure file is switched + assert.True(path2 .. "/A.txt" == get_current_file()) + + git_worktree.switch_worktree(path1) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + + -- Check to make sure directory was switched + assert.are.same(vim.loop.cwd(), path1) + -- Make sure file is switched + assert.True(path1 .. "/A.txt" == get_current_file()) + end) + ) end) - describe('Delete', function() - - it('from a bare repo with one worktree, able to delete the worktree (relative path)', + describe("Delete", function() + it( + "from a bare repo with one worktree, able to delete the worktree (relative path)", in_bare_repo_from_origin_1_worktree(function() - - local path = "master" - git_worktree.delete_worktree(path) - - vim.fn.wait( - 10000, - function() - return completed_delete - end, - 1000 - ) - - -- Check to make sure it is added to git worktree list - assert.False(check_git_worktree_exists(git_worktree:get_root() .. Path.path.sep .. path)) - - -- Check to make sure directory was not switched - assert.are.same(vim.loop.cwd(), git_worktree:get_root()) - - end)) - - it('from a bare repo with one worktree, able to delete the worktree (absolute path)', + local path = "master" + git_worktree.delete_worktree(path) + + vim.fn.wait(10000, function() + return completed_delete + end, 1000) + + -- Check to make sure it is added to git worktree list + assert.False( + check_git_worktree_exists( + git_worktree:get_root() .. Path.path.sep .. path + ) + ) + + -- Check to make sure directory was not switched + assert.are.same(vim.loop.cwd(), git_worktree:get_root()) + end) + ) + + it( + "from a bare repo with one worktree, able to delete the worktree (absolute path)", in_bare_repo_from_origin_1_worktree(function() + local path = git_worktree:get_root() + .. Path.path.sep + .. "master" + git_worktree.delete_worktree(path) - local path = git_worktree:get_root() .. Path.path.sep .. "master" - git_worktree.delete_worktree(path) - - vim.fn.wait( - 10000, - function() - return completed_delete - end, - 1000 - ) - - -- Check to make sure it is added to git worktree list - assert.False(check_git_worktree_exists(path)) + vim.fn.wait(10000, function() + return completed_delete + end, 1000) - -- Check to make sure directory was not switched - assert.are.same(vim.loop.cwd(), git_worktree:get_root()) + -- Check to make sure it is added to git worktree list + assert.False(check_git_worktree_exists(path)) - end)) + -- Check to make sure directory was not switched + assert.are.same(vim.loop.cwd(), git_worktree:get_root()) + end) + ) - it('from a repo with one worktree, able to delete the worktree (relative path)', + it( + "from a repo with one worktree, able to delete the worktree (relative path)", in_repo_from_origin_1_worktree(function() - - local random_str = git_worktree.get_root():sub(git_worktree.get_root():len()-4) - local path = "../git_worktree_test_repo_featB_"..random_str - local absolute_path = "/tmp/git_worktree_test_repo_featB_"..random_str - git_worktree.delete_worktree(path, true) - - vim.fn.wait( - 10000, - function() - return completed_delete - end, - 1000 - ) - - -- Check to make sure it is added to git worktree list - assert.False(check_git_worktree_exists(absolute_path)) - - -- Check to make sure directory was not switched - assert.are.same(vim.loop.cwd(), git_worktree:get_root()) - - end)) - - it('from a repo with one worktree, able to delete the worktree (absolute path)', + local random_str = git_worktree.get_root():sub( + git_worktree.get_root():len() - 4 + ) + local path = "../git_worktree_test_repo_featB_" .. random_str + local absolute_path = "/tmp/git_worktree_test_repo_featB_" + .. random_str + git_worktree.delete_worktree(path, true) + + vim.fn.wait(10000, function() + return completed_delete + end, 1000) + + -- Check to make sure it is added to git worktree list + assert.False(check_git_worktree_exists(absolute_path)) + + -- Check to make sure directory was not switched + assert.are.same(vim.loop.cwd(), git_worktree:get_root()) + end) + ) + + it( + "from a repo with one worktree, able to delete the worktree (absolute path)", in_repo_from_origin_1_worktree(function() - - local random_str = git_worktree.get_root():sub(git_worktree.get_root():len()-4) - local path = "/tmp/git_worktree_test_repo_featB_"..random_str - git_worktree.delete_worktree(path, true) - - vim.fn.wait( - 10000, - function() - return completed_delete - end, - 1000 - ) - - -- Check to make sure it is added to git worktree list - assert.False(check_git_worktree_exists(path)) - - -- Check to make sure directory was not switched - assert.are.same(vim.loop.cwd(), git_worktree:get_root()) - - end)) - + local random_str = git_worktree.get_root():sub( + git_worktree.get_root():len() - 4 + ) + local path = "/tmp/git_worktree_test_repo_featB_" .. random_str + git_worktree.delete_worktree(path, true) + + vim.fn.wait(10000, function() + return completed_delete + end, 1000) + + -- Check to make sure it is added to git worktree list + assert.False(check_git_worktree_exists(path)) + + -- Check to make sure directory was not switched + assert.are.same(vim.loop.cwd(), git_worktree:get_root()) + end) + ) end) - describe('Find Git Root Dir / Current Worktree on load', function() - - it('does not find the paths in a non git repo', + describe("Find Git Root Dir / Current Worktree on load", function() + it( + "does not find the paths in a non git repo", in_non_git_repo(function() - - git_worktree:setup_git_info() - assert.are.same(nil, git_worktree:get_root()) - assert.are.same(nil, git_worktree:get_current_worktree_path()) - - end)) - - it('finds the paths in a git repo', + git_worktree:setup_git_info() + assert.are.same(nil, git_worktree:get_root()) + assert.are.same(nil, git_worktree:get_current_worktree_path()) + end) + ) + + it( + "finds the paths in a git repo", in_repo_from_origin_1_worktree(function() - - git_worktree:setup_git_info() - assert.are.same(vim.loop.cwd(), git_worktree:get_root()) - assert.are.same(vim.loop.cwd(), git_worktree:get_current_worktree_path()) - - end)) - - it('finds the paths in a bare git repo', + git_worktree:setup_git_info() + assert.are.same(vim.loop.cwd(), git_worktree:get_root()) + assert.are.same( + vim.loop.cwd(), + git_worktree:get_current_worktree_path() + ) + end) + ) + + it( + "finds the paths in a bare git repo", in_bare_repo_from_origin_1_worktree(function() - - git_worktree:setup_git_info() - assert.are.same(vim.loop.cwd(), git_worktree:get_root()) - assert.are.same(vim.loop.cwd(), git_worktree:get_current_worktree_path()) - - end)) - - it('finds the paths from a git repo in a worktree', + git_worktree:setup_git_info() + assert.are.same(vim.loop.cwd(), git_worktree:get_root()) + assert.are.same( + vim.loop.cwd(), + git_worktree:get_current_worktree_path() + ) + end) + ) + + it( + "finds the paths from a git repo in a worktree", in_repo_from_origin_1_worktree(function() - - local expected_git_repo = git_worktree:get_root() - -- switch to a worktree - local random_str = git_worktree.get_root():sub(git_worktree.get_root():len()-4) - local path = "/tmp/git_worktree_test_repo_featB_"..random_str - git_worktree.switch_worktree(path) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - - -- Check to make sure directory was switched - assert.are.same(vim.loop.cwd(), path) - - git_worktree:setup_git_info() - assert.are.same(expected_git_repo, git_worktree:get_root()) - assert.are.same(vim.loop.cwd(), git_worktree:get_current_worktree_path()) - - end)) - - it('finds the paths from a bare git repo in a worktree', + local expected_git_repo = git_worktree:get_root() + -- switch to a worktree + local random_str = git_worktree.get_root():sub( + git_worktree.get_root():len() - 4 + ) + local path = "/tmp/git_worktree_test_repo_featB_" .. random_str + git_worktree.switch_worktree(path) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + + -- Check to make sure directory was switched + assert.are.same(vim.loop.cwd(), path) + + git_worktree:setup_git_info() + assert.are.same(expected_git_repo, git_worktree:get_root()) + assert.are.same( + vim.loop.cwd(), + git_worktree:get_current_worktree_path() + ) + end) + ) + + it( + "finds the paths from a bare git repo in a worktree", in_bare_repo_from_origin_1_worktree(function() - - local expected_git_repo = git_worktree:get_root() - -- switch to a worktree - local path = "master" - git_worktree.switch_worktree(path) - - vim.fn.wait( - 10000, - function() - return completed_switch - end, - 1000 - ) - - -- Check to make sure directory was switched - assert.are.same(vim.loop.cwd(), git_worktree:get_root() .. Path.path.sep .. path) - - git_worktree:setup_git_info() - assert.are.same(expected_git_repo, git_worktree:get_root()) - assert.are.same(vim.loop.cwd(), git_worktree:get_current_worktree_path()) - - end)) + local expected_git_repo = git_worktree:get_root() + -- switch to a worktree + local path = "master" + git_worktree.switch_worktree(path) + + vim.fn.wait(10000, function() + return completed_switch + end, 1000) + + -- Check to make sure directory was switched + assert.are.same( + vim.loop.cwd(), + git_worktree:get_root() .. Path.path.sep .. path + ) + + git_worktree:setup_git_info() + assert.are.same(expected_git_repo, git_worktree:get_root()) + assert.are.same( + vim.loop.cwd(), + git_worktree:get_current_worktree_path() + ) + end) + ) end) - end)