1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-06-03 17:14:08 +02:00

add command to checkout branch in all apps

This commit is contained in:
Robin Appelman 2022-08-24 17:04:30 +02:00
commit 5278f0d295
5 changed files with 191 additions and 0 deletions

79
Cargo.lock generated
View file

@ -26,6 +26,15 @@ dependencies = [
"memchr",
]
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]]
name = "async-trait"
version = "0.1.57"
@ -515,6 +524,8 @@ dependencies = [
"tokio",
"tokio-stream",
"toml",
"tracing",
"tracing-subscriber",
"warp",
"warp-reverse-proxy",
]
@ -1320,6 +1331,15 @@ dependencies = [
"digest 0.10.3",
]
[[package]]
name = "sharded-slab"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31"
dependencies = [
"lazy_static",
]
[[package]]
name = "shell-words"
version = "1.1.0"
@ -1344,6 +1364,12 @@ dependencies = [
"autocfg",
]
[[package]]
name = "smallvec"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1"
[[package]]
name = "smawk"
version = "0.3.1"
@ -1545,6 +1571,15 @@ dependencies = [
"syn",
]
[[package]]
name = "thread_local"
version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180"
dependencies = [
"once_cell",
]
[[package]]
name = "tinyvec"
version = "1.6.0"
@ -1678,9 +1713,21 @@ dependencies = [
"cfg-if",
"log",
"pin-project-lite",
"tracing-attributes",
"tracing-core",
]
[[package]]
name = "tracing-attributes"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11c75893af559bc8e10716548bdef5cb2b983f8e637db9d0e15126b61b484ee2"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tracing-core"
version = "0.1.28"
@ -1688,6 +1735,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b7358be39f2f274f322d2aaed611acc57f382e8eb1e5b48cb9ae30933495ce7"
dependencies = [
"once_cell",
"valuable",
]
[[package]]
name = "tracing-log"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922"
dependencies = [
"lazy_static",
"log",
"tracing-core",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60db860322da191b40952ad9affe65ea23e7dd6a5c442c2c42865810c6ab8e6b"
dependencies = [
"ansi_term",
"sharded-slab",
"smallvec",
"thread_local",
"tracing-core",
"tracing-log",
]
[[package]]
@ -1805,6 +1878,12 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "version_check"
version = "0.9.4"

View file

@ -30,6 +30,8 @@ shell-words = "1.1.0"
warp = "0.3"
warp-reverse-proxy = { version = "0.5.0", default_features = false, features = ["rustls-tls"] }
tokio-stream = { version = "0.1.9", features = ["net"] }
tracing = "0.1.35"
tracing-subscriber = "0.3.14"
[profile.release]
lto = true

View file

@ -61,6 +61,9 @@ pub enum HazeArgs {
filter: Option<String>,
},
Proxy,
Checkout {
branch: String,
},
}
#[derive(Debug, Clone, Eq, PartialEq)]
@ -212,6 +215,13 @@ impl HazeArgs {
HazeCommand::Pin => Ok(HazeArgs::Pin { filter }),
HazeCommand::Unpin => Ok(HazeArgs::Unpin { filter }),
HazeCommand::Proxy => Ok(HazeArgs::Proxy),
HazeCommand::Checkout => {
let branch = args
.next()
.map(S::into)
.ok_or_else(|| Report::msg("No branch provided"))?;
Ok(HazeArgs::Checkout { branch })
}
}
}
}
@ -234,6 +244,7 @@ pub enum HazeCommand {
Pin,
Unpin,
Proxy,
Checkout,
}
impl FromStr for HazeCommand {
@ -258,6 +269,7 @@ impl FromStr for HazeCommand {
"pin" => Ok(HazeCommand::Pin),
"unpin" => Ok(HazeCommand::Unpin),
"proxy" => Ok(HazeCommand::Proxy),
"checkout" => Ok(HazeCommand::Checkout),
_ => Err(Report::msg(format!("Unknown command: {}", s))),
}
}
@ -282,6 +294,7 @@ impl HazeCommand {
HazeCommand::Pin => true,
HazeCommand::Unpin => true,
HazeCommand::Proxy => false,
HazeCommand::Checkout => false,
}
}
}

