Skip to content

Commit 0cff6b0

Browse files
committed
Initial Rebase implementation
1 parent ba6b4b4 commit 0cff6b0

21 files changed

+1737
-6
lines changed

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<Compile Include="FileHistoryFixture.cs" />
6464
<Compile Include="GlobalSettingsFixture.cs" />
6565
<Compile Include="PatchStatsFixture.cs" />
66+
<Compile Include="RebaseFixture.cs" />
6667
<Compile Include="RefSpecFixture.cs" />
6768
<Compile Include="EqualityFixture.cs" />
6869
<Compile Include="RevertFixture.cs" />
@@ -158,4 +159,4 @@
158159
<Target Name="AfterBuild">
159160
</Target>
160161
-->
161-
</Project>
162+
</Project>

LibGit2Sharp.Tests/RebaseFixture.cs

Lines changed: 594 additions & 0 deletions
Large diffs are not rendered by default.

LibGit2Sharp.Tests/TestHelpers/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static class Constants
1212
public const string UnknownSha = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
1313
public static readonly Identity Identity = new Identity("A. U. Thor", "thor@valhalla.asgard.com");
1414
public static readonly Signature Signature = new Signature(Identity, new DateTimeOffset(2011, 06, 16, 10, 58, 27, TimeSpan.FromHours(2)));
15+
public static readonly Signature Signature2 = new Signature("nulltoken", "emeric.fermas@gmail.com", DateTimeOffset.Parse("Wed, Dec 14 2011 08:29:03 +0100"));
1516

1617
// Populate these to turn on live credential tests: set the
1718
// PrivateRepoUrl to the URL of a repository that requires

LibGit2Sharp/AfterRebaseStepInfo.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace LibGit2Sharp
7+
{
8+
/// <summary>
9+
/// Information about a rebase step that was just completed.
10+
/// </summary>
11+
public class AfterRebaseStepInfo
12+
{
13+
/// <summary>
14+
/// Needed for mocking.
15+
/// </summary>
16+
protected AfterRebaseStepInfo()
17+
{ }
18+
19+
internal AfterRebaseStepInfo(RebaseStepInfo stepInfo, Commit commit)
20+
{
21+
StepInfo = stepInfo;
22+
Commit = commit;
23+
WasPatchAlreadyApplied = false;
24+
}
25+
26+
/// <summary>
27+
/// Constructor to call when the patch has already been applied for this step.
28+
/// </summary>
29+
/// <param name="stepInfo"></param>
30+
internal AfterRebaseStepInfo(RebaseStepInfo stepInfo)
31+
: this (stepInfo, null)
32+
{
33+
WasPatchAlreadyApplied = true;
34+
}
35+
36+
/// <summary>
37+
/// The info on the completed step.
38+
/// </summary>
39+
public virtual RebaseStepInfo StepInfo { get; private set; }
40+
41+
/// <summary>
42+
/// The commit generated by the step, if any.
43+
/// </summary>
44+
public virtual Commit Commit { get; private set; }
45+
46+
/// <summary>
47+
/// Was the changes for this step already applied. If so,
48+
/// <see cref="AfterRebaseStepInfo.Commit"/> will be null.
49+
/// </summary>
50+
public virtual bool WasPatchAlreadyApplied { get; private set; }
51+
}
52+
}

LibGit2Sharp/BeforeRebaseStepInfo.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace LibGit2Sharp
7+
{
8+
/// <summary>
9+
/// Information about a rebase step that is about to be performed.
10+
/// </summary>
11+
public class BeforeRebaseStepInfo
12+
{
13+
/// <summary>
14+
/// Needed for mocking.
15+
/// </summary>
16+
protected BeforeRebaseStepInfo()
17+
{ }
18+
19+
internal BeforeRebaseStepInfo(RebaseStepInfo stepInfo)
20+
{
21+
StepInfo = stepInfo;
22+
}
23+
24+
/// <summary>
25+
/// Information on the step that is about to be performed.
26+
/// </summary>
27+
public virtual RebaseStepInfo StepInfo { get; private set; }
28+
}
29+
}

