Skip to content

Commit

Permalink
Drop lib.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenvukhang committed Apr 9, 2024
1 parent 296752a commit 82eb0bc
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 105 deletions.
6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ atty = "0.2.14"

[[bin]]
bench = false
test = false
doctest = false
path = "src/main.rs"
name = "git-nu"

[lib]
doctest = false
test = false
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ build:

test:
cargo build
cargo test --lib
cargo test

test-one:
cargo build
Expand Down
97 changes: 0 additions & 97 deletions src/lib.rs

This file was deleted.

100 changes: 98 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,103 @@
mod app;
mod cache;
mod git_cmd;
mod pathdiff;
mod prelude;
mod status;

#[cfg(test)]
mod tests;

use prelude::*;

use std::env::{args, current_dir};
use std::process::ExitCode;
use std::io::BufRead;
use std::path::PathBuf;
use std::process::{Command, ExitCode};

use app::App;
use cache::Cache;

mod git {
use std::path::Path;
use std::process::Output;

use super::*;

/// Path to git's repository (not workspace)
/// * .git/
/// * .git/worktrees/<branch-name>/
///
/// current_dir is intentionally not supplied as it relies on the
/// user's actual PWD or the value of git's `-C` flag, which is not
/// visible to the `git-nu` binary.
pub(crate) fn dir<P: AsRef<Path>>(cwd: P) -> Result<PathBuf> {
_dir(Some(cwd))
}

fn sh<P: AsRef<Path>>(dir: Option<P>, args: &[&str]) -> Result<Output> {
let mut cmd = Command::new("git");
dir.map(|v| cmd.current_dir(v));
Ok(cmd.args(args).output()?)
}

fn _dir<P: AsRef<Path>>(base_dir: Option<P>) -> Result<PathBuf> {
let output = git::sh(base_dir, &["rev-parse", "--git-dir"])?;
if output.stderr.starts_with(b"fatal: not a git repository") {
return error!(NotGitRepository);
}
let stdout = String::from_utf8_lossy(&output.stdout);
Ok(PathBuf::from(stdout.trim_end()))
}

pub(crate) fn aliases() -> Aliases {
let args = ["config", "--global", "--get-regexp", "^alias."];
match git::sh(None::<&str>, &args) {
Ok(v) => Aliases::from_iter(
v.stdout.lines().filter_map(|v| v.ok()).filter_map(|v| {
v.get(6..) // every lines starts with "alias."
.and_then(|v| v.split_once(' '))
.map(|(k, v)| (k.to_string(), v.to_string()))
}),
),
Err(_) => Aliases::new(),
}
}
}

fn cli_init_app(cwd: PathBuf) -> Result<App> {
use std::thread;

let h_git_dir = thread::spawn(move || git::dir(&cwd).map(|gd| (gd, cwd)));
let h_git_aliases = thread::spawn(git::aliases);

let (git_dir, cwd) = h_git_dir.join()??;
let git_aliases = h_git_aliases.join()?;

let cache = Cache::new(&git_dir, &cwd);

let mut final_cmd = Command::new("git");
final_cmd.current_dir(&cwd);

Ok(App { git_aliases, git_cmd: None, git_dir, cwd, final_cmd, cache })
}

pub fn main_inner(cwd: PathBuf, args: &[String]) -> ExitCode {
let exitcode = match cli_init_app(cwd) {
Ok(mut app) => {
app.parse(&args);
app.run()
}
Err(_) => Command::new("git")
.args(&args[1..])
.status()
.map_err(|v| Error::from(v))
.map(|v| v.exitcode()),
};
exitcode.unwrap_or(ExitCode::FAILURE)
}

fn main() -> ExitCode {
let current_dir = current_dir().unwrap_or_default();
gitnu::main(current_dir, &args().collect::<Vec<_>>())
main_inner(current_dir, &args().collect::<Vec<_>>())
}

0 comments on commit 82eb0bc

Please sign in to comment.