根据documentation: $ 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);
}
}
}
}
}
using (var repo = new Repository(repoPath))
{
// The line below is the important one.
var blob = repo.Lookup<Blob>(commitSha + ":" + path);
using (var content = new StreamReader(blob.GetContentStream(), Encoding.UTF8))
{
var fileContent = content.ReadToEnd();
Console.WriteLine(fileContent);
}
}
2条答案
按热度按时间s71maibg1#
根据documentation:
$ git show 807736c691865a8f03c6f433d90db16d2ac7a005:a.txt
等效于下面的代码:
h43kikqp2#
正如nulltoken在注解中所说,
Lookup<T>()
可以使用colon-pathspec语法。标记线是Andrzej Gis's answer的更改。它替换
commit =
、file =
和blob =
线。此外,commitSha
可以是任何refspec:v3.17.0
、HEAD
、origin/master
等等。