dotstate 0.3.4

A modern, secure, and user-friendly dotfile manager built with Rust
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Git service for repository operations.
//!
//! This module provides a service layer for git operations, abstracting
//! the details of the git implementation from the UI layer.

use crate::config::{Config, RepoMode};
use crate::git::GitManager;
use anyhow::Result;
use std::path::Path;
use tracing::warn;

/// Result of checking for changes that need to be pushed.
#[derive(Debug, Clone, Default)]
pub struct ChangesCheckResult {
    /// Whether there are any changes to push.
    pub has_changes: bool,
    /// List of changed files with their status (e.g., "M filename").
    pub changed_files: Vec<String>,
}

/// Result of a sync operation.
#[derive(Debug)]
pub struct SyncResult {
    /// Whether the sync was successful.
    pub success: bool,
    /// Message describing the result.
    pub message: String,
    /// Number of changes pulled from remote (if any).
    pub pulled_count: Option<usize>,
}

/// Detailed status of the git repository.
#[derive(Debug, Clone, Default)]
pub struct GitStatus {
    /// Whether there are any uncommitted changes.
    pub has_changes: bool,
    /// List of uncommitted changed files.
    pub uncommitted_files: Vec<String>,
    /// Number of commits ahead of remote.
    pub ahead: usize,
    /// Number of commits behind remote.
    pub behind: usize,
    /// Any error message encountered during check.
    pub error: Option<String>,
}

/// Service for git-related operations.
///
/// This service provides a clean interface for git operations without
/// direct dependencies on UI state.
pub struct GitService;

impl GitService {
    /// Check for changes that need to be pushed to remote.
    ///
    /// # Arguments
    ///
    /// * `config` - Application configuration containing repo settings.
    ///
    /// # Returns
    ///
    /// A `ChangesCheckResult` containing information about pending changes.
    #[must_use]
    pub fn check_changes_to_push(config: &Config) -> ChangesCheckResult {
        let mut result = ChangesCheckResult::default();

        // Check if repository is configured and repo exists
        if !config.is_repo_configured() {
            return result;
        }

        let repo_path = &config.repo_path;
        if !repo_path.exists() {
            return result;
        }

        // Open git repository
        let git_mgr = match GitManager::open_or_init(repo_path) {
            Ok(mgr) => mgr,
            Err(_) => return result,
        };

        // Get changed files (this includes both uncommitted and unpushed)
        if let Ok(files) = git_mgr.get_changed_files() {
            result.has_changes = !files.is_empty();
            result.changed_files = files;
        } else {
            // Fallback to old method if get_changed_files fails
            // Check for uncommitted changes
            let has_uncommitted = git_mgr.has_uncommitted_changes().unwrap_or(false);

            // Check for unpushed commits
            let branch = git_mgr
                .get_current_branch()
                .unwrap_or_else(|| "main".to_string());
            let has_unpushed = git_mgr
                .has_unpushed_commits("origin", &branch)
                .unwrap_or(false);

            result.has_changes = has_uncommitted || has_unpushed;
        }

        result
    }

