git-tfs/git-tfs — Issue #123: git tfs verify regex check broken
Author: chrisbroome
Created: 2011-12-22 23:38:04+00:00
State: open

I was getting the dreaded "object reference not set to an instance of an object" error using the latest git-tfs on git version 1.7.8.msysgit.0

It turns out that the Regular expression that looks for commits wasn't allowing for extra stuff after the commit id. I set my git logs to automatically decorate commits and this was causing the Regex to not be able to parse the commit. I've posted my fix along with a new unit test below in the form of diffs:

```bash
diff --git a/GitTfs/Core/GitRepository.cs b/GitTfs/Core/GitRepository.cs
index 6d1f0c6..dda0436 100644
--- a/GitTfs/Core/GitRepository.cs
+++ b/GitTfs/Core/GitRepository.cs
@@ -260,7 +260,7 @@ namespace Sep.Git.Tfs.Core
var match = GitTfsConstants.CommitRegex.Match(line);
if (match.Success)
{
- currentCommit = match.Groups[1].Value;
+ currentCommit = match.Groups["commit"].Value;
continue;
}
var changesetInfo = TryParseChangesetInfo(line, currentCommit, includeStubRemotes);
diff --git a/GitTfs/GitTfsConstants.cs b/GitTfs/GitTfsConstants.cs
index 543d3d6..6cd6c5b 100644
--- a/GitTfs/GitTfsConstants.cs
+++ b/GitTfs/GitTfsConstants.cs
@@ -6,7 +6,7 @@ namespace Sep.Git.Tfs
{
public static readonly Regex Sha1 = new Regex("[a-f\\d]{40}", RegexOptions.IgnoreCase);
public static readonly Regex Sha1Short = new Regex("[a-f\\d]{4,40}", RegexOptions.IgnoreCase);
- public static readonly Regex CommitRegex = new Regex("^commit (" + Sha1 + ")\\s*$");
+ public static readonly Regex CommitRegex = new Regex(string.Concat( @"^commit (?", Sha1, @")" ), RegexOptions.Compiled);

public const string DefaultRepositoryId = "default";

diff --git a/GitTfsTest/GitTfsRegexTests.cs b/GitTfsTest/GitTfsRegexTests.cs
index 1711ac0..62aca19 100644
--- a/GitTfsTest/GitTfsRegexTests.cs
+++ b/GitTfsTest/GitTfsRegexTests.cs
@@ -14,6 +14,14 @@ namespace Sep.Git.Tfs.Test
}

[TestMethod]
+ public void CommitRegexShouldApproveGitCommitTitleWithExtraInfoAfterCommit()
+ {
+ const string line = "commit 9b655abe865ef0e4048aba904b79c7a2f10bdfce (HEAD, origin/master)";
+ var match = GitTfsConstants.CommitRegex.Match( line );
+ Assert.IsTrue( match.Success );
+ }
+
+ [TestMethod]
public void CommitRegexShouldDeclineCommitRevertMessage()
{
const string line = " This reverts commit e096daaf57d937fef3c0c639c3a59232310c6a20.";
```

Strictly speaking, the named capture group, "commit", is not necessary, but I find it easier to read when used vs ```match.Groups[1].Value```

I could create a fork and submit a pull request if that's the better way to submit the fix for the issue.