Skip to content

Commit 66b2e60

Browse files
committed
chore: tweaks > clone-action.ts test.sh
bin/utils/clone-action.ts scripts/test.sh
1 parent 1d67ed6 commit 66b2e60

2 files changed

Lines changed: 65 additions & 19 deletions

File tree

bin/utils/clone-action.ts

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ export const cloneAction = async (
2525
await spawn("git", ["config", "--global", "core.longpaths", "true"])
2626
}
2727

28-
const repoUrl = `https://${config.token ? config.token + "@" : config.token}github.com/${config.owner}/${config.repository}.git`
28+
const repoUrl = `https://${config.token ? config.token + "@" : ""}github.com/${config.owner}/${config.repository}.git`
2929
const tempDir = path.resolve(
3030
os.tmpdir(),
3131
`${config.repository}-${Date.now()}${Math.random().toString(16).slice(2, 6)}`,
3232
)
3333

34+
const toPosix = (p: string) => p.split(path.sep).join("/")
35+
3436
const spinner = yoctospinner()
3537
const start = performance.now()
3638

@@ -40,21 +42,66 @@ export const cloneAction = async (
4042
)
4143
}
4244

45+
const wantsSubpath = !!config.path && config.path !== "." && config.type !== "repository"
46+
const isFilePath = wantsSubpath && path.extname(config.path) !== ""
47+
const sparsePath = wantsSubpath
48+
? toPosix(isFilePath ? path.dirname(config.path) : config.path)
49+
: ""
50+
51+
let usedSparse = false
52+
4353
try {
44-
await spawn("git", [
45-
"clone",
46-
repoUrl,
47-
tempDir,
48-
"--branch",
49-
config.branch,
50-
"--depth",
51-
"1",
52-
"--single-branch",
53-
...(options.recursive ? ["--recursive"] : []),
54-
])
54+
if (wantsSubpath) {
55+
await fs.promises.mkdir(tempDir, { recursive: true })
56+
await spawn("git", [
57+
"clone",
58+
"--depth=1",
59+
"--filter=blob:none",
60+
"--sparse",
61+
"--single-branch",
62+
"--no-tags",
63+
"--branch",
64+
config.branch,
65+
repoUrl,
66+
tempDir,
67+
])
68+
69+
await spawn(
70+
"git",
71+
["sparse-checkout", "init", ...(isFilePath ? ["--no-cone"] : ["--cone"])],
72+
{ cwd: tempDir },
73+
)
74+
await spawn("git", ["sparse-checkout", "set", sparsePath], { cwd: tempDir })
75+
await spawn("git", ["checkout"], { cwd: tempDir })
76+
77+
if (options.recursive) {
78+
await spawn("git", ["submodule", "update", "--init", "--recursive"], {
79+
cwd: tempDir,
80+
})
81+
}
82+
83+
usedSparse = true
84+
} else {
85+
await spawn("git", [
86+
"clone",
87+
repoUrl,
88+
tempDir,
89+
"--branch",
90+
config.branch,
91+
"--depth",
92+
"1",
93+
"--single-branch",
94+
"--no-tags",
95+
...(options.recursive ? ["--recursive"] : []),
96+
])
97+
}
5598
} catch {
56-
await spawn("git", ["clone", repoUrl, tempDir, ...(options.recursive ? ["--recursive"] : [])])
57-
await spawn("git", ["checkout", config.branch], { cwd: tempDir })
99+
try {
100+
await spawn("git", ["clone", repoUrl, tempDir, ...(options.recursive ? ["--recursive"] : [])])
101+
await spawn("git", ["checkout", config.branch], { cwd: tempDir })
102+
} catch {
103+
throw new Error("Failed to clone repository")
104+
}
58105
}
59106

60107
const sourcePath = path.resolve(tempDir, config.path)
@@ -65,9 +112,7 @@ export const cloneAction = async (
65112
await fs.promises.mkdir(targetPath, { recursive: true })
66113
await copyDir(sourcePath, targetPath)
67114
} else {
68-
await fs.promises.mkdir(targetPath.split("/").slice(0, -1).join("/"), {
69-
recursive: true,
70-
})
115+
await fs.promises.mkdir(path.dirname(targetPath), { recursive: true })
71116
await fs.promises.copyFile(sourcePath, targetPath)
72117
}
73118

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

scripts/test.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ fi
1717
rm -rf test/*
1818

1919
TEST_CASES=(
20-
'nrjdalal/gitpick/blob/main/bin/index.ts'
2120
'nrjdalal/gitpick'
21+
'nrjdalal/gitpick/blob/main/bin/index.ts'
2222
'https://github.com/nrjdalal/gitpick'
2323
'git@github.com:nrjdalal/gitpick.git'
2424
'https://github.com/nrjdalal/gitpick.git'
@@ -30,6 +30,7 @@ TEST_CASES=(
3030
'https://github.com/nrjdalal/gitpick/tree/master/bin -b main'
3131
'git@github.com:nrjdalal/gitpick.git/tree/master/bin -b main'
3232
'https://github.com/nrjdalal/gitpick.git/tree/master/bin -b main'
33+
'https://github.com/nrjdalal/gitpick/tree/1d67ed603f5bcbab1b84d71db968b7c36e36eca0'
3334
)
3435

3536
# Initialize report variables

0 commit comments

Comments
 (0)