Repository navigation
Expand file tree
/
Copy pathdiff_layers.rs
More file actions
48 lines (46 loc) · 1.82 KB
/
Copy pathdiff_layers.rs
File metadata and controls
48 lines (46 loc) · 1.82 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
use std::env;
use git_rs::{
DiffKind, HostFileSystem, LayerDiffEntry, LayerDiffOptions, Repository, RevisionOptions,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut arguments = env::args().skip(1);
let repository_path = arguments
.next()
.ok_or("usage: diff_layers REPOSITORY --files|--cached|--worktree [TREEISH]")?;
let mode = arguments
.next()
.ok_or("usage: diff_layers REPOSITORY --files|--cached|--worktree [TREEISH]")?;
let repository = Repository::open(HostFileSystem::new(".")?, repository_path)?;
let options = LayerDiffOptions::default();
let entries = if mode == "--files" {
repository.diff_files(&options)?
} else {
let revision = arguments
.next()
.ok_or("cached/worktree mode requires a tree-ish")?;
let id = repository.resolve_revision_id(&revision, &RevisionOptions::default())?;
let tree = repository
.read_commit(id, options.diff.max_object_size)?
.tree();
repository.diff_index(Some(tree), mode == "--cached", &options)?
};
for entry in entries {
match entry {
LayerDiffEntry::Change(change) => {
let status = match change.kind() {
DiffKind::Added => "A",
DiffKind::Deleted => "D",
DiffKind::Modified => "M",
DiffKind::TypeChanged => "T",
DiffKind::Renamed => "R100",
};
let path = change.new_path().or_else(|| change.old_path()).unwrap();
println!("{status}\t{}", String::from_utf8_lossy(path));
}
LayerDiffEntry::Unmerged { path, .. } => {
println!("U\t{}", String::from_utf8_lossy(&path));
}
}
}
Ok(())
}