doutan1671 2018-12-07 11:23
浏览 38

从函数返回时,内存中存储库消失

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: "john@does.com",
            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
", 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
", 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 <john@does.com>
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条回答 默认 最新

  • dpqy77560 2018-12-07 15:06
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划