Skip to content

Commit 1c5e7c9

Browse files
dahlbyknulltoken
authored andcommitted
Add notes retrieval
1 parent a400fa5 commit 1c5e7c9

File tree

7 files changed

+395
-0
lines changed

7 files changed

+395
-0
lines changed

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<Compile Include="ConfigurationFixture.cs" />
4747
<Compile Include="AttributesFixture.cs" />
4848
<Compile Include="CommitAncestorFixture.cs" />
49+
<Compile Include="NoteFixture.cs" />
4950
<Compile Include="DiffBlobToBlobFixture.cs" />
5051
<Compile Include="DiffTreeToTargetFixture.cs" />
5152
<Compile Include="ObjectDatabaseFixture.cs" />

LibGit2Sharp.Tests/NoteFixture.cs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System.Linq;
2+
using LibGit2Sharp.Tests.TestHelpers;
3+
using Xunit;
4+
5+
namespace LibGit2Sharp.Tests
6+
{
7+
public class NoteFixture : BaseFixture
8+
{
9+
[Fact]
10+
public void RetrievingNotesFromANonExistingGitObjectYieldsNoResult()
11+
{
12+
using (var repo = new Repository(BareTestRepoPath))
13+
{
14+
var notes = repo.Notes[ObjectId.Zero];
15+
16+
Assert.Equal(0, notes.Count());
17+
}
18+
}
19+
20+
[Fact]
21+
public void RetrievingNotesFromAGitObjectWhichHasNoNoteYieldsNoResult()
22+
{
23+
using (var repo = new Repository(BareTestRepoPath))
24+
{
25+
var notes = repo.Notes[new ObjectId("4c062a6361ae6959e06292c1fa5e2822d9c96345")];
26+
27+
Assert.Equal(0, notes.Count());
28+
}
29+
}
30+
31+
/*
32+
* $ git show 4a202 --show-notes=*
33+
* commit 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
34+
* Author: Scott Chacon <schacon@gmail.com>
35+
* Date: Mon May 24 10:19:04 2010 -0700
36+
*
37+
* a third commit
38+
*
39+
* Notes:
40+
* Just Note, don't you understand?
41+
*
42+
* Notes (answer):
43+
* Nope
44+
*
45+
* Notes (answer2):
46+
* Not Nope, Note!
47+
*/
48+
[Fact]
49+
public void CanRetrieveNotesFromAGitObject()
50+
{
51+
var expectedMessages = new [] { "Just Note, don't you understand?\n", "Nope\n", "Not Nope, Note!\n" };
52+
53+
using (var repo = new Repository(BareTestRepoPath))
54+
{
55+
var notes = repo.Notes[new ObjectId("4a202b346bb0fb0db7eff3cffeb3c70babbd2045")];
56+
57+
Assert.NotNull(notes);
58+
Assert.Equal(3, notes.Count());
59+
Assert.Equal(expectedMessages, notes.Select(n => n.Message));
60+
}
61+
}
62+
63+
[Fact]
64+
public void CanGetListOfNotesNamespaces()
65+
{
66+
var expectedNamespaces = new[] { "commits", "answer", "answer2" };
67+
68+
using (var repo = new Repository(BareTestRepoPath))
69+
{
70+
Assert.Equal(expectedNamespaces, repo.Notes.Namespaces);
71+
Assert.Equal(repo.Notes.DefaultNamespace, repo.Notes.Namespaces.First());
72+
}
73+
}
74+
75+
/*
76+
* $ git show 4a202b346bb0fb0db7eff3cffeb3c70babbd2045 --show-notes=*
77+
* commit 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
78+
* Author: Scott Chacon <schacon@gmail.com>
79+
* Date: Mon May 24 10:19:04 2010 -0700
80+
*
81+
* a third commit
82+
*
83+
* Notes:
84+
* Just Note, don't you understand?
85+
*
86+
* Notes (answer):
87+
* Nope
88+
*
89+
* Notes (answer2):
90+
* Not Nope, Note!
91+
*/
92+
[Fact]
93+
public void CanAccessNotesFromACommit()
94+
{
95+
var expectedNamespaces = new[] { "Just Note, don't you understand?\n", "Nope\n", "Not Nope, Note!\n" };
96+
97+
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
98+
using (var repo = new Repository(path.RepositoryPath))
99+
{
100+
var commit = repo.Lookup<Commit>("4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
101+
102+
Assert.Equal(expectedNamespaces, commit.Notes.Select(n => n.Message));
103+
}
104+
}
105+
}
106+
}

LibGit2Sharp/Commit.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ public class Commit : GitObject
1414
private readonly Lazy<IEnumerable<Commit>> parents;
1515
private readonly Lazy<Tree> tree;
1616
private readonly Lazy<string> shortMessage;
17+
private readonly Lazy<IEnumerable<Note>> notes;
1718

1819
internal Commit(ObjectId id, ObjectId treeId, Repository repo)
1920
: base(id)
2021
{
2122
tree = new Lazy<Tree>(() => repo.Lookup<Tree>(treeId));
2223
parents = new Lazy<IEnumerable<Commit>>(() => RetrieveParentsOfCommit(id));
2324
shortMessage = new Lazy<string>(ExtractShortMessage);
25+
notes = new Lazy<IEnumerable<Note>>(() => RetrieveNotesOfCommit(id));
2426
this.repo = repo;
2527
}
2628

@@ -102,6 +104,14 @@ public int ParentsCount
102104
}
103105
}
104106

