#How to get commit id when cloning
1 messages · Page 1 of 1 (latest)
How to get commit id when cloning
Hi @rose dirge, currently the cloned working copy does not include the .git directory, preventing you to get the commit id of the cloned ref. There is an issue tracking this: https://github.com/dagger/dagger/issues/3657
There is a workaround though, you can run a git clone from a container directly and read the commit id from the container. If you tell me the SDK language you're using, I can give you a working example.
@unkempt halo I see that the Go SDK v0.3.4 has the keepGitDir option in the git step? IIUC that should hepl here, correct?
I missed this (the issue is outdated then). I’ll give it a try, it will help indeed.
This was already implemented, but the Go SDK hasn't been released yet
It's available in the Python SDK
Yeah, the joys of multiple versions ... that's Engine v0.3.4, but the latest Go SDK is v0.4.0 which uses Engine v0.3.3
@rose dirge let us know the language you use when you’re back. We can provide a snippet.
(hence why I'm reluctant to add more separate versions like CLI version @smoky quartz -- this is a perfect example of 🤯)
Woot!. @vale fog wouldn't go get dagger.io/dagger@main fetch the latest develop SDK?
So users can play with this until we craft a release
@smoky quartz Yep that's true!
There's the chicken and egg problem though -- @main will use the LAST released engine, so API endpoints that haven't been released won't work
/cc @mighty shuttle Actually, would it make sense for the code in main to always use the :main tag so at least go get @main or yarn add . works? But then we'd have to change that on the fly while publishing, which is not ideal
The SDKs would have to be updated to resolve a tag to a SHA then I think since main is fully mutable, which is a significant amount of new code in each sdk. Or other approaches that have similar sort of tradeoffs.
Not 100% sure yet but I feel like this might be easier when we switch the autoprovision implementation of the SDKs to download the binary from somewhere over normal https rather than get it out of the engine image. I'm guessing there could just be a response header that has the checksum or ID of the binary so we can just easily use that as the "pinned sha"? Maybe?
Hi @unkempt halo
I am using the golang SDK.
But I found a way: since the github repo has many files, I am using the git package in golang to get just the refs/head of the branch
This is the function I am currently using instead of cloning.
we don't need to use main correct? Since we're mirroring the SDK's code each time, we can jump bump the engineRef to the latest git sha and that should work?
I don't see any dagger code here, I would assume you used the git go library to clone the code so this does not happen in a container. Here is a working snippet that does the same thing with dagger, containerized and cached:
func main() {
ctx := context.Background()
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
if err != nil {
panic(err)
}
// clone git repo into a dagger dir
sourceCode := client.Git("https://github.com/dagger/dagger", dagger.GitOpts{KeepGitDir: true}).Branch("main").Tree()
// configure a container to be used as our base image (alpine + git)
container := client.Container().From("alpine:3.16").Exec(
dagger.ContainerExecOpts{
Args: []string{"apk", "add", "--no-cache", "git"},
},
)
// set the workdir and mounted directory for the container
container = container.WithMountedDirectory("/code", sourceCode).WithWorkdir("/code")
// fetch the commit id using git
commit, err := container.Exec(dagger.ContainerExecOpts{
Args: []string{"sh", "-c", "git log --pretty=tformat:'%H' -n1"},
}).Stdout().Contents(ctx)
// check error
if err != nil {
panic(err)
}
fmt.Println("Commit id:", commit)
}
Note that I used dagger.io/dagger@main given that KeepDir is not available yet in the last release (0.4.0).