Skip to content

Commit 260c781

Browse files
committed
fix!: don't provide path to object-retrieval callback of Pipeline::convert::to_git::IndexObjectFn().
It implies that one has to be ready to fetch any kind of path from the index, even though it's always the path to the file that is currently converted. Also fix a bug that could cause it to return input as unchanged even though it was read into a buffer already.
1 parent 34318fa commit 260c781

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
lines changed

gix-filter/src/pipeline/convert.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ pub mod configuration {
2121

2222
///
2323
pub mod to_git {
24-
use bstr::BStr;
25-
26-
/// A function that writes a buffer like `fn(rela_path, &mut buf)` with by tes of an object in the index that is the one
27-
/// that should be converted.
24+
/// A function that fills `buf` `fn(&mut buf)` with the data stored in the index of the file that should be converted.
2825
pub type IndexObjectFn<'a> =
29-
dyn FnMut(&BStr, &mut Vec<u8>) -> Result<Option<()>, Box<dyn std::error::Error + Send + Sync>> + 'a;
26+
dyn FnMut(&mut Vec<u8>) -> Result<Option<()>, Box<dyn std::error::Error + Send + Sync>> + 'a;
3027

3128
/// The error returned by [Pipeline::convert_to_git()][super::Pipeline::convert_to_git()].
3229
#[derive(Debug, thiserror::Error)]
@@ -91,7 +88,7 @@ impl Pipeline {
9188
self.options.eol_config,
9289
)?;
9390

94-
let mut changed = false;
91+
let mut in_buffer = false;
9592
// this is just an approximation, but it's as good as it gets without reading the actual input.
9693
let would_convert_eol = eol::convert_to_git(
9794
b"\r\n",
@@ -119,12 +116,13 @@ impl Pipeline {
119116
}
120117
self.bufs.clear();
121118
read.read_to_end(&mut self.bufs.src)?;
122-
changed = true;
119+
in_buffer = true;
123120
}
124121
}
125-
if !changed && (apply_ident_filter || encoding.is_some() || would_convert_eol) {
122+
if !in_buffer && (apply_ident_filter || encoding.is_some() || would_convert_eol) {
126123
self.bufs.clear();
127124
src.read_to_end(&mut self.bufs.src)?;
125+
in_buffer = true;
128126
}
129127

130128
if let Some(encoding) = encoding {
@@ -139,28 +137,25 @@ impl Pipeline {
139137
},
140138
)?;
141139
self.bufs.swap();
142-
changed = true;
143140
}
144141

145142
if eol::convert_to_git(
146143
&self.bufs.src,
147144
digest,
148145
&mut self.bufs.dest,
149-
&mut |buf| index_object(bstr_path.as_ref(), buf),
146+
&mut |buf| index_object(buf),
150147
eol::convert_to_git::Options {
151148
round_trip_check: self.options.crlf_roundtrip_check.to_eol_roundtrip_check(rela_path),
152149
config: self.options.eol_config,
153150
},
154151
)? {
155152
self.bufs.swap();
156-
changed = true;
157153
}
158154

159155
if apply_ident_filter && ident::undo(&self.bufs.src, &mut self.bufs.dest) {
160156
self.bufs.swap();
161-
changed = true;
162157
}
163-
Ok(if changed {
158+
Ok(if in_buffer {
164159
ToGitOutcome::Buffer(&self.bufs.src)
165160
} else {
166161
ToGitOutcome::Unchanged(src)
@@ -325,12 +320,12 @@ where
325320
}
326321
}
327322

328-
impl<R> ToGitOutcome<'_, R>
323+
impl<'a, R> ToGitOutcome<'a, R>
329324
where
330325
R: std::io::Read,
331326
{
332327
/// If we contain a buffer, and not a stream, return it.
333-
pub fn as_bytes(&self) -> Option<&[u8]> {
328+
pub fn as_bytes(&self) -> Option<&'a [u8]> {
334329
match self {
335330
ToGitOutcome::Unchanged(_) | ToGitOutcome::Process(_) => None,
336331
ToGitOutcome::Buffer(b) => Some(b),

gix-filter/tests/pipeline/convert_to_git.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{io::Read, path::Path};
22

3-
use bstr::{BStr, ByteSlice};
3+
use bstr::ByteSlice;
44
use gix_filter::{eol, pipeline::CrlfRoundTripCheck};
55

66
use crate::{driver::apply::driver_with_process, pipeline::pipeline};
@@ -132,14 +132,11 @@ fn no_filter_means_reader_is_returned_unchanged() -> gix_testtools::Result {
132132
}
133133

134134
#[allow(clippy::ptr_arg)]
135-
fn no_call(_path: &BStr, _buf: &mut Vec<u8>) -> Result<Option<()>, Box<dyn std::error::Error + Send + Sync>> {
135+
fn no_call(_buf: &mut Vec<u8>) -> Result<Option<()>, Box<dyn std::error::Error + Send + Sync>> {
136136
unreachable!("index function will not be called")
137137
}
138138

139139
#[allow(clippy::ptr_arg)]
140-
fn no_object_in_index(
141-
_path: &BStr,
142-
_buf: &mut Vec<u8>,
143-
) -> Result<Option<()>, Box<dyn std::error::Error + Send + Sync>> {
140+
fn no_object_in_index(_buf: &mut Vec<u8>) -> Result<Option<()>, Box<dyn std::error::Error + Send + Sync>> {
144141
Ok(None)
145142
}

0 commit comments

Comments
 (0)