Skip to content

Commit

Permalink
GH-127: Modify do/while in GitFindRootFromPath to avoid infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
kcamp authored and nils-a committed Dec 13, 2021
1 parent 307841e commit f7390b1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Cake.Git/GitAliases.Repository.cs
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;
using Cake.Core;
using Cake.Core.Annotations;
Expand Down Expand Up @@ -194,7 +195,7 @@ public static DirectoryPath GitFindRootFromPath(this ICakeContext context, Direc
if (Repository.IsValid(fsPath.FullPath))
return fsPath;
var parentDir = fsPath.Combine("../").Collapse();
if (!context.FileSystem.Exist(parentDir))
if (!context.FileSystem.Exist(parentDir) || new DirectoryInfo(parentDir.FullPath).Parent == null)
throw new RepositoryNotFoundException($"Path '{path}' doesn't point at a valid Git repository or workdir.");
fsPath = parentDir;
}
Expand Down
21 changes: 21 additions & 0 deletions test.cake
Expand Up @@ -477,6 +477,25 @@ Task("Git-Find-Root-From-Path")
throw new Exception(string.Format("Wrong git root found (actual: {0}, expected: {1})", rootFolder, testInitalRepo));
});

Task("Git-Find-Root-From-Path-TempDirectory")
.Does(() =>
{
var tempPath = System.IO.Path.GetTempPath();
Information("Attempting to resolve Git root directory from temp directory '{0}'...", tempPath);
try
{
var result = GitFindRootFromPath(tempPath);
throw new Exception(string.Format("Path at '{0}' should not be a valid Git repository. Found Git root at '{1}'.",
tempPath, result.FullPath));
}
catch(LibGit2Sharp.RepositoryNotFoundException)
{
// this exception is expected when the directory traversal
// does not successfully identify a git repository.
Information("No repository located. This is expected.");
}
});

Task("Git-Tag-Apply-Objectish")
.IsDependentOn("Git-Modify-Commit")
.Does(() =>
Expand Down Expand Up @@ -1086,6 +1105,7 @@ Task("Default-Tests")
.IsDependentOn("Git-Clone-WithCredentialsAndSettings")
.IsDependentOn("Git-Diff")
.IsDependentOn("Git-Find-Root-From-Path")
.IsDependentOn("Git-Find-Root-From-Path-TempDirectory")
.IsDependentOn("Git-Reset")
.IsDependentOn("Git-Describe")
.IsDependentOn("Git-Describe-Annotated")
Expand Down Expand Up @@ -1125,6 +1145,7 @@ Task("Local-Tests")
.IsDependentOn("Git-Modify-Diff")
.IsDependentOn("Git-Diff")
.IsDependentOn("Git-Find-Root-From-Path")
.IsDependentOn("Git-Find-Root-From-Path-TempDirectory")
.IsDependentOn("Git-Reset")
.IsDependentOn("Git-Describe")
.IsDependentOn("Git-Describe-Annotated")
Expand Down

0 comments on commit f7390b1

Please sign in to comment.