eazygit 0.5.1

A fast TUI for Git with staging, conflicts, rebase, and palette-first UX
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Rebase command implementations.

use crate::commands::{Command, CommandResult};
use crate::services::GitService;
use crate::app::{AppState, Action, reducer};
use crate::app::rebase::{RebaseOperations, operations::{RebaseResult, RebaseRecovery}};
use crate::errors::CommandError;
use tracing::instrument;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app::AppState;
    use crate::services::GitService;
    use crate::commands::CommandResult;
    use std::sync::Arc;

    fn create_test_state() -> AppState {
        let mut state = AppState::new();
        state.repo_path = ".".to_string();
        state
    }

    fn create_mock_git_service() -> Arc<GitService> {
        Arc::new(GitService::new())
    }

    #[test]
    fn test_rebase_save_and_run_command_creation() {
        let command = RebaseSaveAndRunCommand;
        // Command struct is just a marker, no fields to test
        assert!(true); // Placeholder test
    }

    #[test]
    fn test_rebase_continue_command_creation() {
        let command = RebaseContinueCommand;
        assert!(true); // Placeholder test
    }

    #[test]
    fn test_rebase_skip_command_creation() {
        let command = RebaseSkipCommand;
        assert!(true); // Placeholder test
    }

    #[test]
    fn test_rebase_abort_command_creation() {
        let command = RebaseAbortCommand;
        assert!(true); // Placeholder test
    }

    #[test]
    fn test_rebase_resolve_conflicts_command_creation() {
        let command = RebaseResolveConflictsCommand;
        assert!(true); // Placeholder test
    }

    #[test]
    fn test_rebase_abort_from_conflict_command_creation() {
        let command = RebaseAbortFromConflictCommand;
        assert!(true); // Placeholder test
    }

    #[test]
    fn test_rebase_continue_interrupted_command_creation() {
        let command = RebaseContinueInterruptedCommand;
        assert!(true); // Placeholder test
    }

    #[test]
    fn test_rebase_abort_interrupted_command_creation() {
        let command = RebaseAbortInterruptedCommand;
        assert!(true); // Placeholder test
    }
}

/// Command to save and run interactive rebase
pub struct RebaseSaveAndRunCommand;

/// Command to continue active rebase to next step
pub struct RebaseContinueCommand;

/// Command to skip current commit during active rebase
pub struct RebaseSkipCommand;

/// Command to abort current rebase
pub struct RebaseAbortCommand;

/// Command to resolve conflicts and continue rebase
pub struct RebaseResolveConflictsCommand;

/// Command to abort rebase from conflict state
pub struct RebaseAbortFromConflictCommand;

/// Command to continue interrupted rebase
pub struct RebaseContinueInterruptedCommand;

/// Command to abort interrupted rebase
pub struct RebaseAbortInterruptedCommand;

impl Command for RebaseSaveAndRunCommand {
    #[instrument(skip(self, git, state))]
    fn execute(
        &self,
        git: &GitService,
        state: &AppState,
    ) -> Result<CommandResult, CommandError> {
        let mut new_state = state.clone();

        // Execute rebase using the new operations system
        match RebaseOperations::execute_rebase(git, &state.repo_path, &mut new_state.rebase_session) {
            Ok(result) => {
                match result {
                    RebaseResult::Success => {
                        // Rebase started successfully
                        new_state = reducer(new_state, Action::SetFeedback(Some("Rebase started successfully".to_string())));
                        new_state.rebase_editor_open = false;
                    }
                    RebaseResult::Conflicts { .. } => {
                        // Conflicts detected
                        new_state = reducer(new_state, Action::SetFeedback(Some("Rebase started but conflicts detected".to_string())));
                        new_state.rebase_editor_open = false;
                    }
                    _ => {
                        // Other results not expected during initial execution
                        new_state = reducer(new_state, Action::SetStatusError(Some("Unexpected rebase result".to_string())));
                    }
                }
                Ok(CommandResult::StateUpdate(new_state))
            }
            Err(e) => {
                // Handle validation or execution errors
                let error_msg = format!("Failed to start rebase: {}", e);
                new_state = reducer(new_state, Action::SetStatusError(Some(error_msg.clone())));
                Err(CommandError::GitError(crate::errors::GitError::OperationError(anyhow::anyhow!(error_msg))))
            }
        }
    }
}

impl Command for RebaseContinueCommand {
    #[instrument(skip(self, git, state))]
    fn execute(
        &self,
        git: &GitService,
        state: &AppState,
    ) -> Result<CommandResult, CommandError> {
        let mut new_state = state.clone();

        match RebaseOperations::continue_rebase(git, &state.repo_path, &mut new_state.rebase_session) {
            Ok(result) => {
                match result {
                    RebaseResult::Success => {
                        new_state = reducer(new_state, Action::SetFeedback(Some("Rebase continued successfully".to_string())));
                    }
                    RebaseResult::Completed => {
                        new_state = reducer(new_state, Action::SetFeedback(Some("Rebase completed successfully".to_string())));
                        // Reset session to planning state
                        new_state.rebase_session = crate::app::rebase::RebaseSession::default();
                    }
                    RebaseResult::Conflicts { .. } => {
                        new_state = reducer(new_state, Action::SetFeedback(Some("Conflicts detected during continue".to_string())));
                    }
                    _ => {
                        new_state = reducer(new_state, Action::SetStatusError(Some("Unexpected rebase result during continue".to_string())));
                    }
                }
                Ok(CommandResult::StateUpdate(new_state))
            }
            Err(e) => {
                let error_msg = format!("Failed to continue rebase: {}", e);
                new_state = reducer(new_state, Action::SetStatusError(Some(error_msg.clone())));
                Err(CommandError::GitError(crate::errors::GitError::OperationError(anyhow::anyhow!(error_msg))))
            }
        }
    }
}

impl Command for RebaseSkipCommand {
    #[instrument(skip(self, git, state))]
    fn execute(
        &self,
        git: &GitService,
        state: &AppState,
    ) -> Result<CommandResult, CommandError> {
        let mut new_state = state.clone();

        match RebaseOperations::skip_rebase_step(git, &state.repo_path, &mut new_state.rebase_session) {
            Ok(result) => {
                match result {
                    RebaseResult::Success => {
                        new_state = reducer(new_state, Action::SetFeedback(Some("Skipped current commit, rebase continued".to_string())));
                    }
                    RebaseResult::Completed => {
                        new_state = reducer(new_state, Action::SetFeedback(Some("Rebase completed successfully".to_string())));
                        new_state.rebase_session = crate::app::rebase::RebaseSession::default();
                    }
                    RebaseResult::Conflicts { .. } => {
                        new_state = reducer(new_state, Action::SetFeedback(Some("Conflicts detected after skipping".to_string())));
                    }
                    _ => {
                        new_state = reducer(new_state, Action::SetStatusError(Some("Unexpected rebase result during skip".to_string())));
                    }
                }
                Ok(CommandResult::StateUpdate(new_state))
            }
            Err(e) => {
                let error_msg = format!("Failed to skip rebase step: {}", e);
                new_state = reducer(new_state, Action::SetStatusError(Some(error_msg.clone())));
                Err(CommandError::GitError(crate::errors::GitError::OperationError(anyhow::anyhow!(error_msg))))
            }
        }
    }
}

impl Command for RebaseAbortCommand {
    #[instrument(skip(self, git, state))]
    fn execute(
        &self,
        git: &GitService,
        state: &AppState,
    ) -> Result<CommandResult, CommandError> {
        let mut new_state = state.clone();

        match RebaseOperations::abort_rebase(git, &state.repo_path, &mut new_state.rebase_session) {
            Ok(result) => {
                match result {
                    RebaseResult::Aborted => {
                        new_state = reducer(new_state, Action::SetFeedback(Some("Rebase aborted successfully".to_string())));
                        // Reset session to planning state
                        new_state.rebase_session = crate::app::rebase::RebaseSession::default();
                    }
                    _ => {
                        new_state = reducer(new_state, Action::SetStatusError(Some("Unexpected rebase result during abort".to_string())));
                    }
                }
                Ok(CommandResult::StateUpdate(new_state))
            }
            Err(e) => {
                let error_msg = format!("Failed to abort rebase: {}", e);
                new_state = reducer(new_state, Action::SetStatusError(Some(error_msg.clone())));
                Err(CommandError::GitError(crate::errors::GitError::OperationError(anyhow::anyhow!(error_msg))))
            }
        }
    }
}

impl Command for RebaseResolveConflictsCommand {
    #[instrument(skip(self, git, state))]
    fn execute(
        &self,
        git: &GitService,
        state: &AppState,
    ) -> Result<CommandResult, CommandError> {
        let mut new_state = state.clone();

        // Check if there are any uncommitted changes (conflicts should be resolved)
        match git.status_porcelain(&state.repo_path) {
            Ok(status) => {
                // Check if there are still conflict markers
                if status.contains("U ") || status.contains("AA ") || status.contains("DD ") {
                    new_state = reducer(new_state, Action::SetStatusError(Some("Conflicts still exist. Please resolve all conflicts before continuing.".to_string())));
                    return Ok(CommandResult::StateUpdate(new_state));
                }

                // Check if there are staged changes (user should have staged resolved files)
                if !status.lines().any(|line| line.starts_with("M ") || line.starts_with("A ") || line.starts_with("D ")) {
                    new_state = reducer(new_state, Action::SetStatusError(Some("No staged changes found. Please stage your conflict resolutions.".to_string())));
                    return Ok(CommandResult::StateUpdate(new_state));
                }

                // Conflicts appear resolved, try to continue rebase
                match RebaseOperations::continue_rebase(git, &state.repo_path, &mut new_state.rebase_session) {
                    Ok(result) => {
                        match result {
                            RebaseResult::Success => {
                                new_state = reducer(new_state, Action::SetFeedback(Some("Conflicts resolved, rebase continued".to_string())));
                            }
                            RebaseResult::Completed => {
                                new_state = reducer(new_state, Action::SetFeedback(Some("Rebase completed successfully after conflict resolution".to_string())));
                                new_state.rebase_session = crate::app::rebase::RebaseSession::default();
                            }
                            RebaseResult::Conflicts { .. } => {
                                new_state = reducer(new_state, Action::SetFeedback(Some("Additional conflicts detected".to_string())));
                            }
                            _ => {
                                new_state = reducer(new_state, Action::SetStatusError(Some("Unexpected rebase result after conflict resolution".to_string())));
                            }
                        }
                        Ok(CommandResult::StateUpdate(new_state))
                    }
                    Err(e) => {
                        let error_msg = format!("Failed to continue rebase after conflict resolution: {}", e);
                        new_state = reducer(new_state, Action::SetStatusError(Some(error_msg.clone())));
                        Err(CommandError::GitError(crate::errors::GitError::OperationError(anyhow::anyhow!(error_msg))))
                    }
                }
            }
            Err(e) => {
                let error_msg = format!("Failed to check repository status: {}", e);
                new_state = reducer(new_state, Action::SetStatusError(Some(error_msg.clone())));
                Err(CommandError::GitError(crate::errors::GitError::OperationError(anyhow::anyhow!(error_msg))))
            }
        }
    }
}

