#How to get the git commit hash / sha of dagger.GitRef

1 messages · Page 1 of 1 (latest)

dense ginkgo
#

I want to use the ref to tag my builds but can't find any api to get it.

is there any way to get this besides breaking out to a cli or an intermediate git container.

references:
removed since not implemented https://github.com/dagger/dagger/issues/5367
alpha https://github.com/dagger/dagger/pull/935/files

GitHub

What is the issue? Calling digest() on a GitRef object instance fails with dagger.exceptions.QueryError: not implemented yet exception Log output Connected to engine 974d679fe8b2 Traceback (most re...

GitHub

TL;DR

Resolves #904
Improve os.#Container to write files
Update git test

Changes

Add git.#Commit definition
Add git.#Image to load alpine image with git package
Add tests on git.#Commit

⚠️ Issu...

dense ginkgo
#

My workaround:

        repoLsRemote, err := client.Container().
        From("alpine/git").
        WithExec([]string{"ls-remote", repoUrlWithCredentials, c.EngineRepositoryBranch}).
        Stdout(ctx)
        if err != nil {
            log.Fatalln(err)
        }

        remoteFields := strings.Fields(repoLsRemote)
        if len(remoteFields)!= 2 {
            log.Fatalf("failed to parse commit hash for banch: %s", c.EngineRepositoryBranch)
        }

        c.EngineRepositorySha = remoteFields[0]
cold mauve
#

sorry for being super late, just catching up with unread unread messages

dense ginkgo
#

ah, yeah that might be also an idea! didn't know that the .git folder was also there 😄 makes sense.

cold mauve
dense ginkgo
dense ginkgo
#

here is the daggerized version

func daggerGetBranchHeadSha(repo, branch string) (string, error) {
    var c ci.Ci
    ctx := context.Background()
    client, err := dagger.Connect(ctx)
    if err != nil {
        log.Fatalln(err)
    }

    defer func(client *dagger.Client) {
        err := client.Close()
        if err != nil {
            log.Println(err)
        }
    }(client)

    sha, err := client.Git(repo, dagger.GitOpts{KeepGitDir: true}).
        Branch(c.EngineRepositoryBranch).
        Tree().
        File(".git/HEAD").
        Contents(ctx)

    if err == nil {
        return strings.TrimSuffix(sha, "\n"), nil
    }

    return sha, err
}

trimming the trailing newline was a little gotcha

#

i've wrapped it in it's own new client context so I do not mess with my state, it might not be required but I had other issues so I was hoping to rule this out as a cause.

cold mauve
dense ginkgo
#

ahhh 😄 that's nice