-- What is never sent along by itself: files that look like they hold secrets, files outside the -- project, and files the project's .gitignore leaves out. Used for what goes with a request -- without the person having pointed at it: pieces of other open files with a suggestion, and the -- names of the other open files with a message. The same rules as the VS Code extension's -- (vscode/src/private.js); tests/rules/private_files.json holds the cases both must answer alike. local M = {} local ENV_EXAMPLES = { [".env.example"] = true, [".env.sample"] = true, [".env.template"] = true } local NAMES = { [".netrc"] = true, ["_netrc"] = true, [".npmrc"] = true, [".pypirc"] = true, [".pgpass"] = true, [".htpasswd"] = true, [".git-credentials"] = true, } local ENDINGS = { pem = true, key = true, p12 = true, pfx = true, jks = true, keystore = true, ppk = true, kdbx = true, env = true } local KEY_NAMES = { "id_rsa", "id_ed25519", "id_ecdsa", "id_dsa" } local DIRECTORIES = { [".ssh"] = true, [".gnupg"] = true, [".aws"] = true, [".kube"] = true, [".docker"] = true, [".azure"] = true } local function parts_of(path) local out = {} for part in (tostring(path or ""):gsub("\\", "/")):gmatch("[^/]+") do out[#out + 1] = part:lower() end return out end --- Whether a file's name or place says it may hold secrets: `.env` and `.env.*` (not the --- `.example`, `.sample` and `.template` ones), `*.env`, key and certificate stores (`*.pem`, --- `*.key`, `*.p12`, `*.pfx`, `*.jks`, `*.keystore`, `*.ppk`, `*.kdbx`), SSH key names (`id_rsa*`, --- `id_ed25519*`, `id_ecdsa*`, `id_dsa*`), `.netrc`, `.npmrc`, `.pypirc`, `.pgpass`, `.htpasswd`, --- `.git-credentials`, `credentials*`, `secrets.*`, `secret.*`, and anything under `.ssh/`, --- `.gnupg/`, `.aws/`, `.kube/`, `.docker/` or `.azure/`. function M.looks_secret(path) local parts = parts_of(path) local base = parts[#parts] if not base then return false end for i = 1, #parts - 1 do if DIRECTORIES[parts[i]] then return true end end if base == ".env" or (base:sub(1, 5) == ".env." and not ENV_EXAMPLES[base]) then return true end if NAMES[base] then return true end local ending = base:match("%.([^.]+)$") if ending and ENDINGS[ending] then return true end for _, k in ipairs(KEY_NAMES) do if base:sub(1, #k) == k then return true end end if base:sub(1, 11) == "credentials" then return true end if base:sub(1, 8) == "secrets." or base:sub(1, 7) == "secret." then return true end return false end --- A path with `.` and `..` worked out and one kind of slash, or nil when it is not absolute. local function absolute(path) local p = tostring(path or ""):gsub("\\", "/") local drive = p:match("^(%a:)/") or "" if drive ~= "" then p = p:sub(3) end if p:sub(1, 1) ~= "/" then return nil end local out = {} for part in p:gmatch("[^/]+") do if part == ".." then table.remove(out) elseif part ~= "." then out[#out + 1] = part end end return drive:lower() .. "/" .. table.concat(out, "/") end --- Whether `path` is a file inside one of the folders `roots` (never a folder itself, and never --- "inside" the root of the disk). function M.inside(path, roots) local p = absolute(path) if not p then return false end for _, root in ipairs(roots or {}) do local r = absolute(root) if r and r ~= "/" and not r:match("^%a:/$") and p:sub(1, #r + 1) == r .. "/" then return true end end return false end local ignored = {} -- path -> true | false | "asking" --- Whether the project's .gitignore leaves `path` out: true or false once git has answered, nil --- while it has not (the question is then on its way; `cb(answer)` is called with it). Without --- git, or outside a repository, nothing is ignored. Asked once a file. function M.ignored(path, cb) local known = ignored[path] if known == true or known == false then if cb then cb(known) end return known end if known == "asking" then return nil end if vim.fn.executable("git") == 0 then ignored[path] = false if cb then cb(false) end return false end ignored[path] = "asking" local ok = pcall(vim.system, { "git", "-C", vim.fs.dirname(path), "check-ignore", "-q", "--", path }, { text = true, timeout = 5000, env = { GIT_OPTIONAL_LOCKS = "0" } }, function(res) -- 0: ignored. 1: not. Anything else: no repository there, or git failed. ignored[path] = res.code == 0 if cb then vim.schedule(function() cb(ignored[path]) end) end end) if not ok then ignored[path] = false if cb then cb(false) end return false end return nil end --- A .gitignore changed, or the directory did: what git said before is asked again. function M.forget() ignored = {} end --- The folders that are the project here: the working directory. function M.roots() return { vim.fn.getcwd() } end --- Whether a file other than the one being worked on may go along by itself: inside the --- project, not secret by its name, and known not to be ignored by the project's .gitignore. function M.may_send(path, roots) if not M.inside(path, roots or M.roots()) or M.looks_secret(path) then return false end return M.ignored(path) == false end return M