libgit2 / libgit2/libgit2sharp

OutOfMemory Error with Repository.Diff.Compare<Patch>(...)

未关闭
#1,411 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
C#
星标
3.5k
派生
925
PR 合并指标
30 天内没有已合并 PR

描述

I get an OutOfMemoryException from Repository.Diff with v0.22.0:

LibGit2Sharp.LibGit2SharpException: Out of memory
   at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) in c:\Git\libgit2sharp\LibGit2Sharp\Core\Ensure.cs:line 160
   at LibGit2Sharp.Core.Proxy.git_patch_from_diff(DiffSafeHandle diff, Int32 idx) in c:\Git\libgit2sharp\LibGit2Sharp\Core\Proxy.cs:line 1555
   at LibGit2Sharp.PatchStats..ctor(DiffSafeHandle diff) in c:\Git\libgit2sharp\LibGit2Sharp\PatchStats.cs:line 31
   at LibGit2Sharp.Diff.<.cctor>b__1d(DiffSafeHandle diff) in c:\Git\libgit2sharp\LibGit2Sharp\Diff.cs:line 106
   at LibGit2Sharp.Diff.BuildDiffResult[T](DiffSafeHandle diff) in c:\Git\libgit2sharp\LibGit2Sharp\Diff.cs:line 120
   at LibGit2Sharp.Diff.Compare[T](Tree oldTree, Tree newTree, IEnumerable`1 paths, ExplicitPathsOptions explicitPathsOptions, CompareOptions compareOptions) in c:\Git\libgit2sharp\LibGit2Sharp\Diff.cs:line 246
   at LibGit2Sharp.Diff.Compare[T](Tree oldTree, Tree newTree) in c:\Git\libgit2sharp\LibGit2Sharp\Diff.cs:line 157
   at ABB.QueryManager.Models.Git.GitCodeChurnToCsvConverter.<GetRowData>d__7.MoveNext() in C:\Workspace\Source\TeamMetrics\QueryManager\QueryManager\Models\Git\GitCodeChurnToCsvConverter.cs:line 52

I'm trying to produce some basic code churn statistics from a Git repository. As some context, I'm implementing this in an ASP.NET application in a Hangfire job.

For each branch, I'm iterating over all of the commits and comparing them to their predecessor. I generate a patch per commit, and generate data for each file path in the commit. Here is the code I'm using:

foreach (var branch in repo.Branches.Where(b => b.Tip.Committer.When >= startDate))
{
	Commit nextCommit = null;
	foreach (var currentCommit in branch.Commits.Where(c => c.Committer.When >= startDate))
	{
		if(null == nextCommit)
		{
			foreach (var change in repo.Diff.Compare<Patch>(currentCommit.Tree, nextCommit.Tree))
			{
				var data = new string[]
				{
					nextCommit.Id.Sha,
					branch.FriendlyName,
					change.Path,
					change.Status.ToString(),
					change.IsBinaryComparison.ToString(),
					GetFileSize(nextCommit, change.Path).ToString(),
					GetLineCount(nextCommit, change.Path).ToString(),
					change.LinesAdded.ToString(),
					change.LinesDeleted.ToString()
				}
				yield return data;
			}
		}
		nextCommit = currentCommit;
	}
}

I found this related Stack Overflow question: libgit2sharp.Patch outofmemory. I tried changing my code to produce both a TreeChanges and a PatchStats, but it fails as well.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先跟踪通过 Core/Ensure.cs、Core/Proxy.cs、PatchStats.cs 和 Diff.cs 的已报告调用路径,然后查看链接的 Stack Overflow 讨论。在具有可比提交历史的 repository 上使用 Repository.Diff.Compare 重现该故障,并确定哪个操作保留了过多内存。当比较能够在不出现已报告的 OutOfMemoryException 的情况下完成,并且为失败案例添加了回归覆盖时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
csharp, git
领域
devtools, performance
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。