107+
/// <summary>
108+
/// Gets the notes of this commit.
109+
/// </summary>
110+
public IEnumerable<Note> Notes
111+
{
112+
get { return notes.Value; }
113+
}
114+
105115
private IEnumerable<Commit> RetrieveParentsOfCommit(ObjectId oid)
106116
{
107117
using (var obj = new ObjectSafeWrapper(oid, repo))
@@ -117,6 +127,11 @@ private IEnumerable<Commit> RetrieveParentsOfCommit(ObjectId oid)
117127
}
118128
}
119129

130+
private IEnumerable<Note> RetrieveNotesOfCommit(ObjectId oid)
131+
{
132+
return repo.Notes[oid];
133+
}
134+
120135
internal static Commit BuildFromPtr(GitObjectSafeHandle obj, ObjectId id, Repository repo)
121136
{
122137
ObjectId treeId = NativeMethods.git_commit_tree_oid(obj).MarshalAsObjectId();

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
<Compile Include="DetachedHead.cs" />
8181
<Compile Include="Diff.cs" />
8282
<Compile Include="DiffTarget.cs" />
83+
<Compile Include="NoteCollection.cs" />
84+
<Compile Include="Note.cs" />
8385
<Compile Include="TreeChanges.cs" />
8486
<Compile Include="TreeEntryChanges.cs" />
8587
<Compile Include="LibGit2Exception.cs" />

LibGit2Sharp/Note.cs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System;
2+
using LibGit2Sharp.Core;
3+
using LibGit2Sharp.Core.Compat;
4+
using LibGit2Sharp.Core.Handles;
5+
6+
namespace LibGit2Sharp
7+
{
8+
/// <summary>
9+
/// A note, attached to a given <see cref = "GitObject"/>.
10+
/// </summary>
11+
public class Note
12+
{
13+
private Note(ObjectId blobId, string message, ObjectId targetObjectId, string @namespace)
14+
{
15+
BlobId = blobId;
16+
Namespace = @namespace;
17+
Message = message;
18+
TargetObjectId = targetObjectId;
19+
}
20+
21+
/// <summary>
22+
/// The <see cref = "ObjectId"/> of the blob containing the note message.
23+
/// </summary>
24+
public ObjectId BlobId { get; private set; }
25+
26+
/// <summary>
27+
/// The message.
28+
/// </summary>
29+
public string Message { get; private set; }
30+
31+
/// <summary>
32+
/// The namespace with which this note is associated.
33+
/// <para>This is the abbreviated namespace (e.g.: commits), and not the canonical namespace (e.g.: refs/notes/commits).</para>
34+
/// </summary>
35+
public string Namespace { get; private set; }
36+
37+
/// <summary>
38+
/// The <see cref = "ObjectId"/> of the target object.
39+
/// </summary>
40+
public ObjectId TargetObjectId { get; private set; }
41+
42+
internal static Note BuildFromPtr(Repository repo, string @namespace, ObjectId targetObjectId, NoteSafeHandle note)
43+
{
44+
ObjectId oid = NativeMethods.git_note_oid(note).MarshalAsObjectId();
45+
string message = NativeMethods.git_note_message(note);
46+
47+
return new Note(oid, message, targetObjectId, @namespace);
48+
}
49+
50+
private static readonly LambdaEqualityHelper<Note> equalityHelper =
51+
new LambdaEqualityHelper<Note>(new Func<Note, object>[] { x => x.BlobId, x => x.TargetObjectId, x => x.Namespace });
52+
53+
/// <summary>
54+
/// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "Note" />.
55+
/// </summary>
56+
/// <param name = "obj">The <see cref = "Object" /> to compare with the current <see cref = "Note" />.</param>
57+
/// <returns>True if the specified <see cref = "Object" /> is equal to the current <see cref = "Note" />; otherwise, false.</returns>
58+
public override bool Equals(object obj)
59+
{
60+
return Equals(obj as Note);
61+
}
62+
63+
/// <summary>
64+
/// Determines whether the specified <see cref = "Note" /> is equal to the current <see cref = "Note" />.
65+
/// </summary>
66+
/// <param name = "other">The <see cref = "Note" /> to compare with the current <see cref = "Note" />.</param>
67+
/// <returns>True if the specified <see cref = "Note" /> is equal to the current <see cref = "Note" />; otherwise, false.</returns>
68+
public bool Equals(Note other)
69+
{
70+
return equalityHelper.Equals(this, other);
71+
}
72+
73+
/// <summary>
74+
/// Returns the hash code for this instance.
75+
/// </summary>
76+
/// <returns>A 32-bit signed integer hash code.</returns>
77+
public override int GetHashCode()
78+
{
79+
return equalityHelper.GetHashCode(this);
80+
}
81+
82+
/// <summary>
83+
/// Tests if two <see cref = "Note" /> are equal.
84+
/// </summary>
85+
/// <param name = "left">First <see cref = "Note" /> to compare.</param>
86+
/// <param name = "right">Second <see cref = "Note" /> to compare.</param>
87+
/// <returns>True if the two objects are equal; false otherwise.</returns>
88+
public static bool operator ==(Note left, Note right)
89+
{
90+
return Equals(left, right);
91+
}
92+
93+
/// <summary>
94+
/// Tests if two <see cref = "Note" /> are different.
95+
/// </summary>
96+
/// <param name = "left">First <see cref = "Note" /> to compare.</param>
97+
/// <param name = "right">Second <see cref = "Note" /> to compare.</param>
98+
/// <returns>True if the two objects are different; false otherwise.</returns>
99+
public static bool operator !=(Note left, Note right)
100+
{
101+
return !Equals(left, right);
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)