Skip to content

Commit 88e89d2

Browse files
authored
fix: git bundle info for build (#64)
1 parent 2307735 commit 88e89d2

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: Download latest llama.cpp release
2121
env:
2222
CI: true
23-
run: node ./dist/cli/cli.js download --release latest --skipBuild --updateBinariesReleaseMetadataAndSaveGitBundle
23+
run: node ./dist/cli/cli.js download --release latest --skipBuild --noBundle --updateBinariesReleaseMetadataAndSaveGitBundle
2424
- name: Upload build artifact
2525
uses: actions/upload-artifact@v3
2626
with:

src/cli/commands/DownloadCommand.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {setBinariesGithubRelease} from "../../utils/binariesGithubRelease.js";
1313
import {downloadCmakeIfNeeded} from "../../utils/cmake.js";
1414
import withStatusLogs from "../../utils/withStatusLogs.js";
1515
import {getIsInDocumentationMode} from "../../state.js";
16-
import {saveCurrentRepoAsReleaseBundle} from "../../utils/gitReleaseBundles.js";
16+
import {unshallowAndSquashCurrentRepoAndSaveItAsReleaseBundle} from "../../utils/gitReleaseBundles.js";
1717
import {cloneLlamaCppRepo} from "../../utils/cloneLlamaCppRepo.js";
1818

1919
type DownloadCommandArgs = {
@@ -24,6 +24,7 @@ type DownloadCommandArgs = {
2424
metal: boolean,
2525
cuda: boolean,
2626
skipBuild?: boolean,
27+
noBundle?: boolean,
2728
updateBinariesReleaseMetadataAndSaveGitBundle?: boolean
2829
};
2930

@@ -71,6 +72,12 @@ export const DownloadCommand: CommandModule<object, DownloadCommandArgs> = {
7172
default: false,
7273
description: "Skip building llama.cpp after downloading it"
7374
})
75+
.option("noBundle", {
76+
alias: "nb",
77+
type: "boolean",
78+
default: false,
79+
description: "Download a llama.cpp only from GitHub, even if a local git bundle exists for the release"
80+
})
7481
.option("updateBinariesReleaseMetadataAndSaveGitBundle", {
7582
type: "boolean",
7683
hidden: true, // this for the CI to use
@@ -82,7 +89,7 @@ export const DownloadCommand: CommandModule<object, DownloadCommandArgs> = {
8289
};
8390

8491
export async function DownloadLlamaCppCommand({
85-
repo, release, arch, nodeTarget, metal, cuda, skipBuild, updateBinariesReleaseMetadataAndSaveGitBundle
92+
repo, release, arch, nodeTarget, metal, cuda, skipBuild, noBundle, updateBinariesReleaseMetadataAndSaveGitBundle
8693
}: DownloadCommandArgs) {
8794
const octokit = new Octokit();
8895
const [githubOwner, githubRepo] = repo.split("/");
@@ -146,7 +153,7 @@ export async function DownloadLlamaCppCommand({
146153
});
147154

148155
console.log(chalk.blue("Cloning llama.cpp"));
149-
await cloneLlamaCppRepo(githubOwner, githubRepo, githubRelease!.data.tag_name);
156+
await cloneLlamaCppRepo(githubOwner, githubRepo, githubRelease!.data.tag_name, noBundle != true);
150157

151158
if (!skipBuild) {
152159
await downloadCmakeIfNeeded(true);
@@ -168,7 +175,7 @@ export async function DownloadLlamaCppCommand({
168175

169176
if (isCI && updateBinariesReleaseMetadataAndSaveGitBundle) {
170177
await setBinariesGithubRelease(githubRelease!.data.tag_name);
171-
await saveCurrentRepoAsReleaseBundle();
178+
await unshallowAndSquashCurrentRepoAndSaveItAsReleaseBundle();
172179
}
173180

174181
console.log();

src/utils/cloneLlamaCppRepo.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {llamaCppDirectory} from "../config.js";
66
import {getGitBundlePathForRelease} from "./gitReleaseBundles.js";
77

88

9-
export async function cloneLlamaCppRepo(githubOwner: string, githubRepo: string, tag: string) {
10-
const gitBundleForTag = await getGitBundlePathForRelease(githubOwner, githubRepo, tag);
9+
export async function cloneLlamaCppRepo(githubOwner: string, githubRepo: string, tag: string, useBundles: boolean = true) {
10+
const gitBundleForTag = !useBundles ? null : await getGitBundlePathForRelease(githubOwner, githubRepo, tag);
1111
const remoteGitUrl = `https://github.com/${githubOwner}/${githubRepo}.git`;
1212

1313
async function withGitCloneProgress<T>(cloneName: string, callback: (gitWithCloneProgress: SimpleGit) => Promise<T>): Promise<T> {
@@ -49,10 +49,7 @@ export async function cloneLlamaCppRepo(githubOwner: string, githubRepo: string,
4949
"--quiet": null
5050
});
5151

52-
await simpleGit(llamaCppDirectory)
53-
.removeRemote("origin");
54-
await simpleGit(llamaCppDirectory)
55-
.addRemote("origin", remoteGitUrl);
52+
await simpleGit(llamaCppDirectory).removeRemote("origin");
5653
});
5754
return;
5855
} catch (err) {

src/utils/gitReleaseBundles.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,50 @@ import {currentReleaseGitBundlePath, defaultLlamaCppGitHubRepo, llamaCppDirector
44
import {getBinariesGithubRelease} from "./binariesGithubRelease.js";
55

66

7-
export async function saveCurrentRepoAsReleaseBundle() {
7+
export async function unshallowAndSquashCurrentRepoAndSaveItAsReleaseBundle() {
88
if (!(await fs.pathExists(llamaCppDirectory)))
99
throw new Error("llama.cpp directory does not exist");
1010

1111
if (await fs.pathExists(currentReleaseGitBundlePath))
1212
await fs.remove(currentReleaseGitBundlePath);
1313

14+
await simpleGit(llamaCppDirectory).addConfig("user.name", "node-llama-cpp-ci");
15+
await simpleGit(llamaCppDirectory).addConfig("user.email", "[email protected]");
16+
17+
const currentBranch = await getCurrentTagOrBranch();
18+
19+
await simpleGit(llamaCppDirectory).fetch(["--unshallow"]);
20+
21+
const lastCommit = await simpleGit(llamaCppDirectory).log(["-1"]);
22+
const lastCommitMessage: string | null = lastCommit?.all?.[0]?.message;
23+
const newCommitMessage = "## SQUASHED ##\n\n" + (lastCommitMessage ?? "");
24+
25+
const newCommitSha = await simpleGit(llamaCppDirectory).raw(["commit-tree", "HEAD^{tree}", "-m", newCommitMessage]);
26+
await simpleGit(llamaCppDirectory).reset(["--hard", newCommitSha.trim()]);
27+
28+
const tags = await simpleGit(llamaCppDirectory).tags();
29+
for (const tag of tags.all) {
30+
await simpleGit(llamaCppDirectory).tag(["--delete", tag]);
31+
}
32+
33+
const branches = await simpleGit(llamaCppDirectory).branch();
34+
for (const branch of branches.all) {
35+
try {
36+
await simpleGit(llamaCppDirectory).branch(["--delete", branch]);
37+
} catch (err) {
38+
// If the branch is not found, it's fine
39+
// this happens as when there are no branches git returnes an output saying so, and `simpleGit` parses it as a branch,
40+
// so the list may contain branches that do not exist.
41+
// Right now, the non-existent branch name returned called `(no`, but I wouldn't want to rely on this specific text,
42+
// as this is a bug in `simpleGit`.
43+
}
44+
}
45+
46+
if (currentBranch != null)
47+
await simpleGit(llamaCppDirectory).tag([currentBranch]);
48+
49+
await simpleGit(llamaCppDirectory).raw(["gc", "--aggressive", "--prune=all"]);
50+
1451
await simpleGit(llamaCppDirectory).raw(["bundle", "create", currentReleaseGitBundlePath, "HEAD"]);
1552
}
1653

@@ -32,3 +69,18 @@ export async function getGitBundlePathForRelease(githubOwner: string, githubRepo
3269

3370
return currentReleaseGitBundlePath;
3471
}
72+
73+
async function getCurrentTagOrBranch() {
74+
const branch = await simpleGit(llamaCppDirectory).revparse(["--abbrev-ref", "HEAD"]);
75+
76+
if (branch !== "HEAD")
77+
return branch;
78+
79+
const tags = await simpleGit(llamaCppDirectory).tag(["--points-at", "HEAD"]);
80+
const tagArray = tags.split("\n").filter(Boolean);
81+
82+
if (tagArray.length > 0)
83+
return tagArray[0];
84+
85+
return null;
86+
}

0 commit comments

Comments
 (0)