Stop gitv-restore copying / on a bad snapshot name, and fix CI
Browse filesgitv-restore looked a snapshot up inside $(...), where die only ends the subshell, so a bad name or a missing latest snapshot left restore with an empty source and it ran rsync -a / into the target. Lookups now stop the script when they fail, restore refuses any source that isn't a snapshot, and an empty name is an error rather than help. The test that exposed it named /tmp/x as its target; it now uses its own scratch folder and checks nothing is created. On GitHub's runner that copy ran until cancelled. The web tests also looked for the gitv binary one folder above their own executable, which newer Cargo's build layout breaks; they now search upward. The CI jobs get time limits, so a hang fails in minutes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Author
- Um9i vulgarmessiah@gmail.com
- Date
- 22 Nov 2026, 16:56
- Commit
- 835619c194ad
- Parent
- dd59c34
4 files changed +45−9
| @@ -17,6 +17,8 @@ jobs: | ||
| check: | ||
| name: Format, lint and test | ||
| runs-on: ubuntu-latest | ||
| # The suite takes seconds; a hang should fail fast rather than run for GitHub's six hours. | ||
| timeout-minutes: 20 | ||
| if: github.event_name != 'schedule' | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| @@ -33,6 +35,7 @@ jobs: | ||
| audit: | ||
| name: Dependency advisories | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 10 | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: taiki-e/install-action@v2 | ||
| @@ -119,7 +119,13 @@ fn snapshots_hold_everything_and_restore_whole() { | ||
| assert!(stderr(&out).contains("2 repositories verified"), "{}", stderr(&out)); | ||
| assert_eq!(git(&target.join("repos/ada/project.git"), &["log", "--format=%s", "main"]), "Second\nFirst"); | ||
| assert!(!setup.run(RESTORE, &["latest", target.to_str().unwrap()]).status.success(), "a full target is refused"); | ||
| assert!(!setup.run(RESTORE, &["../../etc", "/tmp/x"]).status.success(), "snapshot names are validated"); | ||
| // A bad name is refused before anything is copied: the target is never even created. | ||
| let elsewhere = setup.host.dir.join("not-restored"); | ||
| for name in ["../../etc", "2026-01-01T000000Z", ""] { | ||
| let out = setup.run(RESTORE, &[name, elsewhere.to_str().unwrap()]); | ||
| assert!(!out.status.success(), "{name:?} was restored"); | ||
| assert!(!elsewhere.exists(), "{name:?} copied something"); | ||
| } | ||
| let out = setup.run(RESTORE, &["check"]); | ||
| assert!(out.status.success(), "{}", stderr(&out)); | ||
| @@ -163,6 +169,16 @@ fn a_backup_waits_for_a_push_in_progress() { | ||
| assert!(setup.backup().status.success()); | ||
| } | ||
| #[test] | ||
| fn a_restore_check_without_snapshots_copies_nothing() { | ||
| let setup = Setup::new("backup-none"); | ||
| fs::create_dir_all(setup.backups.join("gitv/snapshots")).unwrap(); | ||
| let out = setup.run(RESTORE, &["check"]); | ||
| assert!(!out.status.success()); | ||
| assert!(stderr(&out).contains("no such snapshot"), "{}", stderr(&out)); | ||
| assert!(!setup.backups.join("gitv/restore-check").exists(), "nothing was copied"); | ||
| } | ||
| #[test] | ||
| fn an_unmounted_backup_directory_is_refused() { | ||
| let setup = Setup::new("backup-unmounted"); | ||
| @@ -26,6 +26,8 @@ snapshot_dir() { | ||
| # Copy a snapshot to an empty target with owners and modes kept, then fsck every repository. | ||
| restore() { | ||
| local from=$1 to=$2 | ||
| # Never copy anything but a snapshot: an empty or wrong source must not become `rsync /`. | ||
| [[ -n $from && -d $from/repos && -f $from/MANIFEST ]] || die "not a snapshot: '$from'" | ||
| if [[ -e $to ]] && [[ -n $(ls -A "$to") ]]; then die "$to isn't empty"; fi | ||
| mkdir -p "$to" | ||
| rsync -a "$from/" "$to/" | ||
| @@ -42,21 +44,29 @@ restore() { | ||
| say "restored $(basename "$from") to $to: $count repositories verified" | ||
| } | ||
| case ${1:-} in | ||
| if (($# == 0)); then | ||
| sed -n '2,10p' "$0" | ||
| exit 0 | ||
| fi | ||
| case $1 in | ||
| list) | ||
| ls -1 "$base/snapshots" | ||
| ;; | ||
| check) | ||
| # A failed lookup must stop here: `die` inside $(...) only ends the subshell. | ||
| from=$(snapshot_dir latest) || exit 1 | ||
| scratch=$base/restore-check | ||
| rm -rf "$scratch" | ||
| trap 'rm -rf "$scratch"' EXIT | ||
| restore "$(snapshot_dir latest)" "$scratch" | ||
| restore "$from" "$scratch" | ||
| ;; | ||
| "" | -h | --help) | ||
| -h | --help) | ||
| sed -n '2,10p' "$0" | ||
| ;; | ||
| *) | ||
| [[ $# == 2 ]] || die "usage: gitv-restore <snapshot|latest> DIR" | ||
| restore "$(snapshot_dir "$1")" "$2" | ||
| from=$(snapshot_dir "$1") || exit 1 | ||
| restore "$from" "$2" | ||
| ;; | ||
| esac | ||
| @@ -20,11 +20,18 @@ fn binary(own: Option<&str>, name: &str) -> String { | ||
| if let Some(path) = own { | ||
| return path.to_string(); | ||
| } | ||
| // Test executables live in target/<profile>/deps; binaries one level up. | ||
| // The binaries sit in target/<profile>. Test executables are somewhere below it: in | ||
| // target/<profile>/deps, or deeper in newer Cargo's build directory layout. So look in each | ||
| // folder above this test for a file with the binary's name. | ||
| let exe = std::env::current_exe().unwrap(); | ||
| let path = exe.parent().and_then(Path::parent).unwrap().join(name); | ||
| assert!(path.is_file(), "{} isn't built; run the tests from the workspace root with `cargo test`", path.display()); | ||
| path.to_string_lossy().into_owned() | ||
| exe.ancestors() | ||
| .skip(1) | ||
| .map(|dir| dir.join(name)) | ||
| .find(|path| path.is_file()) | ||
| .map(|path| path.to_string_lossy().into_owned()) | ||
| .unwrap_or_else(|| { | ||
| panic!("{name} isn't built near {}; run the tests from the workspace root with `cargo test`", exe.display()) | ||
| }) | ||
| } | ||
| /// The hostname every test installation is configured with. | ||
| pub const TEST_HOST: &str = "git.example.test"; | ||