Skip to content

Commit 0634ec9

Browse files
committed
refactor: rename identifiers
1 parent aa68679 commit 0634ec9

File tree

11 files changed

+114
-135
lines changed

11 files changed

+114
-135
lines changed

src/backup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::path::PathBuf;
88
use tempfile::tempfile;
99

1010
use crate::CONFIG_ROOT;
11-
use crate::git_commands::GIT_ROOT;
11+
use crate::git::GIT_ROOT;
1212

1313
pub fn files(config_files: ReadDir) -> anyhow::Result<Vec<(OsString, File, String)>> {
1414
let mut backups = Vec::new();

src/cli.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use clap::{
88
};
99
use tap::Pipe as _;
1010

11-
use crate::commit::Commit;
11+
use crate::{commands, commit::Commit};
1212

1313
/// A tool which makes it easy to declaratively manage personal forks by automatically merging pull requests
1414
#[derive(Parser)]
1515
#[command(version, styles = STYLES, long_about = None)]
16-
pub struct Args {
16+
pub struct Cli {
1717
#[command(subcommand)]
1818
pub command: Command,
1919
}
@@ -71,6 +71,32 @@ pub enum Command {
7171
},
7272
}
7373

74+
impl Command {
75+
pub async fn execute(self) -> anyhow::Result<()> {
76+
match self {
77+
Self::Init => commands::init()?,
78+
Self::Run { yes } => commands::run(yes).await?,
79+
Self::GenPatch { commit, filename } => {
80+
commands::gen_patch(commit, filename)?;
81+
}
82+
Self::PrFetch {
83+
pr,
84+
remote,
85+
branch,
86+
commit,
87+
checkout,
88+
} => commands::pr_fetch(pr, remote, branch, commit, checkout).await?,
89+
Self::BranchFetch {
90+
remote,
91+
commit,
92+
checkout,
93+
} => commands::branch_fetch(remote, commit, checkout).await?,
94+
}
95+
96+
Ok(())
97+
}
98+
}
99+
74100
/// Styles for the CLI
75101
const STYLES: clap::builder::Styles = clap::builder::Styles::styled()
76102
.header(AnsiColor::BrightGreen.on_default().effects(Effects::BOLD))

src/commands/branch_fetch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use colored::Colorize as _;
22

33
use crate::cli::Remote;
44
use crate::commit::Commit;
5-
use crate::git_commands::{fetch_branch, git};
5+
use crate::git::{fetch_branch, git};
66
use crate::{fail, success};
77

88
pub async fn branch_fetch(

src/commands/gen_patch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::path::PathBuf;
44
use anyhow::bail;
55

66
use crate::commit::Commit;
7-
use crate::git_commands::{GIT_ROOT, git};
7+
use crate::git::{GIT_ROOT, git};
88
use crate::utils::normalize_commit_msg;
99
use crate::{CONFIG_ROOT, note, success};
1010

src/commands/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::io::Write as _;
44
use anyhow::bail;
55
use colored::Colorize as _;
66

7-
use crate::git_commands::GIT_ROOT;
7+
use crate::git::GIT_ROOT;
88
use crate::{CONFIG_FILE, CONFIG_ROOT, confirm_prompt, success};
99

1010
pub fn init() -> anyhow::Result<()> {

src/commands/pr_fetch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use colored::Colorize as _;
33

44
use crate::cli::Remote;
55
use crate::commit::Commit;
6-
use crate::git_commands::{fetch_pull_request, git};
6+
use crate::git::{fetch_pull_request, git};
77
use crate::utils::display_link;
88
use crate::{fail, success};
99

src/commands/run.rs

Lines changed: 41 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
use std::{fs, process};
1+
use std::fs;
22

3-
use anyhow::anyhow;
3+
use anyhow::{anyhow, bail};
44
use colored::Colorize as _;
55

6-
use crate::backup::{files, restore};
76
use crate::commands::pr_fetch::ignore_octothorpe;
87
use crate::commit::Commit;
9-
use crate::git_commands::{
10-
GIT_ROOT, add_remote_branch, checkout_from_remote, clean_up_remote, fetch_pull_request, git,
11-
merge_pull_request,
12-
};
8+
use crate::git::{self, GIT_ROOT, git};
139
use crate::types::{Branch, BranchAndRemote, Configuration, Remote};
1410
use crate::utils::{display_link, with_uuid};
1511
use crate::{APP_NAME, CONFIG_FILE, CONFIG_ROOT, commands, confirm_prompt, fail, success};
12+
use crate::{backup, note};
1613

1714
/// Parses user inputs of the form `<head><syntax><commit-hash>`
1815
///
@@ -36,77 +33,57 @@ pub fn parse_if_maybe_hash(input: &str, syntax: &str) -> (String, Option<Commit>
3633

3734
pub async fn run(yes: bool) -> anyhow::Result<()> {
3835
println!();
39-
40-
let config_path = GIT_ROOT.join(CONFIG_ROOT.as_str());
36+
let root = CONFIG_ROOT.as_str();
37+
let config_path = GIT_ROOT.join(root);
4138

4239
let config_file_path = config_path.join(CONFIG_FILE);
4340

4441
let Ok(config_raw) = fs::read_to_string(config_file_path.clone()) else {
45-
fail!(
46-
"Could not find configuration file at {}/{CONFIG_FILE}",
47-
CONFIG_ROOT.as_str()
48-
);
42+
fail!("Could not find configuration file at {root}/{CONFIG_FILE}",);
4943

5044
// We don't want to have *any* sort of prompt when using the -y flag since that
5145
// would be problematic in scripts
52-
if !yes
53-
&& confirm_prompt!(
54-
"Would you like us to run {} {} to initialize it?",
55-
"patchy".bright_blue(),
56-
"init".bright_yellow(),
57-
)
58-
{
59-
if let Err(err) = commands::init() {
60-
fail!("{err}");
61-
process::exit(1);
62-
}
46+
if !yes && confirm_prompt!("Would you like us to run `patchy init` to initialize it?",) {
47+
commands::init()?;
6348
} else if yes {
64-
eprintln!(
65-
"You can create it with {} {}",
66-
"patchy".bright_blue(),
67-
"init".bright_yellow()
68-
);
49+
note!("You can create it with `patchy init`",);
6950
} else {
7051
// user said "no" in the prompt, so we don't do any initializing
7152
}
7253

7354
// We don't want to read the default configuration file as config_raw. Since
7455
// it's empty there's no reason why the user would want to run it.
7556

76-
process::exit(0);
57+
return Ok(());
7758
};
7859

7960
log::trace!("Using configuration file {config_file_path:?}");
8061

8162
let config = toml::from_str::<Configuration>(&config_raw).map_err(|err| {
82-
anyhow!(
83-
"Could not parse `{}/{CONFIG_FILE}` configuration file:\n{err}",
84-
CONFIG_ROOT.as_str()
85-
)
63+
anyhow!("Could not parse `{root}/{CONFIG_FILE}` configuration file:\n{err}",)
8664
})?;
8765

8866
let (remote_branch, commit_hash) = parse_if_maybe_hash(&config.remote_branch, " @ ");
8967

9068
if config.repo.is_empty() {
91-
return Err(anyhow::anyhow!(
92-
r#"You haven't specified a `repo` in your config, which can be for example:
93-
- "helix-editor/helix"
94-
- "microsoft/vscode"
69+
bail!(
70+
"You haven't specified a `repo` in your config, which can be for example:
71+
- `helix-editor/helix`
72+
- `microsoft/vscode`
9573
96-
For more information see this guide: https://github.com/nik-rev/patchy/blob/main/README.md""#
97-
));
74+
For more information see this guide: https://github.com/nik-rev/patchy/blob/main/README.md"
75+
);
9876
}
9977

10078
let config_files = fs::read_dir(&config_path).map_err(|err| {
10179
anyhow!(
102-
"Could not read files in directory {:?}\n{err}",
103-
&config_path
80+
"Failed to read files in directory `{}`:\n{err}",
81+
&config_path.display()
10482
)
10583
})?;
10684

107-
let backed_up_files = files(config_files).map_err(|err| {
108-
anyhow!("Could not create backups for configuration files, aborting.\n{err}")
109-
})?;
85+
let backed_up_files = backup::files(config_files)
86+
.map_err(|err| anyhow!("Failed to create backups for configuration files:\n{err}"))?;
11087

11188
let info = BranchAndRemote {
11289
branch: Branch {
@@ -119,9 +96,9 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
11996
},
12097
};
12198

122-
add_remote_branch(&info, commit_hash.as_ref())?;
99+
git::add_remote_branch(&info, commit_hash.as_ref())?;
123100

124-
let previous_branch = checkout_from_remote(
101+
let previous_branch = git::checkout_from_remote(
125102
&info.branch.local_branch_name,
126103
&info.remote.local_remote_alias,
127104
)?;
@@ -142,17 +119,16 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
142119
let pull_request = ignore_octothorpe(pull_request);
143120
let (pull_request, commit_hash) = parse_if_maybe_hash(&pull_request, " @ ");
144121
// TODO: refactor this to not use such deep nesting
145-
match fetch_pull_request(&config.repo, &pull_request, None, commit_hash.as_ref()).await
122+
match git::fetch_pull_request(&config.repo, &pull_request, None, commit_hash.as_ref())
123+
.await
146124
{
147125
Ok((response, info)) => {
148-
match merge_pull_request(
149-
info,
126+
match git::merge_pull_request(
127+
&info,
150128
&pull_request,
151129
&response.title,
152130
&response.html_url,
153-
)
154-
.await
155-
{
131+
) {
156132
Ok(()) => {
157133
success!(
158134
"Merged pull request {}",
@@ -183,7 +159,7 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
183159
if let Err(err) = fs::create_dir_all(GIT_ROOT.join(CONFIG_ROOT.as_str())) {
184160
git(["checkout", &previous_branch])?;
185161

186-
clean_up_remote(
162+
git::delete_remote_and_branch(
187163
&info.remote.local_remote_alias,
188164
&info.branch.local_branch_name,
189165
)?;
@@ -195,7 +171,8 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
195171
}
196172

197173
for (file_name, _file, contents) in &backed_up_files {
198-
restore(file_name, contents).map_err(|err| anyhow!("Could not restore backups:\n{err}"))?;
174+
backup::restore(file_name, contents)
175+
.map_err(|err| anyhow!("Could not restore backups:\n{err}"))?;
199176
}
200177

201178
// apply patches if they exist
@@ -236,7 +213,7 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
236213

237214
git(["switch", "--create", &temporary_branch])?;
238215

239-
clean_up_remote(
216+
git::delete_remote_and_branch(
240217
&info.remote.local_remote_alias,
241218
&info.branch.local_branch_name,
242219
)?;
@@ -258,24 +235,23 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
258235
&config.local_branch,
259236
])?;
260237
if yes {
261-
log::info!(
262-
"Overwrote branch {} since you supplied the {} flag",
238+
note!(
239+
"Automatically overwrote branch {} since you supplied the {} flag",
263240
config.local_branch.cyan(),
264241
"--yes".bright_magenta()
265242
);
266243
}
267-
println!("\n {}", " Success!\n".bright_green().bold());
244+
success!("Success!");
268245
} else {
269-
let command = format!(
270-
" git branch --move --force {temporary_branch} {}",
246+
let overwrite_command = format!(
247+
"git branch --move --force {temporary_branch} {}",
271248
config.local_branch
272249
);
273-
let command = format!("\n {}\n", command.bright_magenta());
274-
println!(
275-
"\n You can still manually overwrite {} with the following command:\n {command}",
250+
note!(
251+
"You can still manually overwrite {} with:\n {overwrite_command}\n",
276252
config.local_branch.cyan(),
277253
);
278-
process::exit(1)
254+
return Ok(());
279255
}
280256

281257
Ok(())

0 commit comments

Comments
 (0)