如何使用 libgit2sharp 获取文件 changed/removed/added?
How to get files changed/removed/added using libgit2sharp?
我有两个日期 - 从和到。我必须找到在该日期差异之间在存储库中更改的文件并列出它。这是一个相关的问题,它得到了树之间的差异。。
所以假设您正在尝试复制:
git log --reverse --since "11/10/2015" --until="11/15/2015" --format="%cD %s"
一旦您有了提交列表,ICommitLog
,通过所有回购 Commits
,过滤的分支列表等。您可以通过 Linq 进行过滤。
所以创建你的提交列表:
var repo = new Repository ("/Users/sushi/code/playscript/mono");
var filter = new CommitFilter {
SortBy = CommitSortStrategies.Time | CommitSortStrategies.Reverse,
};
var commits = repo.Commits.QueryBy(filter);
现在使用 ICommitLog commits
对象,对提交对象应用 Linq
过滤器。在这种情况下,我使用提交者的日期并过滤从今天起 2 到 7 天的提交,但请记住还有一个 Author
日期:
var since = new DateTimeOffset(DateTime.Now.AddDays(-7));
var until = new DateTimeOffset(DateTime.Now.AddDays(-2));
var filteredCommitLog = commitLog.Where(c => c.Committer.When > since && c.Committer.When < until);
foreach (Commit commit in filteredCommitLog)
{
Console.WriteLine("{0} : {1}", commit.Committer.When.ToLocalTime(), commit.MessageShort);
}
结果:
11/15/2015 5:32:36 AM -08:00 : [runtime] Fix Thread.CurrentThread in non-root appdomains by setting the tls slot in start_wrapper, otherwise Thread.CurrentThread would create a new Thread object so there would be two. Fixes #35828.
11/15/2015 12:00:30 AM -08:00 : Fix a warning.
....
11/10/2015 6:41:09 AM -08:00 : Merge pull request #2214 from kumpera/fix_enum_get_get_hashcode
11/10/2015 6:07:50 AM -08:00 : [Mono.Posix] Update incorrect test
更新:
我完全错过了这个答案的一部分,修改后的文件列表...:-/(需要更多咖啡)
git log --name-status --reverse --since "11/10/2015" --until="11/15/2015" --format="%cD %s"
变为:
var since = new DateTimeOffset(DateTime.Now.AddDays(-7));
var until = new DateTimeOffset(DateTime.Now.AddDays(-2));
var filteredCommitLog = commitLog.Where(c => c.Committer.When > since && c.Committer.When < until);
foreach (Commit commit in filteredCommitLog)
{
Console.WriteLine("{0} : {1}", commit.Committer.When.ToLocalTime(), commit.MessageShort);
foreach (var parent in commit.Parents) {
foreach (TreeEntryChanges change in repo.Diff.Compare<TreeChanges>(parent.Tree, commit.Tree)) {
Console.WriteLine ("\t{0} :\t{1}", change.Status, change.OldPath);
}
}
}
输出示例:
11/11/2015 8:09:41 AM -08:00 : Crashing test in mono_class_init() from a MonoGenericClass.
Modified : mcs/class/corlib/Test/System.Reflection/MonoGenericClassTest.cs
11/11/2015 8:12:03 AM -08:00 : [runtime] mono_class_init() - don't look for metadata if the dynamic image doesn't have it.
Modified : mono/metadata/class.c
11/11/2015 9:05:07 AM -08:00 : Merge pull request #2217 from rcruzs00/master
Modified : mcs/tools/macpack/LOADER
11/11/2015 11:26:25 AM -08:00 : Merge pull request #2198 from BrzVlad/feature-concurrent-work
Modified : mono/sgen/sgen-conf.h
Modified : mono/sgen/sgen-gc.c
Modified : mono/sgen/sgen-memory-governor.c
Modified : mono/sgen/sgen-workers.c
Modified : mono/sgen/sgen-workers.h
Modified : acceptance-tests/.gitignore
Added : acceptance-tests/GCStressTests/AssemblyExtensions.cs
Added : acceptance-tests/GCStressTests/AssemblyLoadContext.cs
Modified : acceptance-tests/Makefile.am
Modified : acceptance-tests/SUBMODULES.json
Modified : acceptance-tests/versions.mk
要跳过日志并仅获取过滤后的提交列表中的文件列表:
git log --name-status --since "11/10/2015" --until="11/15/2015" --format=""
变为:
foreach (TreeEntryChanges change in repo.Diff.Compare<TreeChanges>(filteredCommitLog.First().Tree, filteredCommitLog.Last().Tree)) {
Console.WriteLine ("\t{0}\t:\t{1}", change.Status, change.OldPath);
}
示例输出:
Modified : acceptance-tests/Makefile.am
Modified : acceptance-tests/SUBMODULES.json
Modified : external/referencesource
Modified : mcs/class/Facades/Makefile
Modified : mcs/class/Mono.Cairo/Mono.Cairo/Context.cs
Modified : mcs/class/Mono.Security/Mono.Security.Interface/CertificateValidationHelper.cs
Modified : mcs/class/Mono.Security/Mono.Security.Interface/MonoTlsProvider.cs
Modified : mcs/class/System.Threading.Tasks.Dataflow/Test/System.Threading.Tasks.Dataflow/ActionBlockTest.cs
Modified : mcs/class/System.Threading.Tasks.Dataflow/Test/System.Threading.Tasks.Dataflow/BatchBlockTest.cs
Modified : mcs/class/System.Threading.Tasks.Data
后来我确实采用了不同的方法来解决这个问题。
using(var repo = new Repository("c:\_Temp\Repo"))
{
List<string> shalist = new List<string>();
foreach(Commit c in repo.Commits)
{
DateTime since = DateTime.Parse("10/29/2015 12:00:00 AM");
DateTime untill= c.Author.When.Date;
if(untill >= since)
{
shalist.Add(c.sha.Tostring());
}
}
Tree cmTree1 = repo.Lookup<Commit>(shalist.First()).Tree;
Tree cmTree2 = repo.Lookup<Commit>(shalist.Last()).Tree;
var patch = repo.Diff.Compare<patch>(cmTree1, cmTree2);
foreach(var ptc in patch)
{
Console.WriteLine(ptc.Path);
}
}
这将显示在 since - untill
日期范围内更改的所有文件
我有两个日期 - 从和到。我必须找到在该日期差异之间在存储库中更改的文件并列出它。这是一个相关的问题,它得到了树之间的差异。
所以假设您正在尝试复制:
git log --reverse --since "11/10/2015" --until="11/15/2015" --format="%cD %s"
一旦您有了提交列表,ICommitLog
,通过所有回购 Commits
,过滤的分支列表等。您可以通过 Linq 进行过滤。
所以创建你的提交列表:
var repo = new Repository ("/Users/sushi/code/playscript/mono");
var filter = new CommitFilter {
SortBy = CommitSortStrategies.Time | CommitSortStrategies.Reverse,
};
var commits = repo.Commits.QueryBy(filter);
现在使用 ICommitLog commits
对象,对提交对象应用 Linq
过滤器。在这种情况下,我使用提交者的日期并过滤从今天起 2 到 7 天的提交,但请记住还有一个 Author
日期:
var since = new DateTimeOffset(DateTime.Now.AddDays(-7));
var until = new DateTimeOffset(DateTime.Now.AddDays(-2));
var filteredCommitLog = commitLog.Where(c => c.Committer.When > since && c.Committer.When < until);
foreach (Commit commit in filteredCommitLog)
{
Console.WriteLine("{0} : {1}", commit.Committer.When.ToLocalTime(), commit.MessageShort);
}
结果:
11/15/2015 5:32:36 AM -08:00 : [runtime] Fix Thread.CurrentThread in non-root appdomains by setting the tls slot in start_wrapper, otherwise Thread.CurrentThread would create a new Thread object so there would be two. Fixes #35828.
11/15/2015 12:00:30 AM -08:00 : Fix a warning.
....
11/10/2015 6:41:09 AM -08:00 : Merge pull request #2214 from kumpera/fix_enum_get_get_hashcode
11/10/2015 6:07:50 AM -08:00 : [Mono.Posix] Update incorrect test
更新:
我完全错过了这个答案的一部分,修改后的文件列表...:-/(需要更多咖啡)
git log --name-status --reverse --since "11/10/2015" --until="11/15/2015" --format="%cD %s"
变为:
var since = new DateTimeOffset(DateTime.Now.AddDays(-7));
var until = new DateTimeOffset(DateTime.Now.AddDays(-2));
var filteredCommitLog = commitLog.Where(c => c.Committer.When > since && c.Committer.When < until);
foreach (Commit commit in filteredCommitLog)
{
Console.WriteLine("{0} : {1}", commit.Committer.When.ToLocalTime(), commit.MessageShort);
foreach (var parent in commit.Parents) {
foreach (TreeEntryChanges change in repo.Diff.Compare<TreeChanges>(parent.Tree, commit.Tree)) {
Console.WriteLine ("\t{0} :\t{1}", change.Status, change.OldPath);
}
}
}
输出示例:
11/11/2015 8:09:41 AM -08:00 : Crashing test in mono_class_init() from a MonoGenericClass.
Modified : mcs/class/corlib/Test/System.Reflection/MonoGenericClassTest.cs
11/11/2015 8:12:03 AM -08:00 : [runtime] mono_class_init() - don't look for metadata if the dynamic image doesn't have it.
Modified : mono/metadata/class.c
11/11/2015 9:05:07 AM -08:00 : Merge pull request #2217 from rcruzs00/master
Modified : mcs/tools/macpack/LOADER
11/11/2015 11:26:25 AM -08:00 : Merge pull request #2198 from BrzVlad/feature-concurrent-work
Modified : mono/sgen/sgen-conf.h
Modified : mono/sgen/sgen-gc.c
Modified : mono/sgen/sgen-memory-governor.c
Modified : mono/sgen/sgen-workers.c
Modified : mono/sgen/sgen-workers.h
Modified : acceptance-tests/.gitignore
Added : acceptance-tests/GCStressTests/AssemblyExtensions.cs
Added : acceptance-tests/GCStressTests/AssemblyLoadContext.cs
Modified : acceptance-tests/Makefile.am
Modified : acceptance-tests/SUBMODULES.json
Modified : acceptance-tests/versions.mk
要跳过日志并仅获取过滤后的提交列表中的文件列表:
git log --name-status --since "11/10/2015" --until="11/15/2015" --format=""
变为:
foreach (TreeEntryChanges change in repo.Diff.Compare<TreeChanges>(filteredCommitLog.First().Tree, filteredCommitLog.Last().Tree)) {
Console.WriteLine ("\t{0}\t:\t{1}", change.Status, change.OldPath);
}
示例输出:
Modified : acceptance-tests/Makefile.am
Modified : acceptance-tests/SUBMODULES.json
Modified : external/referencesource
Modified : mcs/class/Facades/Makefile
Modified : mcs/class/Mono.Cairo/Mono.Cairo/Context.cs
Modified : mcs/class/Mono.Security/Mono.Security.Interface/CertificateValidationHelper.cs
Modified : mcs/class/Mono.Security/Mono.Security.Interface/MonoTlsProvider.cs
Modified : mcs/class/System.Threading.Tasks.Dataflow/Test/System.Threading.Tasks.Dataflow/ActionBlockTest.cs
Modified : mcs/class/System.Threading.Tasks.Dataflow/Test/System.Threading.Tasks.Dataflow/BatchBlockTest.cs
Modified : mcs/class/System.Threading.Tasks.Data
后来我确实采用了不同的方法来解决这个问题。
using(var repo = new Repository("c:\_Temp\Repo"))
{
List<string> shalist = new List<string>();
foreach(Commit c in repo.Commits)
{
DateTime since = DateTime.Parse("10/29/2015 12:00:00 AM");
DateTime untill= c.Author.When.Date;
if(untill >= since)
{
shalist.Add(c.sha.Tostring());
}
}
Tree cmTree1 = repo.Lookup<Commit>(shalist.First()).Tree;
Tree cmTree2 = repo.Lookup<Commit>(shalist.Last()).Tree;
var patch = repo.Diff.Compare<patch>(cmTree1, cmTree2);
foreach(var ptc in patch)
{
Console.WriteLine(ptc.Path);
}
}
这将显示在 since - untill