Skip to content

Commit

Permalink
refactor (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jan 24, 2022
1 parent 3fc1622 commit 8bf585d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 109 deletions.
3 changes: 1 addition & 2 deletions git-repository/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,8 @@ pub mod prelude {
pub mod path;

mod repository;
pub use repository::{discover, init, open};

use git_features::threading::OwnShared;
pub use repository::{discover, init, open};

/// The standard type for a store to handle git references.
pub type RefStore = git_ref::file::Store;
Expand Down
105 changes: 0 additions & 105 deletions gitoxide-core/src/index.rs

This file was deleted.

67 changes: 67 additions & 0 deletions gitoxide-core/src/index/entries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use git_repository as git;
use git_repository::bstr::ByteSlice;

pub struct Options {
pub object_hash: git::hash::Kind,
pub format: crate::OutputFormat,
}

#[cfg(feature = "serde1")]
pub(crate) fn to_json(
mut out: &mut impl std::io::Write,
file: &git::index::File,
entry: &git::index::Entry,
is_last: bool,
) -> anyhow::Result<()> {
#[cfg_attr(feature = "serde1", derive(serde::Serialize))]
struct Entry<'a> {
stat: &'a git::index::entry::Stat,
hex_id: String,
flags: u32,
mode: u32,
path: std::borrow::Cow<'a, str>,
}

serde_json::to_writer(
&mut out,
&Entry {
stat: &entry.stat,
hex_id: entry.id.to_hex().to_string(),
flags: entry.flags.bits(),
mode: entry.mode.bits(),
path: entry.path(&file.state).to_str_lossy(),
},
)?;

if is_last {
out.write_all(b"\n")?;
} else {
out.write_all(b",\n")?;
}
Ok(())
}

pub(crate) fn to_human(
out: &mut impl std::io::Write,
file: &git::index::File,
entry: &git::index::Entry,
) -> std::io::Result<()> {
writeln!(
out,
"{} {}{:?} {} {}",
match entry.flags.stage() {
0 => "BASE ",
1 => "OURS ",
2 => "THEIRS ",
_ => "UNKNOWN",
},
if entry.flags.is_empty() {
"".to_string()
} else {
format!("{:?} ", entry.flags)
},
entry.mode,
entry.id,
entry.path(&file.state)
)
}
40 changes: 40 additions & 0 deletions gitoxide-core/src/index/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::path::Path;

use git_repository as git;

pub mod entries;

pub fn entries(
index_path: impl AsRef<Path>,
mut out: impl std::io::Write,
entries::Options { object_hash, format }: entries::Options,
) -> anyhow::Result<()> {
use crate::OutputFormat::*;
let file = git::index::File::at(
index_path.as_ref(),
git::index::decode::Options {
object_hash,
..Default::default()
},
)?;

#[cfg(feature = "serde1")]
if let Json = format {
out.write_all(b"[\n")?;
}

let mut entries = file.entries().iter().peekable();
while let Some(entry) = entries.next() {
match format {
Human => entries::to_human(&mut out, &file, entry)?,
#[cfg(feature = "serde1")]
Json => entries::to_json(&mut out, &file, entry, entries.peek().is_none())?,
}
}

#[cfg(feature = "serde1")]
if let Json = format {
out.write_all(b"]\n")?;
}
Ok(())
}
3 changes: 1 addition & 2 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ use clap::Parser;
use gitoxide_core as core;
use gitoxide_core::pack::verify;

use crate::plumbing::options::index;
#[cfg(any(feature = "gitoxide-core-async-client", feature = "gitoxide-core-blocking-client"))]
use crate::plumbing::options::remote;
use crate::{
plumbing::options::{commitgraph, pack, repo, Args, Subcommands},
plumbing::options::{commitgraph, index, pack, repo, Args, Subcommands},
shared::pretty::prepare_and_run,
};

Expand Down

0 comments on commit 8bf585d

Please sign in to comment.