Skip to content

Commit df2e28b

Browse files
committed
refactor: Extract types and error handling to separate modules
- Move type definitions to src/types.rs (105 lines) - SquashedMergeHandling, ReportLevel, MergeResult enums - MergeOptions, MergeCommitInfo, MergeStats structs - BranchSearchResult, SortBranch enums - Move ErrorExt trait to src/error.rs (32 lines) - Update src/main.rs with module declarations - Reduce main.rs from 3,602 to ~3,466 lines - No functional changes, all tests passing Phase 1 of simple file split plan (SIMPLE_SPLIT_PLAN.md)
1 parent ba21d37 commit df2e28b

File tree

3 files changed

+138
-132
lines changed

3 files changed

+138
-132
lines changed

src/error.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use git2::Error;
2+
3+
// For API consistency, we create our own Error variants
4+
pub trait ErrorExt {
5+
#[allow(dead_code)]
6+
fn from_str(message: &str) -> Self;
7+
fn merge_conflict(branch: String, upstream: String, message: Option<String>) -> Self;
8+
fn git_command_failed(command: String, status: i32, stdout: String, stderr: String) -> Self;
9+
}
10+
11+
impl ErrorExt for Error {
12+
fn from_str(message: &str) -> Self {
13+
Error::from_str(message)
14+
}
15+
16+
fn merge_conflict(branch: String, upstream: String, message: Option<String>) -> Self {
17+
let mut error_msg = format!("Merge conflict between {} and {}", upstream, branch);
18+
if let Some(details) = message {
19+
error_msg.push('\n');
20+
error_msg.push_str(&details);
21+
}
22+
Error::from_str(&error_msg)
23+
}
24+
25+
fn git_command_failed(command: String, status: i32, stdout: String, stderr: String) -> Self {
26+
let error_msg = format!(
27+
"Git command failed: {}\nStatus: {}\nStdout: {}\nStderr: {}",
28+
command, status, stdout, stderr
29+
);
30+
Error::from_str(&error_msg)
31+
}
32+
}

src/main.rs

