Skip to content

Commit

Permalink
avoid instantiating a git2 repo just to see if it is bare.
Browse files Browse the repository at this point in the history
Instead, postpone the work-tree check to when it is needed and exit
with the same message.
  • Loading branch information
Byron committed Aug 23, 2022
1 parent 7361a30 commit 6a69c41
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl Info {
let repo = git::discover(&config.input)?;
let workdir = repo
.work_dir()
.context("a non-bare repository is needed")?
.context("please run onefetch inside of a non-bare git repository")?
.to_owned();

let pending_changes = std::thread::spawn({
Expand Down
9 changes: 1 addition & 8 deletions src/info/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ use crate::info::head_refs::HeadRefs;
use anyhow::{Context, Result};
use byte_unit::Byte;
use git::bstr::BString;
use git2::{Repository, RepositoryOpenFlags, Status, StatusOptions, StatusShow};
use git2::{Status, StatusOptions, StatusShow};
use git_repository as git;
use git_repository::bstr::ByteSlice;
use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;
use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime;

Expand Down Expand Up @@ -299,11 +297,6 @@ where
dt.into().format(&Rfc3339).unwrap()
}

pub fn is_valid(repo_path: &PathBuf) -> Result<bool> {
let repo = Repository::open_ext(repo_path, RepositoryOpenFlags::empty(), Vec::<&Path>::new());
Ok(!repo?.is_bare())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
9 changes: 2 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![cfg_attr(feature = "fail-on-deprecated", deny(deprecated))]

use anyhow::{bail, Result};
use anyhow::Result;
use clap::{CommandFactory, Parser};
use cli::Config;
use info::{repo, Info};
use info::Info;
use std::io;
use ui::printer::Printer;

Expand All @@ -27,12 +27,7 @@ fn main() -> Result<()> {
return Ok(());
}

if !repo::is_valid(&config.input)? {
bail!("please run onefetch inside of a non-bare git repository");
}

let info = Info::new(&config)?;

let mut printer = Printer::new(io::BufWriter::new(io::stdout()), info, config)?;

printer.print()?;
Expand Down

0 comments on commit 6a69c41

Please sign in to comment.