Skip to content

Commit 148b358

Browse files
committed
fixup! Ensure Tags can be created in detached Head state
1 parent 1dcc23b commit 148b358

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

LibGit2Sharp.Tests/TagFixture.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public void CreatingATagInAEmptyRepositoryThrows()
234234

235235
using (var repo = new Repository(repoPath))
236236
{
237-
Assert.Throws<LibGit2SharpException>(() => repo.ApplyTag("mynotag"));
237+
Assert.Throws<UnbornBranchException>(() => repo.ApplyTag("mynotag"));
238238
}
239239
}
240240

@@ -246,8 +246,8 @@ public void CreatingATagForHeadInAEmptyRepositoryThrows()
246246

247247
using (var repo = new Repository(repoPath))
248248
{
249-
Assert.Throws<LibGit2SharpException>(() => repo.ApplyTag("mytaghead", "HEAD"));
250-
Assert.Throws<LibGit2SharpException>(() => repo.ApplyTag("mytaghead"));
249+
Assert.Throws<UnbornBranchException>(() => repo.ApplyTag("mytaghead", "HEAD"));
250+
Assert.Throws<UnbornBranchException>(() => repo.ApplyTag("mytaghead"));
251251
}
252252
}
253253

LibGit2Sharp/Core/Ensure.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,32 @@ public static void ArgumentConformsTo<T>(T argumentValue, Func<T, bool> checker,
209209
}
210210

211211
public static void GitObjectIsNotNull(GitObject gitObject, string identifier)
212+
{
213+
Func<string, LibGit2SharpException> exceptionBuilder;
214+
215+
if (string.Equals("HEAD", identifier, StringComparison.Ordinal))
216+
{
217+
exceptionBuilder = m => new UnbornBranchException(m);
218+
}
219+
else
220+
{
221+
exceptionBuilder = m => new LibGit2SharpException(m);
222+
}
223+
224+
GitObjectIsNotNull(gitObject, identifier, exceptionBuilder);
225+
}
226+
227+
public static void GitObjectIsNotNull(
228+
GitObject gitObject,
229+
string identifier,
230+
Func<string, LibGit2SharpException> exceptionBuilder)
212231
{
213232
if (gitObject != null)
214233
{
215234
return;
216235
}
217236

218-
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture,
237+
throw exceptionBuilder(string.Format(CultureInfo.InvariantCulture,
219238
"No valid git object identified by '{0}' exists in the repository.",
220239
identifier));
221240
}

LibGit2Sharp/RepositoryExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private static void EnsureNoGitLink<T>() where T : GitObject
6868
/// <param name="tagName">The name of the tag to create.</param>
6969
public static Tag ApplyTag(this IRepository repository, string tagName)
7070
{
71-
return ApplyTag(repository, tagName, RetrieveHeadCommitId(repository, "Cannot apply Tag.").Sha);
71+
return repository.Tags.Add(tagName, RetrieveHeadCommit(repository));
7272
}
7373

7474
/// <summary>
@@ -91,16 +91,16 @@ public static Tag ApplyTag(this IRepository repository, string tagName, string o
9191
/// <param name="message">The annotation message.</param>
9292
public static Tag ApplyTag(this IRepository repository, string tagName, Signature tagger, string message)
9393
{
94-
return ApplyTag(repository, tagName, RetrieveHeadCommitId(repository, "Cannot apply Tag.").Sha, tagger, message);
94+
return repository.Tags.Add(tagName, RetrieveHeadCommit(repository), tagger, message);
9595
}
9696

97-
private static ObjectId RetrieveHeadCommitId(IRepository repository, string message)
97+
private static Commit RetrieveHeadCommit(IRepository repository)
9898
{
9999
Commit commit = repository.Head.Tip;
100100

101-
Ensure.GitObjectIsNotNull(commit, "HEAD");
101+
Ensure.GitObjectIsNotNull(commit, "HEAD", m => new UnbornBranchException(m));
102102

103-
return commit.Id;
103+
return commit;
104104
}
105105

106106
/// <summary>

0 commit comments

Comments
 (0)