    /// Fetch updates and check comprehensive status (uncommitted + ahead/behind).
    ///
    /// # Arguments
    ///
    /// * `config` - Application configuration.
    ///
    /// # Returns
    ///
    /// A `GitStatus` with detailed repository state.
    pub fn fetch_and_check_status(config: &Config) -> GitStatus {
        let mut status = GitStatus::default();

        // Check if repository is configured and repo exists
        if !config.is_repo_configured() {
            return status;
        }

        let repo_path = &config.repo_path;
        if !repo_path.exists() {
            status.error = Some("Repository path does not exist".to_string());
            return status;
        }

        // Open git repository
        let git_mgr = match GitManager::open_or_init(repo_path) {
            Ok(mgr) => mgr,
            Err(e) => {
                status.error = Some(format!("Failed to open repository: {e}"));
                return status;
            }
        };

        // 1. Check uncommitted changes
        match git_mgr.get_changed_files() {
            Ok(files) => {
                status.has_changes = !files.is_empty();
                status.uncommitted_files = files;
            }
            Err(e) => {
                status.error = Some(format!("Failed to check changes: {e}"));
                return status;
            }
        }

        // 2. Fetch and check ahead/behind (if repo has a remote)
        // For GitHub mode, require token. For Local mode, try without token (SSH or public repo).
        let branch = git_mgr
            .get_current_branch()
            .unwrap_or_else(|| config.default_branch.clone());

        // Check if repo has a remote configured
        let has_remote = git_mgr.has_remote("origin");

        if has_remote {
            let token = match config.repo_mode {
                RepoMode::Local => None, // Local repos use SSH or no auth
                RepoMode::GitHub => config.get_github_token(),
            };

            // Only require token for GitHub mode
            let should_fetch = match config.repo_mode {
                RepoMode::Local => true, // Always try for local repos with remotes
                RepoMode::GitHub => token.is_some(),
            };

            if should_fetch {
                // Try to fetch
                if let Err(e) = git_mgr.fetch("origin", &branch, token.as_deref()) {
                    warn!("Background fetch failed: {}", e);
                    // Don't fail the whole status check, just record error
                    // We can still return uncommitted changes info
                }
            }

            // Check ahead/behind counts (even if fetch failed, we might have cached data)
            match git_mgr.get_ahead_behind("origin", &branch) {
                Ok((ahead, behind)) => {
                    status.ahead = ahead;
                    status.behind = behind;
                }
                Err(e) => {
                    warn!("Failed to get ahead/behind count: {}", e);
                }
            }
        }

        status
    }

    /// Load changed files from the repository.
    ///
    /// # Arguments
    ///
    /// * `repo_path` - Path to the git repository.
    ///
    /// # Returns
    ///
    /// A vector of changed file descriptions.
    #[must_use]
    pub fn load_changed_files(repo_path: &Path) -> Vec<String> {
        if !repo_path.exists() {
            return vec![];
        }

        let git_mgr = match GitManager::open_or_init(repo_path) {
            Ok(mgr) => mgr,
            Err(_) => return vec![],
        };

        git_mgr.get_changed_files().unwrap_or_default()
    }

    /// Get the diff for a specific file.
    ///
    /// # Arguments
    ///
    /// * `repo_path` - Path to the git repository.
    /// * `file_info` - File info string in format "X filename" where X is the status.
    ///
    /// # Returns
    ///
    /// The diff content if available.
    #[must_use]
    pub fn get_diff_for_file(repo_path: &Path, file_info: &str) -> Option<String> {
        // Format is "X filename"
        let parts: Vec<&str> = file_info.splitn(2, ' ').collect();
        if parts.len() != 2 {
            return None;
        }
        let path_str = parts[1].trim();

        let git_mgr = GitManager::open_or_init(repo_path).ok()?;
        git_mgr.get_diff_for_file(path_str).ok().flatten()
    }

