Skip to content

Commit 6777ecb

Browse files
committed
Some minor modifications
1 parent 0c554e0 commit 6777ecb

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

gitoxide-core/src/repository/diff.rs

+46-17
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
use gix::bstr::{BString, ByteSlice};
2+
use gix::objs::tree::EntryMode;
3+
use gix::prelude::ObjectIdExt;
24

35
pub fn tree(
4-
repo: gix::Repository,
6+
mut repo: gix::Repository,
57
out: &mut dyn std::io::Write,
68
old_treeish: BString,
79
new_treeish: BString,
810
) -> anyhow::Result<()> {
11+
repo.object_cache_size_if_unset(repo.compute_object_cache_size_for_tree_diffs(&**repo.index_or_empty()?));
12+
913
let old_tree_id = repo.rev_parse_single(old_treeish.as_bstr())?;
1014
let new_tree_id = repo.rev_parse_single(new_treeish.as_bstr())?;
1115

12-
let old_tree = old_tree_id.object()?.peel_to_kind(gix::object::Kind::Tree)?.into_tree();
13-
let new_tree = new_tree_id.object()?.peel_to_kind(gix::object::Kind::Tree)?.into_tree();
16+
let old_tree = old_tree_id.object()?.peel_to_tree()?;
17+
let new_tree = new_tree_id.object()?.peel_to_tree()?;
1418

1519
let changes = repo.diff_tree_to_tree(&old_tree, &new_tree, None)?;
1620

1721
writeln!(
1822
out,
19-
"Diffing trees `{old_treeish}` ({old_tree_id}) -> `{new_treeish}` ({new_tree_id})"
23+
"Diffing trees `{old_treeish}` ({old_tree_id}) -> `{new_treeish}` ({new_tree_id})\n"
2024
)?;
21-
writeln!(out)?;
22-
23-
write_changes(out, changes)?;
25+
write_changes(&repo, out, changes)?;
2426

2527
Ok(())
2628
}
2729

2830
fn write_changes(
31+
repo: &gix::Repository,
2932
mut out: impl std::io::Write,
3033
changes: Vec<gix::diff::tree_with_rewrites::Change>,
3134
) -> Result<(), std::io::Error> {
@@ -37,8 +40,8 @@ fn write_changes(
3740
entry_mode,
3841
..
3942
} => {
40-
writeln!(out, "A: {location}")?;
41-
writeln!(out, " {id}")?;
43+
writeln!(out, "A: {}", typed_location(location, entry_mode))?;
44+
writeln!(out, " {}", id.attach(repo).shorten_or_id())?;
4245
writeln!(out, " -> {:o}", entry_mode.0)?;
4346
}
4447
gix::diff::tree_with_rewrites::Change::Deletion {
@@ -47,8 +50,8 @@ fn write_changes(
4750
entry_mode,
4851
..
4952
} => {
50-
writeln!(out, "D: {location}")?;
51-
writeln!(out, " {id}")?;
53+
writeln!(out, "D: {}", typed_location(location, entry_mode))?;
54+
writeln!(out, " {}", id.attach(repo).shorten_or_id())?;
5255
writeln!(out, " {:o} ->", entry_mode.0)?;
5356
}
5457
gix::diff::tree_with_rewrites::Change::Modification {
@@ -58,9 +61,16 @@ fn write_changes(
5861
previous_entry_mode,
5962
entry_mode,
6063
} => {
61-
writeln!(out, "M: {location}")?;
62-
writeln!(out, " {previous_id} -> {id}")?;
63-
writeln!(out, " {:o} -> {:o}", previous_entry_mode.0, entry_mode.0)?;
64+
writeln!(out, "M: {}", typed_location(location, entry_mode))?;
65+
writeln!(
66+
out,
67+
" {previous_id} -> {id}",
68+
previous_id = previous_id.attach(repo).shorten_or_id(),
69+
id = id.attach(repo).shorten_or_id()
70+
)?;
71+
if previous_entry_mode != entry_mode {
72+
writeln!(out, " {:o} -> {:o}", previous_entry_mode.0, entry_mode.0)?;
73+
}
6474
}
6575
gix::diff::tree_with_rewrites::Change::Rewrite {
6676
source_location,
@@ -71,12 +81,31 @@ fn write_changes(
7181
entry_mode,
7282
..
7383
} => {
74-
writeln!(out, "R: {source_location} -> {location}")?;
75-
writeln!(out, " {source_id} -> {id}")?;
76-
writeln!(out, " {:o} -> {:o}", source_entry_mode.0, entry_mode.0)?;
84+
writeln!(
85+
out,
86+
"R: {source} -> {dest}",
87+
source = typed_location(source_location, source_entry_mode),
88+
dest = typed_location(location, entry_mode)
89+
)?;
90+
writeln!(
91+
out,
92+
" {source_id} -> {id}",
93+
source_id = source_id.attach(repo).shorten_or_id(),
94+
id = id.attach(repo).shorten_or_id()
95+
)?;
96+
if source_entry_mode != entry_mode {
97+
writeln!(out, " {:o} -> {:o}", source_entry_mode.0, entry_mode.0)?;
98+
}
7799
}
78100
};
79101
}
80102

81103
Ok(())
82104
}
105+
106+
fn typed_location(mut location: BString, mode: EntryMode) -> BString {
107+
if mode.is_tree() {
108+
location.push(b'/');
109+
}
110+
location
111+
}

src/plumbing/options/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,13 @@ pub mod merge {
372372
#[clap(long, short = 'c')]
373373
resolve_with: Option<ResolveWith>,
374374

375-
/// A path or revspec to our file
375+
/// A path or revspec to our file.
376376
#[clap(value_name = "OURS", value_parser = crate::shared::AsBString)]
377377
ours: BString,
378-
/// A path or revspec to the base for both ours and theirs
378+
/// A path or revspec to the base for both ours and theirs.
379379
#[clap(value_name = "BASE", value_parser = crate::shared::AsBString)]
380380
base: BString,
381-
/// A path or revspec to their file
381+
/// A path or revspec to their file.
382382
#[clap(value_name = "OURS", value_parser = crate::shared::AsBString)]
383383
theirs: BString,
384384
},
@@ -397,12 +397,12 @@ pub mod diff {
397397

398398
#[derive(Debug, clap::Subcommand)]
399399
pub enum SubCommands {
400-
/// Diff two trees by specifying their revspecs.
400+
/// Diff two trees.
401401
Tree {
402-
/// A revspec representing the before or old tree
402+
/// A rev-spec representing the 'before' or old tree.
403403
#[clap(value_parser = crate::shared::AsBString)]
404404
old_treeish: BString,
405-
/// A revspec representing the after or new tree
405+
/// A rev-spec representing the 'after' or new tree.
406406
#[clap(value_parser = crate::shared::AsBString)]
407407
new_treeish: BString,
408408
},

0 commit comments

Comments
 (0)