Skip to content

Commit a09f193

Browse files
committed
Allow notes retrieval by namespace and ObjectId
Might help scenarios like http://stackoverflow.com/questions/22468146/
1 parent c2bf697 commit a09f193

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

LibGit2Sharp.Tests/NoteFixture.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ public void CanRetrieveNotesFromAGitObject()
6565
}
6666
}
6767

68+
[Fact]
69+
public void CanRetrieveASpecificNoteFromAKnownNamespace()
70+
{
71+
using (var repo = new Repository(BareTestRepoPath))
72+
{
73+
var singleNote = repo.Notes["answer", new ObjectId("4a202b346bb0fb0db7eff3cffeb3c70babbd2045")];
74+
Assert.Equal("Nope\n", singleNote.Message);
75+
}
76+
}
77+
6878
[Fact]
6979
public void CanGetListOfNotesNamespaces()
7080
{

LibGit2Sharp/NoteCollection.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public virtual IEnumerable<Note> this[ObjectId id]
9494
Ensure.ArgumentNotNull(id, "id");
9595

9696
return NamespaceRefs
97-
.Select(ns => RetrieveNote(id, ns))
97+
.Select(ns => this[ns, id])
9898
.Where(n => n != null);
9999
}
100100
}
@@ -112,16 +112,28 @@ public virtual IEnumerable<Note> this[string @namespace]
112112
string canonicalNamespace = NormalizeToCanonicalName(@namespace);
113113

114114
return Proxy.git_note_foreach(repo.Handle, canonicalNamespace,
115-
(blobId,annotatedObjId) => RetrieveNote(annotatedObjId, canonicalNamespace));
115+
(blobId,annotatedObjId) => this[canonicalNamespace, annotatedObjId]);
116116
}
117117
}
118118

119-
internal Note RetrieveNote(ObjectId targetObjectId, string canonicalNamespace)
119+
/// <summary>
120+
/// Gets the <see cref="Note"/> associated with the specified objectId and the specified namespace.
121+
/// </summary>
122+
public virtual Note this[string @namespace, ObjectId id]
120123
{
121-
using (NoteSafeHandle noteHandle = Proxy.git_note_read(repo.Handle, canonicalNamespace, targetObjectId))
124+
get
122125
{
123-
return noteHandle == null ? null :
124-
Note.BuildFromPtr(noteHandle, UnCanonicalizeName(canonicalNamespace), targetObjectId);
126+
Ensure.ArgumentNotNull(id, "id");
127+
Ensure.ArgumentNotNull(@namespace, "@namespace");
128+
129+
string canonicalNamespace = NormalizeToCanonicalName(@namespace);
130+
131+
using (NoteSafeHandle noteHandle = Proxy.git_note_read(repo.Handle, canonicalNamespace, id))
132+
{
133+
return noteHandle == null
134+
? null
135+
: Note.BuildFromPtr(noteHandle, UnCanonicalizeName(canonicalNamespace), id);
136+
}
125137
}
126138
}
127139

@@ -179,7 +191,7 @@ public virtual Note Add(ObjectId targetId, string message, Signature author, Sig
179191

180192
Proxy.git_note_create(repo.Handle, author, committer, canonicalNamespace, targetId, message, true);
181193

182-
return RetrieveNote(targetId, canonicalNamespace);
194+
return this[canonicalNamespace, targetId];
183195
}
184196

185197
/// <summary>

0 commit comments

Comments
 (0)