8000 Deprecate ObjectId.StartsWith(byte[], int) · GiTechLab/libgit2sharp@c405b0f · GitHub
[go: up one dir, main page]

Skip to content

Commit c405b0f

Browse files
committed
Deprecate ObjectId.StartsWith(byte[], int)
1 parent e5a6ad5 commit c405b0f

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

LibGit2Sharp.Tests/ObjectIdFixture.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,19 @@ public void TryParse(string maybeSha, bool isValidSha)
138138
}
139139

140140
[Theory]
141-
[InlineData(new byte[] { 0xde, 0xad, 0xbe }, 6, true)]
142-
[InlineData(new byte[] { 0xde, 0xad }, 4, true)]
143 8000 -
[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)
141+
[InlineData("d", true)]
142+
[InlineData("dead", true)]
143+
[InlineData("deadbe", true)]
144+
[InlineData("DeAdBEE", true)]
145+
[InlineData("deff", false)]
146+
[InlineData("dfff", false)]
147+
[InlineData("9876", false)]
148+
[InlineData("This is not a valid short hexified sha!!!!!", false)]
149+
public void StartsWith(string shortSha, bool expected)
154150
{
155151
var id = new ObjectId("deadbeef84650f067bd5703b6a59a8b3b3c99a09");
156152

157-
Assert.Equal(expected, id.StartsWith(rawId, len));
153+
Assert.Equal(expected, id.StartsWith(shortSha));
158154
}
159155
}
160156
}

LibGit2Sharp.Tests/OdbBackendFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public override int ReadPrefix(string shortSha, out ObjectId id, out Stream data
229229

230230
foreach (ObjectId objectId in m_objectIdToContent.Keys)
231231
{
232-
if (!objectId.Sha.StartsWith(shortSha))
232+
if (!objectId.StartsWith(shortSha))
233233
{
234234
continue;
235235
}

LibGit2Sharp/ObjectId.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,13 @@ private static byte[] BuildReverseHexDigits()
231231
return bytes;
232232
}
233233

234-
internal static string ToString(byte[] id, int len)
234+
internal static string ToString(byte[] id, int lengthInNibbles)
235235
{
236236
// Inspired from http://stackoverflow.com/questions/623104/c-byte-to-hex-string/3974535#3974535
237237

238-
var c = new char[len];
238+
var c = new char[lengthInNibbles];
239239

240-
for (int i = 0; i < (len & -2); i++)
240+
for (int i = 0; i < (lengthInNibbles & -2); i++)
241241
{
242242
int index0 = i >> 1;
243243
var b = ((byte)(id[index0] >> 4));
@@ -247,11 +247,11 @@ internal static string ToString(byte[] id, int len)
247247
c[i] = hexDigits[b];
248248
}
249249

250-
if ((len & 1) == 1)
250+
if ((lengthInNibbles & 1) == 1)
251251
{
252-
int index0 = len >> 1;
252+
int index0 = lengthInNibbles >> 1;
253253
var b = ((byte)(id[index0] >> 4));
254-
c[len - 1] = hexDigits[b];
254+
c[lengthInNibbles - 1] = hexDigits[b];
255255
}
256256

257257
return new string(c);
@@ -312,6 +312,7 @@ private static bool LooksValid(string objectId, bool throwIfInvalid)
312312
/// <param name="rawId">The byte array to compare the <see cref="ObjectId"/> against.</param>
313313
/// <param name="len">The number of nibbles from <paramref name="rawId"/> </param>
314314
/// <returns></returns>
315+
[Obsolete("This method will be removed in the next release. Please use one of the StartsWith(string) overload instead.")]
315316
public bool StartsWith(byte[] rawId, int len)
316317
{
317318
Ensure.ArgumentNotNull(rawId, "rawId");
@@ -351,5 +352,21 @@ public bool StartsWith(byte[] rawId, int len)
351352

352353
return match;
353354
}
355+
356+
/// <summary>
357+
/// Determine whether <paramref name="shortSha"/> matches the hexified
358+
/// representation of the first nibbles of this instance.
359+
/// <para>
360+
/// Comparison is made in a case insensitive-manner.
361+
/// </para>
362+
/// </summary>
363+
/// <returns>True if this instance starts with <paramref name="shortSha"/>,
364+
/// false otherwise.</returns>
365+
public bool StartsWith(string shortSha)
366+
{
367+
Ensure.ArgumentNotNullOrEmptyString(shortSha, "shortSha");
368+
369+
return Sha.StartsWith(shortSha, StringComparison.OrdinalIgnoreCase);
370+
}
354371
}
355372
}

0 commit comments

Comments
 (0)
0