我有一个带有uncommited / unstaged更改的git存储库。我想将当前提交的文件版本加载到C#中的字符串中,并将另一个字符串加载到当前版本的文件中。 (后者可以在没有git的情况下完成)
是否可以在不更改存储库文件(即存储)的情况下完成此操作,而无需将另一个版本的存储库检出到临时路径?
同样的问题可以通过获得差异的“之前”和“之后”版本来解决。
答案 0 :(得分:3)
您可以从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);
}
}
}
}
}