Skip to content

Commit 8220ec6

Browse files
yorahnulltoken
authored andcommitted
Add notes list
1 parent b475cbf commit 8220ec6

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

LibGit2Sharp.Tests/NoteFixture.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using LibGit2Sharp.Core;
4+
using LibGit2Sharp.Core.Compat;
45
using LibGit2Sharp.Tests.TestHelpers;
56
using Xunit;
67

@@ -223,5 +224,20 @@ public void RemovingANonExistingNoteDoesntThrow()
223224
repo.Notes.Delete(commit.Id, signatureNullToken, signatureYorah, "answer2");
224225
}
225226
}
227+
228+
[Fact]
229+
public void CanRetrieveTheListOfNotesForAGivenNamespace()
230+
{
231+
var expectedNotes = new[] { new Tuple<string, string>("1a550e416326cdb4a8e127a04dd69d7a01b11cf4", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"),
232+
new Tuple<string, string>("272a41cf2b22e57f2bc5bf6ef37b63568cd837e4", "8496071c1b46c854b31185ea97743be6a8774479") };
233+
234+
using (var repo = new Repository(BareTestRepoPath))
235+
{
236+
Assert.Equal(expectedNotes, repo.Notes["commits"].Select(n => new Tuple<string, string>(n.BlobId.Sha, n.TargetObjectId.Sha)).ToArray());
237+
238+
Assert.Equal("commits", repo.Notes.DefaultNamespace);
239+
Assert.Equal(expectedNotes, repo.Notes.Select(n => new Tuple<string, string>(n.BlobId.Sha, n.TargetObjectId.Sha)).ToArray());
240+
}
241+
}
226242
}
227243
}

LibGit2Sharp/NoteCollection.cs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal NoteCollection(Repository repo)
3232
/// <returns>An <see cref = "IEnumerator{T}" /> object that can be used to iterate through the collection.</returns>
3333
public IEnumerator<Note> GetEnumerator()
3434
{
35-
throw new NotImplementedException();
35+
return this[DefaultNamespace].GetEnumerator();
3636
}
3737

3838
/// <summary>
@@ -92,16 +92,33 @@ public IEnumerable<Note> this[ObjectId id]
9292
}
9393
}
9494

95-
internal Note RetrieveNote(ObjectId id, string canonicalNamespace)
95+
/// <summary>
96+
/// Gets the collection of <see cref = "Note"/> associated with the specified namespace.
97+
/// <para>This is similar to the 'get notes list' command.</para>
98+
/// </summary>
99+
public IEnumerable<Note> this[string @namespace]
100+
{
101+
get
102+
{
103+
Ensure.ArgumentNotNull(@namespace, "@namespace");
104+
105+
string canonicalNamespace = NormalizeToCanonicalName(@namespace);
106+
var notesOidRetriever = new NotesOidRetriever(repo, canonicalNamespace);
107+
108+
return notesOidRetriever.Retrieve().Select(oid => RetrieveNote(new ObjectId(oid), canonicalNamespace));
109+
}
110+
}
111+
112+
internal Note RetrieveNote(ObjectId targetObjectId, string canonicalNamespace)
96113
{
97-
using (NoteSafeHandle noteHandle = BuildNoteSafeHandle(id, canonicalNamespace))
114+
using (NoteSafeHandle noteHandle = BuildNoteSafeHandle(targetObjectId, canonicalNamespace))
98115
{
99116
if (noteHandle == null)
100117
{
101118
return null;
102119
}
103120

104-
return Note.BuildFromPtr(repo, UnCanonicalizeName(canonicalNamespace), id, noteHandle);
121+
return Note.BuildFromPtr(repo, UnCanonicalizeName(canonicalNamespace), targetObjectId, noteHandle);
105122
}
106123
}
107124

@@ -184,7 +201,7 @@ public Note Create(ObjectId targetId, string message, Signature author, Signatur
184201
Ensure.Success(NativeMethods.git_note_create(out noteOid, repo.Handle, authorHandle, committerHandle, canonicalNamespace, ref oid, message));
185202
}
186203

187-
return this[targetId].First(n => n.Namespace == @namespace);
204+
return RetrieveNote(targetId, canonicalNamespace);
188205
}
189206

190207
/// <summary>
@@ -219,5 +236,27 @@ public void Delete(ObjectId targetId, Signature author, Signature committer, str
219236

220237
Ensure.Success(res);
221238
}
239+
240+
private class NotesOidRetriever
241+
{
242+
private readonly List<GitOid> notesOid = new List<GitOid>();
243+
244+
internal NotesOidRetriever(Repository repo, string canonicalNamespace)
245+
{
246+
Ensure.Success(NativeMethods.git_note_foreach(repo.Handle, canonicalNamespace, NoteListCallBack, IntPtr.Zero));
247+
}
248+
249+
private int NoteListCallBack(GitNoteData noteData, IntPtr intPtr)
250+
{
251+
notesOid.Add(noteData.TargetOid);
252+
253+
return 0;
254+
}
255+
256+
public IEnumerable<GitOid> Retrieve()
257+
{
258+
return notesOid;
259+
}
260+
}
222261
}
223262
}

0 commit comments

Comments
 (0)