rust 如何正确使用git2::Remote::push?

9bfwbjaz  于 2023-08-05  发布在  Git
关注(0)|答案(1)|浏览(133)

我正在构建一个应用程序,它在内部使用git2.rs来管理一个项目。
我正在尝试为基本用例实现测试,例如git init,git add,commit和push到远程,我在push部分遇到了问题。
我使用一个本地裸远程存储库实现了我的测试用例。我首先创建一个源代码库,在里面初始化git,然后创建一个哑文本文件,将其添加到索引并提交。
一切似乎都在那里工作。
然后我创建一个本地裸仓库,将其设置为源仓库的“origin”remote,并在远程仓库示例上调用push。我没有错误,但源代码库的内容似乎没有被推。
文档对学习者不是很友好,所以我很难理解我在做什么。
我希望在远程仓库目录中看到我的文本文件,但只有git结构。
当我尝试通过在推送后将远程克隆到新目录中来做出Assert时,我检查文本文件是否在那里,但它不在,它只是创建了一个空的存储库。
下面是我的代码的相关部分,它只是我在tests子模块中实现的一个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();
    }
}


我也试着像这样添加refspecs,但它并没有改变任何东西

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();


非常感谢你的帮助。

mf98qq94

mf98qq941#

我在这个线程https://users.rust-lang.org/t/how-to-use-git2-push-correctly/97202/6中得到了一个解决方案,我在这里依赖它,以防它可能有用。
原来问题出在我的git commit实现上。我忘了用新提交更新分支指针。所以才没有推。
这是给我解答的片段

use std::{fs, path};

use git2::build::RepoBuilder;
use git2::{IndexAddOption, Repository, Signature};

fn main() {
    let root_dir = path::Path::new("Z:/Temp");
    let base_path = root_dir.join("base");
    let remote_path = root_dir.join("remote");
    let clone_path = root_dir.join("clone");
    let author = Signature::now("user", "user@example.com").unwrap();

    // create base repo and remote bare repo
    let base_repo = Repository::init(&base_path).unwrap();
    let remote_repo = Repository::init_bare(&remote_path).unwrap();
    let remote_url = format!("file:///{}", remote_repo.path().display());

    // create a text file and add it to index
    fs::write(base_path.join("hello.txt"), "hello world!\n").unwrap();
    let mut base_index = base_repo.index().unwrap();
    base_index
        .add_all(["."], IndexAddOption::DEFAULT, None)
        .unwrap();
    base_index.write().unwrap();

    // make the commit, since it's the initial commit, there's no parent
    let tree = base_repo
        .find_tree(base_index.write_tree().unwrap())
        .unwrap();
    let commit_oid = base_repo
        .commit(None, &author, &author, "initial", &tree, &[])
        .unwrap();

    // update branch pointer
    let branch = base_repo
        .branch("main", &base_repo.find_commit(commit_oid).unwrap(), true)
        .unwrap();
    let branch_ref = branch.into_reference();
    let branch_ref_name = branch_ref.name().unwrap();
    base_repo.set_head(branch_ref_name).unwrap();

    // add remote as "origin" and push the branch
    let mut origin = base_repo.remote("origin", &remote_url).unwrap();
    origin.push(&[branch_ref_name], None).unwrap();

    // clone from remote
    let clone_repo = RepoBuilder::new()
        .branch("main")
        .clone(&remote_url, &clone_path)
        .unwrap();

    // examine the commit message:
    println!(
        "short commit message: {}",
        clone_repo
            .head()
            .unwrap()
            .peel_to_commit()
            .unwrap()
            .summary()
            .unwrap()
    );
}

字符串
如果有用的话,这里是我的add和commit的固定实现,以及推送测试。

fn _add_all<'a, T: Into<PathBuf>, E: Into<&'a str>>(
        repo_dir: T,
        expr: E,
    ) -> Result<git2::Index, git2::Error> {
        let repo = Self::_repo(repo_dir)?;
        let mut index = repo.index()?;
        index.add_all([expr.into()], git2::IndexAddOption::DEFAULT, None)?;
        index.write()?;
        index.write_tree()?;
        Ok(index)
    }

    fn _update_branch<'a, T: Into<PathBuf>, Str: Into<&'a str>>(
        repo_dir: T,
        name: Str,
        commit_oid: &git2::Oid,
    ) -> Result<(), git2::Error> {
        let repo = Self::_repo(repo_dir)?;
        let branch = repo.branch(name.into(), &repo.find_commit(commit_oid.clone())?, true)?;
        let branch_ref = branch.into_reference();
        let branch_ref_name = branch_ref.name().unwrap();
        repo.set_head(branch_ref_name)?;
        Ok(())
    }

    fn _commit<'a, T: Into<PathBuf>, Str: Into<&'a str>>(
        repo_dir: T,
        message: Str,
        update_branch: Str,
    ) -> Result<git2::Oid, git2::Error> {
        let repo_dir: PathBuf = repo_dir.into();
        let repo = Self::_repo(&repo_dir)?;
        let mut index = repo.index()?;
        let sign = Self::_signature(&repo)?;
        let tree = repo.find_tree(index.write_tree()?)?;

        let mut parents = vec![];
        let mut update_ref = Some("HEAD");

        if let Ok(head) = repo.head() {
            parents.push(head.peel_to_commit()?);
        } else {
            update_ref = None; // no HEAD = first commit
        }

        let oid = repo.commit(
            update_ref,
            &sign,
            &sign,
            message.into(),
            &tree,
            &parents.iter().collect::<Vec<&git2::Commit>>()[..],
        )?;

        Self::_update_branch(repo_dir, update_branch.into(), &oid)?;

        Ok(oid)
    }
#[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 = TestGit {
            dir: std::env::current_dir().unwrap().join("test/remote"),
            state: String::from("Hello"),
        };
        <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();

        testgit.git_add(".").unwrap();

        testgit.git_commit("test commit", "master").unwrap();

        let master = base_repo
            .find_branch("master", git2::BranchType::Local)
            .unwrap();

        let mut remote = base_repo.find_remote("origin").unwrap();

        remote
            .push::<&str>(&[master.into_reference().name().unwrap()], None)
            .unwrap();

        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());

        std::fs::remove_dir_all(&testgit.dir.parent().unwrap()).unwrap();
    }

的数据

相关问题