Skip to content

Commit d774b3e

Browse files
committed
refactor: extract many git functions into smaller ones
1 parent 280268a commit d774b3e

File tree

9 files changed

+405
-386
lines changed

9 files changed

+405
-386
lines changed

src/commands/branch_fetch.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use colored::Colorize as _;
44

55
use crate::config::{CommitId, Remote};
6-
use crate::git::{fetch_branch, git};
6+
use crate::git_high_level::git;
7+
use crate::github_api;
78
use anyhow::anyhow;
89

910
/// Fetch the given branch
@@ -12,7 +13,7 @@ pub async fn branch_fetch(
1213
commit: Option<CommitId>,
1314
checkout: bool,
1415
) -> anyhow::Result<()> {
15-
let (_, info) = fetch_branch(&remote).await?;
16+
let (_, info) = github_api::fetch_branch(&remote).await?;
1617

1718
log::info!(
1819
"Fetched branch {}/{}/{} available at branch {}{}",

src/commands/gen_patch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use anyhow::bail;
77

88
use crate::CONFIG_PATH;
99
use crate::config::{CommitId, PatchName};
10-
use crate::git::git;
10+
use crate::git_high_level::git;
1111
use crate::utils::normalize_commit_msg;
1212

1313
/// Generate patch `filename` at the given `Commit`

src/commands/pr_fetch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use anyhow::{Context as _, anyhow};
44
use colored::Colorize as _;
55

66
use crate::config::{BranchName, CommitId, PrNumber, Remote, RepoName, RepoOwner};
7-
use crate::git::{fetch_pull_request, git};
7+
use crate::git_high_level::git;
8+
use crate::github_api::fetch_pull_request;
89

910
/// Fetch the given `pr` of `remote` at `commit` and store it in local `branch`
1011
///
@@ -47,7 +48,6 @@ pub async fn pr_fetch(
4748

4849
let Ok((response, info)) = fetch_pull_request(
4950
&format!("{}/{}", remote.owner, remote.repo),
50-
// TODO: make fetch_pull_request accept a u32 instead
5151
pr,
5252
branch,
5353
commit.as_ref(),

src/commands/run.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ use std::path::PathBuf;
99
use anyhow::{anyhow, bail};
1010
use colored::Colorize as _;
1111

12-
use crate::git::{self, GIT_ROOT, git};
13-
use crate::github_api::{Branch, Remote, RemoteBranch};
12+
use crate::git_high_level::{self, git};
13+
use crate::github_api::{self, Branch, Remote, RemoteBranch};
1414
use crate::utils::{display_link, with_uuid};
15-
use crate::{CONFIG_FILE, CONFIG_FILE_PATH, CONFIG_PATH, CONFIG_ROOT, commands, confirm_prompt};
15+
use crate::{
16+
CONFIG_FILE, CONFIG_FILE_PATH, CONFIG_PATH, CONFIG_ROOT, commands, confirm_prompt, git,
17+
};
1618

1719
/// Backup for a file
1820
struct FileBackup {
@@ -108,9 +110,9 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
108110
},
109111
};
110112

111-
git::add_remote_branch(&info, commit.as_ref())?;
113+
git_high_level::add_remote_branch(&info, commit.as_ref())?;
112114

113-
let previous_branch = git::checkout_from_remote(
115+
let previous_branch = git_high_level::checkout_from_remote(
114116
&info.branch.local_branch_name,
115117
&info.remote.local_remote_alias,
116118
)?;
@@ -136,7 +138,7 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
136138
{
137139
// TODO: refactor this to not use such deep nesting
138140
let Ok((response, info)) =
139-
git::fetch_pull_request(&config.repo, *pull_request, None, commit.as_ref())
141+
github_api::fetch_pull_request(&config.repo, *pull_request, None, commit.as_ref())
140142
.await
141143
.inspect_err(|err| {
142144
log::error!("failed to fetch branch from remote:\n{err}");
@@ -145,9 +147,12 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
145147
continue;
146148
};
147149

148-
if let Err(err) =
149-
git::merge_pull_request(&info, *pull_request, &response.title, &response.html_url)
150-
{
150+
if let Err(err) = git_high_level::merge_pull_request(
151+
&info,
152+
*pull_request,
153+
&response.title,
154+
&response.html_url,
155+
) {
151156
log::error!("failed to merge {pull_request}: {err}");
152157
continue;
153158
}
@@ -172,13 +177,13 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
172177
let owner = &remote.owner;
173178
let repo = &remote.repo;
174179
let branch = &remote.branch;
175-
let Ok((_, info)) = git::fetch_branch(remote).await.inspect_err(|err| {
180+
let Ok((_, info)) = github_api::fetch_branch(remote).await.inspect_err(|err| {
176181
log::error!("failed to fetch branch {owner}/{repo}/{branch}: {err}");
177182
}) else {
178183
continue;
179184
};
180185

181-
if let Err(err) = git::merge_into_main(
186+
if let Err(err) = git_high_level::merge(
182187
&info.branch.local_branch_name,
183188
&info.branch.upstream_branch_name,
184189
) {
@@ -206,7 +211,7 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
206211
}
207212
}
208213

209-
if let Err(err) = fs::create_dir_all(GIT_ROOT.join(CONFIG_ROOT.as_str())) {
214+
if let Err(err) = fs::create_dir_all(git::ROOT.join(CONFIG_ROOT.as_str())) {
210215
git(["checkout", &previous_branch])?;
211216

212217
git::delete_remote_and_branch(
@@ -223,7 +228,7 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
223228
filename, contents, ..
224229
} in &backed_up_files
225230
{
226-
let path = GIT_ROOT.join(PathBuf::from(CONFIG_ROOT.as_str()).join(filename));
231+
let path = git::ROOT.join(PathBuf::from(CONFIG_ROOT.as_str()).join(filename));
227232
let mut file =
228233
File::create(&path).map_err(|err| anyhow!("failed to restore backup: {err}"))?;
229234

@@ -233,7 +238,7 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
233238
// apply patches if they exist
234239

235240
for patch in config.patches {
236-
let file_name = GIT_ROOT
241+
let file_name = git::ROOT
237242
.join(CONFIG_ROOT.as_str())
238243
.join(format!("{patch}.patch"));
239244

@@ -261,7 +266,7 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
261266
}
262267

263268
git(["add", CONFIG_ROOT.as_str()])?;
264-
git(["commit", "--message", "patchy: Restore configuration files"])?;
269+
git::commit("restore configuration files")?;
265270

266271
let temporary_branch = with_uuid("temp-branch");
267272

0 commit comments

Comments
 (0)