In-memory repository disappears when returned from function

767 Views Asked by At

I'm using go-git in a program and trying to test my functions. To test one function I want to first create an in-memory repository, commit a file to it and then let my function use it.

So, in my test i wrote a helper that can create a new in-memory repo (init) in an in-memory filesystem and return it.

But when I try to use the repo in the calling function I don't get anything.

Here's an example reproducing the problem:

package main

import (
    "fmt"
    "os"
    "time"

    "gopkg.in/src-d/go-billy.v4"

    "gopkg.in/src-d/go-git.v4/config"
    "gopkg.in/src-d/go-git.v4/plumbing/object"

    "gopkg.in/src-d/go-billy.v4/memfs"
    "gopkg.in/src-d/go-git.v4"
    "gopkg.in/src-d/go-git.v4/storage/memory"
)

func makeTempRepo() (*git.Repository, billy.Filesystem, error) {
    s := memory.NewStorage()
    f := memfs.New()
    r, err := git.Init(s, f)
    if err != nil {
        return nil, nil, fmt.Errorf("failed to create in-memory repo: %v", err)
    }

    readme, err := f.Create("/README.md")
    if err != nil {
        return nil, nil, fmt.Errorf("failed to create a file in repository: %v", err)
    }
    readme.Write([]byte("Hello world"))

    w, _ := r.Worktree()
    _, err = w.Add("/README.md")
    if err != nil {
        return nil, nil, fmt.Errorf("failed to add file to repository: %v", err)
    }
    commit, err := w.Commit("test commit", &git.CommitOptions{
        Author: &object.Signature{
            Name:  "John Doe",
            Email: "[email protected]",
            When:  time.Now(),
        },
    })
    if err != nil {
        return nil, nil, fmt.Errorf("failed to commit file to repository: %v", err)
    }

    obj, err := r.CommitObject(commit)
    if err != nil {
        return nil, nil, fmt.Errorf("failed to get commit: %v", err)
    }
    if obj == nil {
        return nil, nil, fmt.Errorf("commit object is nil")
    }

    fmt.Printf("%v\n", obj)

    return r, f, nil
}

func clone(URL, tagPrefix string, fs billy.Filesystem) (*git.Repository, error) {
    r, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
        URL: URL,
    })
    if err != nil {
        return nil, fmt.Errorf("failed to clone repo '%s': %v", URL, err)
    }

    err = r.Fetch(&git.FetchOptions{
        Force: true,
        RefSpecs: []config.RefSpec{
            config.RefSpec("refs/*:refs/*"),
            config.RefSpec("HEAD:refs/heads/HEAD"),
        },
    })
    if err != nil {
        return nil, fmt.Errorf("failed to fetch repo '%s': %v", URL, err)
    }
    return r, nil
}

func main() {
    repo, fs, err := makeTempRepo()
    if err != nil {
        fmt.Printf("could not create temp repo: %v", err)
        os.Exit(1)
    }
    commits, err := repo.CommitObjects()
    if err != nil {
        fmt.Printf("unable to get commits: %v", err)
        os.Exit(1)
    }
    fmt.Println("commits:")
    for c, err := commits.Next(); err != nil; {
        fmt.Printf("a commit: %v\n", c)
        os.Exit(1)
    }

    res, err := clone(fs.Root(), "foo", fs)
    if err != nil {
        fmt.Printf("unexpected error: %v", err)
        os.Exit(1)
    }

    h, err := res.Head()
    if err != nil {
        fmt.Printf("error getting HEAD: %v", err)
        os.Exit(1)
    }
    if h == nil {
        fmt.Println("missing HEAD commit")
        os.Exit(1)
    }

    fmt.Printf("HEAD: %v", h)
}

And executing it gives:

$ go run main.go 
commit 936fa4014b70e008bf01338d1c0916e1365f77a6
Author: John Doe <[email protected]>
Date:   Fri Dec 07 12:17:39 2018 +0100

    test commit

commits:
unexpected error: failed to clone repo '/': repository not foundexit status 1

So, the main function can't list any commits from the repo nor clone it.

What am I doing wrong?

1

There are 1 best solutions below

2
On

The problem is in clone method.

URL option must point at git repository, but fs contains only README.md file (try to see output offs.ReadDir("/")), so fs.Root() is not valid URL option.

fs doesn't have .git (see comment) and all git stuff is in your Storer.