-
Notifications
You must be signed in to change notification settings - Fork 894
/
Copy pathRebase.cs
317 lines (275 loc) · 12.7 KB
/
Rebase.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
using System.Globalization;
namespace LibGit2Sharp
{
/// <summary>
/// The type of operation to be performed in a rebase step.
/// </summary>
public enum RebaseStepOperation
{
/// <summary>
/// Commit is to be cherry-picked.
/// </summary>
Pick = 0,
/// <summary>
/// Cherry-pick the commit and edit the commit message.
/// </summary>
Reword,
/// <summary>
/// Cherry-pick the commit but allow user to edit changes.
/// </summary>
Edit,
/// <summary>
/// Commit is to be squashed into previous commit. The commit
/// message will be merged with the previous message.
/// </summary>
Squash,
/// <summary>
/// Commit is to be squashed into previous commit. The commit
/// message will be discarded.
/// </summary>
Fixup,
// <summary>
// No commit to cherry-pick. Run the given command and continue
// if successful.
// </summary>
// Exec
}
/// <summary>
/// Encapsulates a rebase operation.
/// </summary>
public class Rebase
{
internal readonly Repository repository;
/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected Rebase()
{ }
internal Rebase(Repository repo)
{
this.repository = repo;
}
unsafe AnnotatedCommitHandle AnnotatedCommitHandleFromRefHandle(ReferenceHandle refHandle)
{
return (refHandle == null) ?
new AnnotatedCommitHandle(null, false) :
Proxy.git_annotated_commit_from_ref(this.repository.Handle, refHandle);
}
/// <summary>
/// Start a rebase operation.
/// </summary>
/// <param name="branch">The branch to rebase.</param>
/// <param name="upstream">The starting commit to rebase.</param>
/// <param name="onto">The branch to rebase onto.</param>
/// <param name="committer">The <see cref="Identity"/> of who added the change to the repository.</param>
/// <param name="options">The <see cref="RebaseOptions"/> that specify the rebase behavior.</param>
/// <returns>true if completed successfully, false if conflicts encountered.</returns>
public virtual RebaseResult Start(Branch branch, Branch upstream, Branch onto, Identity committer, RebaseOptions options)
{
Ensure.ArgumentNotNull(upstream, "upstream");
options = options ?? new RebaseOptions();
EnsureNonBareRepo();
if (this.repository.Info.CurrentOperation != CurrentOperation.None)
{
throw new LibGit2SharpException("A {0} operation is already in progress.",
this.repository.Info.CurrentOperation);
}
Func<Branch, ReferenceHandle> RefHandleFromBranch = (Branch b) =>
{
return (b == null) ?
null :
this.repository.Refs.RetrieveReferencePtr(b.CanonicalName);
};
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options))
{
GitRebaseOptions gitRebaseOptions = new GitRebaseOptions()
{
version = 1,
checkout_options = checkoutOptionsWrapper.Options,
};
using (ReferenceHandle branchRefPtr = RefHandleFromBranch(branch))
using (ReferenceHandle upstreamRefPtr = RefHandleFromBranch(upstream))
using (ReferenceHandle ontoRefPtr = RefHandleFromBranch(onto))
using (AnnotatedCommitHandle annotatedBranchCommitHandle = AnnotatedCommitHandleFromRefHandle(branchRefPtr))
using (AnnotatedCommitHandle upstreamRefAnnotatedCommitHandle = AnnotatedCommitHandleFromRefHandle(upstreamRefPtr))
using (AnnotatedCommitHandle ontoRefAnnotatedCommitHandle = AnnotatedCommitHandleFromRefHandle(ontoRefPtr))
using (RebaseHandle rebaseOperationHandle = Proxy.git_rebase_init(this.repository.Handle,
annotatedBranchCommitHandle,
upstreamRefAnnotatedCommitHandle,
ontoRefAnnotatedCommitHandle,
gitRebaseOptions))
{
RebaseResult rebaseResult = RebaseOperationImpl.Run(rebaseOperationHandle,
this.repository,
committer,
options);
return rebaseResult;
}
}
}
/// <summary>
/// Continue the current rebase.
/// </summary>
/// <param name="committer">The <see cref="Identity"/> of who added the change to the repository.</param>
/// <param name="options">The <see cref="RebaseOptions"/> that specify the rebase behavior.</param>
public virtual unsafe RebaseResult Continue(Identity committer, RebaseOptions options)
{
Ensure.ArgumentNotNull(committer, "committer");
options = options ?? new RebaseOptions();
EnsureNonBareRepo();
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options))
{
GitRebaseOptions gitRebaseOptions = new GitRebaseOptions()
{
version = 1,
checkout_options = checkoutOptionsWrapper.Options,
};
using (RebaseHandle rebase = Proxy.git_rebase_open(repository.Handle, gitRebaseOptions))
{
// TODO: Should we check the pre-conditions for committing here
// for instance - what if we had failed on the git_rebase_finish call,
// do we want continue to be able to restart afterwords...
var rebaseCommitResult = Proxy.git_rebase_commit(rebase, null, committer);
// Report that we just completed the step
if (options.RebaseStepCompleted != null)
{
// Get information on the current step
long currentStepIndex = Proxy.git_rebase_operation_current(rebase);
long totalStepCount = Proxy.git_rebase_operation_entrycount(rebase);
git_rebase_operation* gitRebasestepInfo = Proxy.git_rebase_operation_byindex(rebase, currentStepIndex);
var stepInfo = new RebaseStepInfo(gitRebasestepInfo->type,
repository.Lookup<Commit>(ObjectId.BuildFromPtr(&gitRebasestepInfo->id)),
LaxUtf8NoCleanupMarshaler.FromNative(gitRebasestepInfo->exec));
if (rebaseCommitResult.WasPatchAlreadyApplied)
{
options.RebaseStepCompleted(new AfterRebaseStepInfo(stepInfo, currentStepIndex, totalStepCount));
}
else
{
options.RebaseStepCompleted(new AfterRebaseStepInfo(stepInfo,
repository.Lookup<Commit>(new ObjectId(rebaseCommitResult.CommitId)),
currentStepIndex,
totalStepCount));
}
}
RebaseResult rebaseResult = RebaseOperationImpl.Run(rebase, repository, committer, options);
return rebaseResult;
}
}
}
/// <summary>
/// Abort the rebase operation.
/// </summary>
public virtual void Abort()
{
Abort(null);
}
/// <summary>
/// Abort the rebase operation.
/// </summary>
/// <param name="options">The <see cref="RebaseOptions"/> that specify the rebase behavior.</param>
public virtual void Abort(RebaseOptions options)
{
options = options ?? new RebaseOptions();
EnsureNonBareRepo();
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options))
{
GitRebaseOptions gitRebaseOptions = new GitRebaseOptions()
{
checkout_options = checkoutOptionsWrapper.Options,
};
using (RebaseHandle rebase = Proxy.git_rebase_open(repository.Handle, gitRebaseOptions))
{
Proxy.git_rebase_abort(rebase);
}
}
}
/// <summary>
/// The info on the current step.
/// </summary>
public virtual unsafe RebaseStepInfo GetCurrentStepInfo()
{
if (repository.Info.CurrentOperation != LibGit2Sharp.CurrentOperation.RebaseMerge)
{
return null;
}
GitRebaseOptions gitRebaseOptions = new GitRebaseOptions()
{
version = 1,
};
using (RebaseHandle rebaseHandle = Proxy.git_rebase_open(repository.Handle, gitRebaseOptions))
{
long currentStepIndex = Proxy.git_rebase_operation_current(rebaseHandle);
git_rebase_operation* gitRebasestepInfo = Proxy.git_rebase_operation_byindex(rebaseHandle, currentStepIndex);
var stepInfo = new RebaseStepInfo(gitRebasestepInfo->type,
repository.Lookup<Commit>(ObjectId.BuildFromPtr(&gitRebasestepInfo->id)),
LaxUtf8Marshaler.FromNative(gitRebasestepInfo->exec));
return stepInfo;
}
}
/// <summary>
/// Get info on the specified step
/// </summary>
/// <param name="stepIndex"></param>
/// <returns></returns>
public virtual unsafe RebaseStepInfo GetStepInfo(long stepIndex)
{
if (repository.Info.CurrentOperation != LibGit2Sharp.CurrentOperation.RebaseMerge)
{
return null;
}
GitRebaseOptions gitRebaseOptions = new GitRebaseOptions()
{
version = 1,
};
using (RebaseHandle rebaseHandle = Proxy.git_rebase_open(repository.Handle, gitRebaseOptions))
{
git_rebase_operation* gitRebasestepInfo = Proxy.git_rebase_operation_byindex(rebaseHandle, stepIndex);
var stepInfo = new RebaseStepInfo(gitRebasestepInfo->type,
repository.Lookup<Commit>(ObjectId.BuildFromPtr(&gitRebasestepInfo->id)),
LaxUtf8Marshaler.FromNative(gitRebasestepInfo->exec));
return stepInfo;
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public virtual long GetCurrentStepIndex()
{
GitRebaseOptions gitRebaseOptions = new GitRebaseOptions()
{
version = 1,
};
using (RebaseHandle rebaseHandle = Proxy.git_rebase_open(repository.Handle, gitRebaseOptions))
{
return Proxy.git_rebase_operation_current(rebaseHandle);
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public virtual long GetTotalStepCount()
{
GitRebaseOptions gitRebaseOptions = new GitRebaseOptions()
{
version = 1,
};
using (RebaseHandle rebaseHandle = Proxy.git_rebase_open(repository.Handle, gitRebaseOptions))
{
return Proxy.git_rebase_operation_entrycount(rebaseHandle);
}
}
private void EnsureNonBareRepo()
{
if (this.repository.Info.IsBare)
{
throw new BareRepositoryException("Rebase operations in a bare repository are not supported.");
}
}
}
}