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

View File

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

View File

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