Commit 0b8fd3b2 authored by Ben Boeckel's avatar Ben Boeckel

third_party: add a test for evil third party merges

parent 4f245af0
Pipeline #28855 failed with stage
......@@ -96,6 +96,9 @@ impl Hook for ThirdParty {
fn check(&self, ctx: &HookGitContext, commit: &Commit, result: &mut HookResult)
-> Result<(), Error> {
for import in &self.imports {
// A flag to indicate that we need to check the commit for an evil merge.
let mut check_tree = false;
for diff in &commit.diffs {
let name_path = Path::new(&diff.name);
......@@ -132,13 +135,53 @@ impl Hook for ThirdParty {
let is_import_res: Result<_, Error> = check_ref(&commit.parents[1]);
let is_import = try!(is_import_res).is_import();
if !is_import {
if is_import {
// Check that the merge commit is not "evil" for the import directory.
check_tree = true;
} else {
try!(check_ref(&commit.sha1)).add_result(result);
}
} else {
try!(check_ref(&commit.sha1)).add_result(result);
}
}
if check_tree {
// Get the tree of the import branch.
let rev_parse = try!(ctx.git()
.arg("rev-parse")
.arg(format!("{}^{{tree}}", commit.parents[1]))
.output());
try!(check_status(rev_parse.status, "rev-parse failed"));
let expected_tree = String::from_utf8_lossy(&rev_parse.stdout);
// Get the tree of the imported tree in the merge commit.
let ls_tree = try!(ctx.git()
.arg("ls-tree")
.arg(&commit.sha1)
.arg(&import.path)
.output());
try!(check_status(ls_tree.status, "ls-tree failed"));
let ls_tree_output = String::from_utf8_lossy(&ls_tree.stdout);
let actual_tree = ls_tree_output.split_whitespace()
.skip(2)
.next()
.unwrap();
// Ensure the trees match.
if actual_tree != expected_tree.trim() {
let msg = format!("commit {} not allowed; the {} directory contains changes \
not on the import branch; merge conflicts should not \
happen and indicate that the import directory was \
manually edited at some point. Please find and revert the \
bad edit, apply it to the imported repository (if \
necessary), and then run the import script.",
commit.sha1_short,
import.path);
result.add_error(msg);
}
}
}
Ok(())
......@@ -156,6 +199,7 @@ mod tests {
static BASE_COMMIT: &'static str = "26576e49345a141eca310af92737e489c9baac24";
static VALID_UPDATE_TOPIC: &'static str = "0bd161c8187d4f727a7acc17020711dcc139b166";
static INVALID_UPDATE_TOPIC: &'static str = "af154fdff05c871125f2db03eccbdde8571d484e";
static EVIL_UPDATE_TOPIC: &'static str = "add18e5ab9a67303337cb2754c675fb2e0a45a79";
fn make_third_party_hook() -> ThirdParty {
ThirdParty::new(vec![
......@@ -209,4 +253,30 @@ mod tests {
assert_eq!(result.allowed(), false);
assert_eq!(result.pass(), false);
}
#[test]
fn test_third_party_invalid_update_evil() {
let hook = make_third_party_hook();
let submodules = SubmoduleMap::new();
let mut conf = GitHookConfiguration::new(None, &submodules);
conf.add_hook(&hook);
let result = test_hook_base("test_third_party_invalid_update_evil",
EVIL_UPDATE_TOPIC,
BASE_COMMIT,
&conf);
assert_eq!(result.warnings().len(), 0);
assert_eq!(result.alerts().len(), 0);
assert_eq!(result.errors().len(), 1);
assert_eq!(result.errors()[0],
"commit add18e5 not allowed; the check_size directory contains changes not on \
the import branch; merge conflicts should not happen and indicate that the \
import directory was manually edited at some point. Please find and revert \
the bad edit, apply it to the imported repository (if necessary), and then \
run the import script.");
assert_eq!(result.allowed(), false);
assert_eq!(result.pass(), false);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment