Skip to content

Commit 32f3409

Browse files
committed
Return TransientIndex from ObjectDatabase.MergeCommitsIntoIndex
1 parent 504f95e commit 32f3409

File tree

4 files changed

+55
-20
lines changed

4 files changed

+55
-20
lines changed

LibGit2Sharp.Tests/MergeFixture.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -910,9 +910,11 @@ public void CanMergeIntoIndex()
910910
{
911911
var master = repo.Lookup<Commit>("master");
912912

913-
Index index = repo.ObjectDatabase.MergeCommitsIntoIndex(master, master, null);
914-
var tree = index.WriteToTree();
915-
Assert.Equal(master.Tree.Id, tree.Id);
913+
using (TransientIndex index = repo.ObjectDatabase.MergeCommitsIntoIndex(master, master, null))
914+
{
915+
var tree = index.WriteToTree();
916+
Assert.Equal(master.Tree.Id, tree.Id);
917+
}
916918
}
917919
}
918920

@@ -925,22 +927,24 @@ public void CanMergeIntoIndexWithConflicts()
925927
var master = repo.Lookup<Commit>("master");
926928
var branch = repo.Lookup<Commit>("conflicts");
927929

928-
Index index = repo.ObjectDatabase.MergeCommitsIntoIndex(branch, master, null);
929-
Assert.False(index.IsFullyMerged);
930+
using (TransientIndex index = repo.ObjectDatabase.MergeCommitsIntoIndex(branch, master, null))
931+
{
932+
Assert.False(index.IsFullyMerged);
930933

931-
var conflict = index.Conflicts.First();
934+
var conflict = index.Conflicts.First();
932935

933-
//Resolve the conflict by taking the blob from branch
934-
var blob = repo.Lookup<Blob>(conflict.Ours.Id);
935-
//Add() does not remove conflict entries for the same path, so they must be explicitly removed first.
936-
index.Remove(conflict.Ours.Path);
937-
index.Add(blob, conflict.Ours.Path, Mode.NonExecutableFile);
936+
//Resolve the conflict by taking the blob from branch
937+
var blob = repo.Lookup<Blob>(conflict.Ours.Id);
938+
//Add() does not remove conflict entries for the same path, so they must be explicitly removed first.
939+
index.Remove(conflict.Ours.Path);
940+
index.Add(blob, conflict.Ours.Path, Mode.NonExecutableFile);
938941

939-
Assert.True(index.IsFullyMerged);
940-
var tree = index.WriteToTree();
942+
Assert.True(index.IsFullyMerged);
943+
var tree = index.WriteToTree();
941944

942-
//Since we took the conflicted blob from the branch, the merged result should be the same as the branch.
943-
Assert.Equal(branch.Tree.Id, tree.Id);
945+
//Since we took the conflicted blob from the branch, the merged result should be the same as the branch.
946+
Assert.Equal(branch.Tree.Id, tree.Id);
947+
}
944948
}
945949
}
946950

LibGit2Sharp/Index.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ internal Index(IndexHandle handle, Repository repo)
3131
this.repo = repo;
3232
this.handle = handle;
3333
conflicts = new ConflictCollection(this);
34-
35-
repo.RegisterForCleanup(handle);
3634
}
3735

3836
internal Index(Repository repo)
3937
: this(Proxy.git_repository_index(repo.Handle), repo)
4038
{
39+
repo.RegisterForCleanup(handle);
4140
}
4241

4342
internal Index(Repository repo, string indexPath)

LibGit2Sharp/ObjectDatabase.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -856,8 +856,9 @@ public virtual PackBuilderResults Pack(PackBuilderOptions options, Action<PackBu
856856
/// <param name="ours">The first tree</param>
857857
/// <param name="theirs">The second tree</param>
858858
/// <param name="options">The <see cref="MergeTreeOptions"/> controlling the merge</param>
859-
/// <returns>The <see cref="Index"/> containing the merged trees and any conflicts, or null if the merge stopped early due to conflicts</returns>
860-
public virtual Index MergeCommitsIntoIndex(Commit ours, Commit theirs, MergeTreeOptions options)
859+
/// <returns>The <see cref="TransientIndex"/> containing the merged trees and any conflicts, or null if the merge stopped early due to conflicts.
860+
/// The index must be disposed by the caller.</returns>
861+
public virtual TransientIndex MergeCommitsIntoIndex(Commit ours, Commit theirs, MergeTreeOptions options)
861862
{
862863
Ensure.ArgumentNotNull(ours, "ours");
863864
Ensure.ArgumentNotNull(theirs, "theirs");
@@ -874,7 +875,7 @@ public virtual Index MergeCommitsIntoIndex(Commit ours, Commit theirs, MergeTree
874875
}
875876
return null;
876877
}
877-
var result = new Index(indexHandle, repo);
878+
var result = new TransientIndex(indexHandle, repo);
878879
return result;
879880
}
880881

LibGit2Sharp/TransientIndex.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using LibGit2Sharp.Core.Handles;
3+
4+
namespace LibGit2Sharp
5+
{
6+
/// <summary>
7+
/// An implementation of <see cref="Index"/> with disposal managed by the caller
8+
/// (instead of automatically disposing when the repository is disposed)
9+
/// </summary>
10+
public class TransientIndex: Index, IDisposable
11+
{
12+
/// <summary>
13+
/// Needed for mocking purposes.
14+
/// </summary>
15+
protected TransientIndex()
16+
{ }
17+
18+
internal TransientIndex(IndexHandle handle, Repository repo)
19+
: base(handle, repo)
20+
{
21+
}
22+
23+
/// <summary>
24+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
25+
/// </summary>
26+
public void Dispose()
27+
{
28+
this.Handle.SafeDispose();
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)