Skip to content

Commit 5607562

Browse files
committed
Code Review feedback
1 parent 6b17d38 commit 5607562

File tree

7 files changed

+164
-93
lines changed

7 files changed

+164
-93
lines changed

LibGit2Sharp.Tests/RebaseFixture.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using LibGit2Sharp.Tests.TestHelpers;
56
using Xunit;
67
using Xunit.Extensions;
7-
using System.IO;
88

99
namespace LibGit2Sharp.Tests
1010
{
@@ -52,6 +52,9 @@ public void CanRebase(string initialBranchName,
5252

5353
int beforeStepCallCount = 0;
5454
int afterStepCallCount = 0;
55+
bool beforeRebaseStepCountCorrect = true;
56+
bool afterRebaseStepCountCorrect = true;
57+
bool totalStepCountCorrect = true;
5558

5659
List<Commit> PreRebaseCommits = new List<Commit>();
5760
List<CompletedRebaseStepInfo> PostRebaseResults = new List<CompletedRebaseStepInfo>();
@@ -61,11 +64,15 @@ public void CanRebase(string initialBranchName,
6164
{
6265
RebaseStepStarting = x =>
6366
{
67+
beforeRebaseStepCountCorrect &= beforeStepCallCount == x.StepIndex;
68+
totalStepCountCorrect &= (x.TotalStepCount == stepCount);
6469
beforeStepCallCount++;
6570
PreRebaseCommits.Add(x.StepInfo.Commit);
6671
},
6772
RebaseStepCompleted = x =>
6873
{
74+
afterRebaseStepCountCorrect &= (afterStepCallCount == x.CompletedStepIndex);
75+
totalStepCountCorrect &= (x.TotalStepCount == stepCount);
6976
afterStepCallCount++;
7077
PostRebaseResults.Add(new CompletedRebaseStepInfo(x.Commit, x.WasPatchAlreadyApplied));
7178
},
@@ -74,6 +81,9 @@ public void CanRebase(string initialBranchName,
7481
RebaseResult rebaseResult = repo.Rebase.Start(branch, upstream, onto, Constants.Identity, options);
7582

7683
// Validation:
84+
Assert.True(afterRebaseStepCountCorrect, "Unexpected CompletedStepIndex value in RebaseStepCompleted");
85+
Assert.True(beforeRebaseStepCountCorrect, "Unexpected StepIndex value in RebaseStepStarting");
86+
Assert.True(totalStepCountCorrect, "Unexpected TotalStepcount value in Rebase step callback");
7787
Assert.Equal(RebaseStatus.Complete, rebaseResult.Status);
7888
Assert.Equal(stepCount, rebaseResult.TotalStepCount);
7989
Assert.Null(rebaseResult.CurrentStepInfo);
@@ -467,8 +477,9 @@ public void CanQueryRebaseOperation()
467477
Assert.Equal(3, rebaseResult.TotalStepCount);
468478

469479
RebaseStepInfo info = repo.Rebase.GetCurrentStepInfo();
470-
Assert.Equal(0, info.CurrentStepIndex);
471-
Assert.Equal(3, info.TotalStepCount);
480+
481+
Assert.Equal(0, repo.Rebase.GetCurrentStepIndex());
482+
Assert.Equal(3, repo.Rebase.GetTotalStepCount());
472483
Assert.Equal(RebaseStepOperation.Pick, info.Type);
473484
}
474485
}

LibGit2Sharp/AfterRebaseStepInfo.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
6-
namespace LibGit2Sharp
1+
namespace LibGit2Sharp
72
{
83
/// <summary>
94
/// Information about a rebase step that was just completed.
@@ -16,19 +11,23 @@ public class AfterRebaseStepInfo
1611
protected AfterRebaseStepInfo()
1712
{ }
1813

19-
internal AfterRebaseStepInfo(RebaseStepInfo stepInfo, Commit commit)
14+
internal AfterRebaseStepInfo(RebaseStepInfo stepInfo, Commit commit, long completedStepIndex, long totalStepCount)
2015
{
2116
StepInfo = stepInfo;
2217
Commit = commit;
2318
WasPatchAlreadyApplied = false;
19+
CompletedStepIndex = completedStepIndex;
20+
TotalStepCount = totalStepCount;
2421
}
2522

2623
/// <summary>
2724
/// Constructor to call when the patch has already been applied for this step.
2825
/// </summary>
2926
/// <param name="stepInfo"></param>
30-
internal AfterRebaseStepInfo(RebaseStepInfo stepInfo)
31-
: this (stepInfo, null)
27+
/// <param name="completedStepIndex"/>
28+
/// <param name="totalStepCount"></param>
29+
internal AfterRebaseStepInfo(RebaseStepInfo stepInfo, long completedStepIndex, long totalStepCount)
30+
: this (stepInfo, null, completedStepIndex, totalStepCount)
3231
{
3332
WasPatchAlreadyApplied = true;
3433
}
@@ -48,5 +47,15 @@ internal AfterRebaseStepInfo(RebaseStepInfo stepInfo)
4847
/// <see cref="AfterRebaseStepInfo.Commit"/> will be null.
4948
/// </summary>
5049
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; }
5160
}
5261
}

LibGit2Sharp/BeforeRebaseStepInfo.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,26 @@ public class BeforeRebaseStepInfo
1616
protected BeforeRebaseStepInfo()
1717
{ }
1818

19-
internal BeforeRebaseStepInfo(RebaseStepInfo stepInfo)
19+
internal BeforeRebaseStepInfo(RebaseStepInfo stepInfo, long stepIndex, long totalStepCount)
2020
{
2121
StepInfo = stepInfo;
22+
StepIndex = stepIndex;
23+
TotalStepCount = totalStepCount;
2224
}
2325

2426
/// <summary>
2527
/// Information on the step that is about to be performed.
2628
/// </summary>
2729
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; }
2840
}
2941
}

