ignoring .git and support for .ignore

This commit is contained in:
cdricms
2025-03-13 00:33:16 +01:00
parent c9f4d70c56
commit 5a36459c07

View File

@@ -1,5 +1,5 @@
local message = require("nvim-sync-ftp.message")
local directory = vim.fn.getcwd();
local directory = vim.fn.getcwd()
local filePath = directory .. "/.sync-ftp-config"
local config = {}
@@ -75,7 +75,7 @@ end
function M.MapToRemote()
if file_exists() then
message.warn("File already exists!!!");
message.warn("File already exists!!!")
else
create_file()
end
@@ -96,8 +96,31 @@ local function read_file(path)
return lines
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()
local configTemp = {};
local configTemp = {}
if file_exists() then
local content, err = read_file(filePath)
@@ -108,7 +131,7 @@ function M.getConfig()
configTemp[first_word] = second_word
end
else
message(err);
message(err)
end
end
@@ -180,9 +203,25 @@ function M.UploadDir()
localDir = "."
remote.parent = config.remote_path
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(
"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",
config.user, config.password, config.host, remote.parent, remote.parent, localDir
"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, exclude_opts, localDir
)
vim.uv.spawn("sh", {