- current = crt_series.get_current()
- if not current:
- raise CmdException('No patch applied')
- patches = [current]
-
- before_patches = after_patches = []
-
- # pop necessary patches
- if oldapplied:
- if options.to:
- pop_idx = oldapplied.index(options.to)
- else:
- pop_idx = 0
- after_patches = [p for p in oldapplied[pop_idx:] if p not in patches]
-
- # find the deepest patch to pop
- sink_applied = [p for p in oldapplied if p in patches]
- if sink_applied:
- sinked_idx = oldapplied.index(sink_applied[0])
- if sinked_idx < pop_idx:
- # this is the case where sink brings patches forward
- before_patches = [p for p in oldapplied[sinked_idx:pop_idx]
- if p not in patches]
- pop_idx = sinked_idx
-
- crt_series.pop_patch(oldapplied[pop_idx])
-
- push_patches(crt_series, before_patches)
- push_patches(crt_series, patches)
- if not options.nopush:
- push_patches(crt_series, after_patches)
+ insert_idx = 0
+ applied = applied[:insert_idx] + patches + applied[insert_idx:]
+
+ unapplied = [p for p in stack.patchorder.unapplied if p not in patches]
+ hidden = list(stack.patchorder.hidden)
+
+ iw = stack.repository.default_iw
+ clean_iw = (not options.keep and iw) or None
+ trans = transaction.StackTransaction(stack, 'sink',
+ check_clean_iw = clean_iw)
+
+ try:
+ trans.reorder_patches(applied, unapplied, hidden, iw)
+ except transaction.TransactionHalted:
+ pass
+ return trans.run(iw)