Skip to content

Commit

Permalink
Refactor: Make it easier to compile gitoxide as dynlib
Browse files Browse the repository at this point in the history
And also optimize compilation time: By making `plumbing` and `porcelain`
as modules the `lib.rs`, they can be compiled after the rmeta for the
dependencies are generated.

For the `uni.rs` which uses both `plumbing` and `porcelain`, this would avoid
compiling these two modules twice.

Signed-off-by: Jiahao XU <[email protected]>
  • Loading branch information
NobodyXu committed May 27, 2024
1 parent e8b6bc2 commit 202f3e4
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 39 deletions.
4 changes: 1 addition & 3 deletions src/ein.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#![deny(rust_2018_idioms, unsafe_code)]

mod porcelain;

fn main() -> anyhow::Result<()> {
porcelain::main()
crate::porcelain::main()
}

#[cfg(not(feature = "pretty-cli"))]
Expand Down
8 changes: 2 additions & 6 deletions src/gix.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#![deny(unsafe_code, rust_2018_idioms)]

mod plumbing;

use anyhow::Result;

#[cfg(feature = "pretty-cli")]
fn main() -> Result<()> {
plumbing::main()
fn main() -> anyhow::Result<()> {
crate::plumbing::main()
}

#[cfg(not(feature = "pretty-cli"))]
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@
#![allow(missing_docs)]
#![forbid(unsafe_code)]

pub mod plumbing;
pub mod porcelain;
/// everything in common between the `gix` and `ein` binaries.
pub mod shared;
2 changes: 1 addition & 1 deletion src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use std::{
},
};

