1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
use std::fs;
use std::io::Write;
mod common;
use common::Environment;
fn create_environment() -> anyhow::Result<(Environment, String)> {
let (e, root) = Environment::scooby_gang_bootstrap()?;
// Create a bare repository at our scratch location.
let scratch = e.scratch_state().display().to_string();
e.git(&["init", "--bare", &scratch])?;
// Set us as update hook.
let update_hook = e.scratch_state().join("hooks").join("update");
let mut update = fs::File::create(&update_hook)?;
writeln!(update, "#!/bin/sh")?;
writeln!(update)?;
writeln!(update,
"{} update-hook --trust-root={} \"$@\"",
Environment::sq_git_path()?.display(),
root)?;
#[cfg(unix)]
{
// Make file executable.
use std::os::unix::fs::PermissionsExt;
let metadata = update.metadata()?;
let mut permissions = metadata.permissions();
permissions.set_mode(0o755);
update.set_permissions(permissions)?;
}
// Add as origin.
e.git(&["remote", "add", "origin", &scratch])?;
Ok((e, root))
}
#[test]
fn update_hook() -> anyhow::Result<()> {
let (e, _root) = create_environment()?;
let p = e.git_state();
// Bookmark.
e.git(&["checkout", "-b", "test-base"])?;
// Willow's code-signing key can change the source code, as she
// has the sign-commit right.
e.git(&["checkout", "-b", "test-willow"])?;
fs::write(p.join("a"), "Aller Anfang ist schwer.")?;
e.git(&["add", "a"])?;
e.git(&[
"commit",
"-m", "First change.",
&format!("-S{}", e.willow.fingerprint),
])?;
e.git(&["push", "origin", "test-willow"])?;
// Reset.
e.git(&["checkout", "test-base"])?;
e.git(&["clean", "-fdx"])?;
// Her release key also has that right, because she needs it in
// order to give it to new users.
e.git(&["checkout", "-b", "test-willow-release"])?;
fs::write(p.join("a"), "Aller Anfang ist schwer. -- Schiller")?;
e.git(&["add", "a"])?;
e.git(&[
"commit",
"-m", "Someone is not quite correct on the internet.",
&format!("-S{}", e.willow_release.fingerprint),
])?;
e.git(&["push", "origin", "test-willow-release"])?;
// Reset.
e.git(&["checkout", "test-base"])?;
e.git(&["clean", "-fdx"])?;
// Buffy's cert was not yet added, so she may not sign commits.
e.git(&["checkout", "-b", "test-buffy"])?;
fs::write(p.join("a"),
"Aller Anfang ist schwer, unless you are super strong!1")?;
e.git(&["add", "a"])?;
e.git(&[
"commit",
"-m", "Well, actually...",
&format!("-S{}", e.buffy.fingerprint),
])?;
if let Ok(output) = e.git(&["push", "origin", "test-buffy"]) {
eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr));
}
assert!(e.git(&["push", "origin", "test-buffy"]).is_err());
Ok(())
}
#[test]
fn rebase() -> anyhow::Result<()> {
let (e, _root) = create_environment()?;
let p = e.git_state();
// Bookmark.
e.git(&["checkout", "-b", "test-base"])?;
// There are two threads of development. Let's start the first
// one.
e.git(&["checkout", "-b", "feature-one"])?;
fs::write(p.join("a"), "Aller Anfang ist schwer.")?;
e.git(&["add", "a"])?;
e.git(&[
"commit",
"-m", "First change of the first feature.",
&format!("-S{}", e.willow.fingerprint),
])?;
e.git(&["push", "origin", "feature-one"])?;
// Reset.
e.git(&["checkout", "test-base"])?;
e.git(&["clean", "-fdx"])?;
// There are two threads of development. Let's start the second
// one.
e.git(&["checkout", "-b", "feature-two"])?;
fs::write(p.join("b"), "And now for something completely different.")?;
e.git(&["add", "b"])?;
e.git(&[
"commit",
"-m", "First change of the second feature.",
&format!("-S{}", e.willow.fingerprint),
])?;
e.git(&["push", "origin", "feature-two"])?;
// Now we rebase feature-two on top of feature-one and push it to
// update the remote feature-two branch.
e.git(&[
"rebase",
"feature-one",
&format!("-S{}", e.willow.fingerprint),
])?;
e.git(&["push", "origin", "--force", "feature-two"])?;
Ok(())
}
|