Skip to content

Commit

Permalink
JSON log support + trace git commands
Browse files Browse the repository at this point in the history
  • Loading branch information
elegaanz committed Jul 24, 2024
1 parent 2d50869 commit c00e6ed
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 54 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ tokio = { version = "1.37.0", features = ["rt-multi-thread", "process", "fs"] }
toml_edit = "0.22.12"
tower-http = { version = "0.5", features = ["trace"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
tracing-subscriber = { version = "0.3.18", features = ["json"] }
typst = "0.11.0"
typst-assets = { version = "0.11.0", features = [ "fonts" ] }
111 changes: 59 additions & 52 deletions src/github/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::{
collections::HashSet,
path::{Path, PathBuf},
process::{Output, Stdio},
};

use eyre::{Context, ContextCompat};
Expand All @@ -19,58 +20,45 @@ impl<'a> GitRepo<'a> {
}

pub async fn clone_if_needed(&self, url: &str) -> eyre::Result<()> {
let status = Command::new("git")
.args(["-C", self.dir()?, "status"])
.status()
.await?;
let status = traced_git(["-C", self.dir()?, "status"]).await?.status;

if !status.success() {
Command::new("git")
.args(["clone", url, self.dir()?])
.spawn()?
.wait()
.await?;
traced_git(["clone", url, self.dir()?]).await?;
}

Ok(())
}

pub async fn pull_main(&self) -> eyre::Result<()> {
debug!("Pulling main branch");
Command::new("git")
.args([
"-C",
self.dir()?,
"-c",
"receive.maxInputSize=134217728", // 128MB
"pull",
"origin",
"main",
"--ff-only",
])
.spawn()?
.wait()
.await?;
traced_git([
"-C",
self.dir()?,
"-c",
"receive.maxInputSize=134217728", // 128MB
"pull",
"origin",
"main",
"--ff-only",
])
.await?;
debug!("Done");
Ok(())
}

pub async fn fetch_commit(&self, sha: impl AsRef<str>) -> eyre::Result<()> {
debug!("Fetching commit: {}", sha.as_ref());
Command::new("git")
.args([
"-C",
self.dir()?,
"-c",
"receive.maxInputSize=134217728", // 128MB
"fetch",
"origin",
sha.as_ref(),
])
.spawn()?
.wait()
.await
.context("Failed to fetch {} (probably because of some large file).")?;
traced_git([
"-C",
self.dir()?,
"-c",
"receive.maxInputSize=134217728", // 128MB
"fetch",
"origin",
sha.as_ref(),
])
.await
.context("Failed to fetch {} (probably because of some large file).")?;
debug!("Done");
Ok(())
}
Expand All @@ -88,21 +76,18 @@ impl<'a> GitRepo<'a> {
);
tokio::fs::create_dir_all(&working_tree).await?;
let working_tree = working_tree.as_ref().canonicalize()?;
Command::new("git")
.args([
"-C",
self.dir
.to_str()
.context("Directory name is not valid unicode")?,
&format!("--work-tree={}", working_tree.display()),
"checkout",
sha.as_ref(),
"--",
".",
])
.spawn()?
.wait()
.await?;
traced_git([
"-C",
self.dir
.to_str()
.context("Directory name is not valid unicode")?,
&format!("--work-tree={}", working_tree.display()),
"checkout",
sha.as_ref(),
"--",
".",
])
.await?;
debug!("Done");
Ok(())
}
Expand Down Expand Up @@ -175,3 +160,25 @@ impl<'a> GitRepo<'a> {
.context("Directory name is not valid unicode")
}
}

#[tracing::instrument(name = "git-command")]
async fn traced_git(
args: impl IntoIterator<Item = &str> + std::fmt::Debug,
) -> eyre::Result<Output> {
let out = Command::new("git")
.args(args)
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?
.wait_with_output()
.await?;

if let Ok(stderr) = std::str::from_utf8(&out.stderr) {
debug!(stderr = stderr)
}
if let Ok(stdout) = std::str::from_utf8(&out.stdout) {
debug!(stdout = stdout)
}

Ok(out)
}
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ mod world;
#[tokio::main]
async fn main() {
dotenvy::dotenv().ok();
tracing_subscriber::fmt::init();

if std::env::var("LOG_STYLE").as_deref().unwrap_or("human") == "json" {
tracing_subscriber::fmt()
.event_format(tracing_subscriber::fmt::format::json())
.init();
} else {
tracing_subscriber::fmt::init();
}

let mut args = std::env::args();
let cmd = args.next();
Expand Down

0 comments on commit c00e6ed

Please sign in to comment.