use crate::shared::pretty::prepare_and_run;
use anyhow::{anyhow, Context, Result};
use clap::{CommandFactory, Parser};
use gitoxide::shared::pretty::prepare_and_run;
use gitoxide_core as core;
use gitoxide_core::{pack::verify, repository::PathsOrPatterns};
use gix::bstr::{io::BufReadExt, BString};
Expand Down
2 changes: 1 addition & 1 deletion src/plumbing/options/free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub mod index {
#[derive(Debug, clap::Parser)]
pub struct Platform {
/// The object format to assume when reading files that don't inherently know about it, or when writing files.
#[clap(long, default_value_t = gix::hash::Kind::default(), value_parser = gitoxide::shared::AsHashKind)]
#[clap(long, default_value_t = gix::hash::Kind::default(), value_parser = crate::shared::AsHashKind)]
pub object_hash: gix::hash::Kind,

/// The path to the index file.
Expand Down
30 changes: 15 additions & 15 deletions src/plumbing/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Args {
///
/// For example, if `key` is `core.abbrev`, set configuration like `[core] abbrev = key`,
/// or `remote.origin.url = foo` to set `[remote "origin"] url = foo`.
#[clap(long, short = 'c', value_parser = gitoxide::shared::AsBString)]
#[clap(long, short = 'c', value_parser = crate::shared::AsBString)]
pub config: Vec<BString>,

#[clap(long, short = 't')]
Expand Down Expand Up @@ -64,12 +64,12 @@ pub struct Args {
long,
short = 'f',
default_value = "human",
value_parser = gitoxide::shared::AsOutputFormat
value_parser = crate::shared::AsOutputFormat
)]
pub format: core::OutputFormat,

/// The object format to assume when reading files that don't inherently know about it, or when writing files.
#[clap(long, default_value_t = gix::hash::Kind::default(), value_parser = gitoxide::shared::AsHashKind)]
#[clap(long, default_value_t = gix::hash::Kind::default(), value_parser = crate::shared::AsHashKind)]
pub object_hash: gix::hash::Kind,

#[clap(subcommand)]
Expand Down Expand Up @@ -201,7 +201,7 @@ pub mod archive {
}

pub mod status {
use gitoxide::shared::{CheckPathSpec, ParseRenameFraction};
use crate::shared::{CheckPathSpec, ParseRenameFraction};
use gix::bstr::BString;

#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)]
Expand Down Expand Up @@ -320,7 +320,7 @@ pub mod config {
///
/// Typical filters are `branch` or `remote.origin` or `remote.or*` - git-style globs are supported
/// and comparisons are case-insensitive.
#[clap(value_parser = gitoxide::shared::AsBString)]
#[clap(value_parser = crate::shared::AsBString)]
pub filter: Vec<BString>,
}
}
Expand Down Expand Up @@ -359,7 +359,7 @@ pub mod fetch {
pub remote: Option<String>,

/// Override the built-in and configured ref-specs with one or more of the given ones.
#[clap(value_parser = gitoxide::shared::AsBString)]
#[clap(value_parser = crate::shared::AsBString)]
pub ref_spec: Vec<gix::bstr::BString>,
}

Expand All @@ -374,11 +374,11 @@ pub mod fetch {
pub deepen: Option<u32>,

/// Cutoff all history past the given date. Can be combined with shallow-exclude.
#[clap(long, help_heading = Some("SHALLOW"), value_parser = gitoxide::shared::AsTime, value_name = "DATE", conflicts_with_all = ["depth", "deepen", "unshallow"])]
#[clap(long, help_heading = Some("SHALLOW"), value_parser = crate::shared::AsTime, value_name = "DATE", conflicts_with_all = ["depth", "deepen", "unshallow"])]
pub shallow_since: Option<gix::date::Time>,

/// Cutoff all history past the tag-name or ref-name. Can be combined with shallow-since.
#[clap(long, help_heading = Some("SHALLOW"), value_parser = gitoxide::shared::AsPartialRefName, value_name = "REF_NAME", conflicts_with_all = ["depth", "deepen", "unshallow"])]
#[clap(long, help_heading = Some("SHALLOW"), value_parser = crate::shared::AsPartialRefName, value_name = "REF_NAME", conflicts_with_all = ["depth", "deepen", "unshallow"])]
pub shallow_exclude: Vec<gix::refs::PartialName>,

/// Remove the shallow boundary and fetch the entire history available on the remote.
Expand Down Expand Up @@ -445,11 +445,11 @@ pub mod clone {
pub depth: Option<NonZeroU32>,

/// Cutoff all history past the given date. Can be combined with shallow-exclude.
#[clap(long, help_heading = Some("SHALLOW"), value_parser = gitoxide::shared::AsTime, value_name = "DATE")]
#[clap(long, help_heading = Some("SHALLOW"), value_parser = crate::shared::AsTime, value_name = "DATE")]
pub shallow_since: Option<gix::date::Time>,

/// Cutoff all history past the tag-name or ref-name. Can be combined with shallow-since.
#[clap(long, help_heading = Some("SHALLOW"), value_parser = gitoxide::shared::AsPartialRefName, value_name = "REF_NAME")]
#[clap(long, help_heading = Some("SHALLOW"), value_parser = crate::shared::AsPartialRefName, value_name = "REF_NAME")]
pub shallow_exclude: Vec<gix::refs::PartialName>,
}

Expand Down Expand Up @@ -501,7 +501,7 @@ pub mod remote {
#[clap(long, short = 'u')]
show_unmapped_remote_refs: bool,
/// Override the built-in and configured ref-specs with one or more of the given ones.
#[clap(value_parser = gitoxide::shared::AsBString)]
#[clap(value_parser = crate::shared::AsBString)]
ref_spec: Vec<gix::bstr::BString>,
},
}
Expand All @@ -517,7 +517,7 @@ pub mod mailmap {

#[cfg(feature = "gitoxide-core-tools-clean")]
pub mod clean {
use gitoxide::shared::CheckPathSpec;
use crate::shared::CheckPathSpec;
use gix::bstr::BString;

#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)]
Expand Down Expand Up @@ -787,7 +787,7 @@ pub mod revision {
}

pub mod attributes {
use gitoxide::shared::CheckPathSpec;
use crate::shared::CheckPathSpec;
use gix::bstr::BString;

#[derive(Debug, clap::Subcommand)]
Expand Down Expand Up @@ -817,7 +817,7 @@ pub mod attributes {
pub mod exclude {
use std::ffi::OsString;

use gitoxide::shared::CheckPathSpec;
use crate::shared::CheckPathSpec;
use gix::bstr::BString;

#[derive(Debug, clap::Subcommand)]
Expand Down Expand Up @@ -847,7 +847,7 @@ pub mod exclude {
pub mod index {
use std::path::PathBuf;

use gitoxide::shared::CheckPathSpec;
use crate::shared::CheckPathSpec;
use gix::bstr::BString;

pub mod entries {
Expand Down
12 changes: 6 additions & 6 deletions src/porcelain/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::sync::{
Arc,
};

use crate::shared::pretty::prepare_and_run;
use anyhow::{anyhow, Result};
use clap::{CommandFactory, Parser};
use gitoxide::shared::pretty::prepare_and_run;
use gitoxide_core as core;

use crate::porcelain::options::{Args, Subcommands};
Expand Down Expand Up @@ -41,7 +41,7 @@ pub fn main() -> Result<()> {
verbose,
progress,
progress_keep_open,
gitoxide::shared::STANDARD_RANGE,
crate::shared::STANDARD_RANGE,
move |_progress, _out, _err| panic!("something went very wrong"),
),
Subcommands::Init { directory } => core::repository::init(directory).map(|_| ()),
Expand All @@ -61,7 +61,7 @@ pub fn main() -> Result<()> {
verbose,
progress,
progress_keep_open,
gitoxide::shared::STANDARD_RANGE,
crate::shared::STANDARD_RANGE,
move |mut progress, out, mut err| {
let engine = query::prepare(
&repo_dir,
Expand Down Expand Up @@ -101,7 +101,7 @@ pub fn main() -> Result<()> {
verbose,
progress,
progress_keep_open,
gitoxide::shared::STANDARD_RANGE,
crate::shared::STANDARD_RANGE,
move |progress, out, _err| {
hours::estimate(
&working_dir,
Expand All @@ -128,7 +128,7 @@ pub fn main() -> Result<()> {
verbose,
progress,
progress_keep_open,
gitoxide::shared::STANDARD_RANGE,
crate::shared::STANDARD_RANGE,
move |progress, out, _err| {
organize::discover(
root.unwrap_or_else(|| [std::path::Component::CurDir].iter().collect()),
Expand All @@ -152,7 +152,7 @@ pub fn main() -> Result<()> {
verbose,
progress,
progress_keep_open,
gitoxide::shared::STANDARD_RANGE,
crate::shared::STANDARD_RANGE,
move |progress, _out, _err| {
organize::run(
if execute {
Expand Down
4 changes: 2 additions & 2 deletions src/porcelain/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub mod tools {

#[cfg(feature = "gitoxide-core-tools-query")]
pub mod query {
use gitoxide::shared::AsPathSpec;
use crate::shared::AsPathSpec;

#[derive(Debug, clap::Subcommand)]
pub enum Command {
Expand All @@ -153,7 +153,7 @@ pub mod tools {
#[clap(default_value = ".")]
pub working_dir: PathBuf,
/// The name of the revision as spec, like 'HEAD' or 'main' at which to start iterating the commit graph.
#[clap(default_value("HEAD"), value_parser = gitoxide::shared::AsBString)]
#[clap(default_value("HEAD"), value_parser = crate::shared::AsBString)]
pub rev_spec: BString,
/// Ignore github bots which match the `[bot]` search string.
#[clap(short = 'b', long)]
Expand Down
7 changes: 2 additions & 5 deletions src/uni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@

use anyhow::{bail, Result};

mod plumbing;
mod porcelain;

#[cfg(feature = "pretty-cli")]
fn main() -> Result<()> {
match std::env::current_exe()?
.file_stem()
.and_then(|stem| stem.to_str())
.unwrap_or("gix")
{
"gix" => plumbing::main(),
"ein" => porcelain::main(),
"gix" => crate::plumbing::main(),
"ein" => crate::porcelain::main(),
unknown => bail!("Executable named '{unknown}' cannot be launched. Exe must be named either `gix` or `ein`."),
}
}
Expand Down

0 comments on commit 202f3e4

Please sign in to comment.