#git as a directory argument seems broken for advance usage

1 messages · Page 1 of 1 (latest)

viscid veldt
#

I could be doing something completely incorrect here but I am running up against numerous issues when using a git repo as a directory argument.

type GitChange struct{}

// ChangedFiles returns a list of files that have changed
func (g *GitChange) ChangedFiles(
    ctx context.Context,
// The source git repository. Can be a local path or a git url
// +required
    src *dagger.Directory,
// ssh auth socket to use for private repositories
// +required
    sshAuthSock *dagger.Socket,
// The base ref that headRef is compared against . Can be a branch, tag, or commit hash
// +optional
// +default="HEAD"
    baseRef string,
// The current ref. Can be a branch, tag, or commit hash
// +optional
// +default="main"
    headRef string,
) (string, error) {
    repoPath := "/repo"
    sockPath := "/ssh-agent/agent.sock"

    output, err := dag.Container().
        From(dockerImage).
        WithDirectory(repoPath, src).
        WithWorkdir(repoPath).
        WithUnixSocket(sockPath, sshAuthSock).
        WithEnvVariable("SSH_AUTH_SOCK", sockPath).
        WithExec([]string{"sh", "-c", "mkdir ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts"}).
        WithExec([]string{"git", "fetch"}).
        WithExec([]string{"git", "tag", "-d", "main"}).
        Terminal().
        WithExec([]string{"git", "diff", "--name-only", fmt.Sprintf("%s..%s", baseRef, headRef)}).
        Stdout(ctx)

    if err != nil {
        return "", err
    }

    return output, nil
}

  • There seems to a random git tag created e.g. main - hence having to do git tag -d main
  • This fails as any commit hash passed as base ref is not in the git history e.g. running git checkout HASH returns fatal: unable to read tree

I have tried git fetch orgin and git reset --hard origin/main to no avail. If I clone the repo directly on the container then it works as expected.

Im happy to be pointed as skills issue with git here....

prisma tide
viscid veldt
#

Thanks - thats sort of, but far less advance use, to solve this. If I could ask a question: you are ignore .git/config is this what is cause issues?

prisma tide
#

Copying @open flame who co-authored this code. In particular he refactored the use of git tags, he may have more details to share

open flame
#

😦 yeah, sadly this is kind of known - we need to do some more work in this area for sure.

#

This fails as any commit hash passed as base ref is not in the git history e.g. running git checkout HASH returns fatal: unable to read tree
This is probably due to the fact that only the commit+branch specified is being cloned - not the entire repository history.

#

I assume the git fetch is failing because there's no remotes defined? You can "fix" this by manually adding the remote back in (which is what we do in version/git.go, linked by solomon above)

#

but yes, the proper fix here is a pretty heavy rework of how we do git cloning to allow a lot more flexibility and cover these cases
i want to get to these at some point - it's kind of top-of-mind, and forever on my backlog