LibGit2Sharp/Core/GitOid.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,13 @@ public static implicit operator ObjectId(GitOid? oid)
2727
{
2828
return oid == null ? null : new ObjectId(oid.Value);
2929
}
30+
31+
/// <summary>
32+
/// Static convenience property to return an id (all zeros).
33+
/// </summary>
34+
public static GitOid Empty
35+
{
36+
get { return new GitOid(); }
37+
}
3038
}
3139
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace LibGit2Sharp.Core
5+
{
6+
[StructLayout(LayoutKind.Sequential)]
7+
internal class GitRebaseOperation
8+
{
9+
internal RebaseStepOperation type;
10+
internal GitOid id;
11+
internal IntPtr exec;
12+
}
13+
}

LibGit2Sharp/Core/GitRebaseOptions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace LibGit2Sharp.Core
5+
{
6+
[StructLayout(LayoutKind.Sequential)]
7+
internal class GitRebaseOptions
8+
{
9+
public uint version = 1;
10+
11+
public int quiet;
12+
13+
public IntPtr rewrite_notes_ref;
14+
15+
public GitCheckoutOpts checkout_options = new GitCheckoutOpts { version = 1 };
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using LibGit2Sharp.Core.Handles;
2+
3+
namespace LibGit2Sharp.Core
4+
{
5+
internal class RebaseSafeHandle : SafeHandleBase
6+
{
7+
protected override bool ReleaseHandleImpl()
8+
{
9+
Proxy.git_rebase_free(handle);
10+
return true;
11+
}
12+
}
13+
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,61 @@ internal static extern int git_branch_remote_name(
191191
RepositorySafeHandle repo,
192192
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string canonical_branch_name);
193193

194+
[DllImport(libgit2)]
195+
internal static extern int git_rebase_init(
196+
out RebaseSafeHandle rebase,
197+
RepositorySafeHandle repo,
198+
GitAnnotatedCommitHandle branch,
199+
GitAnnotatedCommitHandle upstream,
200+
GitAnnotatedCommitHandle onto,
201+
GitRebaseOptions options);
202+
203+
[DllImport(libgit2)]
204+
internal static extern int git_rebase_open(
205+
out RebaseSafeHandle rebase,
206+
RepositorySafeHandle repo,
207+
GitRebaseOptions options);
208+
209+
[DllImport(libgit2)]
210+
internal static extern UIntPtr git_rebase_operation_entrycount(
211+
RebaseSafeHandle rebase);
212+
213+
[DllImport(libgit2)]
214+
internal static extern UIntPtr git_rebase_operation_current(
215+
RebaseSafeHandle rebase);
216+
217+
[DllImport(libgit2)]
218+
internal static extern IntPtr git_rebase_operation_byindex(
219+
RebaseSafeHandle rebase,
220+
UIntPtr index);
221+
222+
[DllImport(libgit2)]
223+
internal static extern int git_rebase_next(
224+
out IntPtr operation,
225+
RebaseSafeHandle rebase);
226+
227+
[DllImport(libgit2)]
228+
internal static extern int git_rebase_commit(
229+
ref GitOid id,
230+
RebaseSafeHandle rebase,
231+
SignatureSafeHandle author,
232+
SignatureSafeHandle committer,
233+
IntPtr message_encoding,
234+
IntPtr message);
235+
236+
[DllImport(libgit2)]
237+
internal static extern int git_rebase_abort(
238+
RebaseSafeHandle rebase);
239+
240+
[DllImport(libgit2)]
241+
internal static extern int git_rebase_finish(
242+
RebaseSafeHandle repo,
243+
SignatureSafeHandle signature);
244+
245+
[DllImport(libgit2)]
246+
internal static extern void git_rebase_free(
247+
IntPtr rebase);
248+
194249
[DllImport(libgit2)]
195250
internal static extern int git_remote_rename(
196251
ref GitStrArray problems,
@@ -202,7 +257,6 @@ internal delegate int git_remote_rename_problem_cb(
202257
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(LaxUtf8NoCleanupMarshaler))] string problematic_refspec,
203258
IntPtr payload);
204259

205-
206260
[DllImport(libgit2)]
207261
internal static extern int git_branch_upstream_name(
208262
GitBuf buf,

0 commit comments

Comments
 (0)