    /// Perform a sync operation: commit -> pull with rebase -> push.
    ///
    /// # Arguments
    ///
    /// * `config` - Application configuration.
    ///
    /// # Returns
    ///
    /// A `SyncResult` describing the outcome of the operation.
    pub fn sync(config: &Config) -> SyncResult {
        // Check if repository is configured
        if !config.is_repo_configured() {
            warn!("Sync attempted but repository not configured");
            return SyncResult {
                success: false,
                message: "Error: Repository not configured.\n\n\
                    Please set up your repository first from the main menu."
                    .to_string(),
                pulled_count: None,
            };
        }

        let repo_path = &config.repo_path;

        // Check if repo exists
        if !repo_path.exists() {
            warn!("Sync attempted but repository not found: {:?}", repo_path);
            return SyncResult {
                success: false,
                message: format!(
                    "Error: Repository not found at {repo_path:?}\n\n\
                    Please sync some files first."
                ),
                pulled_count: None,
            };
        }

        // Open git repository
        let git_mgr = match GitManager::open_or_init(repo_path) {
            Ok(mgr) => mgr,
            Err(e) => {
                return SyncResult {
                    success: false,
                    message: format!("Error: Failed to open repository: {e}"),
                    pulled_count: None,
                }
            }
        };

        let branch = git_mgr
            .get_current_branch()
            .unwrap_or_else(|| config.default_branch.clone());

        // Get token based on repo mode
        let token_string = match config.repo_mode {
            RepoMode::Local => None,
            RepoMode::GitHub => config.get_github_token(),
        };
        let token = token_string.as_deref();

        // Only require token for GitHub mode
        if matches!(config.repo_mode, RepoMode::GitHub) && token.is_none() {
            return SyncResult {
                success: false,
                message: "Error: GitHub token not found.\n\n\
                    Please provide a GitHub token using one of these methods:\n\n\
                    1. Set the DOTSTATE_GITHUB_TOKEN environment variable:\n\
                       export DOTSTATE_GITHUB_TOKEN=ghp_your_token_here\n\n\
                    2. Configure it in the TUI by going to the main menu\n\n\
                    Create a token at: https://github.com/settings/tokens\n\
                    Required scope: repo (full control of private repositories)"
                    .to_string(),
                pulled_count: None,
            };
        }

        // Step 1: Only commit if there are uncommitted changes
        // This prevents creating empty commits on retry after a failed push
        let has_changes = git_mgr.has_uncommitted_changes().unwrap_or(false);
        let mut made_commit = false;

        if has_changes {
            let commit_msg = git_mgr
                .generate_commit_message()
                .unwrap_or_else(|_| "Update dotfiles".to_string());

            if let Err(e) = git_mgr.commit_all(&commit_msg) {
                return SyncResult {
                    success: false,
                    message: Self::format_error_chain("Failed to commit changes", &e),
                    pulled_count: None,
                };
            }
            made_commit = true;
        }

        // Step 2: Pull with rebase
        let pulled_count = match git_mgr.pull_with_rebase("origin", &branch, token) {
            Ok(count) => count,
            Err(e) => {
                // Pull/rebase failed - the rebase.abort() inside pull_with_rebase should
                // have restored the repo state. Try to reset our commit to preserve user's changes.
                if made_commit {
                    if let Err(reset_err) = git_mgr.reset_soft_head() {
                        // reset_soft_head failed - repo might be in a bad state (mid-rebase)
                        // Try cleanup as fallback (this will lose changes but at least recover)
                        warn!("Failed to reset commit: {}, trying cleanup", reset_err);
                        if let Err(cleanup_err) = git_mgr.cleanup_failed_operation(&branch) {
                            warn!("Failed to cleanup after pull failure: {}", cleanup_err);
                            return SyncResult {
                                success: false,
                                message: format!(
                                    "{}\n\nRepository may be in an inconsistent state.\n\
                                    Run 'git status' and 'git rebase --abort' if needed.",
                                    Self::format_error_chain("Failed to pull from remote", &e)
                                ),
                                pulled_count: None,
                            };
                        }
                        // Cleanup succeeded but changes were lost
                        return SyncResult {
                            success: false,
                            message: format!(
                                "{}\n\nYour changes could not be preserved.\n\
                                The repository has been reset to a clean state.",
                                Self::format_error_chain("Failed to pull from remote", &e)
                            ),
                            pulled_count: None,
                        };
                    }
                }
                return SyncResult {
                    success: false,
                    message: if made_commit {
                        format!(
                            "{}\n\nThe commit has been undone. Your changes are still staged.\n\
                            Fix any issues and try syncing again.",
                            Self::format_error_chain("Failed to pull from remote", &e)
                        )
                    } else {
                        Self::format_error_chain("Failed to pull from remote", &e)
                    },
                    pulled_count: None,
                };
            }
        };

        // Step 3: Push to remote
        if let Err(e) = git_mgr.push("origin", &branch, token) {
            // Push failed - reset the commit so user can fix the issue and retry
            // This prevents the bad commit from blocking future pushes
            if made_commit {
                if let Err(reset_err) = git_mgr.reset_soft_head() {
                    warn!("Failed to reset commit after push failure: {}", reset_err);
                    // Include reset failure in the error message
                    return SyncResult {
                        success: false,
                        message: format!(
                            "{}\n\nAdditionally, failed to reset the commit: {}\n\
                            You may need to manually run: git reset --soft HEAD~1",
                            Self::format_error_chain("Failed to push to remote", &e),
                            reset_err
                        ),
                        pulled_count: Some(pulled_count),
                    };
                }
            }
            return SyncResult {
                success: false,
                message: format!(
                    "{}\n\nThe commit has been undone. Your changes are still staged.\n\
                    Fix the issue and try syncing again.",
                    Self::format_error_chain("Failed to push to remote", &e)
                ),
                pulled_count: Some(pulled_count),
            };
        }

        // Success! Build the success message
        let mut success_msg = format!(
            "✓ Successfully synced with remote!\n\n\
            Branch: {branch}\n\
            Repository: {repo_path:?}"
        );

        if pulled_count > 0 {
            success_msg.push_str(&format!("\n\nPulled {pulled_count} change(s) from remote."));

            // Step 4: Ensure symlinks for any new files pulled from remote
            // This is efficient - only creates symlinks for missing files
            use crate::services::ProfileService;
            match ProfileService::ensure_profile_symlinks(
                repo_path,
                &config.active_profile,
                config.backup_enabled,
            ) {
                Ok((created, _skipped, errors)) => {
                    if created > 0 {
                        success_msg
                            .push_str(&format!("\nCreated {created} symlink(s) for new files."));
                    }
                    if !errors.is_empty() {
                        success_msg.push_str(&format!(
                            "\n\nWarning: {} error(s) creating symlinks:\n{}",
                            errors.len(),
                            errors.join("\n")
                        ));
                    }
                }
                Err(e) => {
                    warn!("Failed to ensure symlinks after pull: {}", e);
                    success_msg.push_str(&format!(
                        "\n\nWarning: Failed to create symlinks for new files: {e}"
                    ));
                }
            }

            // Also ensure common symlinks
            match ProfileService::ensure_common_symlinks(repo_path, config.backup_enabled) {
                Ok((created, _skipped, errors)) => {
                    if created > 0 {
                        success_msg.push_str(&format!("\nCreated {created} common symlink(s)."));
                    }
                    if !errors.is_empty() {
                        success_msg.push_str(&format!(
                            "\n\nWarning: {} error(s) creating common symlinks:\n{}",
                            errors.len(),
                            errors.join("\n")
                        ));
                    }
                }
                Err(e) => {
                    warn!("Failed to ensure common symlinks after pull: {}", e);
                    success_msg.push_str(&format!(
                        "\n\nWarning: Failed to create common symlinks: {e}"
                    ));
                }
            }
        } else {
            success_msg.push_str("\n\nNo changes pulled from remote.");
        }

        SyncResult {
            success: true,
            message: success_msg,
            pulled_count: Some(pulled_count),
        }
    }

