Append an inverse operation for the current verified working-copy head. The optional target must be the sole current head. This first implementation supports operations whose inverse is a view/materialization transition; other metadata kinds are refused until their routed deltas are available.
(
&mut self,
working_copy: WorkingCopyId,
target: Option<OperationId>,
)
| 783 | /// supports operations whose inverse is a view/materialization transition; other |
| 784 | /// metadata kinds are refused until their routed deltas are available. |
| 785 | pub fn undo_operation( |
| 786 | &mut self, |
| 787 | working_copy: WorkingCopyId, |
| 788 | target: Option<OperationId>, |
| 789 | ) -> Result<OperationId, RepositoryError> { |
| 790 | let scope = OperationScope::WorkingCopy(working_copy); |
| 791 | let current = self.sole_operation_head(scope)?; |
| 792 | let target = target.unwrap_or(current); |
| 793 | if target != current { |
| 794 | return Err(RepositoryError::OperationNotReversible { |
| 795 | operation: target.to_string(), |
| 796 | kind: "historical".to_string(), |
| 797 | reason: format!( |
| 798 | "undo requires the sole current head {}; use restore for an earlier state", |
| 799 | current |
| 800 | ), |
| 801 | }); |
| 802 | } |
| 803 | let details = self.operation_details(target)?; |
| 804 | if details.verification != OperationVerificationState::Verified { |
| 805 | return Err(RepositoryError::OperationNotVerified { |
| 806 | operation: target.to_string(), |
| 807 | }); |
| 808 | } |
| 809 | if !details.operation.payload().delta.metadata.is_empty() { |
| 810 | return self.append_related_metadata_operation( |
| 811 | working_copy, |
| 812 | &details.operation, |
| 813 | OperationKind::Undo, |
| 814 | OperationRelation::Undo { target }, |
| 815 | true, |
| 816 | ); |
| 817 | } |
| 818 | if !matches!( |
| 819 | details.operation.payload().kind, |
| 820 | OperationKind::SwitchView | OperationKind::Undo | OperationKind::Restore |
| 821 | ) { |
| 822 | return Err(operation_not_reversible( |
| 823 | &details.operation, |
| 824 | "no typed inverse metadata transition is available", |
| 825 | )); |
| 826 | } |
| 827 | let view = details |
| 828 | .operation |
| 829 | .payload() |
| 830 | .before |
| 831 | .view |
| 832 | .as_ref() |
| 833 | .map(|view| view.name.clone()) |
| 834 | .ok_or_else(|| { |
| 835 | operation_not_reversible( |
| 836 | &details.operation, |
| 837 | "the operation does not carry a prior view state", |
| 838 | ) |
| 839 | })?; |
| 840 | self.switch_view_with_operation( |
| 841 | working_copy, |
| 842 | &view, |