Repository navigation
Expand file tree
/
Copy pathdiff.rs
More file actions
471 lines (468 loc) · 16.1 KB
/
Copy pathdiff.rs
File metadata and controls
471 lines (468 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Bounded read-only content comparisons; no external diff/textconv or index writes.
use crate::domain::{digest, now_ms, NativePath, ObjectId};
use crate::inspection::{self, data_error, file_bytes, strings, ReadOptions, Session};
use crate::{Error, ErrorKind, Result};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::time::Duration;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "application-services", derive(schemars::JsonSchema))]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum DiffMode {
Staged,
Unstaged,
Historical {
commit: ObjectId,
parent: Option<ObjectId>,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "application-services", derive(schemars::JsonSchema))]
pub struct FileChange {
pub path: NativePath,
pub original_path: Option<NativePath>,
pub change: String,
pub old_mode: String,
pub new_mode: String,
pub old_oid: Option<ObjectId>,
pub new_oid: Option<ObjectId>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "application-services", derive(schemars::JsonSchema))]
pub struct Diff {
pub mode: DiffMode,
pub files: Vec<FileChange>,
pub patch: Vec<u8>,
pub complete: bool,
pub generation: String,
pub observed_at_ms: u64,
}
fn raw(s: &Session<'_>, bytes: &[u8]) -> Result<Vec<FileChange>> {
let mut parts = bytes.split(|b| *b == 0).filter(|v| !v.is_empty());
let mut files = Vec::new();
while let Some(meta) = parts.next() {
let meta =
std::str::from_utf8(meta).map_err(|_| data_error("Invalid raw diff metadata"))?;
let fields: Vec<_> = meta.trim_start_matches(':').split(' ').collect();
if fields.len() != 5 {
return Err(data_error("Invalid raw diff fields"));
}
let object = |text: &str| {
if text.bytes().all(|b| b == b'0') {
Ok(None)
} else {
ObjectId::parse(text, &s.format).map(Some)
}
};
let first = NativePath::from_git(
parts
.next()
.ok_or_else(|| data_error("Missing diff path"))?,
)?;
let rename = fields[4].starts_with('R') || fields[4].starts_with('C');
let (path, original_path) = if rename {
(
NativePath::from_git(
parts
.next()
.ok_or_else(|| data_error("Missing rename path"))?,
)?,
Some(first),
)
} else {
(first, None)
};
files.push(FileChange {
path,
original_path,
change: fields[4].into(),
old_mode: fields[0].into(),
new_mode: fields[1].into(),
old_oid: object(fields[2])?,
new_oid: object(fields[3])?,
});
}
Ok(files)
}
fn numstat_paths(bytes: &[u8]) -> Result<std::collections::BTreeSet<Vec<u8>>> {
let mut parts = bytes.split(|byte| *byte == 0);
let mut paths = std::collections::BTreeSet::new();
while let Some(record) = parts.next() {
if record.is_empty() {
continue;
}
let mut fields = record.splitn(3, |byte| *byte == b'\t');
for _ in 0..2 {
let count = fields
.next()
.ok_or_else(|| data_error("Missing diff count"))?;
if count != b"-" && (count.is_empty() || !count.iter().all(u8::is_ascii_digit)) {
return Err(data_error("Invalid diff count"));
}
}
let path = fields
.next()
.ok_or_else(|| data_error("Missing diff path"))?;
let path = if path.is_empty() {
let old = parts
.next()
.ok_or_else(|| data_error("Missing rename source"))?;
let new = parts
.next()
.ok_or_else(|| data_error("Missing rename destination"))?;
if old.is_empty() || new.is_empty() {
return Err(data_error("Empty rename path"));
}
new
} else {
path
};
paths.insert(path.to_vec());
}
Ok(paths)
}
fn args(s: &Session<'_>, mode: &DiffMode, patch: bool) -> Result<Vec<std::ffi::OsString>> {
let mut args = strings(&[
"diff",
"--no-ext-diff",
"--no-textconv",
"--no-color",
"--submodule=short",
"--no-abbrev",
"--find-renames=50%",
]);
args.push(
if matches!(mode, DiffMode::Unstaged) {
"--ignore-submodules=all"
} else {
"--ignore-submodules=none"
}
.into(),
);
if patch {
args.extend(strings(&["--patch"]));
} else {
args.extend(strings(&["--raw", "-z"]));
}
match mode {
DiffMode::Staged => {
args.push("--cached".into());
}
DiffMode::Unstaged => {}
DiffMode::Historical { commit, parent } => {
let detail = crate::history::commit_in(s, commit)?;
if detail.parents.is_empty() {
if parent.is_some() {
return Err(Error::with_kind(
ErrorKind::InvalidRequest,
"Root commit has no parent",
));
}
args[0] = "diff-tree".into();
args.extend(strings(&["--root", "--no-commit-id", "-r"]));
args.push(commit.hex.clone().into());
} else {
let parent = parent.as_ref().ok_or_else(|| {
Error::with_kind(
ErrorKind::InvalidRequest,
"Historical diff requires an explicit parent",
)
})?;
if !detail.parents.contains(parent) {
return Err(Error::with_kind(
ErrorKind::InvalidRequest,
"Selected parent is not a commit parent",
));
}
args.push(parent.hex.clone().into());
args.push(commit.hex.clone().into());
}
}
}
Ok(args)
}
pub(crate) fn staged_files(s: &Session<'_>) -> Result<Vec<FileChange>> {
let mut command = args(s, &DiffMode::Staged, false)?;
command.push("--".into());
raw(s, &s.git_os(&command)?)
}
pub fn inspect(repo: &Path, mode: DiffMode, path: Option<&NativePath>) -> Result<Diff> {
inspect_with_options(
repo,
mode,
path,
&ReadOptions {
timeout: Duration::from_secs(30),
..ReadOptions::default()
},
)
}
pub fn inspect_with_options(
repo: &Path,
mode: DiffMode,
path: Option<&NativePath>,
options: &ReadOptions,
) -> Result<Diff> {
inspection::read(repo, options, |s| {
let relative = path.map(NativePath::relative).transpose()?;
let mut metadata_args = args(s, &mode, false)?;
let mut patch_args = args(s, &mode, true)?;
for args in [&mut metadata_args, &mut patch_args] {
args.push("--".into());
if let Some(relative) = &relative {
args.push(relative.as_os_str().to_owned());
}
}
let raw_files = raw(s, &s.git_os(&metadata_args)?)?;
let files = if matches!(mode, DiffMode::Unstaged) && !raw_files.is_empty() {
let mut counts_args = metadata_args.clone();
let raw_flag = counts_args
.iter_mut()
.find(|arg| *arg == "--raw")
.ok_or_else(|| data_error("Missing raw diff argument"))?;
*raw_flag = "--numstat".into();
let changed = numstat_paths(&s.git_os(&counts_args)?)?;
let mut files = Vec::new();
for file in &raw_files {
// Raw diff can report stat-cache misses even when blob content is equal.
let path = file.path.to_path()?;
if file.change != "M"
|| file.old_mode != file.new_mode
|| changed.contains(path.as_os_str().as_encoded_bytes())
{
files.push(file.clone());
}
}
files
} else {
raw_files.clone()
};
let current = if matches!(mode, DiffMode::Unstaged) {
raw_files
.iter()
.filter(|file| file.old_mode != "160000" && file.new_mode != "160000")
.map(|file| Ok((&file.path, fingerprint_in(s, &file.path)?)))
.collect::<Result<Vec<_>>>()?
} else {
Vec::new()
};
let patch = s.git_os(&patch_args)?;
for (path, before) in current {
if before != fingerprint_in(s, path)? {
return Err(Error::with_kind(
ErrorKind::ConcurrentChange,
"Working file changed during diff",
));
}
}
if raw(s, &s.git_os(&metadata_args)?)? != raw_files {
return Err(Error::with_kind(
ErrorKind::ConcurrentChange,
"Diff metadata changed during inspection",
));
}
Ok(Diff {
mode,
files,
patch,
complete: true,
generation: s.generation.clone(),
observed_at_ms: now_ms(),
})
})
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "application-services", derive(schemars::JsonSchema))]
pub struct Preview {
pub path: NativePath,
pub kind: String,
pub bytes: Vec<u8>,
pub binary: bool,
pub fingerprint: String,
pub complete: bool,
}
fn fingerprint_in(s: &Session<'_>, path: &NativePath) -> Result<String> {
use sha2::{Digest, Sha256};
use std::io::Read;
s.check()?;
let target = s.safe_path(path)?;
let metadata = match std::fs::symlink_metadata(&target) {
Ok(metadata) => metadata,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(digest(b"missing")),
Err(e) => return Err(e.into()),
};
if metadata.is_symlink() {
let value = std::fs::read_link(&target)?;
let mut hash = Sha256::new();
hash.update(b"symlink");
hash.update(value.as_os_str().as_encoded_bytes());
return Ok(format!("{:x}", hash.finalize()));
}
if !metadata.is_file() {
return Err(Error::with_kind(
ErrorKind::InvalidPath,
"Unsupported preview file type",
));
}
let mut options = std::fs::OpenOptions::new();
options.read(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK);
}
let mut file = options.open(&target)?;
if !file.metadata()?.is_file() {
return Err(Error::with_kind(
ErrorKind::InvalidPath,
"File type changed during inspection",
));
}
if file.metadata()?.len() > 2 * 1024 * 1024 {
return Err(Error::with_kind(
ErrorKind::OutputLimit,
"File exceeds preview limit",
));
}
let mut hash = Sha256::new();
hash.update(b"regular");
let mut size = 0;
let mut buffer = [0; 65536];
loop {
s.check()?;
let n = file.read(&mut buffer)?;
if n == 0 {
break;
}
size += n;
if size > 2 * 1024 * 1024 {
return Err(Error::with_kind(
ErrorKind::OutputLimit,
"File exceeds preview limit",
));
}
hash.update(&buffer[..n]);
#[cfg(test)]
if fingerprint_tests::CANCEL_ON_CHUNK.replace(false) {
s.options.cancellation.cancel();
}
}
Ok(format!("{:x}", hash.finalize()))
}
fn preview_in(s: &Session<'_>, path: &NativePath) -> Result<Preview> {
let target = s.safe_path(path)?;
let metadata = std::fs::symlink_metadata(&target);
let (kind, bytes) = match metadata {
Ok(metadata) if metadata.is_symlink() => (
"symlink",
NativePath::from_path(&std::fs::read_link(target)?)
.to_path()?
.as_os_str()
.as_encoded_bytes()
.to_vec(),
),
Ok(metadata) if metadata.is_file() => ("regular", file_bytes(&target, 2 * 1024 * 1024)?),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => ("missing", Vec::new()),
Err(error) => return Err(error.into()),
_ => {
return Err(Error::with_kind(
ErrorKind::InvalidPath,
"Unsupported preview file type",
))
}
};
let fingerprint = digest(&[kind.as_bytes(), &bytes].concat());
Ok(Preview {
path: path.clone(),
kind: kind.into(),
binary: bytes.contains(&0) || std::str::from_utf8(&bytes).is_err(),
bytes,
fingerprint,
complete: true,
})
}
pub fn preview(repo: &Path, path: &NativePath) -> Result<Preview> {
preview_with_options(repo, path, &ReadOptions::default())
}
pub fn preview_with_options(
repo: &Path,
path: &NativePath,
options: &ReadOptions,
) -> Result<Preview> {
inspection::read(repo, options, |s| preview_in(s, path))
}
/// Read an immutable blob without invoking content conversion helpers.
pub fn blob(repo: &Path, oid: &ObjectId) -> Result<Vec<u8>> {
inspection::read(
repo,
&ReadOptions {
max_output: 2 * 1024 * 1024,
..ReadOptions::default()
},
|s| s.blob(oid),
)
}
#[cfg(test)]
mod fingerprint_tests {
use super::*;
use std::fs;
thread_local! { pub(super) static CANCEL_ON_CHUNK: std::cell::Cell<bool> = const {std::cell::Cell::new(false)}; }
#[test]
fn streaming_fingerprints_match_preview_contract_and_limits() {
let root = std::env::temp_dir().join(format!("diff-fingerprint-{}", uuid::Uuid::new_v4()));
fs::create_dir(&root).unwrap();
assert!(std::process::Command::new("git")
.args(["init", "-q"])
.arg(&root)
.status()
.unwrap()
.success());
for bytes in [vec![], b"text\n".to_vec(), vec![0; 2 * 1024 * 1024]] {
fs::write(root.join("file"), bytes).unwrap();
inspection::read(&root, &Default::default(), |s| {
let path = NativePath::Utf8("file".into());
assert_eq!(fingerprint_in(s, &path)?, preview_in(s, &path)?.fingerprint);
Ok(())
})
.unwrap();
}
fs::remove_file(root.join("file")).unwrap();
#[cfg(unix)]
std::os::unix::fs::symlink("target", root.join("link")).unwrap();
inspection::read(&root, &Default::default(), |s| {
for name in ["file", "link"] {
let path = NativePath::Utf8(name.into());
assert_eq!(fingerprint_in(s, &path)?, preview_in(s, &path)?.fingerprint);
}
fs::write(root.join("file"), vec![0; 2 * 1024 * 1024 + 1])?;
assert_eq!(
fingerprint_in(s, &NativePath::Utf8("file".into()))
.unwrap_err()
.kind,
ErrorKind::OutputLimit
);
Ok(())
})
.unwrap();
fs::write(root.join("file"), vec![0; 131072]).unwrap();
let options = ReadOptions::default();
let error = inspection::read(&root, &options, |s| {
CANCEL_ON_CHUNK.set(true);
fingerprint_in(s, &NativePath::Utf8("file".into()))
})
.unwrap_err();
assert_eq!(error.kind, ErrorKind::Cancelled);
assert!(options.cancellation.is_cancelled());
let options = ReadOptions {
timeout: Duration::from_millis(500),
..Default::default()
};
let error = inspection::read(&root, &options, |s| {
std::thread::sleep(Duration::from_millis(500));
fingerprint_in(s, &NativePath::Utf8("file".into()))
})
.unwrap_err();
assert_eq!(error.kind, ErrorKind::Timeout);
fs::remove_dir_all(root).unwrap();
}
}