mirror of
https://codeberg.org/icewind/haze.git
synced 2026-06-03 17:14:08 +02:00
switch to multiprogress for git pull
This commit is contained in:
parent
373ce0f3fd
commit
8771e7dc5f
1 changed files with 25 additions and 9 deletions
34
src/git.rs
34
src/git.rs
|
|
@ -2,11 +2,13 @@ use crate::config::HazeConfig;
|
||||||
use crate::Result;
|
use crate::Result;
|
||||||
use git2::build::CheckoutBuilder;
|
use git2::build::CheckoutBuilder;
|
||||||
use git2::{Branch, BranchType, Repository, RepositoryState};
|
use git2::{Branch, BranchType, Repository, RepositoryState};
|
||||||
|
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
|
||||||
use miette::{Context, IntoDiagnostic};
|
use miette::{Context, IntoDiagnostic};
|
||||||
use std::fs::read_dir;
|
use std::fs::read_dir;
|
||||||
use std::iter::once;
|
use std::iter::once;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
fn find_app_repos(config: &HazeConfig) -> Result<impl Iterator<Item = PathBuf>> {
|
fn find_app_repos(config: &HazeConfig) -> Result<impl Iterator<Item = PathBuf>> {
|
||||||
let apps_dirs = once(config.sources_root.as_path().join("apps"))
|
let apps_dirs = once(config.sources_root.as_path().join("apps"))
|
||||||
|
|
@ -83,6 +85,9 @@ const GIT_BINARY: &str = match option_env!("GIT_BINARY") {
|
||||||
pub fn pull_all(config: &HazeConfig) -> Result<()> {
|
pub fn pull_all(config: &HazeConfig) -> Result<()> {
|
||||||
let (max_app, max_branch) = longest_app_branch(config)?;
|
let (max_app, max_branch) = longest_app_branch(config)?;
|
||||||
|
|
||||||
|
let progress = MultiProgress::new();
|
||||||
|
let pull_style = ProgressStyle::with_template("{spinner:.green} {msg}").unwrap();
|
||||||
|
|
||||||
for app_dir in find_app_repos(config)? {
|
for app_dir in find_app_repos(config)? {
|
||||||
let app_name = app_dir.file_name().unwrap().to_string_lossy();
|
let app_name = app_dir.file_name().unwrap().to_string_lossy();
|
||||||
let repo = Repository::init(&app_dir)
|
let repo = Repository::init(&app_dir)
|
||||||
|
|
@ -90,17 +95,26 @@ pub fn pull_all(config: &HazeConfig) -> Result<()> {
|
||||||
.wrap_err_with(|| format!("Failed to open repository {}", app_dir.display()))?;
|
.wrap_err_with(|| format!("Failed to open repository {}", app_dir.display()))?;
|
||||||
let branch_name = current_branch_name(&repo).unwrap_or("unknown".into());
|
let branch_name = current_branch_name(&repo).unwrap_or("unknown".into());
|
||||||
|
|
||||||
print!(
|
let bar = ProgressBar::new_spinner().with_style(pull_style.clone());
|
||||||
"{app_name:<app_width$} - {branch_name:<branch_width$}",
|
bar.enable_steady_tick(Duration::from_millis(100));
|
||||||
app_width = max_app,
|
let bar = progress.add(bar);
|
||||||
branch_width = max_branch
|
|
||||||
);
|
let msg = |state: &str| {
|
||||||
|
format!(
|
||||||
|
"{app_name:<app_width$} - {branch_name:<branch_width$}{state}",
|
||||||
|
app_width = max_app,
|
||||||
|
branch_width = max_branch
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
if repo.state() != RepositoryState::Clean {
|
if repo.state() != RepositoryState::Clean {
|
||||||
println!(": repository not clean ❌");
|
bar.set_message(msg("repository not clean ❌"));
|
||||||
|
bar.finish();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bar.set_message(msg(""));
|
||||||
|
|
||||||
let output = Command::new(GIT_BINARY)
|
let output = Command::new(GIT_BINARY)
|
||||||
.arg("pull")
|
.arg("pull")
|
||||||
.current_dir(&app_dir)
|
.current_dir(&app_dir)
|
||||||
|
|
@ -109,11 +123,13 @@ pub fn pull_all(config: &HazeConfig) -> Result<()> {
|
||||||
.wrap_err_with(|| format!("Failed to run git pull for {}", app_dir.display()))?;
|
.wrap_err_with(|| format!("Failed to run git pull for {}", app_dir.display()))?;
|
||||||
|
|
||||||
if output.status.success() {
|
if output.status.success() {
|
||||||
println!(" ✓");
|
bar.set_message(msg(" ✓"));
|
||||||
} else {
|
} else {
|
||||||
println!(" ❌");
|
let err = String::from_utf8_lossy(&output.stderr);
|
||||||
eprintln!("{}", String::from_utf8_lossy(&output.stderr))
|
let err_line = err.lines().next().unwrap();
|
||||||
|
bar.set_message(msg(&format!(" ❌ {err_line}")));
|
||||||
}
|
}
|
||||||
|
bar.finish();
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue