Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diff against branch in pending PR #13

Merged
merged 3 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
FORK_ARGS = \
--fork-regex /coreos/ \
--fork-replacement /coreosbot-releng/ \
--fork-branch repo-templates

.PHONY: diff
diff: tmpl8
tmpl8/target/debug/tmpl8 diff | less -R
tmpl8/target/debug/tmpl8 diff $(FORK_ARGS) | less -R

.PHONY: output
output: tmpl8
Expand All @@ -9,7 +14,7 @@ output: tmpl8
# Force sync of downstream repo cache
.PHONY: sync
sync: tmpl8
tmpl8/target/debug/tmpl8 update-cache
tmpl8/target/debug/tmpl8 update-cache $(FORK_ARGS)

.PHONY: tmpl8
tmpl8:
Expand Down
32 changes: 32 additions & 0 deletions tmpl8/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::path::PathBuf;

use anyhow::Result;
use clap::Parser;
use regex::Regex;

mod github;
mod render;
Expand Down Expand Up @@ -54,6 +55,8 @@ struct DiffArgs {
/// Config file
#[clap(short = 'c', long, value_name = "file", default_value = "config.yaml")]
config: PathBuf,
#[clap(flatten)]
fork: ForkArgs,
/// Disable color output
#[clap(short = 'n', long)]
no_color: bool,
Expand All @@ -64,6 +67,24 @@ struct UpdateCacheArgs {
/// Config file
#[clap(short = 'c', long, value_name = "file", default_value = "config.yaml")]
config: PathBuf,
#[clap(flatten)]
fork: ForkArgs,
}

#[derive(Debug, Parser)]
struct ForkArgs {
/// Regex for the upstream part of repo URL
#[clap(long = "fork-regex", value_name = "regex")]
#[clap(requires_all = &["replacement", "branch"])]
regex: Option<Regex>,
/// Replacement for upstream part of repo URL
#[clap(long = "fork-replacement", value_name = "string")]
#[clap(requires_all = &["regex", "branch"])]
replacement: Option<String>,
/// Fork branch
#[clap(long = "fork-branch", value_name = "branch")]
#[clap(requires_all = &["regex", "replacement"])]
branch: Option<String>,
}

#[derive(Debug, Parser)]
Expand All @@ -84,3 +105,14 @@ fn main() -> Result<()> {
Cmd::GithubMatrix(c) => github::get_matrix(c),
}
}

#[cfg(test)]
mod test {
use super::*;
use clap::IntoApp;

#[test]
fn clap_app() {
Cmd::command().debug_assert()
}
}
115 changes: 94 additions & 21 deletions tmpl8/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use yansi::Paint;
use super::schema::*;
use super::*;

const DEFAULT_BRANCH: &str = "DEFAULT";

