Skip to content

Commit 87dccce

Browse files
committed
Merge pull request #964 from libgit2/jamill/rebase
Initial Rebase implementation
2 parents c25c6e4 + 3382549 commit 87dccce

22 files changed

+2034
-1
lines changed

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<Compile Include="FilterFixture.cs" />
6767
<Compile Include="GlobalSettingsFixture.cs" />
6868
<Compile Include="PatchStatsFixture.cs" />
69+
<Compile Include="RebaseFixture.cs" />
6970
<Compile Include="RefSpecFixture.cs" />
7071
<Compile Include="EqualityFixture.cs" />
7172
<Compile Include="RevertFixture.cs" />

LibGit2Sharp.Tests/RebaseFixture.cs

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

LibGit2Sharp.Tests/TestHelpers/Constants.cs

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

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

LibGit2Sharp/AfterRebaseStepInfo.cs

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

LibGit2Sharp/BeforeRebaseStepInfo.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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, long stepIndex, long totalStepCount)
20+
{
21+
StepInfo = stepInfo;
22+
StepIndex = stepIndex;
23+
TotalStepCount = totalStepCount;
24+
}
25+
26+
/// <summary>
27+
/// Information on the step that is about to be performed.
28+
/// </summary>
29+
public virtual RebaseStepInfo StepInfo { get; private set; }
30+
31+
/// <summary>
32+
/// The index of the step that is to be run.
33+
/// </summary>
34+
public virtual long StepIndex { get; private set; }
35+
36+
/// <summary>
37+
/// The total number of steps in the rebase operation.
38+
/// </summary>
39+
public virtual long TotalStepCount { get; private set; }
40+
}
41+
}

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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,61 @@ internal static extern int git_branch_remote_name(
190190
RepositorySafeHandle repo,
191191
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string canonical_branch_name);
192192

193+
[DllImport(libgit2)]
194+
internal static extern int git_rebase_init(
195+
out RebaseSafeHandle rebase,
196+
RepositorySafeHandle repo,
197+
GitAnnotatedCommitHandle branch,
198+
GitAnnotatedCommitHandle upstream,
199+
GitAnnotatedCommitHandle onto,
200+
GitRebaseOptions options);
201+
202+
[DllImport(libgit2)]
203+
internal static extern int git_rebase_open(
204+
out RebaseSafeHandle rebase,
205+
RepositorySafeHandle repo,
206+
GitRebaseOptions options);
207+
208+
[DllImport(libgit2)]
209+
internal static extern UIntPtr git_rebase_operation_entrycount(
210+
RebaseSafeHandle rebase);
211+
212+
[DllImport(libgit2)]
213+
internal static extern UIntPtr git_rebase_operation_current(
214+
RebaseSafeHandle rebase);
215+
216+
[DllImport(libgit2)]
217+
internal static extern IntPtr git_rebase_operation_byindex(
218+
RebaseSafeHandle rebase,
219+
UIntPtr index);
220+
221+
[DllImport(libgit2)]
222+
internal static extern int git_rebase_next(
223+
out IntPtr operation,
224+
RebaseSafeHandle rebase);
225+
226+
[DllImport(libgit2)]
227+
internal static extern int git_rebase_commit(
228+
ref GitOid id,
229+
RebaseSafeHandle rebase,
230+
SignatureSafeHandle author,
231+
SignatureSafeHandle committer,
232+
IntPtr message_encoding,
233+
IntPtr message);
234+
235+
[DllImport(libgit2)]
236+
internal static extern int git_rebase_abort(
237+
RebaseSafeHandle rebase);
238+
239+
[DllImport(libgit2)]
240+
internal static extern int git_rebase_finish(
241+
RebaseSafeHandle repo,
242+
SignatureSafeHandle signature);
243+
244+
[DllImport(libgit2)]
245+
internal static extern void git_rebase_free(
246+
IntPtr rebase);
247+
193248
[DllImport(libgit2)]
194249
internal static extern int git_remote_rename(
195250
ref GitStrArray problems,
@@ -1363,6 +1418,12 @@ internal static extern int git_signature_new(
13631418
long time,
13641419
int offset);
13651420

1421+
[DllImport(libgit2)]
1422+
internal static extern int git_signature_now(
1423+
out SignatureSafeHandle signature,
1424+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name,
1425+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string email);
1426+
13661427
[DllImport(libgit2)]
13671428
internal static extern int git_signature_dup(out IntPtr dest, IntPtr sig);
13681429

0 commit comments

Comments
 (0)