1- use std:: { fs , process } ;
1+ use std:: fs ;
22
3- use anyhow:: anyhow;
3+ use anyhow:: { anyhow, bail } ;
44use colored:: Colorize as _;
55
6- use crate :: backup:: { files, restore} ;
76use crate :: commands:: pr_fetch:: ignore_octothorpe;
87use 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} ;
139use crate :: types:: { Branch , BranchAndRemote , Configuration , Remote } ;
1410use crate :: utils:: { display_link, with_uuid} ;
1511use 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
3734pub 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