Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenvukhang committed Nov 3, 2023
2 parents 67002e8 + e41d56a commit b831ad2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gitnu"
version = "0.7.2"
version = "0.7.3-alpha.3"
authors = ["Nguyen Vu Khang <[email protected]>"]
description = """
gitnu indexes your git status so you can use numbers instead of filenames.
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
LOCAL_BIN=$(HOME)/dots/personal/.local/bin
LOCAL_BIN=$(HOME)/.local/bin
GITNU_RELEASE_BIN=$(PWD)/target/release/git-nu
GITNU_DEBUG_BIN=$(PWD)/target/debug/git-nu

Expand Down
57 changes: 22 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,41 +93,31 @@ mod git {
if let Some(current_dir) = base_dir {
cmd.current_dir(current_dir);
}
let output = cmd.args(["rev-parse", "--git-dir"]).output();

match output {
Ok(v) => {
let stderr = String::from_utf8_lossy(&v.stderr);
if stderr.contains("fatal: not a git repository") {
return error!(NotGitRepository);
}
let stdout = String::from_utf8_lossy(&v.stdout);
return Ok(PathBuf::from(stdout.trim()));
}
Err(e) => Err(Error::Io(e)),
let output = cmd
.args(["rev-parse", "--git-dir"])
.output()
.map_err(|e| Error::Io(e))?;
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 mut aliases = Aliases::new();
let output = Command::new("git")
.args(["config", "--global", "--get-regexp", "^alias."])
.output();

let output = match output {
Ok(v) => v,
Err(_) => return aliases,
};

let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines() {
let split = line.get(6..).and_then(|v| v.split_once(' '));
if let Some((key, value)) = split {
aliases.insert(key.to_string(), value.to_string());
}
match output {
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(),
}

aliases
}
}

Expand Down Expand Up @@ -209,14 +199,11 @@ where

let exitcode = match cli_init_app(&cwd) {
Ok(app) => app.parse(args).run(),
Err(_) => {
// eprintln!("{err:?}");
Command::new("git")
.args(args.skip(1))
.status()
.map_err(|v| Error::from(v))
.map(|v| v.exitcode())
}
Err(_) => Command::new("git")
.args(args.skip(1))
.status()
.map_err(|v| Error::from(v))
.map(|v| v.exitcode()),
};
exitcode.unwrap_or(ExitCode::FAILURE)
}
37 changes: 15 additions & 22 deletions src/pathdiff.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
use std::path::{Component, PathBuf, Path};
use std::path::{Component, Path, PathBuf};

pub fn diff_paths<P, B>(path: P, base: B) -> Option<PathBuf>
where
P: AsRef<Path>,
B: AsRef<Path>,
{
let path = path.as_ref();
let base = base.as_ref();

let (path, base) = (path.as_ref(), base.as_ref());
if path.is_absolute() != base.is_absolute() {
if path.is_absolute() {
Some(PathBuf::from(path))
} else {
None
}
path.is_absolute().then(|| PathBuf::from(path))
} else {
let mut ita = path.components();
let mut itb = base.components();
let mut comps: Vec<Component> = vec![];
let (mut ita, mut itb) = (path.components(), base.components());
let mut cs = vec![];
loop {
match (ita.next(), itb.next()) {
(None, None) => break,
(Some(a), None) => {
comps.push(a);
comps.extend(ita.by_ref());
cs.push(a);
cs.extend(ita.by_ref());
break;
}
(None, _) => comps.push(Component::ParentDir),
(Some(a), Some(b)) if comps.is_empty() && a == b => (),
(Some(a), Some(b)) if b == Component::CurDir => comps.push(a),
(None, _) => cs.push(Component::ParentDir),
(Some(a), Some(b)) if cs.is_empty() && a == b => (),
(Some(a), Some(b)) if b == Component::CurDir => cs.push(a),
(Some(_), Some(b)) if b == Component::ParentDir => return None,
(Some(a), Some(_)) => {
comps.push(Component::ParentDir);
cs.push(Component::ParentDir);
for _ in itb {
comps.push(Component::ParentDir);
cs.push(Component::ParentDir);
}
comps.push(a);
comps.extend(ita.by_ref());
cs.push(a);
cs.extend(ita.by_ref());
break;
}
}
}
Some(comps.iter().map(|c| c.as_os_str()).collect())
Some(cs.iter().map(|c| c.as_os_str()).collect())
}
}
3 changes: 1 addition & 2 deletions src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ impl App {
let writer = &mut LineWriter::new(File::create(&cache_filepath)?);

// first line of the cache file is the current directory
writeln!(writer, "{}", self.get_current_dir().to_string_lossy())
.unwrap();
writeln!(writer, "{}", self.get_current_dir().display()).unwrap();

let state = &mut State { seen_untracked: false, count: 1 };

Expand Down
5 changes: 1 addition & 4 deletions src/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ macro_rules! test {
// Sets the $PATH environment variable such that the debug version of
// `git-nu` is front-and-center.
let path = env_var("PATH");
println!("------------------------------------");
println!("{path}");
println!("------------------------------------");
env::set_var("PATH", format!("{}:{path}", bin_dir()));

// run the test
Expand Down Expand Up @@ -154,7 +151,7 @@ macro_rules! sh {
let stdout = String::from_utf8_lossy(&v.stdout).to_string();
let stderr = String::from_utf8_lossy(&v.stderr).to_string();
println!("╭{line} RUN SH {line}╮");
println!("test dir: {}", $t.dir.to_string_lossy());
println!("test dir: {}", $t.dir.display());
println!("relative dir: \x1b[0;32m{}\x1b[0m", $cwd);
println!("cmd: \x1b[0;32m{}\x1b[0m", $cmd);
if !stdout.is_empty() {
Expand Down

0 comments on commit b831ad2

Please sign in to comment.