fix: don't allow trailing . (#4570)

This is a quick-fix for a specific case, even though the actual fix
should be offering branch normalization as part of the validation.
This commit is contained in:
Sebastian Thiel 2024-08-02 09:07:31 +02:00
parent e2bd607c94
commit 7fea6b8130
No known key found for this signature in database
GPG Key ID: 9CB5EE7895E8268B
2 changed files with 7 additions and 11 deletions

View File

@ -14,8 +14,8 @@ pub fn normalize_branch_name(name: &str) -> anyhow::Result<String> {
let space_pattern = Regex::new(r"\s+").unwrap();
result = space_pattern.replace_all(&result, "-").to_string();
// Remove leading and trailing hyphens and slashes
let trim_pattern = Regex::new(r"^[-/]+|[-/]+$").unwrap();
// Remove leading and trailing hyphens and slashes and dots
let trim_pattern = Regex::new(r"^[-/\.]+|[-/\.]+$").unwrap();
result = trim_pattern.replace_all(&result, "").to_string();
let refname = format!("refs/gitbutler/{result}");

View File

@ -22,6 +22,7 @@ mod normalize_branch_name {
("/a/", "a"),
("-/a/-", "a"),
("/-a-/", "a"),
(".a.", "a"),
] {
assert_eq!(normalize_branch_name(input).expect("valid"), expected);
}
@ -37,15 +38,6 @@ mod normalize_branch_name {
normalize_branch_name("#[test]").unwrap_err().to_string(),
"Could not turn \"#[test]\" into a valid reference name"
);
let input = r#"Revert "GitButler Integration Commit"
This reverts commit d6efa5fd96d36da445d5d1345b84163f05f5f229."#;
let err = normalize_branch_name(input).unwrap_err().to_string();
assert_eq!(
err,
"Could not turn \"Revert-\\\"GitButler-Integration-Commit\\\"-This-reverts-commit-d6efa5fd96d36da445d5d1345b84163f05f5f229.\" into a valid reference name"
);
}
#[test]
@ -53,6 +45,10 @@ This reverts commit d6efa5fd96d36da445d5d1345b84163f05f5f229."#;
assert_eq!(normalize_branch_name("feature/branch")?, "feature/branch");
assert_eq!(normalize_branch_name("foo#branch")?, "foo#branch");
assert_eq!(normalize_branch_name("foo!branch")?, "foo!branch");
let input = r#"Revert "GitButler Integration Commit"
This reverts commit d6efa5fd96d36da445d5d1345b84163f05f5f229."#;
assert_eq!(normalize_branch_name(input)?, "Revert-\"GitButler-Integration-Commit\"-This-reverts-commit-d6efa5fd96d36da445d5d1345b84163f05f5f229");
Ok(())
}
}