Skip to content

Commit

Permalink
[git] Add gitCheckout utility function (#68)
Browse files Browse the repository at this point in the history
* Set extra env vars to fix some git commands

* Add `git.gitCheckout()` utility function
  • Loading branch information
kylewlacy authored Jul 15, 2024
1 parent 487de0b commit 27631d3
Showing 1 changed file with 53 additions and 4 deletions.
57 changes: 53 additions & 4 deletions packages/git/project.bri
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as std from "std";
import openssl from "openssl";
import curl from "curl";
import caCertificates from "ca_certificates";

export const project = {
name: "git",
Expand All @@ -17,14 +18,62 @@ const source = std
.unarchive("tar", "gzip")
.peel();

export default (): std.Recipe<std.Directory> => {
const git = std.runBash`
export default function git(): std.Recipe<std.Directory> {
let git = std.runBash`
make prefix=/ all
make prefix=/ install DESTDIR="$BRIOCHE_OUTPUT"
`
.workDir(source)
.dependencies(std.toolchain(), openssl(), curl())
.toDirectory();

return std.withRunnableLink(git, "bin/git");
};
git = std.setEnv(git, {
GIT_EXEC_PATH: { path: "libexec/git-core" },
GIT_TEMPLATE_DIR: { path: "share/git-core/templates" },
});
git = std.withRunnableLink(git, "bin/git");

return git;
}

interface GitCheckoutOptions {
repository: string;
commit: string;
}

/**
* Checkout a git repository at a specific commit. The specified commit will
* be cloned without any history.
*
* ## Options
*
* - `repository`: The URL of the git repository to checkout.
* - `commit`: The full commit hash to checkout.
*/
export function gitCheckout(
options: GitCheckoutOptions,
): std.Recipe<std.Directory> {
// Validate that the commit is a hash
std.assert(
/^[0-9a-f]{40}$/.test(options.commit),
`Invalid git commit hash: ${options.commit}`,
);

// Clone and fetch only the specified commit. See this article:
// https://blog.hartwork.org/posts/clone-arbitrary-single-git-commit/
return std.runBash`
cd "$BRIOCHE_OUTPUT"
git -c init.defaultBranch=main init
git remote add origin "$repository"
git fetch --depth 1 origin "$commit"
git -c advice.detachedHead=false checkout FETCH_HEAD
`
.dependencies(git(), caCertificates())
.env({
repository: options.repository,
commit: options.commit,
})
.outputScaffold(std.directory())
.unsafe({ networking: true })
.toDirectory();
}

0 comments on commit 27631d3

Please sign in to comment.