ignoring .git and support for .ignore
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
local message = require("nvim-sync-ftp.message")
|
local message = require("nvim-sync-ftp.message")
|
||||||
local directory = vim.fn.getcwd();
|
local directory = vim.fn.getcwd()
|
||||||
local filePath = directory .. "/.sync-ftp-config"
|
local filePath = directory .. "/.sync-ftp-config"
|
||||||
local config = {}
|
local config = {}
|
||||||
|
|
||||||
@@ -22,15 +22,15 @@ local function path_components(path)
|
|||||||
if dir == "." then
|
if dir == "." then
|
||||||
parent = ".." -- Parent of current directory
|
parent = ".." -- Parent of current directory
|
||||||
elseif dir == "/" then
|
elseif dir == "/" then
|
||||||
parent = "/" -- Root's parent is itself
|
parent = "/" -- Root's parent is itself
|
||||||
else
|
else
|
||||||
parent = dir:match("^(.*)/[^/]*$") or "/"
|
parent = dir:match("^(.*)/[^/]*$") or "/"
|
||||||
if parent == "" then parent = "/" end -- Handle edge case for "/dir"
|
if parent == "" then parent = "/" end -- Handle edge case for "/dir"
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
dir = dir, -- Directory containing the file (dirname)
|
dir = dir, -- Directory containing the file (dirname)
|
||||||
base = base, -- Filename without directory (basename)
|
base = base, -- Filename without directory (basename)
|
||||||
parent = parent -- Parent directory one level up from dir
|
parent = parent -- Parent directory one level up from dir
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
@@ -75,7 +75,7 @@ end
|
|||||||
|
|
||||||
function M.MapToRemote()
|
function M.MapToRemote()
|
||||||
if file_exists() then
|
if file_exists() then
|
||||||
message.warn("File already exists!!!");
|
message.warn("File already exists!!!")
|
||||||
else
|
else
|
||||||
create_file()
|
create_file()
|
||||||
end
|
end
|
||||||
@@ -96,8 +96,31 @@ local function read_file(path)
|
|||||||
return lines
|
return lines
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Function to read .gitignore and return patterns to exclude
|
||||||
|
local function read_gitignore(gitignore_path)
|
||||||
|
local ignores = { ".git/" } -- Always ignore .git directory
|
||||||
|
local file = io.open(gitignore_path, "r")
|
||||||
|
if not file then
|
||||||
|
return ignores
|
||||||
|
end
|
||||||
|
|
||||||
|
for line in file:lines() do
|
||||||
|
line = line:match("^%s*(.-)%s*$")
|
||||||
|
if line ~= "" and not line:match("^#") and not line:match("^!") then
|
||||||
|
if line:match("/$") or not line:match("/") then
|
||||||
|
table.insert(ignores, line)
|
||||||
|
else
|
||||||
|
table.insert(ignores, line)
|
||||||
|
table.insert(ignores, line .. "/")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
file:close()
|
||||||
|
return ignores
|
||||||
|
end
|
||||||
|
|
||||||
function M.getConfig()
|
function M.getConfig()
|
||||||
local configTemp = {};
|
local configTemp = {}
|
||||||
|
|
||||||
if file_exists() then
|
if file_exists() then
|
||||||
local content, err = read_file(filePath)
|
local content, err = read_file(filePath)
|
||||||
@@ -108,7 +131,7 @@ function M.getConfig()
|
|||||||
configTemp[first_word] = second_word
|
configTemp[first_word] = second_word
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
message(err);
|
message(err)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -180,9 +203,25 @@ function M.UploadDir()
|
|||||||
localDir = "."
|
localDir = "."
|
||||||
remote.parent = config.remote_path
|
remote.parent = config.remote_path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Read .gitignore patterns
|
||||||
|
local gitignore_path = localDir == "." and directoryTemp .. ".gitignore" or localDir .. "/.gitignore"
|
||||||
|
local ignores = read_gitignore(gitignore_path)
|
||||||
|
|
||||||
|
-- Build exclusion options for lftp
|
||||||
|
local exclude_opts = ""
|
||||||
|
for _, pattern in ipairs(ignores) do
|
||||||
|
if pattern:match("[*?[]") then
|
||||||
|
exclude_opts = exclude_opts .. string.format(" --exclude-glob %q", pattern)
|
||||||
|
else
|
||||||
|
exclude_opts = exclude_opts .. string.format(" --exclude %q", pattern)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Construct the lftp command with exclusions
|
||||||
local command = string.format(
|
local command = string.format(
|
||||||
"lftp -u '%s,%s' ftp://%s -e 'set ssl:verify-certificate no;mkdir -p %q;cd %q;mirror -R %q; quit' >/dev/null 2>&1",
|
"lftp -u '%s,%s' ftp://%s -e 'set ssl:verify-certificate no;mkdir -p %q;cd %q;mirror -R%s %q; quit' >/dev/null 2>&1",
|
||||||
config.user, config.password, config.host, remote.parent, remote.parent, localDir
|
config.user, config.password, config.host, remote.parent, remote.parent, exclude_opts, localDir
|
||||||
)
|
)
|
||||||
|
|
||||||
vim.uv.spawn("sh", {
|
vim.uv.spawn("sh", {
|
||||||
|
|||||||
Reference in New Issue
Block a user