-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathRebaseOperationImpl.cs
279 lines (244 loc) · 11.8 KB
/
RebaseOperationImpl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
using System.Globalization;
namespace LibGit2Sharp
{
internal class RebaseOperationImpl
{
/// <summary>
/// Run a rebase to completion, a conflict, or a requested stop point.
/// </summary>
/// <param name="rebaseOperationHandle">Handle to the rebase operation.</param>
/// <param name="repository">Repository in which rebase operation is being run.</param>
/// <param name="committer">Committer Identity to use for the rebased commits.</param>
/// <param name="options">Options controlling rebase behavior.</param>
/// <returns>RebaseResult that describes the result of the rebase operation.</returns>
public static RebaseResult Run(RebaseHandle rebaseOperationHandle,
Repository repository,
Identity committer,
RebaseOptions options)
{
Ensure.ArgumentNotNull(rebaseOperationHandle, "rebaseOperationHandle");
Ensure.ArgumentNotNull(repository, "repository");
Ensure.ArgumentNotNull(committer, "committer");
Ensure.ArgumentNotNull(options, "options");
RebaseResult rebaseResult = null;
// This loop will run until a rebase result has been set.
while (rebaseResult == null)
{
RebaseProgress rebaseStepContext = NextRebaseStep(repository, rebaseOperationHandle);
if (rebaseStepContext.current != -1)
{
rebaseResult = RunRebaseStep(rebaseOperationHandle,
repository,
committer,
options,
rebaseStepContext.current,
rebaseStepContext.total);
}
else
{
// No step to apply - need to complete the rebase.
rebaseResult = CompleteRebase(rebaseOperationHandle, committer);
}
}
return rebaseResult;
}
private static RebaseResult CompleteRebase(RebaseHandle rebaseOperationHandle, Identity committer)
{
long totalStepCount = Proxy.git_rebase_operation_entrycount(rebaseOperationHandle);
// Rebase is completed!
Proxy.git_rebase_finish(rebaseOperationHandle, committer);
var rebaseResult = new RebaseResult(RebaseStatus.Complete,
totalStepCount,
totalStepCount,
null);
return rebaseResult;
}
/// <summary>
/// Run the current rebase step. This will handle reporting that we are about to run a rebase step,
/// identifying and running the operation for the current step, and reporting the current step is completed.
/// </summary>
/// <param name="rebaseOperationHandle"></param>
/// <param name="repository"></param>
/// <param name="committer"></param>
/// <param name="options"></param>
/// <param name="stepToApplyIndex"></param>
/// <param name="totalStepCount"/>
/// <returns></returns>
private static unsafe RebaseResult RunRebaseStep(RebaseHandle rebaseOperationHandle,
Repository repository,
Identity committer,
RebaseOptions options,
long stepToApplyIndex,
long totalStepCount)
{
RebaseStepResult rebaseStepResult = null;
RebaseResult rebaseSequenceResult = null;
git_rebase_operation* rebaseOp = Proxy.git_rebase_operation_byindex(rebaseOperationHandle, stepToApplyIndex);
ObjectId idOfCommitBeingRebased = ObjectId.BuildFromPtr(&rebaseOp->id);
RebaseStepInfo stepToApplyInfo = new RebaseStepInfo(rebaseOp->type,
repository.Lookup<Commit>(idOfCommitBeingRebased),
LaxUtf8NoCleanupMarshaler.FromNative(rebaseOp->exec));
// Report the rebase step we are about to perform.
if (options.RebaseStepStarting != null)
{
options.RebaseStepStarting(new BeforeRebaseStepInfo(stepToApplyInfo, stepToApplyIndex, totalStepCount));
}
// Perform the rebase step
git_rebase_operation* rebaseOpReport = Proxy.git_rebase_next(rebaseOperationHandle);
// Verify that the information from the native library is consistent.
VerifyRebaseOp(rebaseOpReport, stepToApplyInfo);
// Handle the result
switch (stepToApplyInfo.Type)
{
case RebaseStepOperation.Pick:
rebaseStepResult = ApplyPickStep(rebaseOperationHandle, repository, committer, options, stepToApplyInfo);
break;
case RebaseStepOperation.Squash:
case RebaseStepOperation.Edit:
// case RebaseStepOperation.Exec:
case RebaseStepOperation.Fixup:
case RebaseStepOperation.Reword:
// These operations are not yet supported by lg2.
throw new LibGit2SharpException("Rebase Operation Type ({0}) is not currently supported in LibGit2Sharp.",
stepToApplyInfo.Type);
default:
throw new ArgumentException(string.Format(
"Unexpected Rebase Operation Type: {0}", stepToApplyInfo.Type));
}
// Report that we just completed the step
if (options.RebaseStepCompleted != null &&
(rebaseStepResult.Status == RebaseStepStatus.Committed ||
rebaseStepResult.Status == RebaseStepStatus.ChangesAlreadyApplied))
{
if (rebaseStepResult.ChangesAlreadyApplied)
{
options.RebaseStepCompleted(new AfterRebaseStepInfo(stepToApplyInfo, stepToApplyIndex, totalStepCount));
}
else
{
options.RebaseStepCompleted(new AfterRebaseStepInfo(stepToApplyInfo,
repository.Lookup<Commit>(new ObjectId(rebaseStepResult.CommitId)),
stepToApplyIndex,
totalStepCount));
}
}
// If the result of the rebase step is something that requires us to stop
// running the rebase sequence operations, then report the result.
if (rebaseStepResult.Status == RebaseStepStatus.Conflicts)
{
rebaseSequenceResult = new RebaseResult(RebaseStatus.Conflicts,
stepToApplyIndex,
totalStepCount,
null);
}
return rebaseSequenceResult;
}
private static RebaseStepResult ApplyPickStep(RebaseHandle rebaseOperationHandle, Repository repository, Identity committer, RebaseOptions options, RebaseStepInfo stepToApplyInfo)
{
RebaseStepResult rebaseStepResult;
// commit and continue.
if (repository.Index.IsFullyMerged)
{
Proxy.GitRebaseCommitResult rebase_commit_result = Proxy.git_rebase_commit(rebaseOperationHandle, null, committer);
if (rebase_commit_result.WasPatchAlreadyApplied)
{
rebaseStepResult = new RebaseStepResult(RebaseStepStatus.ChangesAlreadyApplied);
}
else
{
rebaseStepResult = new RebaseStepResult(RebaseStepStatus.Committed, rebase_commit_result.CommitId);
}
}
else
{
rebaseStepResult = new RebaseStepResult(RebaseStepStatus.Conflicts);
}
return rebaseStepResult;
}
/// <summary>
/// Verify that the information in a GitRebaseOperation and a RebaseStepInfo agree
/// </summary>
/// <param name="rebaseOpReport"></param>
/// <param name="stepInfo"></param>
private static unsafe void VerifyRebaseOp(git_rebase_operation* rebaseOpReport, RebaseStepInfo stepInfo)
{
// The step reported via querying by index and the step returned from git_rebase_next
// should be the same
if (rebaseOpReport == null ||
ObjectId.BuildFromPtr(&rebaseOpReport->id) != stepInfo.Commit.Id ||
rebaseOpReport->type != stepInfo.Type)
{
// This is indicative of a program error - should never happen.
throw new LibGit2SharpException("Unexpected step info reported by running rebase step.");
}
}
private struct RebaseProgress
{
public long current;
public long total;
}
/// <summary>
/// Returns the next rebase step, or null if there are none,
/// and the rebase operation needs to be finished.
/// </summary>
/// <param name="repository"></param>
/// <param name="rebaseOperationHandle"></param>
/// <returns></returns>
private static RebaseProgress NextRebaseStep(
Repository repository,
RebaseHandle rebaseOperationHandle)
{
// stepBeingApplied indicates the step that will be applied by by git_rebase_next.
// The current step does not get incremented until git_rebase_next (except on
// the initial step), but we want to report the step that will be applied.
long stepToApplyIndex = Proxy.git_rebase_operation_current(rebaseOperationHandle);
stepToApplyIndex++;
long totalStepCount = Proxy.git_rebase_operation_entrycount(rebaseOperationHandle);
if (stepToApplyIndex == totalStepCount)
{
stepToApplyIndex = -1;
}
RebaseProgress progress = new RebaseProgress()
{
current = stepToApplyIndex,
total = totalStepCount,
};
return progress;
}
private enum RebaseStepStatus
{
Committed,
Conflicts,
ChangesAlreadyApplied,
}
private class RebaseStepResult
{
public RebaseStepResult(RebaseStepStatus status)
{
Status = status;
CommitId = GitOid.Empty;
}
public RebaseStepResult(RebaseStepStatus status, GitOid commitId)
{
Status = status;
CommitId = commitId;
}
/// <summary>
/// The ID of the commit that was generated, if any
/// </summary>
public GitOid CommitId;
/// <summary>
/// bool to indicate if the patch was already applied.
/// If Patch was already applied, then CommitId will be empty (all zeros).
/// </summary>
public bool ChangesAlreadyApplied
{
get { return Status == RebaseStepStatus.ChangesAlreadyApplied; }
}
public RebaseStepStatus Status;
}
}
}