修改存储库所有提交的所有注释
amend all comments of all commits of a repository
我正在将我们的本地 tfs 存储库移动到在线 visualstudio。在此过程中,我更愿意将所有内容都转换为 git。
我在网上找到了这个 (*) post,一切正常。但现在我想使用 libgit2sharp
修改所有评论,以便指向正确的工作项。
我拼凑了一些应该可以解决问题的代码:
Dictionary<string,string> links; //contains all links between new and old workitems, keys: old workitems, values: new workitems
using (var repo = new Repository(@"D:\gittfs\testproject"))
{
foreach (var commit in repo.Commits)
{
var commitMessage = commit.Message;
var regex = new Regex(@"#[0-9]*");
foreach (Match match in regex.Matches(commitMessage))
{
string newId;
if (links.TryGetValue(match.ToString(), out newId))
{
commitMessage = commitMessage.Replace(match.ToString(), newId);
}
}
var c = repo.Commit(commitMessage, new CommitOptions { AmendPreviousCommit = true });
}
}
这段代码运行没有问题,如果我将 c.Message
与 commit.Message
进行比较,我可以看到其中有几个被替换了。问题是在程序有 运行 之后,none 的修改提交在存储库中。所以我觉得我还是做错了什么?
我认为您可能更喜欢 git filter-branch 之类的功能。
LibGit2Sharp 通过 HistoryRewriter 类型公开了这一点。
您可以查看 FilterBranchFixture 测试套件以获取灵感。
以下代码对我有用。 thnx nulltoken!
var rewriteHistoryOptions = new RewriteHistoryOptions()
{
CommitHeaderRewriter = commit =>
{
var commitMessage = commit.Message;
bool stuffreplaced = false;
var r = new Regex(@"#[0-9]*\ ");
foreach (Match match in r.Matches(commit.Message))
{
string value;
if (links.TryGetValue(match.ToString().TrimEnd(), out value))
{
commitMessage = commitMessage.Replace(match.ToString().Trim(), value);
Console.WriteLine(commit.Id + ": " + match.ToString() + " replaced by " + value);
stuffreplaced = true;
counter++;
}
}
if (stuffreplaced)
{
return CommitRewriteInfo.From(commit, message: commitMessage);
}
else
{
return CommitRewriteInfo.From(commit);
}
}
};
repo.Refs.RewriteHistory(rewriteHistoryOptions, repo.Commits);
我正在将我们的本地 tfs 存储库移动到在线 visualstudio。在此过程中,我更愿意将所有内容都转换为 git。
我在网上找到了这个 (*) post,一切正常。但现在我想使用 libgit2sharp
修改所有评论,以便指向正确的工作项。
我拼凑了一些应该可以解决问题的代码:
Dictionary<string,string> links; //contains all links between new and old workitems, keys: old workitems, values: new workitems
using (var repo = new Repository(@"D:\gittfs\testproject"))
{
foreach (var commit in repo.Commits)
{
var commitMessage = commit.Message;
var regex = new Regex(@"#[0-9]*");
foreach (Match match in regex.Matches(commitMessage))
{
string newId;
if (links.TryGetValue(match.ToString(), out newId))
{
commitMessage = commitMessage.Replace(match.ToString(), newId);
}
}
var c = repo.Commit(commitMessage, new CommitOptions { AmendPreviousCommit = true });
}
}
这段代码运行没有问题,如果我将 c.Message
与 commit.Message
进行比较,我可以看到其中有几个被替换了。问题是在程序有 运行 之后,none 的修改提交在存储库中。所以我觉得我还是做错了什么?
我认为您可能更喜欢 git filter-branch 之类的功能。
LibGit2Sharp 通过 HistoryRewriter 类型公开了这一点。
您可以查看 FilterBranchFixture 测试套件以获取灵感。
以下代码对我有用。 thnx nulltoken!
var rewriteHistoryOptions = new RewriteHistoryOptions()
{
CommitHeaderRewriter = commit =>
{
var commitMessage = commit.Message;
bool stuffreplaced = false;
var r = new Regex(@"#[0-9]*\ ");
foreach (Match match in r.Matches(commit.Message))
{
string value;
if (links.TryGetValue(match.ToString().TrimEnd(), out value))
{
commitMessage = commitMessage.Replace(match.ToString().Trim(), value);
Console.WriteLine(commit.Id + ": " + match.ToString() + " replaced by " + value);
stuffreplaced = true;
counter++;
}
}
if (stuffreplaced)
{
return CommitRewriteInfo.From(commit, message: commitMessage);
}
else
{
return CommitRewriteInfo.From(commit);
}
}
};
repo.Refs.RewriteHistory(rewriteHistoryOptions, repo.Commits);