Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用libgit2sharp(git cat file)获取文件的头版本_C#_Git_Libgit2sharp - Fatal编程技术网

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

C# 如何使用libgit2sharp(git cat file)获取文件的头版本,c#,git,libgit2sharp,C#,Git,Libgit2sharp,我有一个git存储库,其中包含未限制/未老化的更改。我想将文件的当前提交版本加载到C#中的字符串中,并将另一个字符串加载到文件的当前版本中。(后者可以在没有git的情况下完成) 这是否可以在不更改存储库文件(即隐藏)和不将另一版本的回购签出到临时路径的情况下完成 同样的问题也可以通过获取差异的“之前”和“之后”版本来解决。您可以从GitObject Blob提交的索引器获取单个文件的内容 注意:以下假设您正在处理非二进制UTF8文件,请根据需要进行调整 git cat文件等于: var blob

我有一个git存储库,其中包含未限制/未老化的更改。我想将文件的当前提交版本加载到C#中的字符串中,并将另一个字符串加载到文件的当前版本中。(后者可以在没有git的情况下完成)

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


同样的问题也可以通过获取差异的“之前”和“之后”版本来解决。

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

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

git cat文件
等于:

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#控制台应用程序(只需更改回购协议的位置即可进行测试):

使用系统;
使用System.IO;
使用系统文本;
使用System.Collections.Generic;
使用LibGit2Sharp;
命名空间libgitblob
{
类主类
{
公共静态void Main(字符串[]args)
{
var repo=new Repository(“/Users/administrator/code/playscript/playscriptredux/playscript”);
foreach(repo.RetrieveStatus()中的变量项){
if(item.State==FileStatus.Modified){
var patch=repo.Diff.Compare(新列表(){item.FilePath});
var blob=repo.Head.Tip[item.FilePath]。目标为blob;
字符串委员会内容;
使用(var content=newstreamreader(blob.GetContentStream(),Encoding.UTF8))
{
committecontent=content.ReadToEnd();
}
字符串工作内容;
使用(var content=newstreamreader(repo.Info.WorkingDirectory+Path.directoryseportorchar+item.FilePath,Encoding.UTF8))
{
workingContent=content.ReadToEnd();
}
Console.WriteLine(“~~~~~补丁文件~~~”);
Console.WriteLine(patch.Content);
Console.WriteLine(“\n\n~~~~原始文件~~~”;
控制台.书面线(委员会内容);
Console.WriteLine(“\n\n~~~~当前文件~~~”;
Console.WriteLine(工作内容);
}
}
}
}
}
  • 参考:
  • 参考:
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);
                }
            }
        }
    }
}