簡體   English   中英

如何使用libgit2sharp(git cat-file)獲取文件的頭版

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

我有一個git存儲庫,其中包含未提交/未暫存的更改。 我想將文件的當前提交版本加載到C#中的字符串中,並在文件的當前版本中加載另一個字符串。 (后者可以在沒有git的情況下完成)

是否可以在不更改存儲庫文件(即存儲)且不將存儲庫的另一個版本簽出到臨時路徑的情況下進行此操作?

通過獲取diff的“之前”和“之后”版本,可以解決相同的問題。

您可以從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.
+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#控制台應用程序(只需更改存儲庫的位置即可對其進行測試):

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);
                }
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM