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

本文关键字:文件 获取 版本 cat-file 何使用 libgit2sharp git | 更新日期: 2023-09-27 18:19:16

我有一个git存储库,其中包含未提交/未分级的更改。我想加载一个文件的当前提交的版本到一个字符串在c#和另一个字符串与文件的当前版本。(后者不需要git也可以完成)

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

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

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

您可以从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,上次提交的内容。提示:在本例中)和当前工作目录文件的内容。

~~~~ 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.
+STACKOVERFLOW [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?
STACKOVERFLOW [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/

剪切/粘贴c#控制台应用程序(只需更改您的repo的位置来测试它):

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);
                }
            }
        }
    }
}
  • 裁判:https://libgit2.github.com/libgit2/ex/HEAD/cat-file.html
  • 裁判:https://github.com/libgit2/libgit2sharp/blob/vNext/LibGit2Sharp.Tests/BlobFixture.cs