Show More
@@ -1,100 +1,101 b'' | |||
|
1 | 1 | use crate::error::CommandError; |
|
2 | 2 | use crate::ui::Ui; |
|
3 | use crate::ui::UiError; | |
|
4 | use crate::utils::path_utils::relativize_paths; | |
|
3 | use crate::utils::path_utils::RelativizePaths; | |
|
5 | 4 | use clap::Arg; |
|
6 | 5 | use hg::errors::HgError; |
|
7 | 6 | use hg::operations::list_rev_tracked_files; |
|
8 | 7 | use hg::operations::Dirstate; |
|
9 | 8 | use hg::repo::Repo; |
|
10 | 9 | use hg::utils::hg_path::HgPath; |
|
11 | use std::borrow::Cow; | |
|
12 | 10 | |
|
13 | 11 | pub const HELP_TEXT: &str = " |
|
14 | 12 | List tracked files. |
|
15 | 13 | |
|
16 | 14 | Returns 0 on success. |
|
17 | 15 | "; |
|
18 | 16 | |
|
19 | 17 | pub fn args() -> clap::App<'static, 'static> { |
|
20 | 18 | clap::SubCommand::with_name("files") |
|
21 | 19 | .arg( |
|
22 | 20 | Arg::with_name("rev") |
|
23 | 21 | .help("search the repository as it is in REV") |
|
24 | 22 | .short("-r") |
|
25 | 23 | .long("--revision") |
|
26 | 24 | .value_name("REV") |
|
27 | 25 | .takes_value(true), |
|
28 | 26 | ) |
|
29 | 27 | .about(HELP_TEXT) |
|
30 | 28 | } |
|
31 | 29 | |
|
32 | 30 | pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> { |
|
33 | 31 | let relative = invocation.config.get(b"ui", b"relative-paths"); |
|
34 | 32 | if relative.is_some() { |
|
35 | 33 | return Err(CommandError::unsupported( |
|
36 | 34 | "non-default ui.relative-paths", |
|
37 | 35 | )); |
|
38 | 36 | } |
|
39 | 37 | |
|
40 | 38 | let rev = invocation.subcommand_args.value_of("rev"); |
|
41 | 39 | |
|
42 | 40 | let repo = invocation.repo?; |
|
43 | 41 | |
|
44 | 42 | // It seems better if this check is removed: this would correspond to |
|
45 | 43 | // automatically enabling the extension if the repo requires it. |
|
46 | 44 | // However we need this check to be in sync with vanilla hg so hg tests |
|
47 | 45 | // pass. |
|
48 | 46 | if repo.has_sparse() |
|
49 | 47 | && invocation.config.get(b"extensions", b"sparse").is_none() |
|
50 | 48 | { |
|
51 | 49 | return Err(CommandError::unsupported( |
|
52 | 50 | "repo is using sparse, but sparse extension is not enabled", |
|
53 | 51 | )); |
|
54 | 52 | } |
|
55 | 53 | |
|
56 | 54 | if let Some(rev) = rev { |
|
57 | 55 | if repo.has_narrow() { |
|
58 | 56 | return Err(CommandError::unsupported( |
|
59 | 57 | "rhg files -r <rev> is not supported in narrow clones", |
|
60 | 58 | )); |
|
61 | 59 | } |
|
62 | 60 | let files = list_rev_tracked_files(repo, rev).map_err(|e| (e, rev))?; |
|
63 | 61 | display_files(invocation.ui, repo, files.iter()) |
|
64 | 62 | } else { |
|
65 | 63 | // The dirstate always reflects the sparse narrowspec, so if |
|
66 | 64 | // we only have sparse without narrow all is fine. |
|
67 | 65 | // If we have narrow, then [hg files] needs to check if |
|
68 | 66 | // the store narrowspec is in sync with the one of the dirstate, |
|
69 | 67 | // so we can't support that without explicit code. |
|
70 | 68 | if repo.has_narrow() { |
|
71 | 69 | return Err(CommandError::unsupported( |
|
72 | 70 | "rhg files is not supported in narrow clones", |
|
73 | 71 | )); |
|
74 | 72 | } |
|
75 | 73 | let distate = Dirstate::new(repo)?; |
|
76 | 74 | let files = distate.tracked_files()?; |
|
77 | 75 | display_files(invocation.ui, repo, files.into_iter().map(Ok)) |
|
78 | 76 | } |
|
79 | 77 | } |
|
80 | 78 | |
|
81 | 79 | fn display_files<'a>( |
|
82 | 80 | ui: &Ui, |
|
83 | 81 | repo: &Repo, |
|
84 | 82 | files: impl IntoIterator<Item = Result<&'a HgPath, HgError>>, |
|
85 | 83 | ) -> Result<(), CommandError> { |
|
86 | 84 | let mut stdout = ui.stdout_buffer(); |
|
87 | 85 | let mut any = false; |
|
88 | 86 | |
|
89 | relativize_paths(repo, files, |path: Cow<[u8]>| -> Result<(), UiError> { | |
|
87 | let relativize = RelativizePaths::new(repo)?; | |
|
88 | for result in files { | |
|
89 | let path = result?; | |
|
90 | stdout.write_all(&relativize.relativize(path))?; | |
|
91 | stdout.write_all(b"\n")?; | |
|
90 | 92 | any = true; |
|
91 | stdout.write_all(path.as_ref())?; | |
|
92 | stdout.write_all(b"\n") | |
|
93 | })?; | |
|
93 | } | |
|
94 | ||
|
94 | 95 | stdout.flush()?; |
|
95 | 96 | if any { |
|
96 | 97 | Ok(()) |
|
97 | 98 | } else { |
|
98 | 99 | Err(CommandError::Unsuccessful) |
|
99 | 100 | } |
|
100 | 101 | } |
@@ -1,465 +1,464 b'' | |||
|
1 | 1 | // status.rs |
|
2 | 2 | // |
|
3 | 3 | // Copyright 2020, Georges Racinet <georges.racinets@octobus.net> |
|
4 | 4 | // |
|
5 | 5 | // This software may be used and distributed according to the terms of the |
|
6 | 6 | // GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | use crate::error::CommandError; |
|
9 | 9 | use crate::ui::Ui; |
|
10 |
use crate::utils::path_utils:: |
|
|
10 | use crate::utils::path_utils::RelativizePaths; | |
|
11 | 11 | use clap::{Arg, SubCommand}; |
|
12 | 12 | use format_bytes::format_bytes; |
|
13 | 13 | use hg; |
|
14 | 14 | use hg::config::Config; |
|
15 | 15 | use hg::dirstate::has_exec_bit; |
|
16 | 16 | use hg::dirstate::TruncatedTimestamp; |
|
17 | 17 | use hg::dirstate::RANGE_MASK_31BIT; |
|
18 | 18 | use hg::errors::{HgError, IoResultExt}; |
|
19 | 19 | use hg::lock::LockError; |
|
20 | 20 | use hg::manifest::Manifest; |
|
21 | 21 | use hg::matchers::AlwaysMatcher; |
|
22 | 22 | use hg::repo::Repo; |
|
23 | 23 | use hg::utils::files::get_bytes_from_os_string; |
|
24 | 24 | use hg::utils::files::get_path_from_bytes; |
|
25 | 25 | use hg::utils::hg_path::{hg_path_to_path_buf, HgPath}; |
|
26 | 26 | use hg::{HgPathCow, StatusOptions}; |
|
27 | 27 | use log::{info, warn}; |
|
28 | 28 | use std::io; |
|
29 | 29 | use std::path::PathBuf; |
|
30 | 30 | |
|
31 | 31 | pub const HELP_TEXT: &str = " |
|
32 | 32 | Show changed files in the working directory |
|
33 | 33 | |
|
34 | 34 | This is a pure Rust version of `hg status`. |
|
35 | 35 | |
|
36 | 36 | Some options might be missing, check the list below. |
|
37 | 37 | "; |
|
38 | 38 | |
|
39 | 39 | pub fn args() -> clap::App<'static, 'static> { |
|
40 | 40 | SubCommand::with_name("status") |
|
41 | 41 | .alias("st") |
|
42 | 42 | .about(HELP_TEXT) |
|
43 | 43 | .arg( |
|
44 | 44 | Arg::with_name("all") |
|
45 | 45 | .help("show status of all files") |
|
46 | 46 | .short("-A") |
|
47 | 47 | .long("--all"), |
|
48 | 48 | ) |
|
49 | 49 | .arg( |
|
50 | 50 | Arg::with_name("modified") |
|
51 | 51 | .help("show only modified files") |
|
52 | 52 | .short("-m") |
|
53 | 53 | .long("--modified"), |
|
54 | 54 | ) |
|
55 | 55 | .arg( |
|
56 | 56 | Arg::with_name("added") |
|
57 | 57 | .help("show only added files") |
|
58 | 58 | .short("-a") |
|
59 | 59 | .long("--added"), |
|
60 | 60 | ) |
|
61 | 61 | .arg( |
|
62 | 62 | Arg::with_name("removed") |
|
63 | 63 | .help("show only removed files") |
|
64 | 64 | .short("-r") |
|
65 | 65 | .long("--removed"), |
|
66 | 66 | ) |
|
67 | 67 | .arg( |
|
68 | 68 | Arg::with_name("clean") |
|
69 | 69 | .help("show only clean files") |
|
70 | 70 | .short("-c") |
|
71 | 71 | .long("--clean"), |
|
72 | 72 | ) |
|
73 | 73 | .arg( |
|
74 | 74 | Arg::with_name("deleted") |
|
75 | 75 | .help("show only deleted files") |
|
76 | 76 | .short("-d") |
|
77 | 77 | .long("--deleted"), |
|
78 | 78 | ) |
|
79 | 79 | .arg( |
|
80 | 80 | Arg::with_name("unknown") |
|
81 | 81 | .help("show only unknown (not tracked) files") |
|
82 | 82 | .short("-u") |
|
83 | 83 | .long("--unknown"), |
|
84 | 84 | ) |
|
85 | 85 | .arg( |
|
86 | 86 | Arg::with_name("ignored") |
|
87 | 87 | .help("show only ignored files") |
|
88 | 88 | .short("-i") |
|
89 | 89 | .long("--ignored"), |
|
90 | 90 | ) |
|
91 | 91 | .arg( |
|
92 | 92 | Arg::with_name("no-status") |
|
93 | 93 | .help("hide status prefix") |
|
94 | 94 | .short("-n") |
|
95 | 95 | .long("--no-status"), |
|
96 | 96 | ) |
|
97 | 97 | } |
|
98 | 98 | |
|
99 | 99 | /// Pure data type allowing the caller to specify file states to display |
|
100 | 100 | #[derive(Copy, Clone, Debug)] |
|
101 | 101 | pub struct DisplayStates { |
|
102 | 102 | pub modified: bool, |
|
103 | 103 | pub added: bool, |
|
104 | 104 | pub removed: bool, |
|
105 | 105 | pub clean: bool, |
|
106 | 106 | pub deleted: bool, |
|
107 | 107 | pub unknown: bool, |
|
108 | 108 | pub ignored: bool, |
|
109 | 109 | } |
|
110 | 110 | |
|
111 | 111 | pub const DEFAULT_DISPLAY_STATES: DisplayStates = DisplayStates { |
|
112 | 112 | modified: true, |
|
113 | 113 | added: true, |
|
114 | 114 | removed: true, |
|
115 | 115 | clean: false, |
|
116 | 116 | deleted: true, |
|
117 | 117 | unknown: true, |
|
118 | 118 | ignored: false, |
|
119 | 119 | }; |
|
120 | 120 | |
|
121 | 121 | pub const ALL_DISPLAY_STATES: DisplayStates = DisplayStates { |
|
122 | 122 | modified: true, |
|
123 | 123 | added: true, |
|
124 | 124 | removed: true, |
|
125 | 125 | clean: true, |
|
126 | 126 | deleted: true, |
|
127 | 127 | unknown: true, |
|
128 | 128 | ignored: true, |
|
129 | 129 | }; |
|
130 | 130 | |
|
131 | 131 | impl DisplayStates { |
|
132 | 132 | pub fn is_empty(&self) -> bool { |
|
133 | 133 | !(self.modified |
|
134 | 134 | || self.added |
|
135 | 135 | || self.removed |
|
136 | 136 | || self.clean |
|
137 | 137 | || self.deleted |
|
138 | 138 | || self.unknown |
|
139 | 139 | || self.ignored) |
|
140 | 140 | } |
|
141 | 141 | } |
|
142 | 142 | |
|
143 | 143 | pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> { |
|
144 | 144 | let status_enabled_default = false; |
|
145 | 145 | let status_enabled = invocation.config.get_option(b"rhg", b"status")?; |
|
146 | 146 | if !status_enabled.unwrap_or(status_enabled_default) { |
|
147 | 147 | return Err(CommandError::unsupported( |
|
148 | 148 | "status is experimental in rhg (enable it with 'rhg.status = true' \ |
|
149 | 149 | or enable fallback with 'rhg.on-unsupported = fallback')" |
|
150 | 150 | )); |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | // TODO: lift these limitations |
|
154 | 154 | if invocation.config.get_bool(b"ui", b"tweakdefaults")? { |
|
155 | 155 | return Err(CommandError::unsupported( |
|
156 | 156 | "ui.tweakdefaults is not yet supported with rhg status", |
|
157 | 157 | )); |
|
158 | 158 | } |
|
159 | 159 | if invocation.config.get_bool(b"ui", b"statuscopies")? { |
|
160 | 160 | return Err(CommandError::unsupported( |
|
161 | 161 | "ui.statuscopies is not yet supported with rhg status", |
|
162 | 162 | )); |
|
163 | 163 | } |
|
164 | 164 | if invocation |
|
165 | 165 | .config |
|
166 | 166 | .get(b"commands", b"status.terse") |
|
167 | 167 | .is_some() |
|
168 | 168 | { |
|
169 | 169 | return Err(CommandError::unsupported( |
|
170 | 170 | "status.terse is not yet supported with rhg status", |
|
171 | 171 | )); |
|
172 | 172 | } |
|
173 | 173 | |
|
174 | 174 | let ui = invocation.ui; |
|
175 | 175 | let config = invocation.config; |
|
176 | 176 | let args = invocation.subcommand_args; |
|
177 | 177 | let display_states = if args.is_present("all") { |
|
178 | 178 | // TODO when implementing `--quiet`: it excludes clean files |
|
179 | 179 | // from `--all` |
|
180 | 180 | ALL_DISPLAY_STATES |
|
181 | 181 | } else { |
|
182 | 182 | let requested = DisplayStates { |
|
183 | 183 | modified: args.is_present("modified"), |
|
184 | 184 | added: args.is_present("added"), |
|
185 | 185 | removed: args.is_present("removed"), |
|
186 | 186 | clean: args.is_present("clean"), |
|
187 | 187 | deleted: args.is_present("deleted"), |
|
188 | 188 | unknown: args.is_present("unknown"), |
|
189 | 189 | ignored: args.is_present("ignored"), |
|
190 | 190 | }; |
|
191 | 191 | if requested.is_empty() { |
|
192 | 192 | DEFAULT_DISPLAY_STATES |
|
193 | 193 | } else { |
|
194 | 194 | requested |
|
195 | 195 | } |
|
196 | 196 | }; |
|
197 | 197 | let no_status = args.is_present("no-status"); |
|
198 | 198 | |
|
199 | 199 | let repo = invocation.repo?; |
|
200 | 200 | |
|
201 | 201 | if repo.has_sparse() || repo.has_narrow() { |
|
202 | 202 | return Err(CommandError::unsupported( |
|
203 | 203 | "rhg status is not supported for sparse checkouts or narrow clones yet" |
|
204 | 204 | )); |
|
205 | 205 | } |
|
206 | 206 | |
|
207 | 207 | let mut dmap = repo.dirstate_map_mut()?; |
|
208 | 208 | |
|
209 | 209 | let options = StatusOptions { |
|
210 | 210 | // we're currently supporting file systems with exec flags only |
|
211 | 211 | // anyway |
|
212 | 212 | check_exec: true, |
|
213 | 213 | list_clean: display_states.clean, |
|
214 | 214 | list_unknown: display_states.unknown, |
|
215 | 215 | list_ignored: display_states.ignored, |
|
216 | 216 | collect_traversed_dirs: false, |
|
217 | 217 | }; |
|
218 | 218 | let (mut ds_status, pattern_warnings) = dmap.status( |
|
219 | 219 | &AlwaysMatcher, |
|
220 | 220 | repo.working_directory_path().to_owned(), |
|
221 | 221 | ignore_files(repo, config), |
|
222 | 222 | options, |
|
223 | 223 | )?; |
|
224 | 224 | if !pattern_warnings.is_empty() { |
|
225 | 225 | warn!("Pattern warnings: {:?}", &pattern_warnings); |
|
226 | 226 | } |
|
227 | 227 | |
|
228 | 228 | if !ds_status.bad.is_empty() { |
|
229 | 229 | warn!("Bad matches {:?}", &(ds_status.bad)) |
|
230 | 230 | } |
|
231 | 231 | if !ds_status.unsure.is_empty() { |
|
232 | 232 | info!( |
|
233 | 233 | "Files to be rechecked by retrieval from filelog: {:?}", |
|
234 | 234 | &ds_status.unsure |
|
235 | 235 | ); |
|
236 | 236 | } |
|
237 | 237 | let mut fixup = Vec::new(); |
|
238 | 238 | if !ds_status.unsure.is_empty() |
|
239 | 239 | && (display_states.modified || display_states.clean) |
|
240 | 240 | { |
|
241 | 241 | let p1 = repo.dirstate_parents()?.p1; |
|
242 | 242 | let manifest = repo.manifest_for_node(p1).map_err(|e| { |
|
243 | 243 | CommandError::from((e, &*format!("{:x}", p1.short()))) |
|
244 | 244 | })?; |
|
245 | 245 | for to_check in ds_status.unsure { |
|
246 | 246 | if unsure_is_modified(repo, &manifest, &to_check)? { |
|
247 | 247 | if display_states.modified { |
|
248 | 248 | ds_status.modified.push(to_check); |
|
249 | 249 | } |
|
250 | 250 | } else { |
|
251 | 251 | if display_states.clean { |
|
252 | 252 | ds_status.clean.push(to_check.clone()); |
|
253 | 253 | } |
|
254 | 254 | fixup.push(to_check.into_owned()) |
|
255 | 255 | } |
|
256 | 256 | } |
|
257 | 257 | } |
|
258 | 258 | let relative_paths = (!ui.plain()) |
|
259 | 259 | && config |
|
260 | 260 | .get_option(b"commands", b"status.relative")? |
|
261 | 261 | .unwrap_or(config.get_bool(b"ui", b"relative-paths")?); |
|
262 | 262 | let output = DisplayStatusPaths { |
|
263 | 263 | ui, |
|
264 | repo, | |
|
265 | 264 | no_status, |
|
266 |
relative_paths |
|
|
265 | relativize: if relative_paths { | |
|
266 | Some(RelativizePaths::new(repo)?) | |
|
267 | } else { | |
|
268 | None | |
|
269 | }, | |
|
267 | 270 | }; |
|
268 | 271 | if display_states.modified { |
|
269 | 272 | output.display(b"M", ds_status.modified)?; |
|
270 | 273 | } |
|
271 | 274 | if display_states.added { |
|
272 | 275 | output.display(b"A", ds_status.added)?; |
|
273 | 276 | } |
|
274 | 277 | if display_states.removed { |
|
275 | 278 | output.display(b"R", ds_status.removed)?; |
|
276 | 279 | } |
|
277 | 280 | if display_states.deleted { |
|
278 | 281 | output.display(b"!", ds_status.deleted)?; |
|
279 | 282 | } |
|
280 | 283 | if display_states.unknown { |
|
281 | 284 | output.display(b"?", ds_status.unknown)?; |
|
282 | 285 | } |
|
283 | 286 | if display_states.ignored { |
|
284 | 287 | output.display(b"I", ds_status.ignored)?; |
|
285 | 288 | } |
|
286 | 289 | if display_states.clean { |
|
287 | 290 | output.display(b"C", ds_status.clean)?; |
|
288 | 291 | } |
|
289 | 292 | |
|
290 | 293 | let mut dirstate_write_needed = ds_status.dirty; |
|
291 | 294 | let filesystem_time_at_status_start = ds_status |
|
292 | 295 | .filesystem_time_at_status_start |
|
293 | 296 | .map(TruncatedTimestamp::from); |
|
294 | 297 | |
|
295 | 298 | if (fixup.is_empty() || filesystem_time_at_status_start.is_none()) |
|
296 | 299 | && !dirstate_write_needed |
|
297 | 300 | { |
|
298 | 301 | // Nothing to update |
|
299 | 302 | return Ok(()); |
|
300 | 303 | } |
|
301 | 304 | |
|
302 | 305 | // Update the dirstate on disk if we can |
|
303 | 306 | let with_lock_result = |
|
304 | 307 | repo.try_with_wlock_no_wait(|| -> Result<(), CommandError> { |
|
305 | 308 | if let Some(mtime_boundary) = filesystem_time_at_status_start { |
|
306 | 309 | for hg_path in fixup { |
|
307 | 310 | use std::os::unix::fs::MetadataExt; |
|
308 | 311 | let fs_path = hg_path_to_path_buf(&hg_path) |
|
309 | 312 | .expect("HgPath conversion"); |
|
310 | 313 | // Specifically do not reuse `fs_metadata` from |
|
311 | 314 | // `unsure_is_clean` which was needed before reading |
|
312 | 315 | // contents. Here we access metadata again after reading |
|
313 | 316 | // content, in case it changed in the meantime. |
|
314 | 317 | let fs_metadata = repo |
|
315 | 318 | .working_directory_vfs() |
|
316 | 319 | .symlink_metadata(&fs_path)?; |
|
317 | 320 | if let Some(mtime) = |
|
318 | 321 | TruncatedTimestamp::for_reliable_mtime_of( |
|
319 | 322 | &fs_metadata, |
|
320 | 323 | &mtime_boundary, |
|
321 | 324 | ) |
|
322 | 325 | .when_reading_file(&fs_path)? |
|
323 | 326 | { |
|
324 | 327 | let mode = fs_metadata.mode(); |
|
325 | 328 | let size = fs_metadata.len() as u32 & RANGE_MASK_31BIT; |
|
326 | 329 | let mut entry = dmap |
|
327 | 330 | .get(&hg_path)? |
|
328 | 331 | .expect("ambiguous file not in dirstate"); |
|
329 | 332 | entry.set_clean(mode, size, mtime); |
|
330 | 333 | dmap.add_file(&hg_path, entry)?; |
|
331 | 334 | dirstate_write_needed = true |
|
332 | 335 | } |
|
333 | 336 | } |
|
334 | 337 | } |
|
335 | 338 | drop(dmap); // Avoid "already mutably borrowed" RefCell panics |
|
336 | 339 | if dirstate_write_needed { |
|
337 | 340 | repo.write_dirstate()? |
|
338 | 341 | } |
|
339 | 342 | Ok(()) |
|
340 | 343 | }); |
|
341 | 344 | match with_lock_result { |
|
342 | 345 | Ok(closure_result) => closure_result?, |
|
343 | 346 | Err(LockError::AlreadyHeld) => { |
|
344 | 347 | // Not updating the dirstate is not ideal but not critical: |
|
345 | 348 | // don’t keep our caller waiting until some other Mercurial |
|
346 | 349 | // process releases the lock. |
|
347 | 350 | } |
|
348 | 351 | Err(LockError::Other(HgError::IoError { error, .. })) |
|
349 | 352 | if error.kind() == io::ErrorKind::PermissionDenied => |
|
350 | 353 | { |
|
351 | 354 | // `hg status` on a read-only repository is fine |
|
352 | 355 | } |
|
353 | 356 | Err(LockError::Other(error)) => { |
|
354 | 357 | // Report other I/O errors |
|
355 | 358 | Err(error)? |
|
356 | 359 | } |
|
357 | 360 | } |
|
358 | 361 | Ok(()) |
|
359 | 362 | } |
|
360 | 363 | |
|
361 | 364 | fn ignore_files(repo: &Repo, config: &Config) -> Vec<PathBuf> { |
|
362 | 365 | let mut ignore_files = Vec::new(); |
|
363 | 366 | let repo_ignore = repo.working_directory_vfs().join(".hgignore"); |
|
364 | 367 | if repo_ignore.exists() { |
|
365 | 368 | ignore_files.push(repo_ignore) |
|
366 | 369 | } |
|
367 | 370 | for (key, value) in config.iter_section(b"ui") { |
|
368 | 371 | if key == b"ignore" || key.starts_with(b"ignore.") { |
|
369 | 372 | let path = get_path_from_bytes(value); |
|
370 | 373 | // TODO: expand "~/" and environment variable here, like Python |
|
371 | 374 | // does with `os.path.expanduser` and `os.path.expandvars` |
|
372 | 375 | |
|
373 | 376 | let joined = repo.working_directory_path().join(path); |
|
374 | 377 | ignore_files.push(joined); |
|
375 | 378 | } |
|
376 | 379 | } |
|
377 | 380 | ignore_files |
|
378 | 381 | } |
|
379 | 382 | |
|
380 | 383 | struct DisplayStatusPaths<'a> { |
|
381 | 384 | ui: &'a Ui, |
|
382 | repo: &'a Repo, | |
|
383 | 385 | no_status: bool, |
|
384 | relative_paths: bool, | |
|
386 | relativize: Option<RelativizePaths>, | |
|
385 | 387 | } |
|
386 | 388 | |
|
387 | 389 | impl DisplayStatusPaths<'_> { |
|
388 | 390 | // Probably more elegant to use a Deref or Borrow trait rather than |
|
389 | 391 | // harcode HgPathBuf, but probably not really useful at this point |
|
390 | 392 | fn display( |
|
391 | 393 | &self, |
|
392 | 394 | status_prefix: &[u8], |
|
393 | 395 | mut paths: Vec<HgPathCow>, |
|
394 | 396 | ) -> Result<(), CommandError> { |
|
395 | 397 | paths.sort_unstable(); |
|
396 |
|
|
|
398 | for path in paths { | |
|
399 | let relative; | |
|
400 | let path = if let Some(relativize) = &self.relativize { | |
|
401 | relative = relativize.relativize(&path); | |
|
402 | &*relative | |
|
403 | } else { | |
|
404 | path.as_bytes() | |
|
405 | }; | |
|
397 | 406 | // TODO optim, probably lots of unneeded copies here, especially |
|
398 | 407 | // if out stream is buffered |
|
399 | 408 | if self.no_status { |
|
400 | self.ui.write_stdout(&format_bytes!(b"{}\n", path)) | |
|
409 | self.ui.write_stdout(&format_bytes!(b"{}\n", path))? | |
|
401 | 410 | } else { |
|
402 | 411 | self.ui.write_stdout(&format_bytes!( |
|
403 | 412 | b"{} {}\n", |
|
404 | 413 | status_prefix, |
|
405 | 414 | path |
|
406 | )) | |
|
407 | } | |
|
408 | }; | |
|
409 | ||
|
410 | if self.relative_paths { | |
|
411 | relativize_paths(self.repo, paths.iter().map(Ok), |path| { | |
|
412 | print_path(&path) | |
|
413 | })?; | |
|
414 | } else { | |
|
415 | for path in paths { | |
|
416 | print_path(path.as_bytes())? | |
|
415 | ))? | |
|
417 | 416 | } |
|
418 | 417 | } |
|
419 | 418 | Ok(()) |
|
420 | 419 | } |
|
421 | 420 | } |
|
422 | 421 | |
|
423 | 422 | /// Check if a file is modified by comparing actual repo store and file system. |
|
424 | 423 | /// |
|
425 | 424 | /// This meant to be used for those that the dirstate cannot resolve, due |
|
426 | 425 | /// to time resolution limits. |
|
427 | 426 | fn unsure_is_modified( |
|
428 | 427 | repo: &Repo, |
|
429 | 428 | manifest: &Manifest, |
|
430 | 429 | hg_path: &HgPath, |
|
431 | 430 | ) -> Result<bool, HgError> { |
|
432 | 431 | let vfs = repo.working_directory_vfs(); |
|
433 | 432 | let fs_path = hg_path_to_path_buf(hg_path).expect("HgPath conversion"); |
|
434 | 433 | let fs_metadata = vfs.symlink_metadata(&fs_path)?; |
|
435 | 434 | let is_symlink = fs_metadata.file_type().is_symlink(); |
|
436 | 435 | // TODO: Also account for `FALLBACK_SYMLINK` and `FALLBACK_EXEC` from the |
|
437 | 436 | // dirstate |
|
438 | 437 | let fs_flags = if is_symlink { |
|
439 | 438 | Some(b'l') |
|
440 | 439 | } else if has_exec_bit(&fs_metadata) { |
|
441 | 440 | Some(b'x') |
|
442 | 441 | } else { |
|
443 | 442 | None |
|
444 | 443 | }; |
|
445 | 444 | |
|
446 | 445 | let entry = manifest |
|
447 | 446 | .find_file(hg_path)? |
|
448 | 447 | .expect("ambgious file not in p1"); |
|
449 | 448 | if entry.flags != fs_flags { |
|
450 | 449 | return Ok(true); |
|
451 | 450 | } |
|
452 | 451 | let filelog = repo.filelog(hg_path)?; |
|
453 | 452 | let filelog_entry = |
|
454 | 453 | filelog.data_for_node(entry.node_id()?).map_err(|_| { |
|
455 | 454 | HgError::corrupted("filelog missing node from manifest") |
|
456 | 455 | })?; |
|
457 | 456 | let contents_in_p1 = filelog_entry.data()?; |
|
458 | 457 | |
|
459 | 458 | let fs_contents = if is_symlink { |
|
460 | 459 | get_bytes_from_os_string(vfs.read_link(fs_path)?.into_os_string()) |
|
461 | 460 | } else { |
|
462 | 461 | vfs.read(fs_path)? |
|
463 | 462 | }; |
|
464 | 463 | Ok(contents_in_p1 != &*fs_contents) |
|
465 | 464 | } |
@@ -1,49 +1,55 b'' | |||
|
1 | 1 | // path utils module |
|
2 | 2 | // |
|
3 | 3 | // This software may be used and distributed according to the terms of the |
|
4 | 4 | // GNU General Public License version 2 or any later version. |
|
5 | 5 | |
|
6 | use crate::error::CommandError; | |
|
7 | use crate::ui::UiError; | |
|
8 | 6 | use hg::errors::HgError; |
|
9 | 7 | use hg::repo::Repo; |
|
10 | 8 | use hg::utils::current_dir; |
|
11 | 9 | use hg::utils::files::{get_bytes_from_path, relativize_path}; |
|
12 | 10 | use hg::utils::hg_path::HgPath; |
|
13 | 11 | use hg::utils::hg_path::HgPathBuf; |
|
14 | 12 | use std::borrow::Cow; |
|
15 | 13 | |
|
16 |
pub |
|
|
17 | repo: &Repo, | |
|
18 | paths: impl IntoIterator<Item = Result<impl AsRef<HgPath>, HgError>>, | |
|
19 | mut callback: impl FnMut(Cow<[u8]>) -> Result<(), UiError>, | |
|
20 | ) -> Result<(), CommandError> { | |
|
14 | pub struct RelativizePaths { | |
|
15 | repo_root: HgPathBuf, | |
|
16 | cwd: HgPathBuf, | |
|
17 | outside_repo: bool, | |
|
18 | } | |
|
19 | ||
|
20 | impl RelativizePaths { | |
|
21 | pub fn new(repo: &Repo) -> Result<Self, HgError> { | |
|
21 | 22 | let cwd = current_dir()?; |
|
22 | 23 | let repo_root = repo.working_directory_path(); |
|
23 | 24 | let repo_root = cwd.join(repo_root); // Make it absolute |
|
24 | 25 | let repo_root_hgpath = |
|
25 | 26 | HgPathBuf::from(get_bytes_from_path(repo_root.to_owned())); |
|
26 | let outside_repo: bool; | |
|
27 | let cwd_hgpath: HgPathBuf; | |
|
28 | 27 | |
|
29 | 28 | if let Ok(cwd_relative_to_repo) = cwd.strip_prefix(&repo_root) { |
|
30 | 29 | // The current directory is inside the repo, so we can work with |
|
31 | 30 | // relative paths |
|
32 | outside_repo = false; | |
|
33 | cwd_hgpath = | |
|
34 |
HgPathBuf::from(get_bytes_from_path( |
|
|
31 | Ok(Self { | |
|
32 | repo_root: repo_root_hgpath, | |
|
33 | cwd: HgPathBuf::from(get_bytes_from_path( | |
|
34 | cwd_relative_to_repo, | |
|
35 | )), | |
|
36 | outside_repo: false, | |
|
37 | }) | |
|
35 | 38 | } else { |
|
36 | outside_repo = true; | |
|
37 | cwd_hgpath = HgPathBuf::from(get_bytes_from_path(cwd)); | |
|
39 | Ok(Self { | |
|
40 | repo_root: repo_root_hgpath, | |
|
41 | cwd: HgPathBuf::from(get_bytes_from_path(cwd)), | |
|
42 | outside_repo: true, | |
|
43 | }) | |
|
44 | } | |
|
38 | 45 | } |
|
39 | 46 | |
|
40 | for file in paths { | |
|
41 | if outside_repo { | |
|
42 |
let |
|
|
43 |
|
|
|
47 | pub fn relativize<'a>(&self, path: &'a HgPath) -> Cow<'a, [u8]> { | |
|
48 | if self.outside_repo { | |
|
49 | let joined = self.repo_root.join(path); | |
|
50 | Cow::Owned(relativize_path(&joined, &self.cwd).into_owned()) | |
|
44 | 51 | } else { |
|
45 | callback(relativize_path(file?.as_ref(), &cwd_hgpath))?; | |
|
52 | relativize_path(path, &self.cwd) | |
|
46 | 53 | } |
|
47 | 54 | } |
|
48 | Ok(()) | |
|
49 | 55 | } |
General Comments 0
You need to be logged in to leave comments.
Login now