如何使用 libgit2sharp (git cat-file) 获取文件的头部版本

How to get the head version of a file with libgit2sharp (git cat-file)

我有一个 git 存储库,其中有 uncommited/unstaged 个更改。我想将当前提交的文件版本加载到 C# 中的一个字符串和另一个包含文件当前版本的字符串中。 (后者不用git即可完成)

这是否可以在不更改存储库文件(即隐藏)并且不将另一个版本的存储库检出到临时路径的情况下完成?

同样的问题可以通过获取 "before" 和 "after" 版本的差异来解决。

您可以从 GitObject Blob 提交的索引器中获取单个文件的内容。

注:以下假设您处理的是非二进制UTF8文件,请根据您的需要进行调整。

git cat-file 等于:

var blob = repo.Head.Tip[{FilePathToContentFrom}].Target as Blob;
using (var content = new StreamReader(blob.GetContentStream(), Encoding.UTF8))
{
   commitContent = content.ReadToEnd();
}

我下面示例的输出显示了修改文件的补丁文件,README.md,最后一次提交的内容(在本例中为 Head.tip)和当前工作目录,文件的内容.

~~~~ Patch file ~~~~
diff --git a/README.md b/README.md
index aa2c023..a778f15 100644
--- a/README.md
+++ b/README.md
@@ -14,7 +14,7 @@ The CI builds are generously hosted and run on [Travis][travis]

 ## What is PlayScript?

-[PlayScript](https://github.com/PlayScriptRedux/playscript) is an open source Adobe ActionScript compatible compiler and Flash compatible runtime that runs in the Mono .NET environment, targeting mobile devices through the Xamarin platform. With a combination of Adobe FlashBuilder for Web and Xamarin Studio for mobile complex large scale cross-mobile-web projects can be developed with full IDE, source debugging and intellisense support on all platforms, with access to the full native mobile API's on the mobile platform.
+Whosebug [PlayScript](https://github.com/PlayScriptRedux/playscript) is an open source Adobe ActionScript compatible compiler and Flash compatible runtime that runs in the Mono .NET environment, targeting mobile devices through the Xamarin platform. With a combination of Adobe FlashBuilder for Web and Xamarin Studio for mobile complex large scale cross-mobile-web projects can be developed with full IDE, source debugging and intellisense support on all platforms, with access to the full native mobile API's on the mobile platform.

 In addition to accurate ActionScript language support, the PlayScript compiler also supports a new language - PlayScript - which is derived from both C# and ActionScript.  This new language supports all of the features of C#, including generics, properties, events, value types, operator overloading, async programming, linq, while at the same time being upwards compatible with ActionScript.  The PlayScript language can be used to target both desktop and mobile (via Xamarin), and existing Flash/ActionScript code can easily be converted to PlayScript code by simply renaming files from .as to .play, and fixing a few issues related to the stricter syntax and semantics of the PlayScript language.


~~~~ Original file ~~~~
## What is PlayScript?

[PlayScript](https://github.com/PlayScriptRedux/playscript) is an open source Adobe ActionScript compatible compiler and Flash compatible runtime that runs in the Mono .NET environment, targeting mobile devices through the Xamarin platform. With a combination of Adobe FlashBuilder for Web and Xamarin Studio for mobile complex large scale cross-mobile-web projects can be developed with full IDE, source debugging and intellisense support on all platforms, with access to the full native mobile API's on the mobile platform.

---{250 lines removed}---
 [travis]: https://travis-ci.org/

~~~~ Current file ~~~~
## What is PlayScript?

Whosebug [PlayScript](https://github.com/PlayScriptRedux/playscript) is an open source Adobe ActionScript compatible compiler and Flash compatible runtime that runs in the Mono .NET environment, targeting mobile devices through the Xamarin platform. With a combination of Adobe FlashBuilder for Web and Xamarin Studio for mobile complex large scale cross-mobile-web projects can be developed with full IDE, source debugging and intellisense support on all platforms, with access to the full native mobile API's on the mobile platform.

---{250 lines removed}---
 [travis]: https://travis-ci.org/

Cut/Paste C# 控制台应用程序(只需更改存储库的位置即可对其进行测试):

using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using LibGit2Sharp;

namespace libgitblob
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            var repo = new Repository ("/Users/administrator/code/playscript/playscriptredux/playscript");
            foreach (var item in repo.RetrieveStatus()) {
                if (item.State == FileStatus.Modified) {
                    var patch = repo.Diff.Compare<Patch> (new List<string>() { item.FilePath });
                    var blob = repo.Head.Tip[item.FilePath].Target as Blob;
                    string commitContent;
                    using (var content = new StreamReader(blob.GetContentStream(), Encoding.UTF8))
                    {
                        commitContent = content.ReadToEnd();
                    }
                    string workingContent;
                    using (var content = new StreamReader(repo.Info.WorkingDirectory + Path.DirectorySeparatorChar + item.FilePath, Encoding.UTF8))
                    {
                        workingContent = content.ReadToEnd();
                    }
                    Console.WriteLine ("~~~~ Patch file ~~~~");
                    Console.WriteLine (patch.Content);
                    Console.WriteLine ("\n\n~~~~ Original file ~~~~");
                    Console.WriteLine(commitContent);
                    Console.WriteLine ("\n\n~~~~ Current file ~~~~");
                    Console.WriteLine(workingContent);
                }
            }
        }
    }
}