LibGit2Sharp/Core/Proxy.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,8 +1607,6 @@ public static GitRebaseOperation git_rebase_operation_byindex(
16071607
IntPtr ptr = NativeMethods.git_rebase_operation_byindex(rebase, ((UIntPtr)index));
16081608
GitRebaseOperation operation = ptr.MarshalAs<GitRebaseOperation>();
16091609

1610-
// Workaround until 92e87dd74 from libgit2 is consumed by LibGit2#
1611-
operation.exec = IntPtr.Zero;
16121610
return operation;
16131611
}
16141612

@@ -1631,9 +1629,6 @@ public static GitRebaseOperation git_rebase_next(RebaseSafeHandle rebase)
16311629
// If successsful, then marshal native struct to managed struct.
16321630
operation = ptr.MarshalAs<GitRebaseOperation>();
16331631

1634-
// Workaround until 92e87dd74 from libgit2 is consumed by LibGit2#
1635-
operation.exec = IntPtr.Zero;
1636-
16371632
return operation;
16381633
}
16391634

LibGit2Sharp/Rebase.cs

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
52
using LibGit2Sharp.Core;
63
using LibGit2Sharp.Core.Handles;
74

@@ -39,11 +36,11 @@ public enum RebaseStepOperation
3936
/// </summary>
4037
Fixup,
4138

42-
/// <summary>
43-
/// No commit to cherry-pick. Run the given command and continue
44-
/// if successful.
45-
/// </summary>
46-
Exec
39+
// <summary>
40+
// No commit to cherry-pick. Run the given command and continue
41+
// if successful.
42+
// </summary>
43+
// Exec
4744
}
4845

4946
/// <summary>
@@ -169,17 +166,18 @@ public virtual RebaseResult Continue(Identity committer, RebaseOptions options)
169166

170167
var stepInfo = new RebaseStepInfo(gitRebasestepInfo.type,
171168
repository.Lookup<Commit>(new ObjectId(gitRebasestepInfo.id)),
172-
LaxUtf8NoCleanupMarshaler.FromNative(gitRebasestepInfo.exec),
173-
currentStepIndex,
174-
totalStepCount);
169+
LaxUtf8NoCleanupMarshaler.FromNative(gitRebasestepInfo.exec));
175170