Lines changed: 4 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -14,113 +14,11 @@ use git2::{
1414
use rand::Rng;
1515
use regex::Regex;
1616

17-
// Merge options types
18-
#[derive(Debug, PartialEq, Clone, Copy)]
19-
enum SquashedMergeHandling {
20-
// Reset the branch to the parent branch
21-
Reset,
17+
mod error;
18+
mod types;
2219

23-
// Skip merging the branch
24-
Skip,
25-
26-
// Force a merge despite the squashed merge detection
27-
Merge,
28-
}
29-
30-
#[derive(Debug, PartialEq, Clone, Copy)]
31-
enum ReportLevel {
32-
// Minimal reporting (just success/failure)
33-
Minimal,
34-
35-
// Standard reporting (summary with counts)
36-
Standard,
37-
38-
// Detailed reporting (all actions and their results)
39-
Detailed,
40-
}
41-
42-
enum MergeResult {
43-
// Successfully merged with changes
44-
Success(String), // Contains the merge output message
45-
46-
// Already up-to-date, no changes needed
47-
AlreadyUpToDate,
48-
49-
// Merge conflict occurred
50-
Conflict(String), // Contains the conflict message
51-
}
52-
53-
// For API consistency, we create our own Error variants
54-
trait ErrorExt {
55-
#[allow(dead_code)]
56-
fn from_str(message: &str) -> Self;
57-
fn merge_conflict(branch: String, upstream: String, message: Option<String>) -> Self;
58-
fn git_command_failed(command: String, status: i32, stdout: String, stderr: String) -> Self;
59-
}
60-
61-
impl ErrorExt for Error {
62-
fn from_str(message: &str) -> Self {
63-
Error::from_str(message)
64-
}
65-
66-
fn merge_conflict(branch: String, upstream: String, message: Option<String>) -> Self {
67-
let mut error_msg = format!("Merge conflict between {} and {}", upstream, branch);
68-
if let Some(details) = message {
69-
error_msg.push('\n');
70-
error_msg.push_str(&details);
71-
}
72-
Error::from_str(&error_msg)
73-
}
74-
75-
fn git_command_failed(command: String, status: i32, stdout: String, stderr: String) -> Self {
76-
let error_msg = format!(
77-
"Git command failed: {}\nStatus: {}\nStdout: {}\nStderr: {}",
78-
command, status, stdout, stderr
79-
);
80-
Error::from_str(&error_msg)
81-
}
82-
}
83-
84-
struct MergeOptions {
85-
// Skip the merge of the root branch into the first branch
86-
ignore_root: bool,
87-
88-
// Git merge options passed to all merge operations
89-
merge_flags: Vec<String>,
90-
91-
// Whether to use fork point detection (more accurate but slower)
92-
use_fork_point: bool,
93-
94-
// How to handle squashed merges (reset, skip, merge)
95-
squashed_merge_handling: SquashedMergeHandling,
96-
97-
// Print verbose output
98-
verbose: bool,
99-
100-
// Return to original branch after merging
101-
return_to_original: bool,
102-
103-
// Use simple merge mode
104-
simple_mode: bool,
105-
106-
// Level of detail in the final report
107-
report_level: ReportLevel,
108-
}
109-
110-
impl Default for MergeOptions {
111-
fn default() -> Self {
112-
MergeOptions {
113-
ignore_root: false,
114-
merge_flags: vec![],
115-
use_fork_point: true,
116-
squashed_merge_handling: SquashedMergeHandling::Reset,
117-
verbose: false,
118-
return_to_original: true,
119-
simple_mode: false,
120-
report_level: ReportLevel::Standard,
121-
}
122-
}
123-
}
20+
use error::ErrorExt;
21+
use types::*;
12422

12523
fn executable_name() -> String {
12624
let name = std::env::current_exe()
@@ -201,18 +99,6 @@ fn print_rebase_error(executable_name: &str, branch: &str, upstream_branch: &str
20199
);
202100
}
203101

204-
enum BranchSearchResult {
205-
NotPartOfAnyChain,
206-
Branch(Branch),
207-
}
208-
209-
enum SortBranch {
210-
First,
211-
Last,
212-
Before(Branch),
213-
After(Branch),
214-
}
215-
216102
#[derive(Clone, PartialEq)]
217103
struct Branch {
218104
branch_name: String,
@@ -818,20 +704,6 @@ struct GitChain {
818704
repo: Repository,
819705
}
820706

821-
// Structure to hold merge commit information
822-
#[derive(Debug)]
823-
struct MergeCommitInfo {
824-
message: Option<String>,
825-
stats: Option<MergeStats>,
826-
}
827-
828-
#[derive(Debug)]
829-
struct MergeStats {
830-
files_changed: usize,
831-
insertions: usize,
832-
deletions: usize,
833-
}
834-
835707
impl GitChain {
836708
fn init() -> Result<Self, Error> {
837709
let name_of_current_executable = executable_name();

src/types.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Merge options types
2+
#[derive(Debug, PartialEq, Clone, Copy)]
3+
pub enum SquashedMergeHandling {
4+
// Reset the branch to the parent branch
5+
Reset,
6+
7+
// Skip merging the branch
8+
Skip,
9+
10+
// Force a merge despite the squashed merge detection
11+
Merge,
12+
}
13+
14+
#[derive(Debug, PartialEq, Clone, Copy)]
15+
pub enum ReportLevel {
16+
// Minimal reporting (just success/failure)
17+
Minimal,
18+
19+
// Standard reporting (summary with counts)
20+
Standard,
21+
22+
// Detailed reporting (all actions and their results)
23+
Detailed,
24+
}
25+
26+
pub enum MergeResult {
27+
// Successfully merged with changes
28+
Success(String), // Contains the merge output message
29+
30+
// Already up-to-date, no changes needed
31+
AlreadyUpToDate,
32+
33+
// Merge conflict occurred
34+
Conflict(String), // Contains the conflict message
35+
}
36+
37+
pub struct MergeOptions {
38+
// Skip the merge of the root branch into the first branch
39+
pub ignore_root: bool,
40+
41+
// Git merge options passed to all merge operations
42+
pub merge_flags: Vec<String>,
43+
44+
// Whether to use fork point detection (more accurate but slower)
45+
pub use_fork_point: bool,
46+
47+
// How to handle squashed merges (reset, skip, merge)
48+
pub squashed_merge_handling: SquashedMergeHandling,
49+
50+
// Print verbose output
51+
pub verbose: bool,
52+
53+
// Return to original branch after merging
54+
pub return_to_original: bool,
55+
56+
// Use simple merge mode
57+
pub simple_mode: bool,
58+
59+
// Level of detail in the final report
60+
pub report_level: ReportLevel,
61+
}
62+
63+
impl Default for MergeOptions {
64+
fn default() -> Self {
65+
MergeOptions {
66+
ignore_root: false,
67+
merge_flags: vec![],
68+
use_fork_point: true,
69+
squashed_merge_handling: SquashedMergeHandling::Reset,
70+
verbose: false,
71+
return_to_original: true,
72+
simple_mode: false,
73+
report_level: ReportLevel::Standard,
74+
}
75+
}
76+
}
77+
78+
pub enum BranchSearchResult {
79+
NotPartOfAnyChain,
80+
Branch(crate::Branch),
81+
}
82+
83+
pub enum SortBranch {
84+
First,
85+
Last,
86+
Before(crate::Branch),
87+
After(crate::Branch),
88+
}
89+
90+
// Structure to hold merge commit information
91+
#[derive(Debug)]
92+
pub struct MergeCommitInfo {
93+
pub message: Option<String>,
94+
pub stats: Option<MergeStats>,
95+
}
96+
97+
#[derive(Debug)]
98+
pub struct MergeStats {
99+
pub files_changed: usize,
100+
pub insertions: usize,
101+
pub deletions: usize,
102+
}

0 commit comments

Comments
 (0)