mirror of
https://github.com/martinvonz/jj.git
synced 2025-05-05 15:32:49 +00:00
merge-tools: builtin: refactor mapping of binary files
We no longer need this, but I think this is easier to follow than destructuring FileContents in larger match block.
This commit is contained in:
parent
968806bc64
commit
27a35a79b1
@ -63,6 +63,23 @@ enum FileContents {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FileContents {
|
||||||
|
fn describe(&self) -> Option<String> {
|
||||||
|
match self {
|
||||||
|
FileContents::Absent => None,
|
||||||
|
FileContents::Text {
|
||||||
|
contents: _,
|
||||||
|
hash,
|
||||||
|
num_bytes,
|
||||||
|
}
|
||||||
|
| FileContents::Binary { hash, num_bytes } => match hash {
|
||||||
|
Some(hash) => Some(format!("{hash} ({num_bytes}B)")),
|
||||||
|
None => Some(format!("({num_bytes}B)")),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Information about a file that was read from disk. Note that the file may not
|
/// Information about a file that was read from disk. Note that the file may not
|
||||||
/// have existed, in which case its contents will be marked as absent.
|
/// have existed, in which case its contents will be marked as absent.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
@ -84,15 +101,6 @@ mod mode {
|
|||||||
pub const SYMLINK: scm_record::FileMode = scm_record::FileMode::Unix(0o120000);
|
pub const SYMLINK: scm_record::FileMode = scm_record::FileMode::Unix(0o120000);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn describe_binary(hash: Option<&str>, num_bytes: u64) -> String {
|
|
||||||
match hash {
|
|
||||||
Some(hash) => {
|
|
||||||
format!("{hash} ({num_bytes}B)")
|
|
||||||
}
|
|
||||||
None => format!("({num_bytes}B)"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn buf_to_file_contents(hash: Option<String>, buf: Vec<u8>) -> FileContents {
|
fn buf_to_file_contents(hash: Option<String>, buf: Vec<u8>) -> FileContents {
|
||||||
let num_bytes: u64 = buf.len().try_into().unwrap();
|
let num_bytes: u64 = buf.len().try_into().unwrap();
|
||||||
let text = if buf.contains(&0) {
|
let text = if buf.contains(&0) {
|
||||||
@ -307,14 +315,6 @@ async fn make_diff_files(
|
|||||||
lines: make_section_changed_lines(&contents, scm_record::ChangeType::Added),
|
lines: make_section_changed_lines(&contents, scm_record::ChangeType::Added),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
(FileContents::Absent, FileContents::Binary { hash, num_bytes }) => {
|
|
||||||
sections.push(scm_record::Section::Binary {
|
|
||||||
is_checked: false,
|
|
||||||
old_description: None,
|
|
||||||
new_description: Some(Cow::Owned(describe_binary(hash.as_deref(), num_bytes))),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
(
|
(
|
||||||
FileContents::Text {
|
FileContents::Text {
|
||||||
contents,
|
contents,
|
||||||
@ -341,42 +341,12 @@ async fn make_diff_files(
|
|||||||
sections.extend(make_diff_sections(&old_contents, &new_contents)?);
|
sections.extend(make_diff_sections(&old_contents, &new_contents)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
(
|
(left, right @ FileContents::Binary { .. })
|
||||||
FileContents::Text {
|
| (left @ FileContents::Binary { .. }, right) => {
|
||||||
contents: _,
|
|
||||||
hash: old_hash,
|
|
||||||
num_bytes: old_num_bytes,
|
|
||||||
}
|
|
||||||
| FileContents::Binary {
|
|
||||||
hash: old_hash,
|
|
||||||
num_bytes: old_num_bytes,
|
|
||||||
},
|
|
||||||
FileContents::Text {
|
|
||||||
contents: _,
|
|
||||||
hash: new_hash,
|
|
||||||
num_bytes: new_num_bytes,
|
|
||||||
}
|
|
||||||
| FileContents::Binary {
|
|
||||||
hash: new_hash,
|
|
||||||
num_bytes: new_num_bytes,
|
|
||||||
},
|
|
||||||
) => sections.push(scm_record::Section::Binary {
|
|
||||||
is_checked: false,
|
|
||||||
old_description: Some(Cow::Owned(describe_binary(
|
|
||||||
old_hash.as_deref(),
|
|
||||||
old_num_bytes,
|
|
||||||
))),
|
|
||||||
new_description: Some(Cow::Owned(describe_binary(
|
|
||||||
new_hash.as_deref(),
|
|
||||||
new_num_bytes,
|
|
||||||
))),
|
|
||||||
}),
|
|
||||||
|
|
||||||
(FileContents::Binary { hash, num_bytes }, FileContents::Absent) => {
|
|
||||||
sections.push(scm_record::Section::Binary {
|
sections.push(scm_record::Section::Binary {
|
||||||
is_checked: false,
|
is_checked: false,
|
||||||
old_description: Some(Cow::Owned(describe_binary(hash.as_deref(), num_bytes))),
|
old_description: left.describe().map(Cow::Owned),
|
||||||
new_description: None,
|
new_description: right.describe().map(Cow::Owned),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -539,11 +509,11 @@ fn make_merge_sections(
|
|||||||
.map(|line| Cow::Owned(line.to_owned()))
|
.map(|line| Cow::Owned(line.to_owned()))
|
||||||
.collect(),
|
.collect(),
|
||||||
}),
|
}),
|
||||||
FileContents::Binary { hash, num_bytes } => Some(scm_record::Section::Binary {
|
FileContents::Binary { .. } => Some(scm_record::Section::Binary {
|
||||||
// TODO: Perhaps, this should be an "unchanged" section?
|
// TODO: Perhaps, this should be an "unchanged" section?
|
||||||
is_checked: false,
|
is_checked: false,
|
||||||
old_description: None,
|
old_description: None,
|
||||||
new_description: Some(Cow::Owned(describe_binary(hash.as_deref(), num_bytes))),
|
new_description: contents.describe().map(Cow::Owned),
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
if let Some(section) = section {
|
if let Some(section) = section {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user