Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Download latest llama.cpp release
env:
CI: true
run: node ./dist/cli/cli.js download --release latest --skipBuild --updateBinariesReleaseMetadataAndSaveGitBundle
run: node ./dist/cli/cli.js download --release latest --skipBuild --noBundle --updateBinariesReleaseMetadataAndSaveGitBundle
- name: Upload build artifact
uses: actions/upload-artifact@v3
with:
Expand Down
15 changes: 11 additions & 4 deletions src/cli/commands/DownloadCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {setBinariesGithubRelease} from "../../utils/binariesGithubRelease.js";
import {downloadCmakeIfNeeded} from "../../utils/cmake.js";
import withStatusLogs from "../../utils/withStatusLogs.js";
import {getIsInDocumentationMode} from "../../state.js";
import {saveCurrentRepoAsReleaseBundle} from "../../utils/gitReleaseBundles.js";
import {unshallowAndSquashCurrentRepoAndSaveItAsReleaseBundle} from "../../utils/gitReleaseBundles.js";
import {cloneLlamaCppRepo} from "../../utils/cloneLlamaCppRepo.js";

type DownloadCommandArgs = {
Expand All @@ -24,6 +24,7 @@ type DownloadCommandArgs = {
metal: boolean,
cuda: boolean,
skipBuild?: boolean,
noBundle?: boolean,
updateBinariesReleaseMetadataAndSaveGitBundle?: boolean
};

Expand Down Expand Up @@ -71,6 +72,12 @@ export const DownloadCommand: CommandModule<object, DownloadCommandArgs> = {
default: false,
description: "Skip building llama.cpp after downloading it"
})
.option("noBundle", {
alias: "nb",
type: "boolean",
default: false,
description: "Download a llama.cpp only from GitHub, even if a local git bundle exists for the release"
})
.option("updateBinariesReleaseMetadataAndSaveGitBundle", {
type: "boolean",
hidden: true, // this for the CI to use
Expand All @@ -82,7 +89,7 @@ export const DownloadCommand: CommandModule<object, DownloadCommandArgs> = {
};

export async function DownloadLlamaCppCommand({
repo, release, arch, nodeTarget, metal, cuda, skipBuild, updateBinariesReleaseMetadataAndSaveGitBundle
repo, release, arch, nodeTarget, metal, cuda, skipBuild, noBundle, updateBinariesReleaseMetadataAndSaveGitBundle
}: DownloadCommandArgs) {
const octokit = new Octokit();
const [githubOwner, githubRepo] = repo.split("/");
Expand Down Expand Up @@ -146,7 +153,7 @@ export async function DownloadLlamaCppCommand({
});

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

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

if (isCI && updateBinariesReleaseMetadataAndSaveGitBundle) {
await setBinariesGithubRelease(githubRelease!.data.tag_name);
await saveCurrentRepoAsReleaseBundle();
await unshallowAndSquashCurrentRepoAndSaveItAsReleaseBundle();
}

console.log();
Expand Down
9 changes: 3 additions & 6 deletions src/utils/cloneLlamaCppRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {llamaCppDirectory} from "../config.js";
import {getGitBundlePathForRelease} from "./gitReleaseBundles.js";


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

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

await simpleGit(llamaCppDirectory)
.removeRemote("origin");
await simpleGit(llamaCppDirectory)
.addRemote("origin", remoteGitUrl);
await simpleGit(llamaCppDirectory).removeRemote("origin");
});
return;
} catch (err) {
Expand Down
54 changes: 53 additions & 1 deletion src/utils/gitReleaseBundles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,50 @@ import {currentReleaseGitBundlePath, defaultLlamaCppGitHubRepo, llamaCppDirector
import {getBinariesGithubRelease} from "./binariesGithubRelease.js";


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

if (await fs.pathExists(currentReleaseGitBundlePath))
await fs.remove(currentReleaseGitBundlePath);

await simpleGit(llamaCppDirectory).addConfig("user.name", "node-llama-cpp-ci");
await simpleGit(llamaCppDirectory).addConfig("user.email", "[email protected]");

const currentBranch = await getCurrentTagOrBranch();

await simpleGit(llamaCppDirectory).fetch(["--unshallow"]);

const lastCommit = await simpleGit(llamaCppDirectory).log(["-1"]);
const lastCommitMessage: string | null = lastCommit?.all?.[0]?.message;
const newCommitMessage = "## SQUASHED ##\n\n" + (lastCommitMessage ?? "");

const newCommitSha = await simpleGit(llamaCppDirectory).raw(["commit-tree", "HEAD^{tree}", "-m", newCommitMessage]);
await simpleGit(llamaCppDirectory).reset(["--hard", newCommitSha.trim()]);

const tags = await simpleGit(llamaCppDirectory).tags();
for (const tag of tags.all) {
await simpleGit(llamaCppDirectory).tag(["--delete", tag]);
}

const branches = await simpleGit(llamaCppDirectory).branch();
for (const branch of branches.all) {
try {
await simpleGit(llamaCppDirectory).branch(["--delete", branch]);
} catch (err) {
// If the branch is not found, it's fine
// this happens as when there are no branches git returnes an output saying so, and `simpleGit` parses it as a branch,
// so the list may contain branches that do not exist.
// Right now, the non-existent branch name returned called `(no`, but I wouldn't want to rely on this specific text,
// as this is a bug in `simpleGit`.
}
}

if (currentBranch != null)
await simpleGit(llamaCppDirectory).tag([currentBranch]);

await simpleGit(llamaCppDirectory).raw(["gc", "--aggressive", "--prune=all"]);

await simpleGit(llamaCppDirectory).raw(["bundle", "create", currentReleaseGitBundlePath, "HEAD"]);
}

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

return currentReleaseGitBundlePath;
}

async function getCurrentTagOrBranch() {
const branch = await simpleGit(llamaCppDirectory).revparse(["--abbrev-ref", "HEAD"]);

if (branch !== "HEAD")
return branch;

const tags = await simpleGit(llamaCppDirectory).tag(["--points-at", "HEAD"]);
const tagArray = tags.split("\n").filter(Boolean);

if (tagArray.length > 0)
return tagArray[0];

return null;
}