| 1 |
//! All of hg-core's warnings, to be exposed to its consumers so they can |
| 2 |
//! handle the actual formatting. |
| 3 |
|
| 4 |
use crossbeam_channel::Receiver; |
| 5 |
use crossbeam_channel::Sender; |
| 6 |
|
| 7 |
use crate::file_patterns::PatternFileWarning; |
| 8 |
use crate::sparse::SparseNarrowWarning; |
| 9 |
use crate::update::UpdateWarning; |
| 10 |
|
| 11 |
/// A warning in any Mercurial logic |
| 12 |
#[derive(Debug, derive_more::From)] |
| 13 |
pub enum HgWarning { |
| 14 |
SparseNarrow(SparseNarrowWarning), |
| 15 |
PatternFile(PatternFileWarning), |
| 16 |
Update(UpdateWarning), |
| 17 |
} |
| 18 |
|
| 19 |
/// A simple convenience wrapper around a [`Sender<HgWarning>`]. |
| 20 |
pub struct HgWarningSender { |
| 21 |
inner: Sender<HgWarning>, |
| 22 |
} |
| 23 |
|
| 24 |
impl HgWarningSender { |
| 25 |
/// Send a warning through the channel. |
| 26 |
/// |
| 27 |
/// # Panics |
| 28 |
/// |
| 29 |
/// Panics if the channel has been closed. |
| 30 |
pub fn send<T: Into<HgWarning>>(&self, warning: T) { |
| 31 |
self.inner.send(warning.into()).expect("warning channel must stay open") |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
// This way we keep track of the sender at compile-time and don't risk causing |
| 36 |
// a deadlock because a sender has been cloned, unless someone does something |
| 37 |
// quite careless and explicit with `mem::forget` or something. |
| 38 |
static_assertions_next::assert_impl!(HgWarningSender: !Clone); |
| 39 |
|
| 40 |
/// Manages the context for the passing of [`HgWarning`] to high-level logic. |
| 41 |
/// It is the only way of getting an `HgWarningSender`, which lower-level code |
| 42 |
/// expects as the blessed way of bubbling up warnings while being a nicely |
| 43 |
/// behaved library. |
| 44 |
pub struct HgWarningContext { |
| 45 |
sender: HgWarningSender, |
| 46 |
receiver: Receiver<HgWarning>, |
| 47 |
} |
| 48 |
|
| 49 |
impl HgWarningContext { |
| 50 |
/// Returns a brand new context with a new channel. |
| 51 |
pub fn new() -> Self { |
| 52 |
let (sender, receiver) = crossbeam_channel::unbounded(); |
| 53 |
Self { sender: HgWarningSender { inner: sender }, receiver } |
| 54 |
} |
| 55 |
|
| 56 |
/// Returns a reference to the sender for this context. |
| 57 |
pub fn sender(&self) -> &HgWarningSender { |
| 58 |
&self.sender |
| 59 |
} |
| 60 |
|
| 61 |
// TODO progressive/polled display of warnings |
| 62 |
|
| 63 |
/// Ends the warning context with a way of handling queued up messages. |
| 64 |
/// |
| 65 |
/// The sender is first dropped, then every message in the channel is passed |
| 66 |
/// to the `on_message` callback, and then the context is dropped. |
| 67 |
pub fn finish<E>( |
| 68 |
self, |
| 69 |
mut on_message: impl FnMut(HgWarning) -> Result<(), E>, |
| 70 |
) -> Result<(), E> { |
| 71 |
drop(self.sender); |
| 72 |
for warning in self.receiver.iter() { |
| 73 |
on_message(warning)?; |
| 74 |
} |
| 75 |
Ok(()) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
impl Default for HgWarningContext { |
| 80 |
fn default() -> Self { |
| 81 |
Self::new() |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/// /!\ |
| 86 |
/// This module shouldn't really exist in `hg-core`, since it's the consumer |
| 87 |
/// crates' responsibility to handle any user-facing formatting. However, |
| 88 |
/// both `rhg` and `hg-pyo3` share the exact same behavior w.r.t displaying |
| 89 |
/// warnings, so we keep it in here. |
| 90 |
/// Creating a separate crate for this seems heavy-handed, so make sure to not |
| 91 |
/// use anything that wouldn't work (the orphan rule in particular). |
| 92 |
/// /!\ |
| 93 |
pub mod format { |
| 94 |
use std::path::Path; |
| 95 |
|
| 96 |
use format_bytes::write_bytes; |
| 97 |
|
| 98 |
use super::*; |
| 99 |
use crate::utils::files::get_bytes_from_path; |
| 100 |
|
| 101 |
/// See this module's doc for why this function is in `hg-core`. |
| 102 |
#[inline(always)] |
| 103 |
pub fn write_warning( |
| 104 |
warning: &HgWarning, |
| 105 |
output: &mut dyn std::io::Write, |
| 106 |
working_directory: &Path, |
| 107 |
) -> std::io::Result<()> { |
| 108 |
match warning { |
| 109 |
HgWarning::SparseNarrow(w) => { |
| 110 |
write_sparse_narrow_warning(w, output) |
| 111 |
} |
| 112 |
HgWarning::PatternFile(w) => { |
| 113 |
write_pattern_file_warning(w, output, working_directory) |
| 114 |
} |
| 115 |
HgWarning::Update(w) => { |
| 116 |
write_update_warning(w, output, working_directory) |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
fn write_sparse_narrow_warning( |
| 122 |
warning: &SparseNarrowWarning, |
| 123 |
output: &mut dyn std::io::Write, |
| 124 |
) -> std::io::Result<()> { |
| 125 |
match warning { |
| 126 |
SparseNarrowWarning::RootWarning { context, line } => write_bytes!( |
| 127 |
output, |
| 128 |
b"warning: {} profile cannot use paths \ |
| 129 |
starting with /, ignoring {}\n", |
| 130 |
context, |
| 131 |
line |
| 132 |
), |
| 133 |
SparseNarrowWarning::ProfileNotFound { profile, node } => { |
| 134 |
write_bytes!( |
| 135 |
output, |
| 136 |
b"warning: sparse profile '{}' not found \ |
| 137 |
in rev {} - ignoring it\n", |
| 138 |
profile, |
| 139 |
node.map(|n| format!("{:x}", n.short())) |
| 140 |
.unwrap_or_else(|| String::from("unknown")) |
| 141 |
.as_bytes() |
| 142 |
) |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
fn write_pattern_file_warning( |
| 148 |
warning: &PatternFileWarning, |
| 149 |
output: &mut dyn std::io::Write, |
| 150 |
working_directory: &Path, |
| 151 |
) -> std::io::Result<()> { |
| 152 |
match warning { |
| 153 |
PatternFileWarning::InvalidSyntax(path, syntax) => { |
| 154 |
write_bytes!( |
| 155 |
output, |
| 156 |
b"{}: ignoring invalid syntax '{}'\n", |
| 157 |
get_bytes_from_path(path), |
| 158 |
syntax |
| 159 |
) |
| 160 |
} |
| 161 |
PatternFileWarning::NoSuchFile(path) => { |
| 162 |
let path = if let Ok(relative) = |
| 163 |
path.strip_prefix(working_directory) |
| 164 |
{ |
| 165 |
relative |
| 166 |
} else { |
| 167 |
path |
| 168 |
}; |
| 169 |
write_bytes!( |
| 170 |
output, |
| 171 |
b"skipping unreadable pattern file '{}': \ |
| 172 |
No such file or directory\n", |
| 173 |
get_bytes_from_path(path), |
| 174 |
) |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
fn write_update_warning( |
| 180 |
warning: &UpdateWarning, |
| 181 |
output: &mut dyn std::io::Write, |
| 182 |
working_directory: &Path, |
| 183 |
) -> std::io::Result<()> { |
| 184 |
match warning { |
| 185 |
UpdateWarning::UnlinkFailure(path, err) => { |
| 186 |
write_bytes!( |
| 187 |
output, |
| 188 |
b"update failed to remove {}: \"{}\"!\n", |
| 189 |
get_bytes_from_path(path), |
| 190 |
err.to_string().as_bytes(), |
| 191 |
) |
| 192 |
} |
| 193 |
UpdateWarning::CwdRemoved => { |
| 194 |
write_bytes!( |
| 195 |
output, |
| 196 |
b"current directory was removed\n\ |
| 197 |
(consider changing to repo root: {})\n", |
| 198 |
get_bytes_from_path(working_directory), |
| 199 |
) |
| 200 |
} |
| 201 |
UpdateWarning::UntrackedConflict(path) => { |
| 202 |
write_bytes!( |
| 203 |
output, |
| 204 |
b"{}: untracked file differs\n", |
| 205 |
path.as_bytes(), |
| 206 |
) |
| 207 |
} |
| 208 |
UpdateWarning::ReplacingUntracked(path) => { |
| 209 |
write_bytes!( |
| 210 |
output, |
| 211 |
b"{}: replacing untracked file\n", |
| 212 |
path.as_bytes(), |
| 213 |
) |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
} |