From 5a36459c074990d4948637db239d2e2238523f59 Mon Sep 17 00:00:00 2001 From: cdricms <36056008+cdricms@users.noreply.github.com> Date: Thu, 13 Mar 2025 00:33:16 +0100 Subject: [PATCH] ignoring .git and support for .ignore --- lua/nvim-sync-ftp/cli.lua | 57 ++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/lua/nvim-sync-ftp/cli.lua b/lua/nvim-sync-ftp/cli.lua index e6e3f43..5fd2ce5 100644 --- a/lua/nvim-sync-ftp/cli.lua +++ b/lua/nvim-sync-ftp/cli.lua @@ -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 = {} @@ -22,15 +22,15 @@ local function path_components(path) if dir == "." then parent = ".." -- Parent of current directory elseif dir == "/" then - parent = "/" -- Root's parent is itself + parent = "/" -- Root's parent is itself else parent = dir:match("^(.*)/[^/]*$") or "/" if parent == "" then parent = "/" end -- Handle edge case for "/dir" end return { - dir = dir, -- Directory containing the file (dirname) - base = base, -- Filename without directory (basename) + dir = dir, -- Directory containing the file (dirname) + base = base, -- Filename without directory (basename) parent = parent -- Parent directory one level up from dir } end @@ -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", {