Skip to content

Commit

Permalink
Merge pull request #129 from KatoStoelen/get-tag-targets
Browse files Browse the repository at this point in the history
Add option to load tag targets before disposing the repository
  • Loading branch information
pascalberger authored Nov 28, 2020
2 parents 63ee1c9 + 456cdad commit 3d42824
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/Cake.Git/GitAliases.Tags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ namespace Cake.Git
partial class GitAliases
{
/// <summary>
/// Gets a list of all tags from the repo
/// Gets a list of all tags from the repository.
/// </summary>
/// <remarks>
/// If you need to access the targets of the tags use <see cref="GitTags(ICakeContext, DirectoryPath, bool)"/>
/// to make sure targerts are loaded.
/// </remarks>
/// <param name="context"></param>
/// <param name="repositoryDirectoryPath"></param>
/// <exception cref="ArgumentNullException"></exception>
Expand All @@ -29,8 +33,48 @@ public static List<Tag> GitTags(
if (repositoryDirectoryPath == null)
throw new ArgumentNullException(nameof(repositoryDirectoryPath));

return context.GitTags(repositoryDirectoryPath, false);
}

/// <summary>
/// Gets a list of all tags from the repository with the option to load targets of the tags.
/// </summary>
/// <remarks>
/// If you need to access the targets of the tags, set <paramref name="loadTargets"/> to <see langword="true"/>.
/// This will make sure that the targets are loaded before the <see cref="Repository"/> is disposed.
/// Otherwise, accessing a tag's target will throw an exception.
/// </remarks>
/// <param name="context"></param>
/// <param name="repositoryDirectoryPath"></param>
/// <param name="loadTargets">A value indicating whether targets of the tags should be loaded.</param>
/// <exception cref="ArgumentNullException"></exception>
[CakeMethodAlias]
[CakeAliasCategory("Tags")]
public static List<Tag> GitTags(
this ICakeContext context,
DirectoryPath repositoryDirectoryPath,
bool loadTargets)
{
if (context == null)
throw new ArgumentNullException(nameof(context));

if (repositoryDirectoryPath == null)
throw new ArgumentNullException(nameof(repositoryDirectoryPath));

var retval =
context.UseRepository(repositoryDirectoryPath, repo => SortedTags(repo.Tags, t => t));
context.UseRepository(
repositoryDirectoryPath,
repo => SortedTags(
repo.Tags,
t =>
{
if (loadTargets)
{
_ = t.PeeledTarget;
}

return t;
}));

return retval;
}
Expand Down
16 changes: 16 additions & 0 deletions test.cake
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,20 @@ Task("Git-AllTags-Annotated")
throw new Exception("test-annotated-tag-objectish not found");
});

Task("Git-AllTags-Targets")
.IsDependentOn("Git-Tag-Annotated")
.Does(() =>
{
var tags = GitTags(testInitalRepo, loadTargets: true);

foreach (var tag in tags)
{
// When loadTargets is true, this should not throw an exception
_ = tag.Target;
_ = tag.PeeledTarget;
}
});

Task("Git-Describe-Generic")
.IsDependentOn("Git-Tag")
.Does(() =>
Expand Down Expand Up @@ -909,6 +923,7 @@ Task("Default-Tests")
.IsDependentOn("Git-Checkout")
.IsDependentOn("Git-AllTags")
.IsDependentOn("Git-AllTags-Annotated")
.IsDependentOn("Git-AllTags-Targets")
.IsDependentOn("Git-Clean");

Task("Local-Tests")
Expand Down Expand Up @@ -944,6 +959,7 @@ Task("Local-Tests")
.IsDependentOn("Git-Checkout")
.IsDependentOn("Git-AllTags")
.IsDependentOn("Git-AllTags-Annotated")
.IsDependentOn("Git-AllTags-Targets")
.IsDependentOn("Git-Clean");

///////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 3d42824

Please sign in to comment.