Skip to content

Commit b09eebc

Browse files
committed
refactor: remove nesting
1 parent 438b950 commit b09eebc

File tree

7 files changed

+208
-236
lines changed

7 files changed

+208
-236
lines changed

src/commands/branch_fetch.rs

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,39 @@ use colored::Colorize as _;
55
use crate::cli::Remote;
66
use crate::commit::Commit;
77
use crate::git::{fetch_branch, git};
8+
use anyhow::anyhow;
89

910
/// Fetch the given branch
1011
pub async fn branch_fetch(
1112
remote: Remote,
1213
commit: Option<Commit>,
1314
checkout: bool,
1415
) -> anyhow::Result<()> {
15-
match fetch_branch(&remote, commit.as_ref()).await {
16-
Ok((_, info)) => {
17-
log::info!(
18-
"Fetched branch {}/{}/{} available at branch {}{}",
19-
remote.owner,
20-
remote.repo,
21-
info.branch.upstream_branch_name,
22-
info.branch.local_branch_name.bright_cyan(),
23-
commit
24-
.map(|commit_hash| {
25-
format!(", at commit {}", commit_hash.as_ref().bright_yellow())
26-
})
27-
.unwrap_or_default()
28-
);
29-
30-
// Attempt to cleanup after ourselves
31-
let _ = git(["remote", "remove", &info.remote.local_remote_alias]);
32-
33-
if checkout {
34-
if let Err(cant_checkout) = git(["checkout", &info.branch.local_branch_name]) {
35-
log::error!(
36-
"Could not check out branch
37-
{}:\n{cant_checkout}",
38-
info.branch.local_branch_name
39-
);
40-
} else {
41-
log::info!("checked out: {}", info.branch.local_branch_name);
42-
}
43-
}
44-
}
45-
Err(err) => {
46-
log::error!("{err}");
47-
}
16+
let (_, info) = fetch_branch(&remote, commit.as_ref()).await?;
17+
18+
log::info!(
19+
"Fetched branch {}/{}/{} available at branch {}{}",
20+
remote.owner,
21+
remote.repo,
22+
info.branch.upstream_branch_name,
23+
info.branch.local_branch_name.bright_cyan(),
24+
commit
25+
.map(|commit_hash| { format!(", at commit {}", commit_hash.as_ref().bright_yellow()) })
26+
.unwrap_or_default()
27+
);
28+
29+
// Attempt to cleanup after ourselves
30+
let _ = git(["remote", "remove", &info.remote.local_remote_alias]);
31+
32+
if checkout {
33+
git(["checkout", &info.branch.local_branch_name]).map_err(|err| {
34+
anyhow!(
35+
"failed to check out branch {}:\n{err}",
36+
info.branch.local_branch_name
37+
)
38+
})?;
39+
40+
log::info!("checked out: {}", info.branch.local_branch_name);
4841
}
4942

5043
Ok(())

src/commands/gen_patch.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@ use std::path::PathBuf;
55

66
use anyhow::bail;
77

8-
use crate::CONFIG_ROOT;
8+
use crate::CONFIG_PATH;
99
use crate::commit::Commit;
10-
use crate::git::{GIT_ROOT, git};
10+
use crate::git::git;
1111
use crate::utils::normalize_commit_msg;
1212

1313
/// Generate patch `filename` at the given `Commit`
1414
pub fn gen_patch(commit: Commit, filename: Option<PathBuf>) -> anyhow::Result<()> {
15-
let config_path = GIT_ROOT.join(CONFIG_ROOT.as_str());
16-
17-
if !config_path.exists() {
15+
if !CONFIG_PATH.exists() {
1816
log::info!(
1917
"Config directory {} does not exist, creating it...",
20-
config_path.to_string_lossy()
18+
CONFIG_PATH.to_string_lossy()
2119
);
22-
fs::create_dir_all(&config_path)?;
20+
fs::create_dir_all(&*CONFIG_PATH)?;
2321
}
2422

2523
// 1. if the user provides a custom filename for the patch file, use that
@@ -32,17 +30,15 @@ pub fn gen_patch(commit: Commit, filename: Option<PathBuf>) -> anyhow::Result<()
3230
|commit_msg| normalize_commit_msg(&commit_msg),
3331
)
3432
},
35-
|filename| filename.to_str().unwrap_or_default().to_string(),
33+
|filename| filename.to_str().unwrap_or("").to_string(),
3634
);
3735

38-
let patch_filename = format!("{patch_filename}.patch");
39-
40-
let patch_file_path = config_path.join(&patch_filename);
36+
let patch_file_path = CONFIG_PATH.join(format!("{patch_filename}.patch"));
4137

4238
// Paths are UTF-8 encoded. If we cannot convert to UTF-8 that means it is not a
4339
// valid path
4440
let Some(patch_file_path_str) = patch_file_path.as_os_str().to_str() else {
45-
bail!("Not a valid path: {patch_file_path:?}");
41+
bail!("invalid path: {patch_file_path:?}");
4642
};
4743

4844
if let Err(err) = git([

src/commands/init.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,26 @@ use std::io::Write as _;
66
use anyhow::bail;
77
use colored::Colorize as _;
88

9-
use crate::git::GIT_ROOT;
10-
use crate::{CONFIG_FILE, CONFIG_ROOT, confirm_prompt};
9+
use crate::{CONFIG_FILE_PATH, CONFIG_PATH, confirm_prompt};
1110

1211
/// Initialize the Patchy config file
1312
pub fn init() -> anyhow::Result<()> {
14-
let example_config = include_bytes!("../../example-config.toml");
15-
16-
let config_path = GIT_ROOT.join(CONFIG_ROOT.as_str());
17-
18-
let config_file_path = config_path.join(CONFIG_FILE);
19-
20-
if config_file_path.exists()
13+
if CONFIG_FILE_PATH.exists()
2114
&& !confirm_prompt!(
2215
"File {} already exists. Overwrite it?",
23-
config_file_path.to_string_lossy().bright_blue(),
16+
CONFIG_FILE_PATH.to_string_lossy().bright_blue(),
2417
)
2518
{
26-
bail!("Did not overwrite {}", config_file_path.display());
19+
bail!("Did not overwrite {}", CONFIG_FILE_PATH.display());
2720
}
2821

29-
fs::create_dir_all(config_path)?;
22+
fs::create_dir_all(&*CONFIG_PATH)?;
3023

31-
let mut file = File::create(&config_file_path)?;
24+
let mut file = File::create(&*CONFIG_FILE_PATH)?;
3225

33-
file.write_all(example_config)?;
26+
file.write_all(include_bytes!("../../example-config.toml"))?;
3427

35-
log::info!("Created config file {}", config_file_path.display());
28+
log::info!("Created config file {}", CONFIG_FILE_PATH.display());
3629

3730
Ok(())
3831
}

src/commands/pr_fetch.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ pub async fn pr_fetch(
2424
let remote = remote.map_or_else(
2525
|| -> anyhow::Result<Remote> {
2626
let remote = git(["remote", "get-url", "origin"])?;
27-
let err = || anyhow!("git command returned invalid remote. Output {remote}");
27+
let err = || anyhow!("git command returned invalid remote: {remote}");
28+
2829
if remote.starts_with(GITHUB_REMOTE_PREFIX) && remote.ends_with(GITHUB_REMOTE_SUFFIX) {
2930
let start = GITHUB_REMOTE_PREFIX.len();
3031
let end = remote.len() - GITHUB_REMOTE_SUFFIX.len();

0 commit comments

Comments
 (0)