Skip to content

Commit

Permalink
Some minor modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Oct 15, 2024
1 parent 2e8d0f0 commit b6b732e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
63 changes: 46 additions & 17 deletions gitoxide-core/src/repository/diff.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
use gix::bstr::{BString, ByteSlice};
use gix::objs::tree::EntryMode;
use gix::prelude::ObjectIdExt;

pub fn tree(
repo: gix::Repository,
mut repo: gix::Repository,
out: &mut dyn std::io::Write,
old_treeish: BString,
new_treeish: BString,
) -> anyhow::Result<()> {
repo.object_cache_size_if_unset(repo.compute_object_cache_size_for_tree_diffs(&**repo.index_or_empty()?));

let old_tree_id = repo.rev_parse_single(old_treeish.as_bstr())?;
let new_tree_id = repo.rev_parse_single(new_treeish.as_bstr())?;

let old_tree = old_tree_id.object()?.peel_to_kind(gix::object::Kind::Tree)?.into_tree();
let new_tree = new_tree_id.object()?.peel_to_kind(gix::object::Kind::Tree)?.into_tree();
let old_tree = old_tree_id.object()?.peel_to_tree()?;
let new_tree = new_tree_id.object()?.peel_to_tree()?;

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

writeln!(
out,
"Diffing trees `{old_treeish}` ({old_tree_id}) -> `{new_treeish}` ({new_tree_id})"
"Diffing trees `{old_treeish}` ({old_tree_id}) -> `{new_treeish}` ({new_tree_id})\n"
)?;
writeln!(out)?;

write_changes(out, changes)?;
write_changes(&repo, out, changes)?;

Ok(())
}

fn write_changes(
repo: &gix::Repository,
mut out: impl std::io::Write,
changes: Vec<gix::diff::tree_with_rewrites::Change>,
) -> Result<(), std::io::Error> {
Expand All @@ -37,8 +40,8 @@ fn write_changes(
entry_mode,
..
} => {
writeln!(out, "A: {location}")?;
writeln!(out, " {id}")?;
writeln!(out, "A: {}", typed_location(location, entry_mode))?;
writeln!(out, " {}", id.attach(repo).shorten_or_id())?;
writeln!(out, " -> {:o}", entry_mode.0)?;
}
gix::diff::tree_with_rewrites::Change::Deletion {
Expand All @@ -47,8 +50,8 @@ fn write_changes(
entry_mode,
..
} => {
writeln!(out, "D: {location}")?;
writeln!(out, " {id}")?;
writeln!(out, "D: {}", typed_location(location, entry_mode))?;
writeln!(out, " {}", id.attach(repo).shorten_or_id())?;
writeln!(out, " {:o} ->", entry_mode.0)?;
}
gix::diff::tree_with_rewrites::Change::Modification {
Expand All @@ -58,9 +61,16 @@ fn write_changes(
previous_entry_mode,
entry_mode,
} => {
writeln!(out, "M: {location}")?;
writeln!(out, " {previous_id} -> {id}")?;
writeln!(out, " {:o} -> {:o}", previous_entry_mode.0, entry_mode.0)?;
writeln!(out, "M: {}", typed_location(location, entry_mode))?;
writeln!(
out,
" {previous_id} -> {id}",
previous_id = previous_id.attach(repo).shorten_or_id(),
id = id.attach(repo).shorten_or_id()
)?;
if previous_entry_mode != entry_mode {
writeln!(out, " {:o} -> {:o}", previous_entry_mode.0, entry_mode.0)?;
}
}
gix::diff::tree_with_rewrites::Change::Rewrite {
source_location,
Expand All @@ -71,12 +81,31 @@ fn write_changes(
entry_mode,
..
} => {
writeln!(out, "R: {source_location} -> {location}")?;
writeln!(out, " {source_id} -> {id}")?;
writeln!(out, " {:o} -> {:o}", source_entry_mode.0, entry_mode.0)?;
writeln!(
out,
"R: {source} -> {dest}",
source = typed_location(source_location, source_entry_mode),
dest = typed_location(location, entry_mode)
)?;
writeln!(
out,
" {source_id} -> {id}",
source_id = source_id.attach(repo).shorten_or_id(),
id = id.attach(repo).shorten_or_id()
)?;
if source_entry_mode != entry_mode {
writeln!(out, " {:o} -> {:o}", source_entry_mode.0, entry_mode.0)?;
}
}
};
}

Ok(())
}

fn typed_location(mut location: BString, mode: EntryMode) -> BString {
if mode.is_tree() {
location.push(b'/');
}
location
}
12 changes: 6 additions & 6 deletions src/plumbing/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,13 @@ pub mod merge {
#[clap(long, short = 'c')]
resolve_with: Option<ResolveWith>,

/// A path or revspec to our file
/// A path or revspec to our file.
#[clap(value_name = "OURS", value_parser = crate::shared::AsBString)]
ours: BString,
/// A path or revspec to the base for both ours and theirs
/// A path or revspec to the base for both ours and theirs.
#[clap(value_name = "BASE", value_parser = crate::shared::AsBString)]
base: BString,
/// A path or revspec to their file
/// A path or revspec to their file.
#[clap(value_name = "OURS", value_parser = crate::shared::AsBString)]
theirs: BString,
},
Expand All @@ -397,12 +397,12 @@ pub mod diff {

#[derive(Debug, clap::Subcommand)]
pub enum SubCommands {
/// Diff two trees by specifying their revspecs.
/// Diff two trees.
Tree {
/// A revspec representing the before or old tree
/// A rev-spec representing the 'before' or old tree.
#[clap(value_parser = crate::shared::AsBString)]
old_treeish: BString,
/// A revspec representing the after or new tree
/// A rev-spec representing the 'after' or new tree.
#[clap(value_parser = crate::shared::AsBString)]
new_treeish: BString,
},
Expand Down

0 comments on commit b6b732e

Please sign in to comment.