use std::path::{Path, PathBuf};
pub mod tracked;
use crate::names::old;
struct Move {
old: &'static str,
new: &'static str,
header: Option<(&'static str, &'static str)>,
}
use crate::names as n;
const MOVES: &[Move] = &[
Move {
old: "amont-bypasses",
new: n::BYPASS_LEDGER,
header: Some(("amont-bypass-v1", n::BYPASS_FORMAT)),
},
Move {
old: "amont-downgrades",
new: n::DOWNGRADE_LEDGER,
header: Some(("amont-downgrade-v1", n::DOWNGRADE_FORMAT)),
},
Move {
old: "amont-waves",
new: n::WAVES_LEDGER,
header: Some(("amont-waves-v1", n::WAVES_FORMAT)),
},
Move {
old: "amont-rehearsal",
new: n::REHEARSAL_STATE,
header: Some(("amont-rehearsal-v1", n::REHEARSAL_FORMAT)),
},
Move {
old: "amont-rehearsal.log",
new: n::REHEARSAL_LOG,
header: None,
},
Move {
old: "amont-skew",
new: n::SKEW_MARKER,
header: Some(("amont-skew-v1", n::SKEW_FORMAT)),
},
Move {
old: "amont-gate",
new: n::GATE_MARKER,
header: None,
},
Move {
old: "amont-warm.log",
new: n::WARM_LOG,
header: None,
},
Move {
old: "amont-cache",
new: n::TREE_CACHE,
header: None,
},
Move {
old: "amont-preserved",
new: n::PRESERVED_STORE,
header: None,
},
Move {
old: "amont",
new: n::STATE_DIR,
header: None,
},
];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Forward,
Reverse,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Repo {
pub common: PathBuf,
}
impl Repo {
pub fn locate(start: &Path) -> Option<Repo> {
Repo::locate_with(start, std::env::var_os("GIT_DIR"))
}
pub fn locate_with(start: &Path, git_dir: Option<std::ffi::OsString>) -> Option<Repo> {
let private = match git_dir.filter(|d| !d.is_empty()) {
Some(dir) => absolute(start, Path::new(&dir)),
None => find_git_dir(start)?,
};
let common = match std::fs::read_to_string(private.join("commondir")) {
Ok(text) => absolute(&private, Path::new(text.trim())),
Err(_) => private.clone(),
};
Some(Repo { common })
}
pub fn worktrees(&self) -> Vec<PathBuf> {
match std::fs::read_dir(self.common.join("worktrees")) {
Ok(entries) => {
let mut dirs: Vec<PathBuf> = entries
.filter_map(Result::ok)
.map(|e| e.path())
.filter(|p| p.is_dir())
.collect();
dirs.sort();
dirs
}
Err(_) => Vec::new(),
}
}
pub fn git_dirs(&self) -> Vec<PathBuf> {
std::iter::once(self.common.clone())
.chain(self.worktrees())
.collect()
}
pub fn marker(&self) -> PathBuf {
self.common.join(n::MIGRATED_MARKER)
}
}
fn absolute(base: &Path, path: &Path) -> PathBuf {
if path.is_absolute() {
path.to_path_buf()
} else {
base.join(path)
}
}
fn find_git_dir(start: &Path) -> Option<PathBuf> {
let mut here = Some(start);
while let Some(dir) = here {
let dotgit = dir.join(".git");
if dotgit.is_dir() {
return Some(dotgit);
}
if let Ok(text) = std::fs::read_to_string(&dotgit) {
let pointer = text.lines().find_map(|l| l.strip_prefix("gitdir:"))?;
return Some(absolute(dir, Path::new(pointer.trim())));
}
here = dir.parent();
}
None
}
pub fn old_held_store(git_dir: &Path) -> Option<PathBuf> {
let store = git_dir.join(old::HELD_STORE);
store.is_dir().then_some(store)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MigrateError {
HeldStore { path: PathBuf },
PendingDetached { path: PathBuf },
PendingForDowngrade { path: PathBuf },
PendingUnreadable { path: PathBuf, why: String },
BothCommitted { source: PathBuf, target: PathBuf },
BothExist { source: PathBuf, target: PathBuf },
Locked { path: PathBuf },
Io {
op: &'static str,
path: PathBuf,
why: String,
},
Git { args: String, why: String },
}
impl std::error::Error for MigrateError {}
impl MigrateError {
pub fn staged_nothing(&self) -> bool {
match self {
MigrateError::BothCommitted { .. } => true,
MigrateError::HeldStore { .. }
| MigrateError::PendingDetached { .. }
| MigrateError::PendingForDowngrade { .. }
| MigrateError::PendingUnreadable { .. }
| MigrateError::BothExist { .. }
| MigrateError::Locked { .. }
| MigrateError::Io { .. }
| MigrateError::Git { .. } => false,
}
}
}
impl std::fmt::Display for MigrateError {
fn fmt(&self, out: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MigrateError::HeldStore { path } => write!(
out,
"{} holds unstaged changes a check parked; put them back with `{} restore` \
in that worktree, then run the command again",
path.display(),
n::BIN
),
MigrateError::PendingDetached { path } => write!(
out,
"{} lists a detached cluster run greengate 3.x submitted,\n \
which 4.0 cannot collect. Once `kubectl get workflows -n {}`\n \
no longer shows it (or its verdict no longer matters),\n \
remove the file:\n rm {}\n then run the command again",
path.display(),
old::CLUSTER_NAMESPACE,
path.display()
),
MigrateError::PendingForDowngrade { path } => write!(
out,
"{} lists a detached cluster run greengate 4.0 submitted,\n \
which 3.x cannot collect. Collect it first (`{} cluster collect`),\n \
or, if its verdict no longer matters, remove the file:\n rm {}\n \
then run the command again",
path.display(),
n::BIN,
path.display()
),
MigrateError::PendingUnreadable { path, why } => write!(
out,
"{} (detached cluster runs)\n cannot be read: \
{why}.\n If no run in it matters any more, remove it:\n rm {}\n \
then run the command again",
path.display(),
path.display()
),
MigrateError::BothExist { source, target } => write!(
out,
"both {} and {} exist; remove the one you do not need (`rm -r {}` or \
`rm -r {}`), then run `{} migrate`",
source.display(),
target.display(),
source.display(),
target.display(),
n::BIN
),
MigrateError::BothCommitted { source, target } => write!(
out,
"both {} and {} are here; keep the one that is meant, remove the \
other (`git rm` it) and commit, then run `{} migrate --tracked` again",
source.display(),
target.display(),
n::BIN
),
MigrateError::Locked { path } => write!(
out,
"another {} is migrating this repository ({} is held); run the command again",
n::BIN,
path.display()
),
MigrateError::Io { op, path, why } => {
write!(out, "could not {op} {}: {why}", path.display())
}
MigrateError::Git { args, why } => write!(out, "git {args} failed: {why}"),
}
}
}
fn io<'a>(op: &'static str, path: &'a Path) -> impl FnOnce(std::io::Error) -> MigrateError + 'a {
move |e| MigrateError::Io {
op,
path: path.to_path_buf(),
why: e.to_string(),
}
}
const LOCK_DEADLINE: std::time::Duration = std::time::Duration::from_secs(10);
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Moved {
pub state: usize,
pub keys: usize,
}
pub fn ensure(cwd: &Path, git: &GitFn<'_>, now: u64) -> Result<Moved, MigrateError> {
let mut moved = ensure_global(git)?;
let here = ensure_repo(cwd, git, now)?;
moved.state += here.state;
moved.keys += here.keys;
Ok(moved)
}
pub fn ensure_repo(cwd: &Path, git: &GitFn<'_>, now: u64) -> Result<Moved, MigrateError> {
let Some(repo) = Repo::locate(cwd) else {
return Ok(Moved::default());
};
if repo.marker().exists() || repo.common.join(n::REVERSED_MARKER).exists() {
return Ok(Moved::default());
}
migrate_repo(&repo, Direction::Forward, git, now)
}
pub fn migrate_repo(
repo: &Repo,
direction: Direction,
git: &GitFn<'_>,
now: u64,
) -> Result<Moved, MigrateError> {
let _lock = lock(&repo.common.join(n::MIGRATE_LOCK))?;
let reversed = repo.common.join(n::REVERSED_MARKER);
if direction == Direction::Forward && (repo.marker().exists() || reversed.exists()) {
return Ok(Moved::default());
}
let dirs = repo.git_dirs();
for dir in &dirs {
let held = match direction {
Direction::Forward => old_held_store(dir),
Direction::Reverse => Some(dir.join(n::HELD_STORE)).filter(|p| p.is_dir()),
};
if let Some(path) = held {
return Err(MigrateError::HeldStore { path });
}
}
let state_dir = match direction {
Direction::Forward => old::STATE_DIR,
Direction::Reverse => n::STATE_DIR,
};
let pending = repo
.common
.join(state_dir)
.join(crate::cluster::PENDING_FILE);
match crate::cluster::lists_live(&pending, now) {
Ok(false) => {}
Ok(true) if direction == Direction::Forward => {
return Err(MigrateError::PendingDetached { path: pending })
}
Ok(true) => return Err(MigrateError::PendingForDowngrade { path: pending }),
Err(why) => return Err(MigrateError::PendingUnreadable { path: pending, why }),
}
if direction == Direction::Reverse {
std::fs::write(&reversed, format!("{}-v1\n", n::REVERSED_MARKER))
.map_err(io("write", &reversed))?;
remove_if_present(&repo.marker())?;
}
let mut moved = Moved::default();
for dir in &dirs {
moved.state += move_state(dir, direction)?;
}
if direction == Direction::Forward {
drop_old_audit_ref(repo, git)?;
}
let local_files = [
repo.common.join("config"),
repo.common.join("config.worktree"),
];
match may_hold(&local_files, direction) {
Some(true) => moved.keys += move_keys(&Scope::Local(repo.common.clone()), direction, git)?,
Some(false) => {}
None if direction == Direction::Forward => return Ok(moved),
None => {
return Err(MigrateError::Io {
op: "read",
path: repo.common.join("config"),
why: "the repository's config cannot be read".into(),
})
}
}
if direction == Direction::Forward {
let text = format!("{}-v1\n{}\n", n::MIGRATED_MARKER, env!("CARGO_PKG_VERSION"));
std::fs::write(repo.marker(), text).map_err(io("write", &repo.marker()))?;
remove_if_present(&reversed)?;
}
Ok(moved)
}
fn drop_old_audit_ref(repo: &Repo, git: &GitFn<'_>) -> Result<(), MigrateError> {
let name = format!("refs/notes/{}", old::AUDIT_REF);
let present = repo.common.join(&name).exists()
|| std::fs::read_to_string(repo.common.join("packed-refs"))
.is_ok_and(|t| t.lines().any(|l| l.ends_with(&format!(" {name}"))));
if present {
run_git(git, Some(&repo.common), &["update-ref", "-d", &name])?;
eprintln!("removed the old audit cache; the next tag push audits once");
}
Ok(())
}
fn remove_if_present(path: &Path) -> Result<(), MigrateError> {
match std::fs::remove_file(path) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(io("remove", path)(e)),
_ => Ok(()),
}
}
fn lock(path: &Path) -> Result<std::fs::File, MigrateError> {
if let Some(file) = greenharness_slots::lock_within(path, std::time::Duration::ZERO)
.map_err(io("lock", path))?
{
return Ok(file);
}
eprintln!("{}: waiting for {}", n::BIN, path.display());
greenharness_slots::lock_within(path, LOCK_DEADLINE)
.map_err(io("lock", path))?
.ok_or_else(|| MigrateError::Locked {
path: path.to_path_buf(),
})
}
pub fn move_state(dir: &Path, direction: Direction) -> Result<usize, MigrateError> {
let mut moved = 0;
for m in MOVES {
let (from, to, header) = match direction {
Direction::Forward => (m.old, m.new, m.header),
Direction::Reverse => (m.new, m.old, m.header.map(|(o, n)| (n, o))),
};
let source = dir.join(from);
let target = dir.join(to);
if !source.exists() {
continue;
}
moved += 1;
if source.is_dir() && target.is_dir() {
merge_dirs(&source, &target)?;
continue;
}
if target.exists() {
return Err(MigrateError::BothExist { source, target });
}
if let Some((old_header, new_header)) = header {
rewrite_header(&source, old_header, new_header)?;
}
std::fs::rename(&source, &target).map_err(io("move", &source))?;
}
Ok(moved)
}
fn merge_dirs(source: &Path, target: &Path) -> Result<(), MigrateError> {
let read = |dir: &Path| -> Result<Vec<PathBuf>, MigrateError> {
let mut out: Vec<PathBuf> = std::fs::read_dir(dir)
.map_err(io("read", dir))?
.filter_map(Result::ok)
.map(|e| e.path())
.collect();
out.sort();
Ok(out)
};
let children = read(source)?;
for child in &children {
let there = target.join(child.file_name().unwrap_or_default());
let log = child.extension().is_some_and(|e| e == "log");
let both_dirs = child.is_dir() && there.is_dir();
if there.exists() && !log && !both_dirs {
return Err(MigrateError::BothExist {
source: child.clone(),
target: there,
});
}
}
for child in children {
let there = target.join(child.file_name().unwrap_or_default());
if child.is_dir() && there.is_dir() {
merge_dirs(&child, &there)?;
} else if there.exists() {
let mut joined = std::fs::read(&child).map_err(io("read", &child))?;
joined.extend(std::fs::read(&there).map_err(io("read", &there))?);
let staged = there.with_extension("log.greengate-merge");
std::fs::write(&staged, joined).map_err(io("write", &staged))?;
std::fs::rename(&staged, &there).map_err(io("replace", &there))?;
std::fs::remove_file(&child).map_err(io("remove", &child))?;
} else {
std::fs::rename(&child, &there).map_err(io("move", &child))?;
}
}
std::fs::remove_dir(source).map_err(io("remove", source))
}
fn rewrite_header(file: &Path, old: &str, new: &str) -> Result<(), MigrateError> {
if !file.is_file() {
return Ok(());
}
let text = std::fs::read(file).map_err(io("read", file))?;
let Ok(text) = String::from_utf8(text) else {
return Ok(());
};
if !text.lines().any(|l| l == old) {
return Ok(());
}
let mut out = String::with_capacity(text.len() + 8);
for line in text.split_inclusive('\n') {
let (body, end) = match line.strip_suffix('\n') {
Some(body) => (body, "\n"),
None => (line, ""),
};
out.push_str(if body == old { new } else { body });
out.push_str(end);
}
let staged = file.with_extension("greengate-migrate.tmp");
std::fs::write(&staged, out).map_err(io("write", &staged))?;
std::fs::rename(&staged, file).map_err(io("replace", file))
}
#[derive(Debug, Clone)]
pub enum Scope {
Local(PathBuf),
Global,
}
pub type GitFn<'a> = dyn Fn(Option<&Path>, &[&str]) -> Result<Option<String>, String> + 'a;
fn run_git(
git: &GitFn<'_>,
dir: Option<&Path>,
args: &[&str],
) -> Result<Option<String>, MigrateError> {
git(dir, args).map_err(|why| MigrateError::Git {
args: args.join(" "),
why,
})
}
fn values(raw: &str) -> Vec<String> {
match raw.strip_suffix('\0') {
Some(body) => body.split('\0').map(str::to_string).collect(),
None => Vec::new(),
}
}
pub fn move_keys(
scope: &Scope,
direction: Direction,
git: &GitFn<'_>,
) -> Result<usize, MigrateError> {
let (from, to) = match direction {
Direction::Forward => (old::CONFIG_PREFIX, n::CONFIG_PREFIX),
Direction::Reverse => (n::CONFIG_PREFIX, old::CONFIG_PREFIX),
};
let (dir, flag) = match scope {
Scope::Local(common) => (Some(common.as_path()), "--local"),
Scope::Global => (None, "--global"),
};
let pattern = format!("^{}", from.replace('.', r"\."));
let listed = run_git(
git,
dir,
&["config", flag, "--name-only", "--get-regexp", &pattern],
)?
.unwrap_or_default();
let keys: std::collections::BTreeSet<&str> = listed
.lines()
.filter(|k| !k.is_empty() && !k.starts_with(old::FOREIGN_PREFIX))
.collect();
let mut moved = 0;
for key in keys {
let rest = &key[from.len()..];
let target = format!("{to}{rest}");
let old_values = values(
&run_git(git, dir, &["config", flag, "-z", "--get-all", key])?.unwrap_or_default(),
);
let new_values = values(
&run_git(git, dir, &["config", flag, "-z", "--get-all", &target])?.unwrap_or_default(),
);
let owned_by_person = new_values.iter().any(|v| !old_values.contains(v));
if !owned_by_person {
for value in &old_values {
let dropped = direction == Direction::Forward
&& rest.eq_ignore_ascii_case("cluster.namespace")
&& value == old::CLUSTER_NAMESPACE;
if dropped || new_values.contains(value) {
continue;
}
run_git(git, dir, &["config", flag, "--add", &target, value])?;
}
}
run_git(git, dir, &["config", flag, "--unset-all", key])?;
moved += 1;
}
Ok(moved)
}
fn ensure_global(git: &GitFn<'_>) -> Result<Moved, MigrateError> {
let Some(dir) = config_dir() else {
return Ok(Moved::default());
};
let marker = dir.join(n::MIGRATED_MARKER);
if marker.exists() || dir.join(n::REVERSED_MARKER).exists() {
return Ok(Moved::default());
}
let Some(holds) = may_hold(&global_config_files(), Direction::Forward) else {
return Ok(Moved::default());
};
if std::fs::create_dir_all(&dir).is_err() && !holds {
return Ok(Moved::default());
}
std::fs::create_dir_all(&dir).map_err(io("make", &dir))?;
let _lock = lock(&dir.join(n::MIGRATE_LOCK))?;
if marker.exists() || dir.join(n::REVERSED_MARKER).exists() {
return Ok(Moved::default());
}
let mut moved = Moved::default();
if holds {
let mut attempts = 0;
moved.keys = loop {
match move_keys(&Scope::Global, Direction::Forward, git) {
Err(MigrateError::Git { why, .. }) if why.contains(".lock") && attempts < 50 => {
attempts += 1;
std::thread::sleep(std::time::Duration::from_millis(100));
}
done => break done?,
}
};
}
std::fs::write(&marker, format!("{}-v1\n", n::MIGRATED_MARKER))
.map_err(io("write", &marker))?;
Ok(moved)
}
pub fn forget_global() -> Result<(), MigrateError> {
match config_dir() {
Some(dir) => {
remove_if_present(&dir.join(n::MIGRATED_MARKER))?;
remove_if_present(&dir.join(n::REVERSED_MARKER))
}
None => Ok(()),
}
}
fn may_hold(files: &[PathBuf], direction: Direction) -> Option<bool> {
let section = match direction {
Direction::Forward => "[amont",
Direction::Reverse => "[greengate",
};
let mut holds = false;
for file in files {
let found = match std::fs::read(file) {
Ok(bytes) => String::from_utf8_lossy(&bytes)
.to_ascii_lowercase()
.lines()
.any(|line| {
let line = line.trim_start();
line.starts_with(section)
&& matches!(
line[section.len()..].chars().next(),
Some(']' | ' ' | '"' | '.')
)
}),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => false,
Err(_) => return None,
};
holds |= found;
}
Some(holds)
}
fn global_config_files() -> Vec<PathBuf> {
if let Some(f) = std::env::var_os("GIT_CONFIG_GLOBAL").filter(|f| !f.is_empty()) {
return vec![PathBuf::from(f)];
}
let home = std::env::var_os("HOME").map(PathBuf::from);
let xdg = std::env::var_os("XDG_CONFIG_HOME")
.filter(|d| !d.is_empty())
.map(PathBuf::from)
.or_else(|| home.as_ref().map(|h| h.join(".config")));
let mut files = Vec::new();
if let Some(x) = xdg {
files.push(x.join("git").join("config"));
}
if let Some(h) = home {
files.push(h.join(".gitconfig"));
}
files
}
pub fn reverse_global(git: &GitFn<'_>) -> Result<Moved, MigrateError> {
let mut _lock = None;
if let Some(dir) = config_dir() {
std::fs::create_dir_all(&dir).map_err(io("make", &dir))?;
_lock = Some(lock(&dir.join(n::MIGRATE_LOCK))?);
let reversed = dir.join(n::REVERSED_MARKER);
std::fs::write(&reversed, format!("{}-v1\n", n::REVERSED_MARKER))
.map_err(io("write", &reversed))?;
remove_if_present(&dir.join(n::MIGRATED_MARKER))?;
}
let keys = move_keys(&Scope::Global, Direction::Reverse, git)?;
Ok(Moved { state: 0, keys })
}
pub fn config_dir() -> Option<PathBuf> {
if let Some(x) = std::env::var_os("XDG_CONFIG_HOME").filter(|d| !d.is_empty()) {
return Some(PathBuf::from(x).join(n::STATE_DIR));
}
let home = std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE"))?;
Some(PathBuf::from(home).join(".config").join(n::STATE_DIR))
}
pub fn leftovers(cwd: &Path, git: &GitFn<'_>) -> Result<Vec<String>, MigrateError> {
let mut out = Vec::new();
let mut env: Vec<String> = std::env::vars_os()
.filter_map(|(k, _)| k.into_string().ok())
.filter(|k| k.starts_with(old::ENV_PREFIX) && !k.starts_with(old::FOREIGN_ENV_PREFIX))
.collect();
env.sort();
for k in env {
let rest = &k[old::ENV_PREFIX.len()..];
let new = match rest {
"HOST_SLOT" => greenharness_slots::HELD_ENV.to_string(),
"SLOT_DIR" => greenharness_slots::DIR_ENV.to_string(),
_ => format!("{}_{rest}", n::ENV_PREFIX),
};
out.push(format!(
"environment: {k} is ignored by greengate 4.0; set {new} instead"
));
}
let pattern = r"^amont\.";
let listed = |dir: Option<&Path>, flag: &str| -> Result<Vec<String>, MigrateError> {
let names = run_git(
git,
dir,
&["config", flag, "--name-only", "--get-regexp", pattern],
)?
.unwrap_or_default();
Ok(names
.lines()
.filter(|k| !k.starts_with(old::FOREIGN_PREFIX))
.collect::<std::collections::BTreeSet<_>>()
.into_iter()
.map(str::to_string)
.collect())
};
if config_dir().is_some_and(|dir| dir.join(n::REVERSED_MARKER).exists()) {
out.push(format!(
"reversed: the global keys were moved back to greengate 3.x's names; \
greengate 4.0 leaves them until `{} migrate --global`",
n::BIN
));
}
for key in listed(None, "--global")? {
out.push(format!(
"global config: {key} is ignored by greengate 4.0; `{} migrate --global` moves it",
n::BIN
));
}
let kubeconfig = run_git(
git,
None,
&[
"config",
"--global",
"--get",
"greengate.cluster.kubeconfig",
],
)?
.unwrap_or_default();
if old_kubeconfig(kubeconfig.trim()) {
out.push(format!(
"cluster: greengate.cluster.kubeconfig is greengate 3.x's ({}), which logs in \
as the old namespace's runner; run homelab's \
scripts/greengate-gates-kubeconfig.sh, then \
`git config --global greengate.cluster.kubeconfig ~/.config/{}/kube/homelab.yaml`",
kubeconfig.trim(),
n::STATE_DIR
));
}
let located = Repo::locate(cwd);
let configured = run_git(
git,
located.as_ref().map(|r| r.common.as_path()),
&["config", "--get", crate::attest::KEY_CONFIG],
)?
.is_some_and(|k| !k.trim().is_empty());
if !configured {
if let Some(line) = std::env::var_os("HOME")
.or_else(|| std::env::var_os("USERPROFILE"))
.and_then(|home| crate::attest::key_move_hint_in(Path::new(&home)))
{
out.push(format!("signing key: {line}"));
}
}
if let Some(repo) = located {
let old_refs = run_git(
git,
Some(&repo.common),
&[
"for-each-ref",
"--format=%(refname)",
"refs/notes/attest-local/amont-*",
],
)?
.unwrap_or_default();
for name in old_refs.lines().filter(|l| !l.trim().is_empty()) {
out.push(format!(
"local notes: {name} was left by an older greengate and is no longer read; `git update-ref -d {name}` removes it"
));
}
if repo.common.join(n::REVERSED_MARKER).exists() {
out.push(format!(
"reversed: this repository was moved back to greengate 3.x's names; \
greengate 4.0 leaves it until `{} migrate`",
n::BIN
));
}
for key in listed(Some(&repo.common), "--local")? {
out.push(format!(
"local config: {key} is ignored by greengate 4.0; `{} migrate` moves it",
n::BIN
));
}
for dir in repo.git_dirs() {
for m in MOVES {
if dir.join(m.old).exists() {
out.push(format!(
"state: {} was left by greengate 3.x; `{} migrate` moves it",
dir.join(m.old).display(),
n::BIN
));
}
}
if let Some(store) = old_held_store(&dir) {
out.push(format!(
"state: {} holds parked changes; `{} restore` puts them back",
store.display(),
n::BIN
));
}
}
}
Ok(out)
}
fn old_kubeconfig(path: &str) -> bool {
let folder = format!("/.config/{}/", old::STATE_DIR);
path.replace('\\', "/").contains(&folder)
}
pub fn system_git(dir: Option<&Path>, args: &[&str]) -> Result<Option<String>, String> {
let mut cmd = std::process::Command::new("git");
if let Some(dir) = dir {
cmd.env("GIT_DIR", dir);
}
let out = cmd
.args(args)
.stdin(std::process::Stdio::null())
.output()
.map_err(|e| e.to_string())?;
match out.status.code() {
Some(0) => Ok(Some(String::from_utf8_lossy(&out.stdout).into_owned())),
Some(1) => Ok(None),
code => Err(format!(
"exit {}: {}",
code.map_or("signal".to_string(), |c| c.to_string()),
String::from_utf8_lossy(&out.stderr).trim()
)),
}
}
#[cfg(test)]
mod tests {
use super::*;
const NOW: u64 = 1_800_000_000;
fn scratch(name: &str) -> PathBuf {
let scratch_dir =
std::env::temp_dir().join(format!("greengate-migrate-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&scratch_dir); std::fs::create_dir_all(&scratch_dir).unwrap();
scratch_dir
}
fn git_in(
repo: &Path,
) -> impl Fn(Option<&Path>, &[&str]) -> Result<Option<String>, String> + '_ {
move |_dir, args| {
let out = std::process::Command::new("git")
.current_dir(repo)
.env("GIT_CONFIG_GLOBAL", repo.join("global-config"))
.env("GIT_CONFIG_NOSYSTEM", "1")
.args(args)
.output()
.map_err(|e| e.to_string())?;
match out.status.code() {
Some(0) => Ok(Some(String::from_utf8_lossy(&out.stdout).into_owned())),
Some(1) => Ok(None),
code => Err(format!("{code:?}")),
}
}
}
fn get(git: &GitFn<'_>, key: &str) -> String {
git(None, &["config", "--get-all", key])
.unwrap()
.unwrap_or_default()
}
fn init(dir: &Path) {
let ok = std::process::Command::new("git")
.args(["init", "-q"])
.arg(dir)
.status()
.unwrap()
.success();
assert!(ok);
}
#[test]
fn three_x_state_moves_with_its_headers_and_a_second_run_is_a_no_op() {
let root = scratch("state");
init(&root);
let gd = root.join(".git");
std::fs::write(gd.join("amont-bypasses"), "amont-bypass-v1\n1 abc gate\n").unwrap();
std::fs::write(
gd.join("amont-waves"),
"amont-waves-v1\n1 2\namont-waves-v1\n3 4\n",
)
.unwrap();
std::fs::create_dir_all(gd.join("amont/logs")).unwrap();
std::fs::write(gd.join("amont/logs/x.log"), "log").unwrap();
std::fs::create_dir_all(gd.join("amont-cache/cargo-test")).unwrap();
let repo = Repo::locate_with(&root, None).unwrap();
let git = git_in(&root);
migrate_repo(&repo, Direction::Forward, &git, NOW).unwrap();
assert_eq!(
std::fs::read_to_string(gd.join(n::BYPASS_LEDGER)).unwrap(),
"greengate-bypass-v1\n1 abc gate\n"
);
assert_eq!(
std::fs::read_to_string(gd.join(n::WAVES_LEDGER)).unwrap(),
"greengate-waves-v1\n1 2\ngreengate-waves-v1\n3 4\n"
);
assert!(gd.join("greengate/logs/x.log").is_file());
assert!(gd.join(n::TREE_CACHE).join("cargo-test").is_dir());
for old in ["amont-bypasses", "amont-waves", "amont", "amont-cache"] {
assert!(!gd.join(old).exists(), "{old} left behind");
}
assert!(repo.marker().is_file());
std::fs::remove_file(repo.marker()).unwrap();
migrate_repo(&repo, Direction::Forward, &git, NOW).unwrap();
assert!(gd.join(n::BYPASS_LEDGER).is_file());
}
#[test]
fn an_interrupted_move_resumes_and_both_names_are_refused() {
let root = scratch("resume");
init(&root);
let gd = root.join(".git");
std::fs::write(
gd.join("amont-downgrades"),
"greengate-downgrade-v1\n1 a b c\n",
)
.unwrap();
move_state(&gd, Direction::Forward).unwrap();
assert_eq!(
std::fs::read_to_string(gd.join(n::DOWNGRADE_LEDGER)).unwrap(),
"greengate-downgrade-v1\n1 a b c\n"
);
std::fs::write(gd.join("amont-downgrades"), "amont-downgrade-v1\n").unwrap();
let err = move_state(&gd, Direction::Forward).unwrap_err();
assert!(matches!(err, MigrateError::BothExist { .. }), "{err}");
assert!(gd.join("amont-downgrades").is_file());
}
#[test]
fn linked_worktrees_are_walked_and_a_held_store_refuses() {
let root = scratch("worktrees");
init(&root);
let wt = root.join(".git/worktrees/side");
std::fs::create_dir_all(&wt).unwrap();
std::fs::write(wt.join("amont-skew"), "amont-skew-v1\n3.0.0\n").unwrap();
let repo = Repo::locate_with(&root, None).unwrap();
assert_eq!(repo.worktrees(), vec![wt.clone()]);
std::fs::create_dir_all(wt.join("amont-held")).unwrap();
let git = git_in(&root);
let err = migrate_repo(&repo, Direction::Forward, &git, NOW).unwrap_err();
assert_eq!(
err,
MigrateError::HeldStore {
path: wt.join("amont-held")
}
);
assert!(err.to_string().contains("restore"), "{err}");
assert!(
wt.join("amont-skew").is_file(),
"nothing moved while refused"
);
assert!(!repo.marker().exists());
std::fs::remove_dir(wt.join("amont-held")).unwrap();
migrate_repo(&repo, Direction::Forward, &git, NOW).unwrap();
assert_eq!(
std::fs::read_to_string(wt.join(n::SKEW_MARKER)).unwrap(),
"greengate-skew-v1\n3.0.0\n"
);
}
#[test]
fn the_old_audit_cache_ref_is_deleted_and_an_absent_one_is_no_error() {
let root = scratch("auditref");
init(&root);
let git = git_in(&root);
let repo = Repo::locate_with(&root, None).unwrap();
migrate_repo(&repo, Direction::Forward, &git, NOW).unwrap();
for args in [
vec![
"-c",
"user.name=t",
"-c",
"user.email=t@t",
"commit",
"--allow-empty",
"-qm",
"x",
],
vec![
"-c",
"user.name=t",
"-c",
"user.email=t@t",
"notes",
"--ref",
"amont-audit",
"add",
"-m",
"n",
"HEAD",
],
vec![
"-c",
"user.name=t",
"-c",
"user.email=t@t",
"notes",
"--ref",
"greengate-audit",
"add",
"-m",
"n",
"HEAD",
],
] {
git(None, &args).unwrap();
}
std::fs::remove_file(repo.marker()).unwrap();
let refs = || {
git(None, &["for-each-ref", "refs/notes"])
.unwrap()
.unwrap_or_default()
};
assert!(refs().contains("refs/notes/amont-audit"), "{}", refs());
migrate_repo(&repo, Direction::Forward, &git, NOW).unwrap();
assert!(!refs().contains("amont-audit"), "{}", refs());
assert!(
refs().contains("refs/notes/greengate-audit"),
"the new one stays"
);
git(
None,
&[
"-c",
"user.name=t",
"-c",
"user.email=t@t",
"notes",
"--ref",
"amont-audit",
"add",
"-fm",
"n",
"HEAD",
],
)
.unwrap();
git(None, &["pack-refs", "--all"]).unwrap();
std::fs::remove_file(repo.marker()).unwrap();
migrate_repo(&repo, Direction::Forward, &git, NOW).unwrap();
assert!(!refs().contains("amont-audit"), "{}", refs());
}
#[test]
fn local_keys_move_with_every_value_and_the_new_name_wins() {
let root = scratch("keys");
init(&root);
let git = git_in(&root);
for args in [
["config", "--add", "amont.trusted", "aaa"],
["config", "--add", "amont.timeout", "600"],
["config", "--add", "amont.trusted", "bbb"],
["config", "--add", "greengate.timeout", "900"],
["config", "--add", "amont.cluster.namespace", "amont-gates"],
["config", "--add", "amont.agent.suppressions", "allow"],
] {
git(None, &args).unwrap();
}
let repo = Repo::locate_with(&root, None).unwrap();
let moved = migrate_repo(&repo, Direction::Forward, &git, NOW).unwrap();
assert_eq!(moved.keys, 3, "trusted, timeout, cluster.namespace");
assert_eq!(get(&git, "greengate.trusted"), "aaa\nbbb\n");
assert_eq!(get(&git, "greengate.timeout"), "900\n", "the new name wins");
assert_eq!(get(&git, "greengate.cluster.namespace"), "");
assert_eq!(get(&git, "amont.trusted"), "");
assert_eq!(get(&git, "amont.timeout"), "");
assert_eq!(get(&git, "amont.agent.suppressions"), "allow\n");
migrate_repo(&repo, Direction::Reverse, &git, NOW).unwrap();
assert_eq!(get(&git, "amont.trusted"), "aaa\nbbb\n");
assert_eq!(get(&git, "greengate.trusted"), "");
assert!(!repo.marker().exists());
assert!(repo.common.join(n::REVERSED_MARKER).is_file());
ensure_repo(&root, &git, NOW).unwrap();
assert_eq!(
get(&git, "amont.trusted"),
"aaa\nbbb\n",
"a reverse is not undone"
);
}
#[test]
fn a_copy_cut_off_between_two_values_resumes_without_losing_one() {
let root = scratch("partial");
init(&root);
let git = git_in(&root);
for args in [
["config", "--add", "amont.trusted", "aaa"],
["config", "--add", "amont.trusted", "bbb"],
["config", "--add", "amont.trusted", "ccc"],
["config", "--add", "greengate.trusted", "aaa"],
] {
git(None, &args).unwrap();
}
move_keys(&Scope::Local(root.join(".git")), Direction::Forward, &git).unwrap();
assert_eq!(get(&git, "greengate.trusted"), "aaa\nbbb\nccc\n");
assert_eq!(get(&git, "amont.trusted"), "");
}
#[test]
fn a_git_that_fails_stops_the_move_and_keeps_the_old_key() {
let root = scratch("git-fails");
init(&root);
let real = git_in(&root);
real(None, &["config", "--add", "amont.trusted", "aaa"]).unwrap();
let failing = |dir: Option<&Path>, args: &[&str]| -> Result<Option<String>, String> {
if args.contains(&"--add") {
return Err("exit 255: could not lock config file".into());
}
real(dir, args)
};
let err = move_keys(
&Scope::Local(root.join(".git")),
Direction::Forward,
&failing,
)
.unwrap_err();
assert!(matches!(err, MigrateError::Git { .. }), "{err}");
assert_eq!(get(&real, "amont.trusted"), "aaa\n", "the old key stays");
}
#[test]
fn a_3x_pending_file_this_version_cannot_read_refuses_the_move() {
let root = scratch("pending");
init(&root);
let gd = root.join(".git");
std::fs::create_dir_all(gd.join("amont")).unwrap();
let pending = gd.join("amont/cluster-pending");
std::fs::write(&pending, "{\"format\":1,\"records\":[{\"name\":\"x\"}]}\n").unwrap();
let repo = Repo::locate_with(&root, None).unwrap();
let git = git_in(&root);
let err = migrate_repo(&repo, Direction::Forward, &git, NOW).unwrap_err();
assert!(
matches!(err, MigrateError::PendingUnreadable { .. }),
"{err}"
);
assert!(err.to_string().contains("rm "), "{err}");
std::fs::write(&pending, "{\"format\":1,\"records\":[]}\n").unwrap();
migrate_repo(&repo, Direction::Forward, &git, NOW).unwrap();
assert!(gd.join("greengate/cluster-pending").is_file());
}
#[test]
fn a_live_3x_run_refuses_the_move_and_an_expired_one_lets_it_through() {
let root = scratch("pending-live");
init(&root);
let gd = root.join(".git");
std::fs::create_dir_all(gd.join("amont")).unwrap();
let pending = gd.join("amont/cluster-pending");
std::fs::write(&pending, crate::cluster::sample_pending_file(NOW)).unwrap();
let repo = Repo::locate_with(&root, None).unwrap();
let git = git_in(&root);
assert_eq!(
migrate_repo(&repo, Direction::Forward, &git, NOW).unwrap_err(),
MigrateError::PendingDetached {
path: pending.clone()
}
);
migrate_repo(&repo, Direction::Forward, &git, NOW + 7 * 86_400).unwrap();
assert!(gd.join("greengate/cluster-pending").is_file());
}
#[test]
fn a_live_4_0_run_refuses_a_reverse() {
let root = scratch("pending-reverse");
init(&root);
let gd = root.join(".git");
std::fs::create_dir_all(gd.join(n::STATE_DIR)).unwrap();
let pending = gd.join(n::STATE_DIR).join(crate::cluster::PENDING_FILE);
std::fs::write(&pending, crate::cluster::sample_pending_file(NOW)).unwrap();
let repo = Repo::locate_with(&root, None).unwrap();
let git = git_in(&root);
let err = migrate_repo(&repo, Direction::Reverse, &git, NOW).unwrap_err();
assert_eq!(
err,
MigrateError::PendingForDowngrade {
path: pending.clone()
}
);
assert!(err.to_string().contains("cluster collect"), "{err}");
assert!(pending.is_file() && !gd.join(n::REVERSED_MARKER).exists());
migrate_repo(&repo, Direction::Reverse, &git, NOW + 7 * 86_400).unwrap();
assert!(gd
.join(old::STATE_DIR)
.join(crate::cluster::PENDING_FILE)
.is_file());
}
#[test]
fn a_first_run_stands_down_while_a_reverse_is_marked() {
let root = scratch("stand-down");
init(&root);
let gd = root.join(".git");
std::fs::write(gd.join("amont-bypasses"), "amont-bypass-v1\n").unwrap();
let repo = Repo::locate_with(&root, None).unwrap();
std::fs::write(repo.common.join(n::REVERSED_MARKER), "").unwrap();
let git = git_in(&root);
migrate_repo(&repo, Direction::Forward, &git, NOW).unwrap();
assert!(gd.join("amont-bypasses").is_file(), "the reverse holds");
}
#[test]
fn a_reverse_that_stops_half_way_is_moved_forward_again() {
let root = scratch("half-reverse");
init(&root);
let gd = root.join(".git");
let git = git_in(&root);
std::fs::write(gd.join("amont-bypasses"), "amont-bypass-v1\n").unwrap();
git(None, &["config", "amont.timeout", "700"]).unwrap();
let repo = Repo::locate_with(&root, None).unwrap();
migrate_repo(&repo, Direction::Forward, &git, NOW).unwrap();
let failing = |dir: Option<&Path>, args: &[&str]| -> Result<Option<String>, String> {
if args.contains(&"--add") {
return Err("exit 255: could not lock config file".into());
}
git(dir, args)
};
assert!(migrate_repo(&repo, Direction::Reverse, &failing, NOW).is_err());
assert!(gd.join("amont-bypasses").is_file(), "the state went back");
assert!(!repo.marker().exists(), "no done-marker over 3.x state");
std::fs::remove_file(repo.common.join(n::REVERSED_MARKER)).unwrap();
migrate_repo(&repo, Direction::Forward, &git, NOW).unwrap();
assert!(gd.join(n::BYPASS_LEDGER).is_file());
assert_eq!(get(&git, "greengate.timeout"), "700\n");
}
#[test]
fn a_reverse_refuses_while_4_0_holds_parked_changes() {
let root = scratch("reverse-held");
init(&root);
let gd = root.join(".git");
std::fs::create_dir_all(gd.join(n::HELD_STORE)).unwrap();
let repo = Repo::locate_with(&root, None).unwrap();
let git = git_in(&root);
assert!(matches!(
migrate_repo(&repo, Direction::Reverse, &git, NOW).unwrap_err(),
MigrateError::HeldStore { .. }
));
}
#[test]
fn only_a_file_naming_the_namespace_may_hold_its_keys() {
let root = scratch("may-hold");
let config_file = root.join("config");
std::fs::write(&config_file, "[core]\n\tbare = false\n[amontx]\n\ta = 1\n").unwrap();
assert_eq!(
may_hold(std::slice::from_ref(&config_file), Direction::Forward),
Some(false)
);
for section in ["[amont]", "[Amont \"severity\"]", " [amont.cluster]"] {
std::fs::write(&config_file, format!("[core]\n{section}\n\tx = 1\n")).unwrap();
assert_eq!(
may_hold(std::slice::from_ref(&config_file), Direction::Forward),
Some(true),
"{section}"
);
}
assert_eq!(
may_hold(std::slice::from_ref(&config_file), Direction::Reverse),
Some(false)
);
assert_eq!(
may_hold(&[root.join("absent")], Direction::Forward),
Some(false)
);
}
#[test]
fn a_state_folder_both_versions_wrote_is_merged() {
let root = scratch("merge");
init(&root);
let gd = root.join(".git");
std::fs::create_dir_all(gd.join("amont/land")).unwrap();
std::fs::create_dir_all(gd.join("greengate/logs")).unwrap();
std::fs::write(gd.join("amont/cluster.log"), "1 old\n").unwrap();
std::fs::write(gd.join("amont/land/r.json"), "{}").unwrap();
std::fs::write(gd.join("greengate/cluster.log"), "2 new\n").unwrap();
move_state(&gd, Direction::Forward).unwrap();
assert!(!gd.join("amont").exists());
assert_eq!(
std::fs::read_to_string(gd.join("greengate/cluster.log")).unwrap(),
"1 old\n2 new\n"
);
assert!(gd.join("greengate/land/r.json").is_file());
assert!(gd.join("greengate/logs").is_dir());
std::fs::create_dir_all(gd.join("amont")).unwrap();
std::fs::write(gd.join("amont/cluster-pending"), "a").unwrap();
std::fs::write(gd.join("greengate/cluster-pending"), "b").unwrap();
assert!(matches!(
move_state(&gd, Direction::Forward).unwrap_err(),
MigrateError::BothExist { .. }
));
assert!(gd.join("amont/cluster-pending").is_file());
}
#[test]
fn the_reverse_puts_state_back_under_3x_names() {
let root = scratch("reverse");
init(&root);
let gd = root.join(".git");
std::fs::write(
gd.join(n::REHEARSAL_STATE),
"greengate-rehearsal-v1\npid=1\n",
)
.unwrap();
move_state(&gd, Direction::Reverse).unwrap();
assert_eq!(
std::fs::read_to_string(gd.join("amont-rehearsal")).unwrap(),
"amont-rehearsal-v1\npid=1\n"
);
}
#[test]
fn a_gitdir_file_is_followed_to_its_common_dir() {
let root = scratch("pointer");
init(&root);
let wt_git = root.join(".git/worktrees/w");
std::fs::create_dir_all(&wt_git).unwrap();
std::fs::write(wt_git.join("commondir"), "../..\n").unwrap();
let checkout = root.join("w");
std::fs::create_dir_all(checkout.join("src")).unwrap();
std::fs::write(
checkout.join(".git"),
format!("gitdir: {}\n", wt_git.display()),
)
.unwrap();
let repo = Repo::locate_with(&checkout.join("src"), None).unwrap();
assert_eq!(
repo.common.canonicalize().unwrap(),
root.join(".git").canonicalize().unwrap()
);
}
}