Skip to content

Commit 41ef06e

Browse files
committed
Introduce ObjectId.StartsWith()
1 parent 5264090 commit 41ef06e

File tree

3 files changed

+68
-24
lines changed

3 files changed

+68
-24
lines changed

LibGit2Sharp.Tests/ObjectIdFixture.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,25 @@ public void TryParse(string maybeSha, bool isValidSha)
136136
Assert.True(maybeSha.StartsWith(parsedObjectId.ToString(3)));
137137
Assert.Equal(maybeSha, parsedObjectId.ToString(42));
138138
}
139+
140+
[Theory]
141+
[InlineData(new byte[] { 0xde, 0xad, 0xbe }, 6, true)]
142+
[InlineData(new byte[] { 0xde, 0xad }, 4, true)]
143+
[InlineData(new byte[] { 0xde, 0xad }, 3, true)]
144+
[InlineData(new byte[] { 0xde, 0xad }, 2, true)]
145+
[InlineData(new byte[] { 0xde, 0xad }, 1, true)]
146+
[InlineData(new byte[] { 0xde, 0xaf }, 3, true)]
147+
[InlineData(new byte[] { 0xde, 0xff }, 2, true)]
148+
[InlineData(new byte[] { 0xdf, 0xff }, 1, true)]
149+
[InlineData(new byte[] { 0x98, 0x76 }, 4, false)]
150+
[InlineData(new byte[] { 0x98, 0x76 }, 3, false)]
151+
[InlineData(new byte[] { 0x98, 0x76 }, 2, false)]
152+
[InlineData(new byte[] { 0x98, 0x76 }, 1, false)]
153+
public void StartsWith(byte[] rawId, int len, bool expected)
154+
{
155+
var id = new ObjectId("deadbeef84650f067bd5703b6a59a8b3b3c99a09");
156+
157+
Assert.Equal(expected, id.StartsWith(rawId, len));
158+
}
139159
}
140160
}

LibGit2Sharp.Tests/OdbBackendFixture.cs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -198,30 +198,7 @@ public override int ReadPrefix(byte[] shortOid, int len, out byte[] oid, out Str
198198

199199
foreach (ObjectId objectId in m_objectIdToContent.Keys)
200200
{
201-
bool match = true;
202-
203-
int length = len >> 1;
204-
for (int i = 0; i < length; i++)
205-
{
206-
if (objectId.RawId[i] != shortOid[i])
207-
{
208-
match = false;
209-
break;
210-
}
211-
}
212-
213-
if (match && ((len & 1) == 1))
214-
{
215-
var a = objectId.RawId[length] >> 4;
216-
var b = shortOid[length] >> 4;
217-
218-
if (a != b)
219-
{
220-
match = false;
221-
}
222-
}
223-
224-
if (!match)
201+
if (!objectId.StartsWith(shortOid, len))
225202
{
226203
continue;
227204
}

LibGit2Sharp/ObjectId.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,5 +297,52 @@ private static bool LooksValid(string objectId, bool throwIfInvalid)
297297

298298
return objectId.All(c => hexDigits.Contains(c.ToString(CultureInfo.InvariantCulture)));
299299
}
300+
301+
/// <summary>
302+
/// Determine whether the beginning of this instance matches the
303+
/// <paramref name="len"/> first nibbles of <paramref name="rawId"/>.
304+
/// </summary>
305+
/// <param name="rawId">The byte array to compare the <see cref="ObjectId"/> against.</param>
306+
/// <param name="len">The number of nibbles from <paramref name="rawId"/> </param>
307+
/// <returns></returns>
308+
public bool StartsWith(byte[] rawId, int len)
309+
{
310+
Ensure.ArgumentNotNull(rawId, "rawId");
311+
312+
if (len < 1 || len > HexSize)
313+
{
314+
throw new ArgumentOutOfRangeException("len");
315+
}
316+
317+
if (len > rawId.Length * 2)
318+
{
319+
throw new ArgumentOutOfRangeException("len", "len exceeds the size of rawId");
320+
}
321+
322+
bool match = true;
323+
324+
int length = len >> 1;
325+
for (int i = 0; i < length; i++)
326+
{
327+
if (RawId[i] != rawId[i])
328+
{
329+
match = false;
330+
break;
331+
}
332+
}
333+
334+
if (match && ((len & 1) == 1))
335+
{
336+
var a = RawId[length] >> 4;
337+
var b = rawId[length] >> 4;
338+
339+
if (a != b)
340+
{
341+
match = false;
342+
}
343+
}
344+
345+
return match;
346+
}
300347
}
301348
}

0 commit comments

Comments
 (0)