Skip to content

Commit 18c291f

Browse files
committed
Avoid regex since it's a simple string prefix removal
1 parent 11b34e5 commit 18c291f

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/NerdBank.GitVersioning/VersionOracle.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ private static string FindGitDir(string startingDir)
280280
else if (File.Exists(dirOrFilePath))
281281
{
282282
var relativeGitDirPath = ReadGitDirFromFile(dirOrFilePath);
283-
if (!String.IsNullOrWhiteSpace(relativeGitDirPath))
283+
if (!string.IsNullOrWhiteSpace(relativeGitDirPath))
284284
{
285285
var fullGitDirPath = Path.GetFullPath(Path.Combine(startingDir, relativeGitDirPath));
286286
if (Directory.Exists(fullGitDirPath))
@@ -298,15 +298,14 @@ private static string FindGitDir(string startingDir)
298298

299299
private static string ReadGitDirFromFile(string fileName)
300300
{
301-
try
301+
const string expectedPrefix = "gitdir: ";
302+
var firstLineOfFile = File.ReadLines(fileName).FirstOrDefault();
303+
if (firstLineOfFile?.StartsWith(expectedPrefix) ?? false)
302304
{
303-
var firstLineOfFile = File.ReadLines(fileName).FirstOrDefault();
304-
return Regex.Replace(firstLineOfFile, "^gitdir: *", String.Empty);
305-
}
306-
catch
307-
{
308-
return String.Empty;
305+
return firstLineOfFile.Substring(expectedPrefix.Length); // strip off the prefix, leaving just the path
309306
}
307+
308+
return null;
310309
}
311310

312311
private static Version GetAssemblyVersion(Version version, VersionOptions versionOptions)

0 commit comments

Comments
 (0)