    /// Format an error with its full chain for display.
    fn format_error_chain(context: &str, error: &anyhow::Error) -> String {
        let mut msg = format!("Error: {context}: {error}");
        for cause in error.chain().skip(1) {
            msg.push_str(&format!("\n  Caused by: {cause}"));
        }
        msg
    }

    /// Clone or open a repository.
    ///
    /// # Arguments
    ///
    /// * `remote_url` - The remote URL to clone from.
    /// * `local_path` - The local path to clone to.
    /// * `token` - Optional authentication token.
    ///
    /// # Returns
    ///
    /// A tuple of (`GitManager`, `was_existing`).
    pub fn clone_or_open(
        remote_url: &str,
        local_path: &Path,
        token: Option<&str>,
    ) -> Result<(GitManager, bool)> {
        GitManager::clone_or_open(remote_url, local_path, token)
    }

    /// Initialize a new repository or open existing one.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to initialize or open.
    ///
    /// # Returns
    ///
    /// A `GitManager` instance.
    pub fn open_or_init(path: &Path) -> Result<GitManager> {
        GitManager::open_or_init(path)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    #[test]
    fn test_check_changes_unconfigured() {
        let config = Config::default();
        let result = GitService::check_changes_to_push(&config);
        assert!(!result.has_changes);
        assert!(result.changed_files.is_empty());
    }

    #[test]
    fn test_load_changed_files_nonexistent() {
        let result = GitService::load_changed_files(&PathBuf::from("/nonexistent/path"));
        assert!(result.is_empty());
    }

    #[test]
    fn test_get_diff_invalid_format() {
        let result = GitService::get_diff_for_file(&PathBuf::from("/tmp"), "invalid");
        assert!(result.is_none());
    }
}