【问题标题】:How do I get a previous version of a file with libgit2sharp如何使用 libgit2sharp 获取文件的先前版本
【发布时间】:2013-08-26 18:12:58
【问题描述】:

我正在尝试使用libgit2sharp 来获取文件的先前版本。我希望工作目录保持原样,至少恢复到以前的状态。

我最初的方法是尝试在我想要的文件上存储、签出路径,将其保存到字符串变量,然后存储弹出。有没有办法储存流行音乐?我不容易找到。这是我到目前为止的代码:

        using (var repo = new Repository(DirectoryPath, null))
        {
            var currentCommit = repo.Head.Tip.Sha;
            var commit = repo.Commits.Where(c => c.Sha == commitHash).FirstOrDefault();
            if (commit == null)
                return null;

            var sn = "Stash Name";
            var now = new DateTimeOffset(DateTime.Now);

            var diffCount = repo.Diff.Compare().Count();

            if(diffCount > 0)
                repo.Stashes.Add(new Signature(sn, "x@y.com", now), options: StashModifiers.Default);

            repo.CheckoutPaths(commit.Sha, new List<string>{ path }, CheckoutModifiers.None, null, null);
            var fileText = File.ReadAllText(path);

            repo.CheckoutPaths(currentCommit, new List<string>{path}, CheckoutModifiers.None, null, null);
            if(diffCount > 0)
                ; // stash Pop?
        }

如果有比使用 Stash 更简单的方法,那也很好用。

【问题讨论】:

    标签: c# libgit2sharp


    【解决方案1】:

    有没有办法隐藏流行音乐?我不容易找到它

    不幸的是,Stash pop 需要合并,这在 libgit2 中尚不可用。

    我正在尝试使用 libgit2sharp 来获取文件的先前版本。我希望工作目录保持原样

    您可以通过打开同一存储库的两个实例来实现这样的结果,每个实例都指向不同的工作目录。 Repository 构造函数接受 RepositoryOptions 参数,它应该允许您这样做。

    以下代码演示了此功能。这会创建一个额外的实例 (otherRepo),您可以使用它来检索当前在主工作目录中检出的文件的不同版本。

    string repoPath = "path/to/your/repo";
    
    // Create a temp folder for a second working directory
    string tempWorkDir = Path.Combine(Path.GetTempPath(), "tmp_wd");
    Directory.CreateDirectory(newWorkdir);
    
    // Also create a new index to not alter the main repository
    string tempIndex = Path.Combine(Path.GetTempPath(), "tmp_idx");
    
    var opts = new RepositoryOptions
    {
        WorkingDirectoryPath = tempWorkDir,
        IndexPath = tempIndex
    };
    
    using (var mainRepo = new Repository(repoPath))
    using (var otherRepo = new Repository(mainRepo.Info.Path, opts))
    {
        string path = "file.txt";
    
        // Do your stuff with mainrepo
        mainRepo.CheckoutPaths("HEAD", new[] { path });
        var currentVersion = File.ReadAllText(Path.Combine(mainRepo.Info.WorkingDirectory, path));
    
        // Use otherRepo to temporarily checkout previous versions of files
        // Thank to the passed in RepositoryOptions, this checkout will not
        // alter the workdir nor the index of the main repository.
        otherRepo.CheckoutPaths("HEAD~2", new [] { path });
        var olderVersion = File.ReadAllText(Path.Combine(otherRepo.Info.WorkingDirectory, path));
    }
    

    您可以通过查看 RepositoryOptionFixture 中的测试来更好地掌握这种 RepositoryOptions 类型。

    【讨论】:

    • 谢谢。只是给别人的注释:@nulltoken 使用的方法Repository.CheckoutPaths 在 v0.13 中不存在。但它包含在 vNext 分支中。所以当发布时,(或者如果你正在使用 vAlpha)这将完美地工作。在此之前,您需要使用稍微不同的签名重新编写这些调用。
    • @Shlomo 非常感谢! :)
    猜你喜欢
    • 2010-12-10
    • 2014-06-25
    • 2020-09-23
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多