impl Command for RebaseAbortFromConflictCommand {
    #[instrument(skip(self, git, state))]
    fn execute(
        &self,
        git: &GitService,
        state: &AppState,
    ) -> Result<CommandResult, CommandError> {
        let mut new_state = state.clone();

        match RebaseOperations::abort_rebase(git, &state.repo_path, &mut new_state.rebase_session) {
            Ok(result) => {
                match result {
                    RebaseResult::Aborted => {
                        new_state = reducer(new_state, Action::SetFeedback(Some("Rebase aborted from conflict state".to_string())));
                        // Reset session to planning state
                        new_state.rebase_session = crate::app::rebase::RebaseSession::default();
                    }
                    _ => {
                        new_state = reducer(new_state, Action::SetStatusError(Some("Unexpected rebase result during abort from conflict".to_string())));
                    }
                }
                Ok(CommandResult::StateUpdate(new_state))
            }
            Err(e) => {
                let error_msg = format!("Failed to abort rebase from conflict state: {}", e);
                new_state = reducer(new_state, Action::SetStatusError(Some(error_msg.clone())));
                Err(CommandError::GitError(crate::errors::GitError::OperationError(anyhow::anyhow!(error_msg))))
            }
        }
    }
}

impl Command for RebaseContinueInterruptedCommand {
    #[instrument(skip(self, git, state))]
    fn execute(
        &self,
        git: &GitService,
        state: &AppState,
    ) -> Result<CommandResult, CommandError> {
        let mut new_state = state.clone();

        match RebaseRecovery::continue_interrupted_rebase(git, &state.repo_path, &mut new_state.rebase_session) {
            Ok(result) => {
                match result {
                    RebaseResult::Success => {
                        new_state = reducer(new_state, Action::SetFeedback(Some("Continued interrupted rebase".to_string())));
                        new_state.rebase_recovery_open = false;
                    }
                    RebaseResult::Completed => {
                        new_state = reducer(new_state, Action::SetFeedback(Some("Rebase completed successfully".to_string())));
                        new_state.rebase_session = crate::app::rebase::RebaseSession::default();
                        new_state.rebase_recovery_open = false;
                    }
                    RebaseResult::Conflicts { .. } => {
                        new_state = reducer(new_state, Action::SetFeedback(Some("Conflicts detected in interrupted rebase".to_string())));
                        new_state.rebase_recovery_open = false;
                    }
                    _ => {
                        new_state = reducer(new_state, Action::SetStatusError(Some("Unexpected rebase result during recovery".to_string())));
                    }
                }
                Ok(CommandResult::StateUpdate(new_state))
            }
            Err(e) => {
                let error_msg = format!("Failed to continue interrupted rebase: {}", e);
                new_state = reducer(new_state, Action::SetStatusError(Some(error_msg.clone())));
                Err(CommandError::GitError(crate::errors::GitError::OperationError(anyhow::anyhow!(error_msg))))
            }
        }
    }
}

impl Command for RebaseAbortInterruptedCommand {
    #[instrument(skip(self, git, state))]
    fn execute(
        &self,
        git: &GitService,
        state: &AppState,
    ) -> Result<CommandResult, CommandError> {
        let mut new_state = state.clone();

        match RebaseRecovery::abort_interrupted_rebase(git, &state.repo_path) {
            Ok(()) => {
                new_state = reducer(new_state, Action::SetFeedback(Some("Aborted interrupted rebase".to_string())));
                // Reset session to planning state
                new_state.rebase_session = crate::app::rebase::RebaseSession::default();
                new_state.rebase_recovery_open = false;
                Ok(CommandResult::StateUpdate(new_state))
            }
            Err(e) => {
                let error_msg = format!("Failed to abort interrupted rebase: {}", e);
                new_state = reducer(new_state, Action::SetStatusError(Some(error_msg.clone())));
                Err(CommandError::GitError(crate::errors::GitError::OperationError(anyhow::anyhow!(error_msg))))
            }
        }
    }
}