Skip to content

Commit

Permalink
(#127) Removed the System.IO reference
Browse files Browse the repository at this point in the history
and added a workaround for the hitherto missing DirectoryInfo.GetParent()
  • Loading branch information
nils-a committed Dec 13, 2021
1 parent f7390b1 commit 612a762
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/Cake.Git/GitAliases.Repository.cs
@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.Linq;
using Cake.Core;
using Cake.Core.Annotations;
Expand Down Expand Up @@ -194,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) || new DirectoryInfo(parentDir.FullPath).Parent == null)
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));
}
}
}

0 comments on commit 612a762

Please sign in to comment.