Skip to content

Commit

Permalink
Merge pull request #128 from kcamp/GH-127
Browse files Browse the repository at this point in the history
GH-127: Modify do/while in GitFindRootFromPath to avoid infinite loop
  • Loading branch information
nils-a committed Dec 13, 2021
2 parents 307841e + 612a762 commit 85395d9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/Cake.Git/GitAliases.Repository.cs
Expand Up @@ -193,12 +193,42 @@ 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))
var parentDir = GetParent(context, fsPath);
if (!context.FileSystem.Exist(parentDir) || GetParent(context, parentDir) == null)
throw new RepositoryNotFoundException($"Path '{path}' doesn't point at a valid Git repository or workdir.");
fsPath = parentDir;
}
while (true);
}

// replace with DirectoryPath.GetParent(), once https://github.com/cake-build/cake/pull/3349/files
// ReSharper disable once InconsistentNaming
private static DirectoryPath GetParent(ICakeContext context, DirectoryPath path)
{
path = path.MakeAbsolute(context.Environment); // to be sure. It's no use splitting the segments of "."

if (path.Segments.Length == 1)
{
// one segment on Windows is e.g. "C:/"
// on all other systems one segment is e.g "/home"
if (context.Environment.Platform.Family == PlatformFamily.Windows)
{
// no more parents
return null;
}

// root ("/") is not really a segment for Cake,
// so we return that directly.
return new DirectoryPath("/");
}

if(path.Segments.Length == 0)
{
return null;
}

var segments = path.Segments.Take(path.Segments.Length - 1);
return new DirectoryPath(string.Join(path.Separator.ToString(), segments));
}
}
}
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 85395d9

Please sign in to comment.