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 Apr 10, 2024
2 parents a0cca5a + bb95a0f commit a3ca73c
Show file tree
Hide file tree
Showing 17 changed files with 1,041 additions and 1,064 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
cargo build
cargo test --lib
cargo test
inc:
name: Increment 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.

8 changes: 2 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gitnu"
version = "0.7.5"
version = "0.7.6-alpha.6"
authors = ["Nguyen Vu Khang <[email protected]>"]
description = """
gitnu indexes your git status so you can use numbers instead of filenames.
Expand All @@ -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
12 changes: 5 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ LOCAL_BIN=$(HOME)/.local/bin
GITNU_RELEASE_BIN=$(PWD)/target/release/git-nu
GITNU_DEBUG_BIN=$(PWD)/target/debug/git-nu

ONE_TEST := 'tests::add_and_status_diff_dirs'
ONE_TEST := 'tests::exit_codes'
ONE_TEST := 'tests::git_add_untracked'

current:
make test
# make test-one
current: test

install:
make build
make load-bin
cargo install --all-features --path . --locked --bins

build:
cargo build --bin git-nu --release
@echo "Release build complete."

test:
cargo build
cargo test --lib
cargo test

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

This file was deleted.

25 changes: 14 additions & 11 deletions src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::pathdiff;
use crate::prelude::*;

use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::process::Command;

#[derive(Debug, Default)]
pub struct Cache {
Expand All @@ -13,23 +11,25 @@ pub struct Cache {
}

impl Cache {
/// Initialize cache by reading the cache file in `git_dir`.
pub fn new<P>(git_dir: &PathBuf, cwd: P) -> Self
where
P: AsRef<Path>,
{
Self::try_read(git_dir, cwd).unwrap_or_default()
}

/// Try to read the cache file from `git_dir`.
fn try_read<P>(git_dir: &PathBuf, cwd: P) -> Result<Self>
where
P: AsRef<Path>,
{
let mut filepath = cwd.as_ref().to_path_buf();
filepath.push(git_dir);
filepath.push(CACHE_FILE_NAME);
let mut cache_path = cwd.as_ref().to_path_buf();
cache_path.push(git_dir);
cache_path.push(CACHE_FILE_NAME);

let file = File::open(filepath)?;
let mut lines = BufReader::new(file).lines().filter_map(|v| v.ok());
let f = File::open(cache_path)?;
let mut lines = BufReader::new(f).lines().filter_map(|v| v.ok());

let prefix = {
let first_line = lines.next().ok_or(Error::InvalidCache)?;
Expand All @@ -47,11 +47,14 @@ impl Cache {
Ok(Self { prefix, files })
}

pub fn load(&self, index: usize, cmd: &mut Command) {
/// Append the `index`-th cached value into an ArgHolder.
pub fn load<A: ArgHolder>(&self, index: usize, argh: &mut A) {
match (&self.prefix, self.files.get(index)) {
(Some(prefix), Some(pathspec)) => cmd.arg(prefix.join(pathspec)),
(None, Some(pathspec)) => cmd.arg(pathspec),
_ => cmd.arg(index.to_string()),
(Some(prefix), Some(pathspec)) => {
argh.add_arg(prefix.join(pathspec))
}
(None, Some(pathspec)) => argh.add_arg(pathspec),
_ => argh.add_arg(index.to_string()),
};
}
}
45 changes: 45 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::any::Any;
use std::io;

#[derive(Debug)]
pub enum Error {
InvalidCache,
NotGitCommand,
NotGitRepository,
NotImplemented,
Io(io::Error),
ThreadError(Box<dyn Any + Send + 'static>),
}

impl PartialEq for Error {
fn eq(&self, rhs: &Error) -> bool {
let lhs = self;
use Error::*;
match (lhs, rhs) {
(NotGitRepository, NotGitRepository) => true,
(NotImplemented, NotImplemented) => true,
(InvalidCache, InvalidCache) => true,
(NotGitCommand, NotGitCommand) => true,
(Io(lhs), Io(rhs)) => lhs.kind() == rhs.kind(),
(ThreadError(_), ThreadError(_)) => true,
_ => false,
}
}
}

#[macro_export]
macro_rules! error {
($enum:ident, $from:ty) => {
impl From<$from> for Error {
fn from(err: $from) -> Self {
Self::$enum(err)
}
}
};
($enum:ident) => {
Err(Error::$enum)
};
}

error!(Io, io::Error);
error!(ThreadError, Box<dyn Any + Send + 'static>);
Loading

0 comments on commit a3ca73c

Please sign in to comment.