使用libgit2sharp从远程下载一个文件(git show) [英] Download one file from remote (git show) using libgit2sharp

查看:166
本文介绍了使用libgit2sharp从远程下载一个文件(git show)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用git show,我可以从特定的提交中获取特定文件的内容,而无需更改本地克隆的状态:

Using git show, I can fetch the contents of a particular file from a particular commit, without changing the state of my local clone:

$ git show <file>
$ git show <commit>:<file>

如何使用以编程方式实现此目标?

How can I achieve this programatically using libgit2sharp?

推荐答案

根据文档:

$ git show 807736c691865a8f03c6f433d90db16d2ac7a005:a.txt

等效于以下代码:

using System;
using System.IO;
using System.Linq;
using System.Text;
using LibGit2Sharp;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            var pathToFile = "a.txt";
            var commitSha = "807736c691865a8f03c6f433d90db16d2ac7a005";
            var repoPath = @"path/to/repo";

            using (var repo =
                new Repository(repoPath))
            {
                var commit = repo.Commits.Single(c => c.Sha == commitSha);
                var file =  commit[pathToFile];

                var blob = file.Target as Blob;
                using (var content = new StreamReader(blob.GetContentStream(), Encoding.UTF8))
                {
                    var fileContent = content.ReadToEnd();
                    Console.WriteLine(fileContent);
                }
            }
        }
    }
}

这篇关于使用libgit2sharp从远程下载一个文件(git show)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