Skip to content

Commit 5264090

Browse files
committed
Slight OdbBackendFixture refactoring
1 parent 8d9fdc9 commit 5264090

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

LibGit2Sharp.Tests/OdbBackendFixture.cs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,21 @@ public override int Read(ObjectId oid, out Stream data, out ObjectType objectTyp
171171

172172
MockGitObject gitObject;
173173

174-
if (m_objectIdToContent.TryGetValue(oid, out gitObject))
174+
if (!m_objectIdToContent.TryGetValue(oid, out gitObject))
175175
{
176-
data = Allocate(gitObject.Length);
177-
178-
foreach (var chunk in gitObject.Data)
179-
{
180-
data.Write(chunk, 0, chunk.Length);
181-
}
176+
return (int) ReturnCode.GIT_ENOTFOUND;
177+
}
182178

183-
objectType = gitObject.ObjectType;
179+
data = Allocate(gitObject.Length);
184180

185-
return (int)ReturnCode.GIT_OK;
181+
foreach (var chunk in gitObject.Data)
182+
{
183+
data.Write(chunk, 0, chunk.Length);
186184
}
187185

188-
return (int)ReturnCode.GIT_ENOTFOUND;
186+
objectType = gitObject.ObjectType;
187+
188+
return (int)ReturnCode.GIT_OK;
189189
}
190190

191191
public override int ReadPrefix(byte[] shortOid, int len, out byte[] oid, out Stream data, out ObjectType objectType)
@@ -194,16 +194,16 @@ public override int ReadPrefix(byte[] shortOid, int len, out byte[] oid, out Str
194194
data = null;
195195
objectType = default(ObjectType);
196196

197-
MockGitObject gitObjectAlreadyFound = null;
197+
ObjectId matchingKey = null;
198198

199-
foreach (MockGitObject gitObject in m_objectIdToContent.Values)
199+
foreach (ObjectId objectId in m_objectIdToContent.Keys)
200200
{
201201
bool match = true;
202202

203203
int length = len >> 1;
204204
for (int i = 0; i < length; i++)
205205
{
206-
if (gitObject.ObjectId.RawId[i] != shortOid[i])
206+
if (objectId.RawId[i] != shortOid[i])
207207
{
208208
match = false;
209209
break;
@@ -212,7 +212,7 @@ public override int ReadPrefix(byte[] shortOid, int len, out byte[] oid, out Str
212212

213213
if (match && ((len & 1) == 1))
214214
{
215-
var a = gitObject.ObjectId.RawId[length] >> 4;
215+
var a = objectId.RawId[length] >> 4;
216216
var b = shortOid[length] >> 4;
217217

218218
if (a != b)
@@ -226,30 +226,29 @@ public override int ReadPrefix(byte[] shortOid, int len, out byte[] oid, out Str
226226
continue;
227227
}
228228

229-
if (null != gitObjectAlreadyFound)
229+
if (matchingKey != null)
230230
{
231231
return (int)ReturnCode.GIT_EAMBIGUOUS;
232232
}
233233

234-
gitObjectAlreadyFound = gitObject;
234+
matchingKey = objectId;
235235
}
236236

237-
if (null != gitObjectAlreadyFound)
237+
if (matchingKey == null)
238238
{
239-
oid = gitObjectAlreadyFound.ObjectId.RawId;
240-
objectType = gitObjectAlreadyFound.ObjectType;
241-
242-
data = Allocate(gitObjectAlreadyFound.Length);
239+
return (int)ReturnCode.GIT_ENOTFOUND;
240+
}
243241

244-
foreach (var chunk in gitObjectAlreadyFound.Data)
245-
{
246-
data.Write(chunk, 0, chunk.Length);
247-
}
242+
int ret = Read(matchingKey, out data, out objectType);
248243

249-
return (int)ReturnCode.GIT_OK;
244+
if (ret != (int)ReturnCode.GIT_OK)
245+
{
246+
return ret;
250247
}
251248

252-
return (int)ReturnCode.GIT_ENOTFOUND;
249+
oid = matchingKey.RawId;
250+
251+
return (int)ReturnCode.GIT_OK;
253252
}
254253

255254
public override int Write(ObjectId oid, Stream dataStream, long length, ObjectType objectType)

0 commit comments

Comments
 (0)