我有一个用 Go 编写的程序,它运行 Git 以连接到 GitHub。我正在尝试为这个程序编写一个(单元)测试,因此我想在测试中设置自己的 HTTP 服务器来模拟 GitHub。本质上,是在本地运行最简单的假版本 GitHub(远程 Git 托管站点)。
我认为创建一个测试 Git 存储库并通过 HTTP 提供该目录就足够了。然而,这似乎不起作用。这是我的尝试(用 Go 编写)和输出。 (假设
/my/local/testrepo
是我机器上包含 Git 存储库的目录)。
package main
import (
"fmt"
"net/http"
"os/exec"
"time"
)
func main() {
// Set up Git server
http.Handle("/", http.FileServer(http.Dir("/my/local/testrepo")))
go http.ListenAndServe(":8080", nil)
time.Sleep(1 * time.Second)
out, err := exec.Command("git", "clone", "http://localhost:8080/").CombinedOutput()
if err != nil {
fmt.Printf("exec error: %v\n\n", err)
}
fmt.Println(string(out))
}
输出:
exec error: exit status 128
Cloning into 'localhost'...
remote: 404 page not found
fatal: repository 'http://localhost:8080/' not found
我该如何进行这项工作?
感谢有人留下关于“愚蠢”和“智能”Git HTTP 协议的评论,后来他们删除了这些评论,但这帮助我找到了解决方案。
我做错了两件事。首先,在源存储库中,您需要运行
git update-server-info
,然后才能通过 HTTP 提供服务。我假设这会设置 Git 客户端在克隆时所需的正确文件。
其次,提供给
git clone
的存储库 URL 需要是存储库的 .git
文件夹的确切路径。就我而言,我需要传入 http://localhost:8080/.git
。