Proper formatting

This commit is contained in:
2021-12-23 18:33:14 +01:00
parent 552b3a6aad
commit 02e9de0cbd
3 changed files with 81 additions and 53 deletions

View File

@@ -3,9 +3,9 @@ use std::process;
mod cmd;
use grm::repo;
use grm::config;
use grm::output::*;
use grm::repo;
fn main() {
let opts = cmd::parse();
@@ -177,7 +177,10 @@ fn main() {
let worktree_config = match repo::read_worktree_root_config(&cwd) {
Ok(config) => config,
Err(error) => {
print_error(&format!("Error getting worktree configuration: {}", error));
print_error(&format!(
"Error getting worktree configuration: {}",
error
));
process::exit(1);
}
};
@@ -187,8 +190,12 @@ fn main() {
process::exit(1);
});
match repo.remove_worktree(&action_args.name, &worktree_dir, action_args.force, &worktree_config)
{
match repo.remove_worktree(
&action_args.name,
&worktree_dir,
action_args.force,
&worktree_config,
) {
Ok(_) => print_success(&format!("Worktree {} deleted", &action_args.name)),
Err(error) => {
match error {

View File

@@ -485,9 +485,7 @@ pub fn add_worktree(
let mut remote_branch_exists = false;
let default_checkout = || {
repo.default_branch()?.to_commit()
};
let default_checkout = || repo.default_branch()?.to_commit();
let checkout_commit;
if no_track {
@@ -507,32 +505,29 @@ pub fn add_worktree(
}
}
}
None => {
match &config {
None => match &config {
None => checkout_commit = default_checkout()?,
Some(config) => match &config.track {
None => checkout_commit = default_checkout()?,
Some(config) => {
match &config.track {
None => checkout_commit = default_checkout()?,
Some(track_config) => {
if track_config.default {
let remote_branch = repo.find_remote_branch(&track_config.default_remote, name);
match remote_branch {
Ok(branch) => {
remote_branch_exists = true;
checkout_commit = branch.to_commit()?;
}
Err(_) => {
checkout_commit = default_checkout()?;
}
}
} else {
Some(track_config) => {
if track_config.default {
let remote_branch =
repo.find_remote_branch(&track_config.default_remote, name);
match remote_branch {
Ok(branch) => {
remote_branch_exists = true;
checkout_commit = branch.to_commit()?;
}
Err(_) => {
checkout_commit = default_checkout()?;
}
}
} else {
checkout_commit = default_checkout()?;
}
}
}
}
},
},
};
}
@@ -541,9 +536,17 @@ 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>{
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()));
return Err(format!(
"Cannot push to non-pushable remote {}",
remote.url()
));
}
remote.push(branch_name, remote_branch_name, repo)
}
@@ -558,7 +561,12 @@ pub fn add_worktree(
.map_err(|error| format!("Error getting remote {}: {}", remote_name, error))?
.ok_or_else(|| format!("Remote {} not found", remote_name))?;
push(&mut remote, &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)?;
}
@@ -570,19 +578,31 @@ pub fn add_worktree(
target_branch.set_upstream(&remote_name, name)?;
} else {
let remote_branch_name = match track_config.default_remote_prefix {
Some(prefix) => format!("{}{}{}", &prefix, BRANCH_NAMESPACE_SEPARATOR, &name),
Some(prefix) => {
format!("{}{}{}", &prefix, BRANCH_NAMESPACE_SEPARATOR, &name)
}
None => name.to_string(),
};
let mut remote = repo
.find_remote(&remote_name)
.map_err(|error| format!("Error getting remote {}: {}", remote_name, error))?
.map_err(|error| {
format!("Error getting remote {}: {}", remote_name, error)
})?
.ok_or_else(|| format!("Remote {} not found", remote_name))?;
if !remote.is_pushable()? {
return Err(format!("Cannot push to non-pushable remote {}", remote.url()));
return Err(format!(
"Cannot push to non-pushable remote {}",
remote.url()
));
}
push(&mut remote, &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)?;
}

View File

@@ -58,16 +58,22 @@ pub struct WorktreeRootConfig {
pub track: Option<TrackingConfig>,
}
pub fn read_worktree_root_config(worktree_root: &Path) -> Result<Option<WorktreeRootConfig>, String> {
pub fn read_worktree_root_config(
worktree_root: &Path,
) -> Result<Option<WorktreeRootConfig>, String> {
let path = worktree_root.join(WORKTREE_CONFIG_FILE_NAME);
let content = match std::fs::read_to_string(&path) {
Ok(s) => s,
Err(e) => {
match e.kind() {
std::io::ErrorKind::NotFound => return Ok(None),
_ => return Err(format!("Error reading configuration file \"{}\": {}", path.display(), e)),
Err(e) => match e.kind() {
std::io::ErrorKind::NotFound => return Ok(None),
_ => {
return Err(format!(
"Error reading configuration file \"{}\": {}",
path.display(),
e
))
}
}
},
};
let config: WorktreeRootConfig = match toml::from_str(&content) {
@@ -75,7 +81,8 @@ pub fn read_worktree_root_config(worktree_root: &Path) -> Result<Option<Worktree
Err(e) => {
return Err(format!(
"Error parsing configuration file \"{}\": {}",
path.display(), e
path.display(),
e
))
}
};
@@ -280,11 +287,7 @@ impl Repo {
// unwrap() is safe here, as we can be certain that a branch with that
// name exists
let branch = self
.find_local_branch(
head
.shorthand()
.expect("Branch name is not valid utf-8"),
)
.find_local_branch(head.shorthand().expect("Branch name is not valid utf-8"))
.unwrap();
Ok(branch)
}
@@ -981,7 +984,8 @@ impl RemoteHandle<'_> {
}
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"))?;
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))
}
@@ -1011,12 +1015,9 @@ impl RemoteHandle<'_> {
Some(username) => username,
None => panic!("Could not get username. This is a bug"),
};
git2::Cred::ssh_key_from_agent(username).or_else(|_| git2::Cred::ssh_key(
username,
None,
&crate::env_home().join(".ssh/id_rsa"),
None,
))
git2::Cred::ssh_key_from_agent(username).or_else(|_| {
git2::Cred::ssh_key(username, None, &crate::env_home().join(".ssh/id_rsa"), None)
})
});
let mut push_options = git2::PushOptions::new();