Skip to content

Commit

Permalink
Merge branch 'mbeelman-AddGitClean' into develop
Browse files Browse the repository at this point in the history
* mbeelman-AddGitClean:
  Add the git clean command to project.
  • Loading branch information
devlead committed Jul 3, 2018
2 parents 1864dd5 + de1aaa1 commit ad0b56e
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Cake.Git/Cake.Git.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="GitAliases.Clean.cs" />
<Compile Include="GitAliases.Tags.cs" />
<Compile Include="GitAliases.Unstage.cs" />
<Compile Include="GitAliases.Repository.cs" />
Expand Down
53 changes: 53 additions & 0 deletions src/Cake.Git/GitAliases.Clean.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using Cake.Core;
using Cake.Core.Annotations;
using Cake.Core.IO;
using Cake.Git.Extensions;
using LibGit2Sharp;

// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
namespace Cake.Git
{
// ReSharper disable once PublicMembersMustHaveComments
public static partial class GitAliases
{
/// <summary>
/// Remove untracked file(s) workspace.
/// </summary>
/// <example>
/// <code>
/// var filePaths = new FilePath[] { ".\\test.txt" };
/// GitClean("c:/temp/cake");
/// </code>
/// </example>
/// <param name="context">The context.</param>
/// <param name="repositoryDirectoryPath">Path to repository.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
[CakeMethodAlias]
[CakeAliasCategory("Clean")]
public static void GitClean(
this ICakeContext context,
DirectoryPath repositoryDirectoryPath
)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (string.IsNullOrWhiteSpace(repositoryDirectoryPath.FullPath))
{
throw new ArgumentException(nameof(repositoryDirectoryPath));
}

context.UseRepository(
repositoryDirectoryPath,
repository => repository.RemoveUntrackedFiles()
);
}


}
}
36 changes: 36 additions & 0 deletions src/Cake.Git/GitAliases.Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,42 @@ public static bool GitHasStagedChanges(this ICakeContext context, DirectoryPath
repository => repository.RetrieveStatus().Staged.Any());
}

/// <summary>
/// Checks if a repository contains untracked files.
/// </summary>
/// <example>
/// <code>
/// var result = GitHasStagedChanges("c:/temp/cake");
/// </code>
/// </example>
/// <param name="context">The context.</param>
/// <param name="path">Path to the repository to check.</param>
/// <returns>True if the Git repository contains staged changes.</returns>
/// <exception cref="ArgumentNullException">If any of the parameters are null.</exception>
/// <exception cref="RepositoryNotFoundException">If path doesn't exist.</exception>
[CakeMethodAlias]
[CakeAliasCategory("Repository")]
public static bool GitHasUntrackedFiles(this ICakeContext context, DirectoryPath path)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (path == null)
{
throw new ArgumentNullException(nameof(path));
}

if (!context.FileSystem.Exist(path))
{
throw new RepositoryNotFoundException($"Path '{path}' doesn't exists.");
}
return context.UseRepository(
path,
repository => repository.RetrieveStatus().Untracked.Any());
}

/// <summary>
/// Finding git root path from subtree.
/// </summary>
Expand Down
44 changes: 42 additions & 2 deletions test.cake
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,28 @@ Task("Create-Test-Files")
.ToArray();
});

Task("Create-Untracked-Test-Files")
.Does(() =>
{
Information("Creating untracked test files...");
Enumerable
.Range(1, 10)
.Select(
index=> {
FilePath filePath = testInitalRepo.CombineWithFilePath(string.Format("{0}.{1:000}",
Guid.NewGuid(),
index
));
Information("Creating file {0}...", filePath);
CreateRandomDataFile(Context, filePath);
Information("File {0} created.", filePath);

return filePath;
}
)
.ToArray();
});

Task("Git-Init-Add")
.IsDependentOn("Create-Test-Files")
.Does(() =>
Expand Down Expand Up @@ -698,6 +720,22 @@ Task("Git-Reset-Hard")
GitReset(testInitalRepo, GitResetMode.Hard);
});

Task("Git-Clean")
.IsDependentOn("Create-Untracked-Test-Files")
.Does(() =>
{
Information("Performing hard reset on files...");

if (!GitHasUntrackedFiles(testInitalRepo))
throw new InvalidOperationException("Repo contains no untracked files");

GitClean(testInitalRepo);

if (GitHasUntrackedFiles(testInitalRepo))
throw new InvalidOperationException("Git clean is not working properly");
});




Task("Git-Tag")
Expand Down Expand Up @@ -777,7 +815,8 @@ Task("Default-Tests")
.IsDependentOn("Git-Remote-Branch")
.IsDependentOn("Git-Checkout")
.IsDependentOn("Git-AllTags")
.IsDependentOn("Git-AllTags-Annotated");
.IsDependentOn("Git-AllTags-Annotated")
.IsDependentOn("Git-Clean");

Task("Local-Tests")
.IsDependentOn("Git-Init")
Expand Down Expand Up @@ -809,7 +848,8 @@ Task("Local-Tests")
.IsDependentOn("Git-Remote-Branch")
.IsDependentOn("Git-Checkout")
.IsDependentOn("Git-AllTags")
.IsDependentOn("Git-AllTags-Annotated");
.IsDependentOn("Git-AllTags-Annotated")
.IsDependentOn("Git-Clean");

///////////////////////////////////////////////////////////////////////////////
// EXECUTION
Expand Down

0 comments on commit ad0b56e

Please sign in to comment.