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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
pub use crate::graph::Node;
pub fn protect_branches(
root: &mut Node,
repo: &dyn crate::git::Repo,
protected_branches: &crate::git::Branches,
) {
let mut protected_commits = std::collections::HashSet::new();
for protected_oid in protected_branches.oids() {
if let Some(merge_base_oid) = repo.merge_base(root.commit.id, protected_oid) {
if merge_base_oid == root.commit.id {
for commit in repo.commits_from(protected_oid) {
protected_commits.insert(commit.id);
if commit.id == root.commit.id {
break;
}
}
}
}
}
protect_branches_node(root, &protected_commits);
}
fn protect_branches_node(
node: &mut Node,
protected_commits: &std::collections::HashSet<git2::Oid>,
) {
if protected_commits.contains(&node.commit.id) {
node.action = crate::graph::Action::Protected;
for child in node.children.values_mut() {
protect_branches_node(child, protected_commits);
}
}
}
pub fn rebase_branches(node: &mut Node, new_base_id: git2::Oid) {
debug_assert_eq!(
node.find_commit_mut(new_base_id).unwrap().action,
crate::graph::Action::Protected
);
let mut rebaseable = Vec::new();
pop_rebaseable_stacks(node, &mut rebaseable);
let new_base = node.find_commit_mut(new_base_id).unwrap();
new_base
.children
.extend(rebaseable.into_iter().map(|n| (n.commit.id, n)));
}
fn pop_rebaseable_stacks(node: &mut Node, rebaseable: &mut Vec<Node>) {
if !node.action.is_protected() {
return;
}
let mut base_ids = Vec::new();
for (child_id, child) in node.children.iter_mut() {
if child.action.is_protected() {
pop_rebaseable_stacks(child, rebaseable);
} else {
base_ids.push(*child_id);
}
}
for base_id in base_ids {
let child = node.children.remove(&base_id).unwrap();
rebaseable.push(child);
}
}
pub fn pushable(node: &mut Node) {
if node.action.is_protected() {
for child in node.children.values_mut() {
pushable_node(child, None);
}
} else {
}
}
fn pushable_node(node: &mut Node, mut cause: Option<&str>) {
if node.action.is_protected() {
assert_eq!(cause, None);
for child in node.children.values_mut() {
pushable_node(child, cause);
}
return;
}
if node.commit.wip_summary().is_some() {
cause = Some("contains WIP commit");
}
if !node.branches.is_empty() {
let branch = &node.branches[0];
if let Some(cause) = cause {
log::debug!("{} isn't pushable, {}", branch.name, cause);
} else if node.branches.iter().all(|b| Some(b.id) == b.push_id) {
log::debug!("{} is already pushed", branch.name);
} else {
log::debug!("{} is pushable", branch.name);
node.pushable = true;
}
return;
}
for stack in node.children.values_mut() {
pushable_node(stack, cause);
}
}
pub fn drop_by_tree_id(node: &mut Node) {
if node.action.is_protected() {
track_protected_tree_id(node, std::collections::HashSet::new());
}
}
fn track_protected_tree_id(
node: &mut Node,
mut protected_tree_ids: std::collections::HashSet<git2::Oid>,
) {
assert!(node.action.is_protected());
protected_tree_ids.insert(node.commit.tree_id);
match node.children.len() {
0 => (),
1 => {
let child = node.children.values_mut().next().unwrap();
if child.action.is_protected() {
track_protected_tree_id(child, protected_tree_ids);
} else {
drop_first_branch_by_tree_id(child, protected_tree_ids);
}
}
_ => {
for child in node.children.values_mut() {
if child.action.is_protected() {
track_protected_tree_id(child, protected_tree_ids.clone());
} else {
drop_first_branch_by_tree_id(child, protected_tree_ids.clone());
}
}
}
}
}
fn drop_first_branch_by_tree_id(
node: &mut Node,
protected_tree_ids: std::collections::HashSet<git2::Oid>,
) -> bool {
#![allow(clippy::if_same_then_else)]
assert!(!node.action.is_protected());
if node.branches.is_empty() {
match node.children.len() {
0 => false,
1 => {
let child = node.children.values_mut().next().unwrap();
let all_dropped = drop_first_branch_by_tree_id(child, protected_tree_ids);
if all_dropped {
node.action = crate::graph::Action::Delete;
}
all_dropped
}
_ => {
let mut all_dropped = true;
for child in node.children.values_mut() {
all_dropped &= drop_first_branch_by_tree_id(child, protected_tree_ids.clone());
}
if all_dropped {
node.action = crate::graph::Action::Delete;
}
all_dropped
}
}
} else if !protected_tree_ids.contains(&node.commit.tree_id) {
false
} else if node.commit.revert_summary().is_some() {
false
} else {
node.action = crate::graph::Action::Delete;
true
}
}
pub fn fixup(node: &mut Node, effect: crate::config::Fixup) {
if effect == crate::config::Fixup::Ignore {
return;
}
let mut outstanding = std::collections::BTreeMap::new();
fixup_nodes(node, effect, &mut outstanding);
if !outstanding.is_empty() {
assert!(!node.action.is_protected());
for nodes in outstanding.into_values() {
for mut other in nodes.into_iter() {
std::mem::swap(node, &mut other);
node.children.insert(other.commit.id, other);
}
}
}
}
fn fixup_nodes(
node: &mut Node,
effect: crate::config::Fixup,
outstanding: &mut std::collections::BTreeMap<bstr::BString, Vec<Node>>,
) {
let mut fixups = Vec::new();
for (id, child) in node.children.iter_mut() {
fixup_nodes(child, effect, outstanding);
if child.action.is_protected() || child.action.is_delete() {
continue;
}
if let Some(summary) = node.commit.fixup_summary() {
fixups.push((*id, summary.to_owned()));
}
}
for (id, summary) in fixups {
let mut child = node.children.remove(&id).unwrap();
let mut new_children = Default::default();
std::mem::swap(&mut child.children, &mut new_children);
node.children.extend(new_children);
let mut new_branches = Default::default();
std::mem::swap(&mut child.branches, &mut new_branches);
node.branches.extend(new_branches);
outstanding
.entry(summary)
.or_insert_with(Default::default)
.push(child);
}
if let Some(mut fixups) = outstanding.remove(&node.commit.summary) {
if effect == crate::config::Fixup::Squash {
for fixup in fixups.iter_mut() {
assert!(fixup.action == crate::graph::Action::Pick);
fixup.action = crate::graph::Action::Squash;
}
}
splice_after(node, fixups);
} else if (node.action.is_protected() || node.action.is_delete()) && !outstanding.is_empty() {
let mut local = Default::default();
std::mem::swap(&mut local, outstanding);
let mut outstanding = local.into_values();
let mut fixups = outstanding.next().unwrap();
fixups.extend(outstanding.flatten());
splice_after(node, fixups);
}
}
fn splice_after(node: &mut Node, fixups: Vec<Node>) -> &mut Node {
let mut new_children = Default::default();
std::mem::swap(&mut node.children, &mut new_children);
let mut new_branches = Default::default();
std::mem::swap(&mut node.branches, &mut new_branches);
let mut current = node;
for fixup in fixups.into_iter().rev() {
current = current.children.entry(fixup.commit.id).or_insert(fixup);
}
std::mem::swap(&mut current.children, &mut new_children);
assert!(new_children.is_empty());
std::mem::swap(&mut current.branches, &mut new_branches);
assert!(new_branches.is_empty());
current
}
pub fn to_script(node: &Node) -> crate::git::Script {
let mut script = crate::git::Script::new();
match node.action {
crate::graph::Action::Pick | crate::graph::Action::Protected => {
let node_dependents: Vec<_> = node
.children
.values()
.filter_map(|child| node_to_script(child))
.collect();
if !node_dependents.is_empty() {
let stack_mark = node.commit.id;
script
.commands
.push(crate::git::Command::SwitchCommit(stack_mark));
let transaction = false;
extend_dependents(node, &mut script, node_dependents, transaction);
}
}
crate::graph::Action::Squash => unreachable!("base should be immutable"),
crate::graph::Action::Delete => unreachable!("base should be immutable"),
}
script
}
fn node_to_script(node: &Node) -> Option<crate::git::Script> {
let mut script = crate::git::Script::new();
match node.action {
crate::graph::Action::Pick => {
script
.commands
.push(crate::git::Command::CherryPick(node.commit.id));
for branch in node.branches.iter() {
script
.commands
.push(crate::git::Command::CreateBranch(branch.name.clone()));
}
let node_dependents: Vec<_> = node
.children
.values()
.filter_map(|child| node_to_script(child))
.collect();
if !node_dependents.is_empty() {
let transaction = !node.branches.is_empty();
extend_dependents(node, &mut script, node_dependents, transaction);
}
}
crate::graph::Action::Squash => {
script
.commands
.push(crate::git::Command::Squash(node.commit.id));
for branch in node.branches.iter() {
script
.commands
.push(crate::git::Command::CreateBranch(branch.name.clone()));
}
let node_dependents: Vec<_> = node
.children
.values()
.filter_map(|child| node_to_script(child))
.collect();
if !node_dependents.is_empty() {
let transaction = !node.branches.is_empty();
extend_dependents(node, &mut script, node_dependents, transaction);
}
}
crate::graph::Action::Protected => {
let node_dependents: Vec<_> = node
.children
.values()
.filter_map(|child| node_to_script(child))
.collect();
if !node_dependents.is_empty() {
let stack_mark = node.commit.id;
script
.commands
.push(crate::git::Command::SwitchCommit(stack_mark));
let transaction = false;
extend_dependents(node, &mut script, node_dependents, transaction);
}
}
crate::graph::Action::Delete => {
for branch in node.branches.iter() {
script
.commands
.push(crate::git::Command::DeleteBranch(branch.name.clone()));
}
let node_dependents: Vec<_> = node
.children
.values()
.filter_map(|child| node_to_script(child))
.collect();
if !node_dependents.is_empty() {
let transaction = !node.branches.is_empty();
extend_dependents(node, &mut script, node_dependents, transaction);
}
}
}
if script.is_empty() {
None
} else {
Some(script)
}
}
fn extend_dependents(
node: &Node,
script: &mut crate::git::Script,
mut dependents: Vec<crate::git::Script>,
transaction: bool,
) {
if !transaction && dependents.len() == 1 {
let dependent = dependents.remove(0);
script.commands.extend(dependent.commands);
script.dependents.extend(dependent.dependents);
} else {
let stack_mark = node.commit.id;
script
.commands
.push(crate::git::Command::RegisterMark(stack_mark));
for dependent in dependents.iter_mut() {
dependent
.commands
.insert(0, crate::git::Command::SwitchMark(stack_mark));
}
script.dependents.extend(dependents);
}
}