我正在构建一个内部使用git2.rs来管理项目的应用程序.
我正在try 实现基本用例的测试,例如git init、git添加、提交和推送到遥控器,但推送部分遇到了问题.
我使用本地裸远程存储库实现了我的测试用例.我首先创建一个源存储库,在其中初始化git,然后创建一个哑文本文件,将其添加到索引中并提交.
在此之前,一切似乎都很顺利.
然后,我创建一个本地裸repo,将其设置为源repo的"源"远程,并在远程repo实例上调用Push.我没有错误,但源回购的内容似乎没有被推送.
文档对学习者不是很友好,所以我很难理解我在做什么.
我希望在远程repo目录中可能会看到我的文本文件,但那里只有git struct .
当我在按下后试图通过将遥控器克隆到新目录中来进行断言时,我会判断文本文件是否在那里,但它不在,它只会创建一个空的存储库.
以下是我的代码的相关部分,这只是我在Test子模块中实现的一个特征.
源头trait
use git2::Repository;
use std::path::PathBuf;
pub trait Git {
// ... other methods...
fn _set_remote<'a, T: Into<PathBuf>>(
repo_dir: T,
name: &str,
url: &str,
) -> Result<(), git2::Error> {
let repo = Self::_repo(repo_dir)?;
repo.remote(name, url)?;
Ok(())
}
fn git_init(&self) -> Result<Repository, git2::Error>;
fn git_add<'a, E: Into<&'a str>>(&self, expr: E) -> Result<git2::Index, git2::Error>;
fn git_commit<'a, M: Into<&'a str>>(&self, message: M) -> Result<git2::Oid, git2::Error>;
fn git_set_remote(&self, name: &str, url: &str) -> Result<(), git2::Error>;
}
测试实现
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
struct TestGit {
pub dir: PathBuf,
pub state: String,
}
// Impl TestGit ...
impl Git for TestGit {
fn git_init(&self) -> Result<Repository, git2::Error> {
// ...
}
fn git_add<'a, E: Into<&'a str>>(&self, expr: E) -> Result<git2::Index, git2::Error> {
// ...
}
fn git_commit<'a, M: Into<&'a str>>(&self, message: M) -> Result<git2::Oid, git2::Error> {
// ...
}
fn git_set_remote(&self, name: &str, url: &str) -> Result<(), git2::Error> {
Self::_set_remote(&self.dir, name, url)
}
}
// Some first tests for init, add, commit, write file, etc.
// ...
#[test]
fn test_push() {
let testgit = TestGit {
dir: std::env::current_dir().unwrap().join("test/base"),
state: String::from("Hello"),
};
let base_repo = testgit.git_init().unwrap();
let testgitremote = create_testgit_instance("test/remote");
<TestGit as Git>::_init::<&PathBuf>(&testgitremote.dir, true).unwrap();
testgit
.git_set_remote(
"origin",
format!("file://{}", testgitremote.dir.to_str().unwrap()).as_str(),
)
.unwrap();
testgit.write_file("test.txt").unwrap(); // This creates a test.txt file with "Hello" in it at the root of the repo.
testgit.git_add(".").unwrap();
testgit.git_commit("test commit").unwrap();
// This works find until there becauses I tested it elsewhere, the index contains one more element after the commit.
let mut remote = base_repo.find_remote("origin").unwrap();
remote.push::<&str>(&[], None).unwrap(); // This is what I'm having troubles to understand, I'm guessing I'm just pushing nothing but I don't find anything clear in the docs and there is no "push" example it the git2.rs sources.
let mut clonebuilder = git2::build::RepoBuilder::new();
let clonerepo_dir = testgit.dir.parent().unwrap().join("clone");
clonebuilder
.clone(remote.url().unwrap(), &clonerepo_dir)
.unwrap();
assert!(clonerepo_dir.join("test.txt").exists()); // This fails...
std::fs::remove_dir_all(&testgit.dir.parent().unwrap()).unwrap();
}
}
我也试着添加这样的参考规范,但它没有改变任何事情
let mut remote = base_repo.find_remote("origin").unwrap();
remote.push::<&str>(&["refs/heads/master:refs/heads/master")], None).unwrap();
或者像这样,同样的结果.
let mut remote = base_repo.find_remote("origin").unwrap();
base_repo
.remote_add_push("origin", "refs/heads/master:refs/heads/master")
.unwrap();
remote.push::<&str>(&[], None).unwrap();
非常感谢你的帮助.