merge-request: normalize target observation refs
This commit is contained in:
@@ -221,12 +221,7 @@ impl RepositoryRegistryReader {
|
|||||||
.map(str::to_owned)
|
.map(str::to_owned)
|
||||||
.or_else(|| repository.default_selector.clone())
|
.or_else(|| repository.default_selector.clone())
|
||||||
.ok_or_else(|| RepositoryLookupError::MissingDefaultSelector { id: id.to_string() })?;
|
.ok_or_else(|| RepositoryLookupError::MissingDefaultSelector { id: id.to_string() })?;
|
||||||
if selector.starts_with('-') || selector.as_bytes().contains(&0) {
|
let selector = normalize_target_branch_selector(id, &selector)?;
|
||||||
return Err(RepositoryLookupError::InvalidSelector {
|
|
||||||
id: id.to_string(),
|
|
||||||
selector,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
let spec = format!("{selector}^{{commit}}");
|
let spec = format!("{selector}^{{commit}}");
|
||||||
let commit = merge_git_stdout(
|
let commit = merge_git_stdout(
|
||||||
repository,
|
repository,
|
||||||
@@ -319,29 +314,7 @@ impl RepositoryRegistryReader {
|
|||||||
result_commit: &str,
|
result_commit: &str,
|
||||||
) -> Result<(), RepositoryLookupError> {
|
) -> Result<(), RepositoryLookupError> {
|
||||||
let repository = self.merge_repository(id)?;
|
let repository = self.merge_repository(id)?;
|
||||||
let target_ref = if selector.starts_with("refs/heads/") {
|
let target_ref = normalize_target_branch_selector(id, selector)?;
|
||||||
selector.to_owned()
|
|
||||||
} else if selector.starts_with("refs/") {
|
|
||||||
return Err(RepositoryLookupError::InvalidSelector {
|
|
||||||
id: id.into(),
|
|
||||||
selector: selector.into(),
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
format!("refs/heads/{selector}")
|
|
||||||
};
|
|
||||||
let valid_ref = Command::new("git")
|
|
||||||
.args(["check-ref-format", target_ref.as_str()])
|
|
||||||
.status()
|
|
||||||
.map_err(|_| RepositoryLookupError::ProviderFailure {
|
|
||||||
id: id.into(),
|
|
||||||
operation: "validate target branch ref".into(),
|
|
||||||
})?;
|
|
||||||
if !valid_ref.success() || selector.starts_with('-') || selector.as_bytes().contains(&0) {
|
|
||||||
return Err(RepositoryLookupError::InvalidSelector {
|
|
||||||
id: id.into(),
|
|
||||||
selector: selector.into(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
self.observe_commit(id, result_commit)?;
|
self.observe_commit(id, result_commit)?;
|
||||||
let status = Command::new("git")
|
let status = Command::new("git")
|
||||||
.arg("-C")
|
.arg("-C")
|
||||||
@@ -474,6 +447,37 @@ impl RepositoryRegistryReader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn normalize_target_branch_selector(
|
||||||
|
id: &str,
|
||||||
|
selector: &str,
|
||||||
|
) -> Result<String, RepositoryLookupError> {
|
||||||
|
let selector = selector.trim();
|
||||||
|
let target_ref = if selector.starts_with("refs/heads/") {
|
||||||
|
selector.to_owned()
|
||||||
|
} else if selector.starts_with("refs/") {
|
||||||
|
return Err(RepositoryLookupError::InvalidSelector {
|
||||||
|
id: id.into(),
|
||||||
|
selector: selector.into(),
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
format!("refs/heads/{selector}")
|
||||||
|
};
|
||||||
|
let valid_ref = Command::new("git")
|
||||||
|
.args(["check-ref-format", target_ref.as_str()])
|
||||||
|
.status()
|
||||||
|
.map_err(|_| RepositoryLookupError::ProviderFailure {
|
||||||
|
id: id.into(),
|
||||||
|
operation: "validate target branch ref".into(),
|
||||||
|
})?;
|
||||||
|
if !valid_ref.success() || selector.starts_with('-') || selector.as_bytes().contains(&0) {
|
||||||
|
return Err(RepositoryLookupError::InvalidSelector {
|
||||||
|
id: id.into(),
|
||||||
|
selector: selector.into(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Ok(target_ref)
|
||||||
|
}
|
||||||
|
|
||||||
fn merge_git_stdout(
|
fn merge_git_stdout(
|
||||||
repository: &ConfiguredRepository,
|
repository: &ConfiguredRepository,
|
||||||
operation: &str,
|
operation: &str,
|
||||||
@@ -725,6 +729,15 @@ mod tests {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.trim()
|
.trim()
|
||||||
.to_string();
|
.to_string();
|
||||||
|
assert!(
|
||||||
|
Command::new("git")
|
||||||
|
.arg("-C")
|
||||||
|
.arg(path)
|
||||||
|
.args(["update-ref", "refs/tags/main", &source])
|
||||||
|
.status()
|
||||||
|
.unwrap()
|
||||||
|
.success()
|
||||||
|
);
|
||||||
|
|
||||||
let reader = RepositoryRegistryReader::new(vec![ConfiguredRepository {
|
let reader = RepositoryRegistryReader::new(vec![ConfiguredRepository {
|
||||||
id: "main".into(),
|
id: "main".into(),
|
||||||
@@ -734,8 +747,8 @@ mod tests {
|
|||||||
uri: path.display().to_string(),
|
uri: path.display().to_string(),
|
||||||
default_selector: Some("main".into()),
|
default_selector: Some("main".into()),
|
||||||
}]);
|
}]);
|
||||||
let target = reader.observe_merge_target("main", None).unwrap();
|
let target = reader.observe_merge_target("main", Some("main")).unwrap();
|
||||||
assert_eq!(target.selector, "main");
|
assert_eq!(target.selector, "refs/heads/main");
|
||||||
assert_eq!(target.commit, base);
|
assert_eq!(target.commit, base);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
reader.observe_commit("main", &source).unwrap().parents,
|
reader.observe_commit("main", &source).unwrap().parents,
|
||||||
|
|||||||
Reference in New Issue
Block a user