|
| 1 | +// TODO: remove this - only needed while &mut Vec<u8> isn't used. |
| 2 | +#![allow(clippy::ptr_arg)] |
| 3 | + |
| 4 | +use bstr::BString; |
| 5 | +use std::path::PathBuf; |
| 6 | + |
| 7 | +/// |
| 8 | +pub mod builtin_driver; |
| 9 | +/// |
| 10 | +pub mod pipeline; |
| 11 | +/// |
| 12 | +pub mod platform; |
| 13 | + |
| 14 | +/// Identify a merge resolution. |
| 15 | +#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 16 | +pub enum Resolution { |
| 17 | + /// Everything could be resolved during the merge. |
| 18 | + Complete, |
| 19 | + /// A conflict is still present. |
| 20 | + Conflict, |
| 21 | +} |
| 22 | + |
| 23 | +/// A way to classify a resource suitable for merging. |
| 24 | +#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)] |
| 25 | +pub enum ResourceKind { |
| 26 | + /// Our side of the state. |
| 27 | + CurrentOrOurs, |
| 28 | + /// Their side of the state. |
| 29 | + OtherOrTheirs, |
| 30 | + /// The state of the common base of both ours and theirs. |
| 31 | + CommonAncestorOrBase, |
| 32 | +} |
| 33 | + |
| 34 | +/// Define a driver program that merges |
| 35 | +/// |
| 36 | +/// Some values are related to diffing, some are related to conversions. |
| 37 | +#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 38 | +pub enum BuiltinDriver { |
| 39 | + /// Perform a merge between text-sources such that conflicts are marked according to |
| 40 | + /// `merge.conflictStyle` in the Git configuration. |
| 41 | + /// |
| 42 | + /// If any of the inputs, *base*, *ours* or *theirs* looks like non-text/binary, |
| 43 | + /// the [`Binary`](Self::Binary) driver will be used instead. |
| 44 | + /// |
| 45 | + /// Also see [`builtin_driver::text::ConflictStyle`]. |
| 46 | + #[default] |
| 47 | + Text, |
| 48 | + /// Merge 'unmergable' content by choosing *ours* or *theirs*, without performing |
| 49 | + /// an actual merge. |
| 50 | + /// |
| 51 | + /// Note that if the merge operation is for virtual ancestor (a merge for merge-bases), |
| 52 | + /// then *ours* will always be chosen. |
| 53 | + Binary, |
| 54 | + /// Merge text-sources and resolve conflicts by adding conflicting lines one after another, |
| 55 | + /// in random order, without adding conflict markers either. |
| 56 | + /// |
| 57 | + /// This can be useful for files that change a lot, but will remain usable merely by adding |
| 58 | + /// all changed lines. |
| 59 | + Union, |
| 60 | +} |
| 61 | + |
| 62 | +/// Define a driver program that merges |
| 63 | +/// |
| 64 | +/// Some values are related to diffing, some are related to conversions. |
| 65 | +#[derive(Default, Debug, Clone, PartialEq, Eq)] |
| 66 | +pub struct Driver { |
| 67 | + /// The name of the driver, as referred to by `[merge "name"]` in the git configuration. |
| 68 | + pub name: BString, |
| 69 | + /// The human-readable version of `name`, only to be used for displaying driver-information to the user. |
| 70 | + pub display_name: BString, |
| 71 | + /// The command to execute to perform the merge entirely like `<command> %O %A %B %L %P %S %X %Y`. |
| 72 | + /// |
| 73 | + /// * **%O** |
| 74 | + /// - the common ancestor version, or *base*. |
| 75 | + /// * **%A** |
| 76 | + /// - the current version, or *ours*. |
| 77 | + /// * **%B** |
| 78 | + /// - the other version, or *theirs*. |
| 79 | + /// * **%L** |
| 80 | + /// - The conflict-marker size as positive number. |
| 81 | + /// * **%P** |
| 82 | + /// - The path in which the merged result will be stored. |
| 83 | + /// * **%S** |
| 84 | + /// - The conflict-label for the common ancestor or *base*. |
| 85 | + /// * **%X** |
| 86 | + /// - The conflict-label for the current version or *ours*. |
| 87 | + /// * **%Y** |
| 88 | + /// - The conflict-label for the other version or *theirs*. |
| 89 | + /// |
| 90 | + /// Note that conflict-labels are behind the conflict markers, to annotate them. |
| 91 | + /// |
| 92 | + /// A typical invocation with all arguments substituted could then look like this: |
| 93 | + /// |
| 94 | + /// ``` |
| 95 | + /// <driver-program> .merge_file_nR2Qs1 .merge_file_WYXCJe .merge_file_UWbzrm 7 file e2a2970 HEAD feature |
| 96 | + /// ``` |
| 97 | + pub command: BString, |
| 98 | + /// If `true`, this is the `name` of the driver to use when a virtual-merge-base is created, as a merge of all |
| 99 | + /// available merge-bases if there are more than one. |
| 100 | + /// |
| 101 | + /// This value can also be special built-in drivers named `text`, `binary` or `union`. Note that user-defined |
| 102 | + /// drivers with the same name will be preferred over built-in ones, but only for files whose git attributes |
| 103 | + /// specified the driver by *name*. |
| 104 | + pub recursive: Option<BString>, |
| 105 | +} |
| 106 | + |
| 107 | +/// A conversion pipeline to take an object or path from what's stored in Git to what can be merged, while |
| 108 | +/// following the guidance of git-attributes at the respective path to learn how the merge should be performed. |
| 109 | +/// |
| 110 | +/// Depending on the source, different conversions are performed: |
| 111 | +/// |
| 112 | +/// * `worktree on disk` -> `object for storage in git` |
| 113 | +/// * `object` -> `possibly renormalized object` |
| 114 | +/// - Renormalization means that the `object` is converted to what would be checked out into the work-tree, |
| 115 | +/// just to turn it back into an object. |
| 116 | +#[derive(Clone)] |
| 117 | +pub struct Pipeline { |
| 118 | + /// A way to read data directly from the worktree. |
| 119 | + pub roots: pipeline::WorktreeRoots, |
| 120 | + /// A pipeline to convert objects from the worktree to Git, and also from Git to the worktree, and back to Git. |
| 121 | + pub filter: gix_filter::Pipeline, |
| 122 | + /// Options affecting the way we read files. |
| 123 | + pub options: pipeline::Options, |
| 124 | + /// All available merge drivers. |
| 125 | + /// |
| 126 | + /// They are referenced in git-attributes by name, and we hand out indices into this array. |
| 127 | + drivers: Vec<Driver>, |
| 128 | + /// Pre-configured attributes to obtain additional merge-related information. |
| 129 | + attrs: gix_filter::attributes::search::Outcome, |
| 130 | + /// A buffer to produce disk-accessible paths from worktree roots. |
| 131 | + path: PathBuf, |
| 132 | +} |
| 133 | + |
| 134 | +/// A utility for gathering and processing all state necessary to perform a three-way merge. |
| 135 | +/// |
| 136 | +/// It can re-use buffers if all three parts of participating in the merge are |
| 137 | +/// set repeatedly. |
| 138 | +#[derive(Clone)] |
| 139 | +pub struct Platform { |
| 140 | + /// The current version (ours). |
| 141 | + current: Option<platform::Resource>, |
| 142 | + /// The ancestor version (base). |
| 143 | + ancestor: Option<platform::Resource>, |
| 144 | + /// The other version (theirs). |
| 145 | + other: Option<platform::Resource>, |
| 146 | + |
| 147 | + /// A way to convert objects into a diff-able format. |
| 148 | + pub filter: Pipeline, |
| 149 | + /// A way to access `.gitattributes` |
| 150 | + pub attr_stack: gix_worktree::Stack, |
| 151 | + |
| 152 | + /// The way we convert resources into mergeable states. |
| 153 | + filter_mode: pipeline::Mode, |
| 154 | +} |
0 commit comments