Refuse to push against non-pushable remotes (e.g. HTTPS for now)

This commit is contained in:
2021-12-23 18:33:14 +01:00
parent b183590096
commit fcc22791e5
2 changed files with 22 additions and 3 deletions

View File

@@ -66,7 +66,7 @@ pub fn path_as_string(path: &Path) -> String {
path.to_path_buf().into_os_string().into_string().unwrap()
}
fn env_home() -> PathBuf {
pub fn env_home() -> PathBuf {
match std::env::var("HOME") {
Ok(path) => Path::new(&path).to_path_buf(),
Err(e) => {
@@ -541,6 +541,13 @@ pub fn add_worktree(
Err(_) => repo.create_branch(&branch_name, &checkout_commit)?,
};
fn push(remote: &mut repo::RemoteHandle, branch_name: &str, remote_branch_name: &str, repo: &repo::Repo) -> Result<(), String>{
if !remote.is_pushable()? {
return Err(format!("Cannot push to non-pushable remote {}", remote.url()));
}
remote.push(branch_name, remote_branch_name, repo)
}
if !no_track {
if let Some((remote_name, remote_branch_name)) = track {
if remote_branch_exists {
@@ -551,7 +558,7 @@ pub fn add_worktree(
.map_err(|error| format!("Error getting remote {}: {}", remote_name, error))?
.ok_or_else(|| format!("Remote {} not found", remote_name))?;
remote.push(&target_branch.name()?, remote_branch_name, &repo)?;
push(&mut remote, &target_branch.name()?, remote_branch_name, &repo)?;
target_branch.set_upstream(remote_name, remote_branch_name)?;
}
@@ -572,7 +579,10 @@ pub fn add_worktree(
.map_err(|error| format!("Error getting remote {}: {}", remote_name, error))?
.ok_or_else(|| format!("Remote {} not found", remote_name))?;
remote.push(&target_branch.name()?, &remote_branch_name, &repo)?;
if !remote.is_pushable()? {
return Err(format!("Cannot push to non-pushable remote {}", remote.url()));
}
push(&mut remote, &target_branch.name()?, &remote_branch_name, &repo)?;
target_branch.set_upstream(&remote_name, &remote_branch_name)?;
}

View File

@@ -980,12 +980,21 @@ impl RemoteHandle<'_> {
.to_string()
}
pub fn is_pushable(&self) -> Result<bool, String> {
let remote_type = detect_remote_type(self.0.url().expect("Remote name is not valid utf-8")).ok_or_else(|| String::from("Could not detect remote type"))?;
Ok(matches!(remote_type, RemoteType::Ssh | RemoteType::File))
}
pub fn push(
&mut self,
local_branch_name: &str,
remote_branch_name: &str,
_repo: &Repo,
) -> Result<(), String> {
if !self.is_pushable()? {
return Err(String::from("Trying to push to a non-pushable remote"));
}
let mut callbacks = git2::RemoteCallbacks::new();
callbacks.push_update_reference(|_, status| {
if let Some(message) = status {