简体   繁体   English

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

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

I have a git repository with uncommited/unstaged changes. 我有一个git存储库,其中包含未提交/未暂存的更改。 I'd like to load the currently committed version of a file into a string in C# and another string with the current version of the file. 我想将文件的当前提交版本加载到C#中的字符串中,并在文件的当前版本中加载另一个字符串。 (The latter can be accomplished without git) (后者可以在没有git的情况下完成)

Can this be done without changing the files of the repository (ie stashing) and without checking out another version of the repo to a temporary path? 是否可以在不更改存储库文件(即存储)且不将存储库的另一个版本签出到临时路径的情况下进行此操作?

The same problem would be solved by getting the "before" and "after" version of a diff. 通过获取diff的“之前”和“之后”版本,可以解决相同的问题。

You can get content for an individual file from a GitObject Blob commit's indexer. 您可以从GitObject Blob提交的索引器获取单个文件的内容。

Note: The following assumes you are dealing with a non-binary UTF8 file, adjust to your needs. 注意:以下假设您正在处理非二进制UTF8文件,请根据需要进行调整。

git cat-file equals: git cat-file等于:

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

The output from my example below showing the patch file of a modified file, README.md, the content from the last commit (Head.tip in this case) and the current, working directory, file's content. 以下示例的输出显示了修改后的文件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/

Cut/Paste C# Console app (just change the location of your repo to test it): 剪切/粘贴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