176171
if (rebaseCommitResult.WasPatchAlreadyApplied)
177172
{
178-
options.RebaseStepCompleted(new AfterRebaseStepInfo(stepInfo));
173+
options.RebaseStepCompleted(new AfterRebaseStepInfo(stepInfo, currentStepIndex, totalStepCount));
179174
}
180175
else
181176
{
182-
options.RebaseStepCompleted(new AfterRebaseStepInfo(stepInfo, repository.Lookup<Commit>(new ObjectId(rebaseCommitResult.CommitId))));
177+
options.RebaseStepCompleted(new AfterRebaseStepInfo(stepInfo,
178+
repository.Lookup<Commit>(new ObjectId(rebaseCommitResult.CommitId)),
179+
currentStepIndex,
180+
totalStepCount));
183181
}
184182
}
185183

@@ -239,17 +237,75 @@ public virtual RebaseStepInfo GetCurrentStepInfo()
239237
using (RebaseSafeHandle rebaseHandle = Proxy.git_rebase_open(repository.Handle, gitRebaseOptions))
240238
{
241239
long currentStepIndex = Proxy.git_rebase_operation_current(rebaseHandle);
242-
long totalStepCount = Proxy.git_rebase_operation_entrycount(rebaseHandle);
243240
GitRebaseOperation gitRebasestepInfo = Proxy.git_rebase_operation_byindex(rebaseHandle, currentStepIndex);
244241
var stepInfo = new RebaseStepInfo(gitRebasestepInfo.type,
245242
repository.Lookup<Commit>(new ObjectId(gitRebasestepInfo.id)),
246-
LaxUtf8NoCleanupMarshaler.FromNative(gitRebasestepInfo.exec),
247-
currentStepIndex,
248-
totalStepCount);
243+
LaxUtf8Marshaler.FromNative(gitRebasestepInfo.exec));
249244
return stepInfo;
250245
}
251246
}
252247

248+
/// <summary>
249+
/// Get info on the specified step
250+
/// </summary>
251+
/// <param name="stepIndex"></param>
252+
/// <returns></returns>
253+
public virtual RebaseStepInfo GetStepInfo(long stepIndex)
254+
{
255+
if (repository.Info.CurrentOperation != LibGit2Sharp.CurrentOperation.RebaseMerge)
256+
{
257+
return null;
258+
}
259+
260+
GitRebaseOptions gitRebaseOptions = new GitRebaseOptions()
261+
{
262+
version = 1,
263+
};
264+
265+
using (RebaseSafeHandle rebaseHandle = Proxy.git_rebase_open(repository.Handle, gitRebaseOptions))
266+
{
267+
GitRebaseOperation gitRebasestepInfo = Proxy.git_rebase_operation_byindex(rebaseHandle, stepIndex);
268+
var stepInfo = new RebaseStepInfo(gitRebasestepInfo.type,
269+
repository.Lookup<Commit>(new ObjectId(gitRebasestepInfo.id)),
270+
LaxUtf8Marshaler.FromNative(gitRebasestepInfo.exec));
271+
return stepInfo;
272+
}
273+
}
274+
275+
/// <summary>
276+
///
277+
/// </summary>
278+
/// <returns></returns>
279+
public virtual long GetCurrentStepIndex()
280+
{
281+
GitRebaseOptions gitRebaseOptions = new GitRebaseOptions()
282+
{
283+
version = 1,
284+
};
285+
286+
using (RebaseSafeHandle rebaseHandle = Proxy.git_rebase_open(repository.Handle, gitRebaseOptions))
287+
{
288+
return Proxy.git_rebase_operation_current(rebaseHandle);
289+
}
290+
}
291+
292+
/// <summary>
293+
///
294+
/// </summary>
295+
/// <returns></returns>
296+
public virtual long GetTotalStepCount()
297+
{
298+
GitRebaseOptions gitRebaseOptions = new GitRebaseOptions()
299+
{
300+
version = 1,
301+
};
302+
303+
using (RebaseSafeHandle rebaseHandle = Proxy.git_rebase_open(repository.Handle, gitRebaseOptions))
304+
{
305+
return Proxy.git_rebase_operation_entrycount(rebaseHandle);
306+
}
307+
}
308+
253309
private void EnsureNonBareRepo()
254310
{
255311
if (this.repository.Info.IsBare)

0 commit comments

Comments
 (0)