pub(super) fn render(args: RenderArgs) -> Result<()> {
let cfg = Config::parse(&args.config)?;
if let Some(repo) = &args.repo {
Expand Down Expand Up @@ -62,7 +64,7 @@ pub(super) fn diff(args: DiffArgs) -> Result<()> {

// update Git cache
let cache_dir = cache_dir(&args.config)?;
do_update_cache(&cfg, &cache_dir, false)?;
do_update_cache(&cfg, &cache_dir, &args.fork, false)?;

if args.no_color {
Paint::disable();
Expand Down Expand Up @@ -102,7 +104,7 @@ pub(super) fn diff(args: DiffArgs) -> Result<()> {

pub(super) fn update_cache(args: UpdateCacheArgs) -> Result<()> {
let cfg = Config::parse(&args.config)?;
do_update_cache(&cfg, &cache_dir(&args.config)?, true)
do_update_cache(&cfg, &cache_dir(&args.config)?, &args.fork, true)
}

fn do_render(config_path: &Path, cfg: &Config) -> Result<BTreeMap<PathBuf, String>> {
Expand Down Expand Up @@ -149,37 +151,103 @@ fn clean_rendered_output(output: &str) -> String {
output.to_string()
}

fn do_update_cache(cfg: &Config, cache_dir: &Path, force: bool) -> Result<()> {
fn do_update_cache(cfg: &Config, cache_dir: &Path, fork: &ForkArgs, force: bool) -> Result<()> {
for (name, repo) in &cfg.repos {
// clone repo if missing
let path = cache_dir.join(name);
let stderr_fd = nix::unistd::dup(2_i32.as_raw_fd()).context("duplicating stderr")?;
let stderr = unsafe { Stdio::from_raw_fd(stderr_fd) };
match fs::metadata(&path) {
Ok(meta) => {
if !path.exists() {
run_command(
Command::new("git")
.args(["clone", "--depth=1", &repo.url])
.arg(&path)
.stdout(stderr()?),
)?;
// use consistent name for default branch
run_command(
Command::new("git")
.args(["branch", "-m", DEFAULT_BRANCH])
.stdout(stderr()?)
.current_dir(&path),
)?;
}

// compute unique identifier of remote branch
let remote_url = fork
.regex
.as_ref()
.map(|re| re.replace(&repo.url, fork.replacement.as_ref().unwrap()));
let ident = if let Some(url) = &remote_url {
format!("{} {}\n", url, fork.branch.as_ref().unwrap())
} else {
DEFAULT_BRANCH.into()
};

// see if we need to update
let stamp_path = path.join(".git/tmpl8-stamp");
// need to switch branches if the stamp contents are different
match fs::read(&stamp_path) {
Ok(id) if id == ident.as_bytes() => {
// update anyway if stale or forced
let meta = fs::metadata(&stamp_path)
.with_context(|| format!("statting {}", stamp_path.display()))?;
// Update the cache at most once per hour, unless forced
let age = FileTime::now().seconds()
- FileTime::from_last_modification_time(&meta).seconds();
if force || !(0..3600).contains(&age) {
run_command(
Command::new("git")
.arg("pull")
.stdout(stderr)
.current_dir(&path),
)?;
filetime::set_file_mtime(&path, FileTime::now())
.with_context(|| format!("updating timestamp of {}", path.display()))?;
if (0..3600).contains(&age) && !force {
continue;
}
}
Err(e) if e.kind() == io::ErrorKind::NotFound => {
Ok(_) => (),
Err(e) if e.kind() == io::ErrorKind::NotFound => (),
Err(e) => return Err(e).with_context(|| format!("reading {}", stamp_path.display())),
}

// update checkout
let mut updated = false;
// remote fork branch exists?
if let Some(remote_url) = &remote_url {
if Command::new("git")
.args(&[
"fetch",
"--depth",
"1",
remote_url,
fork.branch.as_ref().unwrap(),
])
.stdout(stderr()?)
.current_dir(&path)
.status()
.context("running git fetch")?
.success()
{
run_command(
Command::new("git")
.args(["clone", "--depth=1", &repo.url])
.arg(&path)
.stdout(stderr),
.args(&["-c", "advice.detachedHead=false", "checkout", "FETCH_HEAD"])
.stdout(stderr()?)
.current_dir(&path),
)?;
updated = true;
}
Err(e) => return Err(e).with_context(|| format!("querying {}", path.display())),
}
// fall back to default branch
if !updated {
run_command(
Command::new("git")
.args(&["checkout", DEFAULT_BRANCH])
.stdout(stderr()?)
.current_dir(&path),
)?;
run_command(
Command::new("git")
.arg("pull")
.stdout(stderr()?)
.current_dir(&path),
)?;
}

// update stamp
fs::write(&stamp_path, &ident)
.with_context(|| format!("writing {}", stamp_path.display()))?;
}
Ok(())
}
Expand Down Expand Up @@ -228,3 +296,8 @@ fn run_command(cmd: &mut Command) -> Result<()> {
}
Ok(())
}

fn stderr() -> Result<Stdio> {
let stderr_fd = nix::unistd::dup(2_i32.as_raw_fd()).context("duplicating stderr")?;
Ok(unsafe { Stdio::from_raw_fd(stderr_fd) })
}