Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenvukhang committed Mar 5, 2024
2 parents ebe0e0f + a50e3e3 commit 2dd630b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 43 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ jobs:
inc:
name: Increment version
needs: [pre, build-and-test]
if: startsWith(github.event.head_commit.message, 'ver:') == false
runs-on: ubuntu-latest
outputs:
version: ${{ steps.semver.outputs.version }}
Expand Down
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.4"
version = "0.7.5-alpha.3"
authors = ["Nguyen Vu Khang <[email protected]>"]
description = """
gitnu indexes your git status so you can use numbers instead of filenames.
Expand Down
22 changes: 13 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,20 @@ impl App {
mod tests {
use super::*;

#[cfg(test)]
fn parse(args: &[&str]) -> Vec<String> {
let mut a = vec!["git"];
a.extend(args);
let mut app = App::default();
app.parse(&string_vec(a));
app.final_cmd.real_args()
}

macro_rules! test {
($name:ident, $input_args:expr, $output_args:expr) => {
#[test]
fn $name() {
use $crate::prelude::*;
let mut args = vec!["git"];
args.extend($input_args);
let mut app = App::default();
app.parse(&string_vec(args));
let received_args = app.final_cmd.real_args();
let received_args = parse(&$input_args);
let expected_args = string_vec($output_args);
assert_eq!(received_args, expected_args);
}
Expand All @@ -166,9 +170,9 @@ mod tests {
["add", "3", "4", "5", "--", "2", "3", "4"]
);

test!(test_zeroes_1, ["add", "0"], ["add", "0"]);
test!(test_zeroes_2, ["add", "0-1"], ["add", "0", "1"]);
test!(test_zeroes_3, ["add", "0-0"], ["add", "0"]);
test!(test_zeros_1, ["add", "0"], ["add", "0"]);
test!(test_zeros_2, ["add", "0-1"], ["add", "0", "1"]);
test!(test_zeros_3, ["add", "0-0"], ["add", "0"]);

// Filenames containing dashed dates
test!(test_date_filename, ["add", "2021-01-31"], ["add", "2021-01-31"]);
Expand Down
12 changes: 3 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,14 @@ mod git {
/// 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>(cwd: P) -> Result<PathBuf>
where
P: AsRef<Path>,
{
pub(crate) fn dir<P: AsRef<Path>>(cwd: P) -> Result<PathBuf> {
_dir(Some(cwd))
}

fn sh<P>(dir: Option<P>, args: &[&str]) -> Result<Output>
where
P: AsRef<Path>,
{
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().map_err(|e| Error::Io(e))?)
Ok(cmd.args(args).output()?)
}

fn _dir<P: AsRef<Path>>(base_dir: Option<P>) -> Result<PathBuf> {
Expand Down
38 changes: 17 additions & 21 deletions src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,17 @@ use std::process::{ExitCode, Stdio};

/// Removes all ANSI color codes
pub fn uncolor(src: &str) -> Vec<u8> {
let (mut src, mut dst) = (src.as_bytes(), vec![]);
while !src.is_empty() {
match src.iter().position(|v| v == &b'\x1b') {
None => break,
Some(i) => {
dst.extend(&src[..i]);
src = &src[i..];
}
}
match src.iter().position(|v| v == &b'm') {
None => break,
Some(i) => src = &src[i + 1..],
}
let (mut b, mut j, mut on) = (src.as_bytes().to_owned(), 0, true);
for i in 0..b.len() {
match (on, b[i]) {
(_, b'\x1b') => on = false,
(true, _) => (b[j], j) = (b[i], j + 1),
(_, b'm') => on = true,
_ => (),
};
}
dst.extend(src);
dst
b.truncate(j);
b
}

struct State {
Expand Down Expand Up @@ -51,7 +46,10 @@ fn normal(state: &mut State, line: String) -> Option<String> {

let line = &uncolor(&line);
let line: &str = std::str::from_utf8(line).unwrap();
let line = line.rsplit_once('\t').unwrap().1;
let line = line
.rsplit_once('\t')
.expect("There should be a tab character in the line")
.1;

// Example:
// ```
Expand All @@ -68,8 +66,7 @@ fn normal(state: &mut State, line: String) -> Option<String> {
true => ("", line),
};

let pathspec =
pathspec.trim_start_matches(|v: char| v.is_ascii_whitespace());
let pathspec = pathspec.trim_start();

let pathspec = match delta {
// Example:
Expand All @@ -81,7 +78,7 @@ fn normal(state: &mut State, line: String) -> Option<String> {
.split_once("->")
.expect("There should be a `->` in the line with a rename")
.1
.trim_start_matches(|v: char| v.is_ascii_whitespace()),
.trim_start(),
_ => pathspec,
};

Expand All @@ -100,8 +97,7 @@ impl App {
/// Runs `git status` then parses its output, enumerates it, and
/// prints it out to stdout.
pub(crate) fn git_status(mut self) -> Result<ExitCode> {
let mut git =
self.final_cmd.stdout(Stdio::piped()).spawn()?;
let mut git = self.final_cmd.stdout(Stdio::piped()).spawn()?;

let lines = match git.stdout.take() {
Some(v) => BufReader::new(v).lines().filter_map(|v| v.ok()),
Expand Down
2 changes: 0 additions & 2 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ test!(
sh!(t, "git init -b main");
sh!(t, "mkdir src");
sh!(t, "touch A B src/C src/D");
println!("GOT HERE");
gitnu!(t, status).unwrap();
println!("GOT HERE");
},
"src",
["add", "2", "3"],
Expand Down

0 comments on commit 2dd630b

Please sign in to comment.