1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-06-03 09:04:12 +02:00

use extra app directories for git operations

This commit is contained in:
Robin Appelman 2026-03-06 00:15:35 +01:00
commit e9cb4f08e3
3 changed files with 42 additions and 21 deletions

View file

@ -1,22 +1,29 @@
use crate::config::HazeConfig;
use crate::Result;
use git2::build::CheckoutBuilder;
use git2::{Branch, BranchType, Repository, RepositoryState};
use miette::{Context, IntoDiagnostic};
use std::fs::read_dir;
use std::path::{Path, PathBuf};
use std::iter::once;
use std::path::PathBuf;
use std::process::Command;
fn find_app_repos(root: impl AsRef<Path>) -> Result<impl Iterator<Item = PathBuf>> {
let apps_dir = root.as_ref().join("apps");
Ok(read_dir(apps_dir)
.into_diagnostic()?
fn find_app_repos(config: &HazeConfig) -> Result<impl Iterator<Item = PathBuf>> {
let apps_dirs = once(config.sources_root.as_path().join("apps"))
.chain(config.app_directories.iter().cloned());
let dir_handles = apps_dirs
.map(|dir| read_dir(dir).into_diagnostic())
.collect::<Result<Vec<_>>>()?;
Ok(dir_handles
.into_iter()
.flatten()
.flatten()
.filter(|app| app.path().join(".git").is_dir())
.map(|app| app.path()))
}
fn longest_app_branch(root: impl AsRef<Path>) -> Result<(usize, usize)> {
Ok(find_app_repos(root)?
fn longest_app_branch(config: &HazeConfig) -> Result<(usize, usize)> {
Ok(find_app_repos(config)?
.filter_map(|app_dir| {
let app_name = app_dir.file_name()?.to_str()?;
let repo = Repository::init(&app_dir).ok()?;
@ -27,27 +34,32 @@ fn longest_app_branch(root: impl AsRef<Path>) -> Result<(usize, usize)> {
.unwrap_or_default())
}
pub fn checkout_all<P: AsRef<Path>>(sources_root: P, mut name: &str) -> Result<()> {
pub fn checkout_all(config: &HazeConfig, mut name: &str, verbose: bool) -> Result<()> {
// "main" and "master" are interchangeable
if name == "main" {
name = "master";
}
for app_dir in find_app_repos(sources_root)? {
for app_dir in find_app_repos(config)? {
let repo = Repository::init(&app_dir)
.into_diagnostic()
.wrap_err_with(|| format!("Failed to open repository {}", app_dir.display()))?;
let app_name = app_dir.file_name().unwrap().to_string_lossy();
if let Some(branch) = get_branch(&repo, name)? {
if !branch.is_head() {
let is_remote = branch.get().is_remote();
print!("{}", app_dir.file_name().unwrap().to_string_lossy());
print!("{app_name}");
if let Err(e) = checkout(&repo, &branch, is_remote.then_some(name)) {
println!(": {:#}", e);
} else {
println!("");
}
} else if verbose {
println!("{app_name} -");
}
} else if verbose {
println!("{app_name} 🛇 Branch not found");
};
}
Ok(())
@ -68,11 +80,10 @@ const GIT_BINARY: &str = match option_env!("GIT_BINARY") {
None => "git",
};
pub fn pull_all<P: AsRef<Path>>(sources_root: P) -> Result<()> {
let sources_root = sources_root.as_ref();
let (max_app, max_branch) = longest_app_branch(sources_root)?;
pub fn pull_all(config: &HazeConfig) -> Result<()> {
let (max_app, max_branch) = longest_app_branch(config)?;
for app_dir in find_app_repos(sources_root)? {
for app_dir in find_app_repos(config)? {
let app_name = app_dir.file_name().unwrap().to_string_lossy();
let repo = Repository::init(&app_dir)
.into_diagnostic()