91
src/git.rs Normal file
View file

@ -0,0 +1,91 @@
use crate::Result;
use miette::{miette, IntoDiagnostic};
use std::fs::read_dir;
use std::path::Path;
use std::process::Command;
use tracing::error;
pub fn checkout_all<P: AsRef<Path>>(sources_root: P, branch: &str) -> Result<()> {
let apps_dir = sources_root.as_ref().join("apps");
for app in read_dir(apps_dir).into_diagnostic()? {
let app = app.into_diagnostic()?;
if app.metadata().into_diagnostic()?.is_dir() {
let app_dir = app.path();
if has_branch(&app_dir, &branch).unwrap_or_default()
&& get_branch(&app_dir).unwrap_or_default().trim() != branch
{
print!("{}", app.file_name().to_string_lossy());
if let Err(e) = checkout(&app_dir, &branch) {
println!(": {}", e);
} else {
println!("");
}
}
}
}
Ok(())
}
fn has_branch<P: AsRef<Path>>(repo: P, branch: &str) -> Result<bool> {
let output = Command::new("git")
.current_dir(repo)
.arg("branch")
.arg("-a")
.output()
.into_diagnostic()?;
let stdout = String::from_utf8(output.stdout).into_diagnostic()?;
let stderr = String::from_utf8(output.stderr).into_diagnostic()?;
if output.status.code().unwrap_or_default() != 0 {
error!(stdout = stdout, stderr = stderr, "git command failed");
}
for line in stdout.split("\n") {
let line = line.trim();
if line == branch {
return Ok(true);
}
if let Some((_, part)) = line.rsplit_once('/') {
if part == branch {
return Ok(true);
}
}
}
Ok(false)
}
fn checkout<P: AsRef<Path>>(repo: P, branch: &str) -> Result<()> {
let output = Command::new("git")
.current_dir(repo)
.arg("checkout")
.arg(branch)
.output()
.into_diagnostic()?;
let stdout = String::from_utf8(output.stdout).into_diagnostic()?;
let stderr = String::from_utf8(output.stderr).into_diagnostic()?;
let code = output.status.code().unwrap_or_default();
if code != 0 {
if stderr.contains("would be overwritten") {
return Err(miette!("uncommited changes"));
}
error!(
stdout = stdout,
stderr = stderr,
code = code,
"git command failed"
);
}
Ok(())
}
fn get_branch<P: AsRef<Path>>(repo: P) -> Result<String> {
let output = Command::new("git")
.current_dir(repo)
.arg("rev-parse")
.arg("--abbrev-ref")
.arg("HEAD")
.output()
.into_diagnostic()?;
String::from_utf8(output.stdout).into_diagnostic()
}

View file

@ -2,6 +2,7 @@ use crate::args::{ExecService, HazeArgs};
use crate::cloud::{Cloud, CloudOptions};
use crate::config::HazeConfig;
use crate::exec::container_logs;
use crate::git::checkout_all;
use crate::network::clear_networks;
use crate::php::PhpVersion;
use crate::proxy::proxy;
@ -16,6 +17,7 @@ mod cloud;
mod config;
mod database;
mod exec;
mod git;
mod image;
mod mapping;
mod network;
@ -26,6 +28,7 @@ mod service;
#[tokio::main]
async fn main() -> Result<()> {
miette::set_panic_hook();
tracing_subscriber::fmt::init();
let mut docker = Docker::connect_with_local_defaults()
.into_diagnostic()
@ -290,6 +293,9 @@ async fn main() -> Result<()> {
HazeArgs::Proxy => {
proxy(docker, config).await?;
}
HazeArgs::Checkout { branch } => {
checkout_all(&config.sources_root, &branch)?;
}
};
Ok(())