gix_filter/pipeline/mod.rs
1use bstr::BString;
2
3use crate::{driver, eol, Driver, Pipeline};
4
5/// Define how to perform CRLF round-trip checking when converting to git.
6#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
7pub enum CrlfRoundTripCheck {
8 /// Fail with an error if CRLF conversion isn't round-trip safe.
9 Fail,
10 /// Emit a warning using `gix_trace::warn!`, but don't fail.
11 ///
12 /// Note that the parent application has to setup tracing to make these events visible, along with a parent `span!`.
13 #[default]
14 Warn,
15 /// Do nothing, do not perform round-trip check at all.
16 Skip,
17}
18
19/// Additional configuration for the filter pipeline.
20#[derive(Default, Clone)]
21pub struct Options {
22 /// Available (external) driver programs to invoke if attributes for path configure them.
23 pub drivers: Vec<Driver>,
24 /// Global options to configure end-of-line conversions, to worktree or to git.
25 pub eol_config: eol::Configuration,
26 /// How to perform round-trip checks during end-of-line conversions to git.
27 pub crlf_roundtrip_check: CrlfRoundTripCheck,
28 /// All worktree encodings for round-trip checks should be performed.
29 pub encodings_with_roundtrip_check: Vec<&'static encoding_rs::Encoding>,
30 /// The object hash to use when applying the `ident` filter.
31 pub object_hash: gix_hash::Kind,
32}
33
34/// Context that typically doesn't change throughout the lifetime of a pipeline, for use with `process` filters.
35///
36/// Note that this is quite specific to third-party filters that actually make use of this additional context.
37#[derive(Default, Debug, Clone)]
38pub struct Context {
39 /// The name of the reference that `HEAD` is pointing to. It's passed to `process` filters if present.
40 pub ref_name: Option<BString>,
41 /// The root-level tree that contains the current entry directly or indirectly, or the commit owning the tree (if available).
42 ///
43 /// This is passed to `process` filters if present.
44 pub treeish: Option<gix_hash::ObjectId>,
45 /// The actual blob-hash of the data we are processing. It's passed to `process` filters if present.
46 ///
47 /// Note that this hash might be different from the `$Id$` of the respective `ident` filter, as the latter generates the hash itself.
48 pub blob: Option<gix_hash::ObjectId>,
49}
50
51const ATTRS: [&str; 6] = ["crlf", "ident", "filter", "eol", "text", "working-tree-encoding"];
52
53/// Lifecycle
54impl Pipeline {
55 /// Create a new pipeline with configured `drivers` (which should be considered safe to invoke), which are passed `context`.
56 /// `eol_config` serves as fallback to understand how to convert line endings if no line-ending attributes are present.
57 /// `crlf_roundtrip_check` corresponds to the git-configuration of `core.safecrlf`.
58 /// `object_hash` is relevant for the `ident` filter.
59 pub fn new(context: gix_command::Context, options: Options) -> Self {
60 let mut attrs = gix_attributes::search::Outcome::default();
61 attrs.initialize_with_selection(&Default::default(), ATTRS);
62 Pipeline {
63 attrs,
64 context: Context::default(),
65 processes: driver::State::new(context),
66 options,
67 bufs: Default::default(),
68 }
69 }
70
71 /// Turn ourselves into state managing possibly running driver processes.
72 ///
73 /// This can be used to control how these are terminated via [driver::State::shutdown()].
74 pub fn into_driver_state(self) -> driver::State {
75 self.processes
76 }
77}
78
79impl Default for Pipeline {
80 fn default() -> Self {
81 Pipeline::new(Default::default(), Default::default())
82 }
83}
84
85/// Access
86impl Pipeline {
87 /// Return a mutable reference to the state that handles long running processes.
88 /// Interacting with it directly allows to handle delayed results.
89 pub fn driver_state_mut(&mut self) -> &mut driver::State {
90 &mut self.processes
91 }
92
93 /// Provide mutable context that is made available to the process filters.
94 ///
95 /// The context set here is relevant for the [`convert_to_git()`][Self::convert_to_git()] and
96 /// [`convert_to_worktree()`][Self::convert_to_worktree()] methods.
97 pub fn driver_context_mut(&mut self) -> &mut Context {
98 &mut self.context
99 }
100
101 /// Return a set of options for configuration after instantiation.
102 pub fn options_mut(&mut self) -> &mut Options {
103 &mut self.options
104 }
105
106 /// Return our double-buffers for reuse by the caller.
107 pub fn buffers_mut(&mut self) -> &mut gix_utils::Buffers {
108 &mut self.bufs
109 }
110}
111
112///
113pub mod convert;
114
115pub(crate) mod util;