|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { execFileSync } = require("child_process"); |
| 4 | + |
| 5 | +/** |
| 6 | + * @param {string} repoUrl |
| 7 | + * @returns {string | null} |
| 8 | + */ |
| 9 | +function getGitHubLfsUrl(repoUrl) { |
| 10 | + let url; |
| 11 | + try { |
| 12 | + url = new URL(repoUrl); |
| 13 | + } catch { |
| 14 | + const scpMatch = repoUrl.match( |
| 15 | + /^git@github\.com:([^/]+)\/(.+?)(?:\.git)?$/, |
| 16 | + ); |
| 17 | + if (!scpMatch) return null; |
| 18 | + return `https://github.com/${scpMatch[1]}/${scpMatch[2]}.git/info/lfs`; |
| 19 | + } |
| 20 | + |
| 21 | + if (url.protocol !== "https:" && url.protocol !== "ssh:") return null; |
| 22 | + if (url.hostname.toLowerCase() !== "github.com") return null; |
| 23 | + if (url.protocol === "ssh:" && url.username && url.username !== "git") { |
| 24 | + return null; |
| 25 | + } |
| 26 | + |
| 27 | + const parts = url.pathname.replace(/^\/+|\/+$/g, "").split("/"); |
| 28 | + if (parts.length !== 2 || !parts[0] || !parts[1]) return null; |
| 29 | + |
| 30 | + const repoName = parts[1].replace(/\.git$/i, ""); |
| 31 | + return `https://github.com/${parts[0]}/${repoName}.git/info/lfs`; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * @param {string} repoDir |
| 36 | + * @returns {string} |
| 37 | + */ |
| 38 | +function getOriginUrl(repoDir) { |
| 39 | + return execFileSync( |
| 40 | + "git", |
| 41 | + ["-C", repoDir, "config", "--get", "remote.origin.url"], |
| 42 | + { |
| 43 | + encoding: "utf8", |
| 44 | + }, |
| 45 | + ).trim(); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * @param {string} repoDir |
| 50 | + * @param {string} lfsUrl |
| 51 | + */ |
| 52 | +function setGitLfsUrl(repoDir, lfsUrl) { |
| 53 | + execFileSync("git", ["-C", repoDir, "config", "--local", "lfs.url", lfsUrl], { |
| 54 | + stdio: "inherit", |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * @param {string} repoDir |
| 60 | + * @returns {string | null} |
| 61 | + */ |
| 62 | +function configureGitLfsUrl(repoDir) { |
| 63 | + const originUrl = getOriginUrl(repoDir); |
| 64 | + const lfsUrl = getGitHubLfsUrl(originUrl); |
| 65 | + if (!lfsUrl) return null; |
| 66 | + |
| 67 | + setGitLfsUrl(repoDir, lfsUrl); |
| 68 | + return lfsUrl; |
| 69 | +} |
| 70 | + |
| 71 | +function main() { |
| 72 | + const repoDir = process.argv[2] || "."; |
| 73 | + const lfsUrl = configureGitLfsUrl(repoDir); |
| 74 | + if (lfsUrl) { |
| 75 | + console.log(`Configured Git LFS URL for GitHub remote: ${lfsUrl}`); |
| 76 | + } else { |
| 77 | + console.log( |
| 78 | + "Repository remote is not a github.com URL; leaving Git LFS URL unchanged.", |
| 79 | + ); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +module.exports = { |
| 84 | + configureGitLfsUrl, |
| 85 | + getGitHubLfsUrl, |
| 86 | +}; |
| 87 | + |
| 88 | +if (require.main === module) { |
| 89 | + main(); |
| 90 | +} |
0 commit comments