Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 61 additions & 17 deletions bin/utils/clone-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export const cloneAction = async (
`${config.repository}-${Date.now()}${Math.random().toString(16).slice(2, 6)}`,
)

const toPosix = (p: string) => p.split(path.sep).join("/")

const spinner = yoctospinner()
const start = performance.now()

Expand All @@ -40,21 +42,65 @@ export const cloneAction = async (
)
}

const wantsSubpath = !!config.path && config.path !== "." && config.type !== "repository"
const isFilePath = wantsSubpath && path.extname(config.path) !== ""
const sparsePath = wantsSubpath
? toPosix(isFilePath ? path.dirname(config.path) : config.path)
: ""

let usedSparse = false

try {
await spawn("git", [
"clone",
repoUrl,
tempDir,
"--branch",
config.branch,
"--depth",
"1",
"--single-branch",
...(options.recursive ? ["--recursive"] : []),
])
if (wantsSubpath) {
await fs.promises.mkdir(tempDir, { recursive: true })
await spawn("git", [
"clone",
repoUrl,
tempDir,
"--branch",
config.branch,
"--depth=1",
"--single-branch",
"--no-tags",
"--filter=blob:none",
"--sparse",
])

await spawn(
"git",
["sparse-checkout", "init", ...(isFilePath ? ["--no-cone"] : ["--cone"])],
{ cwd: tempDir },
)
await spawn("git", ["sparse-checkout", "set", sparsePath], { cwd: tempDir })
await spawn("git", ["checkout"], { cwd: tempDir })

if (options.recursive) {
await spawn("git", ["submodule", "update", "--init", "--recursive"], {
cwd: tempDir,
})
}

usedSparse = true
} else {
await spawn("git", [
"clone",
repoUrl,
tempDir,
"--branch",
config.branch,
"--depth=1",
"--single-branch",
"--no-tags",
...(options.recursive ? ["--recursive"] : []),
])
}
} catch {
await spawn("git", ["clone", repoUrl, tempDir, ...(options.recursive ? ["--recursive"] : [])])
await spawn("git", ["checkout", config.branch], { cwd: tempDir })
try {
await spawn("git", ["clone", repoUrl, tempDir, ...(options.recursive ? ["--recursive"] : [])])
await spawn("git", ["checkout", config.branch], { cwd: tempDir })
} catch {
throw new Error("Failed to clone repository")
}
}

const sourcePath = path.resolve(tempDir, config.path)
Expand All @@ -65,9 +111,7 @@ export const cloneAction = async (
await fs.promises.mkdir(targetPath, { recursive: true })
await copyDir(sourcePath, targetPath)
} else {
await fs.promises.mkdir(targetPath.split("/").slice(0, -1).join("/"), {
recursive: true,
})
await fs.promises.mkdir(path.dirname(targetPath), { recursive: true })
await fs.promises.copyFile(sourcePath, targetPath)
}

Expand All @@ -76,7 +120,7 @@ export const cloneAction = async (
`Picked ${config.type}${config.type === "repository" ? " without .git" : " from repository"} in ${(
(performance.now() - start) /
1000
).toFixed(2)} seconds.`,
).toFixed(2)} seconds${usedSparse ? green(" (sparse)") : ""}.`,
)
} else console.log("- Synced at " + new Date().toLocaleTimeString())

Expand Down
3 changes: 2 additions & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ fi
rm -rf test/*

TEST_CASES=(
'nrjdalal/gitpick/blob/main/bin/index.ts'
'nrjdalal/gitpick'
'nrjdalal/gitpick/blob/main/bin/index.ts'
'https://github.com/nrjdalal/gitpick'
'git@github.com:nrjdalal/gitpick.git'
'https://github.com/nrjdalal/gitpick.git'
Expand All @@ -30,6 +30,7 @@ TEST_CASES=(
'https://github.com/nrjdalal/gitpick/tree/master/bin -b main'
'git@github.com:nrjdalal/gitpick.git/tree/master/bin -b main'
'https://github.com/nrjdalal/gitpick.git/tree/master/bin -b main'
'https://github.com/nrjdalal/gitpick/tree/1d67ed603f5bcbab1b84d71db968b7c36e36eca0'
)

# Initialize report variables
Expand Down