forked from ThePrimeagen/git-worktree.nvim
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d54e015
commit cf8cb15
Showing
8 changed files
with
198 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,5 @@ | |
.direnv | ||
.pre-commit-config.yaml | ||
/luarocks | ||
/lua | ||
/lua_modules | ||
/.luarocks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,5 @@ build = { | |
type = 'builtin', | ||
copy_directories = { | ||
'doc', | ||
'tests' | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
local git_harness = require('util.git_harness') | ||
local gwt_git = require('git-worktree.git') | ||
local Status = require("git-worktree.status") | ||
|
||
local status = Status:new() | ||
|
||
|
||
describe('git-worktree git operations', function() | ||
describe('finds git toplevel in normal repo', function() | ||
before_each(function() | ||
repo_dir = git_harness.prepare_repo() | ||
end) | ||
it('Public API is available after setup.', function() | ||
local ret_git_dir = gwt_git.find_git_dir() | ||
assert.are.same(ret_git_dir, repo_dir) | ||
end) | ||
end) | ||
|
||
describe('finds git toplevel in bare repo', function() | ||
before_each(function() | ||
repo_dir = git_harness.prepare_repo_bare() | ||
end) | ||
it('no toplevel in a bare repo', function() | ||
local ret_git_dir = gwt_git.find_git_dir() | ||
assert.are.same(ret_git_dir, nil) | ||
end) | ||
end) | ||
|
||
describe('finds git toplevel in worktree repo', function() | ||
before_each(function() | ||
repo_dir = git_harness.prepare_repo_worktree() | ||
end) | ||
it('Public API is available after setup.', function() | ||
local ret_git_dir = gwt_git.find_git_dir() | ||
status:log().info("ret_git_dir: " .. ret_git_dir .. ".") | ||
status:log().info("repo_dir : " .. repo_dir .. ".") | ||
assert.are.same(ret_git_dir, repo_dir) | ||
end) | ||
end) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
local system = require('util.system') | ||
|
||
local M = {} | ||
|
||
local origin_repo_path = nil | ||
|
||
function M.setup_origin_repo() | ||
if origin_repo_path ~= nil then | ||
return origin_repo_path | ||
end | ||
|
||
local workspace_dir = system.create_temp_dir("workspace-dir") | ||
vim.api.nvim_set_current_dir(vim.fn.getcwd()) | ||
system.run("cp -r spec/.repo " .. workspace_dir) | ||
vim.api.nvim_set_current_dir(workspace_dir) | ||
system.run([[ | ||
mv .repo/.git-orig ./.git | ||
mv .repo/* . | ||
git config user.email "[email protected]" | ||
git config user.name "Test User" | ||
]]) | ||
|
||
origin_repo_path = system.create_temp_dir("origin-repo") | ||
system.run(string.format("git clone --bare %s %s", workspace_dir, origin_repo_path)) | ||
|
||
return origin_repo_path | ||
end | ||
|
||
function M.prepare_repo() | ||
M.setup_origin_repo() | ||
|
||
local working_dir = system.create_temp_dir("working-dir") | ||
vim.api.nvim_set_current_dir(working_dir) | ||
system.run(string.format("git clone %s %s", origin_repo_path, working_dir)) | ||
system.run([[ | ||
git config remote.origin.url [email protected]:test/test.git | ||
git config user.email "[email protected]" | ||
git config user.name "Test User" | ||
]]) | ||
return working_dir | ||
end | ||
|
||
function M.prepare_repo_bare() | ||
M.setup_origin_repo() | ||
|
||
local working_dir = system.create_temp_dir("working-bare-dir") | ||
vim.api.nvim_set_current_dir(working_dir) | ||
system.run(string.format("git clone --bare %s %s", origin_repo_path, working_dir)) | ||
return working_dir | ||
end | ||
|
||
function M.prepare_repo_worktree() | ||
M.setup_origin_repo() | ||
|
||
local working_dir = system.create_temp_dir("working-worktree-dir") | ||
vim.api.nvim_set_current_dir(working_dir) | ||
system.run(string.format("git clone --bare %s %s", origin_repo_path, working_dir)) | ||
system.run("git worktree add wt master") | ||
local worktree_dir = working_dir .. "/wt" | ||
vim.api.nvim_set_current_dir(worktree_dir) | ||
return worktree_dir | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
local M = {} | ||
|
||
---Runs a system command and errors if it fails | ||
---@param cmd string | table Command to be ran | ||
---@param ignore_err boolean? Whether the error should be ignored | ||
---@param error_msg string? The error message to be emitted on command failure | ||
---@return string The output of the system command | ||
function M.run(cmd, ignore_err, error_msg) | ||
if ignore_err == nil then | ||
ignore_err = false | ||
end | ||
|
||
local output = vim.fn.system(cmd) | ||
if vim.v.shell_error ~= 0 and not ignore_err then | ||
error(error_msg or ("Command failed: ↓\n" .. cmd .. "\nOutput from command: ↓\n" .. output)) | ||
end | ||
return output | ||
end | ||
|
||
local function is_macos() | ||
return vim.loop.os_uname().sysname == "Darwin" | ||
end | ||
|
||
---Create a temporary directory for use | ||
---@param suffix string? The suffix to be appended to the temp directory, ideally avoid spaces in your suffix | ||
---@return string The path to the temporary directory | ||
function M.create_temp_dir(suffix) | ||
suffix = "git-worktree-" .. (suffix or "") | ||
|
||
local cmd | ||
if is_macos() then | ||
cmd = string.format("mktemp -d -t %s", suffix) | ||
else | ||
cmd = string.format("mktemp -d --suffix=%s", suffix) | ||
end | ||
|
||
local prefix = is_macos() and "/private" or "" | ||
return prefix .. vim.trim(M.run(cmd)) | ||
end | ||
|
||
return M |