ag-git 0.15.1

Agentty is an ADE (Agentic Development Environment) for structured, controllable AI-assisted software development.
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
use std::fs;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use std::process::Output;
use std::time::Duration;

use tokio::task::spawn_blocking;

use super::error::GitError;
use super::repo::{
    command_output_detail, resolve_git_dir, run_git_command_output_sync,
    run_git_command_output_with_env_sync, run_git_command_sync,
};
use crate::{Sleeper, ThreadSleeper};

pub(super) const GIT_INDEX_LOCK_RETRY_ATTEMPTS: usize = 5;
pub(super) const GIT_INDEX_LOCK_RETRY_DELAY: Duration = Duration::from_millis(100);

/// Executes git commands for rebase operations.
#[cfg_attr(test, mockall::automock)]
trait GitCommandRunner: Send + Sync {
    /// Runs a git command in `repo_path` with environment overrides.
    fn run_git_command_output_with_env(
        &self,
        repo_path: &Path,
        args: &[String],
        environment: &[(String, String)],
    ) -> Result<Output, GitError>;
}

/// Removes stale rebase metadata through an injectable filesystem boundary.
#[cfg_attr(test, mockall::automock)]
trait RebaseMetadataCleaner: Send + Sync {
    /// Removes exact rebase metadata entries under the resolved git directory.
    fn clean_stale_metadata(&self, repo_path: &Path) -> Result<bool, GitError>;
}

/// Rebase metadata cleaner backed by the local filesystem.
struct FilesystemRebaseMetadataCleaner;

impl RebaseMetadataCleaner for FilesystemRebaseMetadataCleaner {
    fn clean_stale_metadata(&self, repo_path: &Path) -> Result<bool, GitError> {
        clean_stale_rebase_metadata(repo_path)
    }
}

/// Git command runner backed by process execution.
struct ProcessGitCommandRunner;

impl GitCommandRunner for ProcessGitCommandRunner {
    fn run_git_command_output_with_env(
        &self,
        repo_path: &Path,
        args: &[String],
        environment: &[(String, String)],
    ) -> Result<Output, GitError> {
        let args = args.iter().map(String::as_str).collect::<Vec<_>>();
        let environment = environment
            .iter()
            .map(|(key, value)| (key.as_str(), value.as_str()))
            .collect::<Vec<_>>();

        run_git_command_output_with_env_sync(repo_path, &args, &environment)
    }
}

/// Result of attempting a rebase step.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RebaseStepResult {
    /// Rebase step completed successfully.
    Completed,
    /// Rebase step stopped because of merge conflicts.
    Conflict {
        /// Git diagnostic describing the conflict state.
        detail: String,
    },
}

/// Git operation metadata that marks a worktree as unsafe for branch pushes.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum InProgressGitOperation {
    /// A cherry-pick is in progress.
    CherryPick,
    /// A merge is in progress.
    Merge,
    /// A rebase is in progress.
    Rebase,
    /// A revert is in progress.
    Revert,
}

impl InProgressGitOperation {
    /// Returns an indefinite article plus the operation name for user-facing
    /// status text.
    pub fn article_name(self) -> &'static str {
        match self {
            Self::CherryPick => "a cherry-pick",
            Self::Merge => "a merge",
            Self::Rebase => "a rebase",
            Self::Revert => "a revert",
        }
    }

    /// Returns the operation name for user-facing status text.
    pub fn name(self) -> &'static str {
        match self {
            Self::CherryPick => "cherry-pick",
            Self::Merge => "merge",
            Self::Rebase => "rebase",
            Self::Revert => "revert",
        }
    }
}

/// Rebases the current branch onto `target_branch`.
///
/// If the rebase fails due to conflict, this function aborts it immediately so
/// the repository does not remain in an in-progress rebase state.
///
/// # Arguments
/// * `repo_path` - Path to the git repository or worktree
/// * `target_branch` - Branch to rebase onto (e.g., `main`)
///
/// # Returns
/// Ok(()) on success.
///
/// # Errors
/// Returns a [`GitError`] if rebase fails, or aborting a conflicted rebase
/// also fails.
pub(crate) async fn rebase(repo_path: PathBuf, target_branch: String) -> Result<(), GitError> {
    match rebase_start(repo_path.clone(), target_branch.clone()).await? {
        RebaseStepResult::Completed => Ok(()),
        RebaseStepResult::Conflict { detail } => {
            let abort_suffix = match abort_rebase(repo_path).await {
                Ok(()) => String::new(),
                Err(error) => format!(" {error}"),
            };

            Err(GitError::CommandFailed {
                command: "git rebase".to_string(),
                stderr: format!("Failed to rebase onto {target_branch}: {detail}.{abort_suffix}"),
            })
        }
    }
}

/// Rebases the current branch onto `target_branch`.
///
/// Returns a conflict outcome when the rebase stops for manual resolution.
///
/// # Arguments
/// * `repo_path` - Path to the git repository or worktree
/// * `target_branch` - Branch to rebase onto (e.g., `main`)
///
/// # Returns
/// A [`RebaseStepResult`] describing whether the rebase completed or
/// encountered conflicts.
///
/// # Errors
/// Returns a [`GitError`] for non-conflict git failures.
pub(crate) async fn rebase_start(
    repo_path: PathBuf,
    target_branch: String,
) -> Result<RebaseStepResult, GitError> {
    spawn_blocking(move || {
        let rebase_args = ["rebase", target_branch.as_str()];
        run_rebase_step(&repo_path, &rebase_args, "git rebase", |detail| {
            format!("Failed to rebase onto {target_branch}: {detail}.")
        })
    })
    .await?
}

/// Starts a rebase that moves commits after `old_base` onto `new_base`.
///
/// This is used for stacked sessions to drop commits that came from a parent
/// branch after that parent has moved or squash-merged into its own base.
///
/// # Arguments
/// * `repo_path` - Path to the git repository or worktree.
/// * `new_base` - Ref that should become the new base of replayed commits.
/// * `old_base` - Commit/ref whose ancestors should be left behind.
///
/// # Returns
/// A [`RebaseStepResult`] describing whether the rebase completed or
/// encountered conflicts.
///
/// # Errors
/// Returns a [`GitError`] for non-conflict git failures.
pub(crate) async fn rebase_onto_start(
    repo_path: PathBuf,
    new_base: String,
    old_base: String,
) -> Result<RebaseStepResult, GitError> {
    spawn_blocking(move || {
        let rebase_args = ["rebase", "--onto", new_base.as_str(), old_base.as_str()];
        run_rebase_step(&repo_path, &rebase_args, "git rebase --onto", |detail| {
            format!("Failed to rebase onto {new_base} after {old_base}: {detail}.")
        })
    })
    .await?
}

/// Continues an in-progress rebase.
///
/// # Arguments
/// * `repo_path` - Path to the git repository or worktree
///
/// # Returns
/// A [`RebaseStepResult`] describing whether the rebase completed or
/// encountered conflicts.
///
/// # Errors
/// Returns a [`GitError`] for non-conflict git failures.
pub(crate) async fn rebase_continue(repo_path: PathBuf) -> Result<RebaseStepResult, GitError> {
    spawn_blocking(move || {
        let output = run_git_command_with_index_lock_retry(
            &repo_path,
            &["rebase", "--continue"],
            &[("GIT_EDITOR", ":"), ("GIT_SEQUENCE_EDITOR", ":")],
        )?;

        if output.status.success() {
            return Ok(RebaseStepResult::Completed);
        }

        let detail = command_output_detail(&output.stdout, &output.stderr);
        if is_rebase_conflict(&detail) {
            return Ok(RebaseStepResult::Conflict { detail });
        }

        Err(GitError::CommandFailed {
            command: "git rebase --continue".to_string(),
            stderr: format!("Failed to continue rebase: {detail}."),
        })
    })
    .await?
}

/// Aborts an in-progress rebase.
///
/// When Git reports a known stale or inactive rebase state, this removes only
/// `rebase-merge` and `rebase-apply` under the resolved git directory. Other
/// failures are returned unchanged with their command output.
///
/// # Arguments
/// * `repo_path` - Path to the git repository or worktree
///
/// # Returns
/// Ok(()) on success.
///
/// # Errors
/// Returns a [`GitError`] when `git rebase --abort` cannot be executed.
pub(crate) async fn abort_rebase(repo_path: PathBuf) -> Result<(), GitError> {
    spawn_blocking(move || {
        let command_runner = ProcessGitCommandRunner;
        let metadata_cleaner = FilesystemRebaseMetadataCleaner;
        let sleeper = ThreadSleeper;

        abort_rebase_with_dependencies(&repo_path, &command_runner, &sleeper, &metadata_cleaner)
    })
    .await?
}

/// Returns whether a rebase is currently in progress in the repository or
/// worktree.
///
/// # Arguments
/// * `repo_path` - Path to the git repository or worktree
///
/// # Returns
/// `true` when `.git/rebase-merge` or `.git/rebase-apply` exists, `false`
/// otherwise.
///
/// # Errors
/// Returns a [`GitError`] when the git directory cannot be resolved.
pub(crate) async fn is_rebase_in_progress(repo_path: PathBuf) -> Result<bool, GitError> {
    spawn_blocking(move || -> Result<bool, GitError> {
        let git_dir = resolve_git_dir(&repo_path)
            .ok_or_else(|| GitError::OutputParse("Failed to resolve git directory".to_string()))?;

        Ok(has_rebase_metadata(&git_dir))
    })
    .await?
}

/// Returns the first detected in-progress git operation in `repo_path`.
///
/// # Arguments
/// * `repo_path` - Path to the git repository or worktree
///
/// # Returns
/// An operation when rebase, merge, cherry-pick, or revert metadata exists.
///
/// # Errors
/// Returns a [`GitError`] when the git directory cannot be resolved.
pub(crate) async fn in_progress_operation(
    repo_path: PathBuf,
) -> Result<Option<InProgressGitOperation>, GitError> {
    spawn_blocking(move || in_progress_operation_sync(&repo_path)).await?
}

fn in_progress_operation_sync(
    repo_path: &Path,
) -> Result<Option<InProgressGitOperation>, GitError> {
    let git_dir = resolve_git_dir(repo_path)
        .ok_or_else(|| GitError::OutputParse("Failed to resolve git directory".to_string()))?;
    if has_rebase_metadata(&git_dir) {
        return Ok(Some(InProgressGitOperation::Rebase));
    }
    if git_dir.join("MERGE_HEAD").exists() {
        return Ok(Some(InProgressGitOperation::Merge));
    }
    if git_dir.join("CHERRY_PICK_HEAD").exists() {
        return Ok(Some(InProgressGitOperation::CherryPick));
    }
    if git_dir.join("REVERT_HEAD").exists() {
        return Ok(Some(InProgressGitOperation::Revert));
    }

    Ok(None)
}

fn has_rebase_metadata(git_dir: &Path) -> bool {
    let rebase_merge = git_dir.join("rebase-merge");
    let rebase_apply = git_dir.join("rebase-apply");

    rebase_merge.exists() || rebase_apply.exists()
}

/// Returns whether unresolved paths still exist in the index.
///
/// # Arguments
/// * `repo_path` - Path to the git repository or worktree
///
/// # Returns
/// `true` when unresolved paths exist, `false` otherwise.
///
/// # Errors
/// Returns a [`GitError`] when conflicted files cannot be queried.
pub(crate) async fn has_unmerged_paths(repo_path: PathBuf) -> Result<bool, GitError> {
    let conflicted_files = list_conflicted_files(repo_path).await?;

    Ok(!conflicted_files.is_empty())
}

/// Returns which of the given `paths` still contain git conflict markers
/// (`<<<<<<<`) in their staged content.
///
/// Uses `git grep --cached -l` to search indexed content directly, so it
/// detects files that were staged via `git add` while still containing
/// unresolved conflict markers. The search is scoped to `paths` to avoid
/// false positives from files that legitimately contain `<<<<<<<` (e.g.
/// test fixtures or documentation).
///
/// # Arguments
/// * `repo_path` - Path to the git repository or worktree
/// * `paths` - Relative file paths to inspect (typically the files that were
///   involved in the current conflict)
///
/// # Returns
/// The subset of `paths` whose staged content contains lines starting with
/// `<<<<<<<`. Returns an empty list when no matches are found or when
/// `paths` is empty.
///
/// # Errors
/// Returns a [`GitError`] if `git grep` cannot be executed or exits with an
/// unexpected error code. An exit code of `1` (no matches) is treated as
/// success with an empty result.
pub(crate) async fn list_staged_conflict_marker_files(
    repo_path: PathBuf,
    paths: Vec<String>,
) -> Result<Vec<String>, GitError> {
    if paths.is_empty() {
        return Ok(vec![]);
    }

    spawn_blocking(move || -> Result<Vec<String>, GitError> {
        let mut grep_arguments = vec!["grep", "--cached", "-l", "^<<<<<<<", "--"];
        let path_arguments: Vec<&str> = paths.iter().map(String::as_str).collect();
        grep_arguments.extend(path_arguments);
        let output = run_git_command_output_sync(&repo_path, &grep_arguments)?;

        // git grep exits with 1 when no matches are found.
        let exit_code = output.status.code().unwrap_or(2);
        if !output.status.success() && exit_code != 1 {
            let detail = command_output_detail(&output.stdout, &output.stderr);

            return Err(GitError::CommandFailed {
                command: "git grep".to_string(),
                stderr: format!("Failed to check for staged conflict markers: {detail}"),
            });
        }

        let files = String::from_utf8_lossy(&output.stdout)
            .lines()
            .map(str::trim)
            .filter(|line| !line.is_empty())
            .map(ToString::to_string)
            .collect();

        Ok(files)
    })
    .await?
}

/// Returns conflicted file paths for the current index.
///
/// # Arguments
/// * `repo_path` - Path to the git repository or worktree
///
/// # Returns
/// A list of relative file paths with unresolved conflicts.
///
/// # Errors
/// Returns a [`GitError`] if invoking `git diff --name-only --diff-filter=U`
/// fails.
pub(crate) async fn list_conflicted_files(repo_path: PathBuf) -> Result<Vec<String>, GitError> {
    spawn_blocking(move || -> Result<Vec<String>, GitError> {
        let output = run_git_command_sync(
            &repo_path,
            &["diff", "--name-only", "--diff-filter=U"],
            "Failed to read conflicted files",
        )?;
        let files = output
            .lines()
            .map(str::trim)
            .filter(|line| !line.is_empty())
            .map(ToString::to_string)
            .collect();

        Ok(files)
    })
    .await?
}

/// Runs one rebase command and maps git output to a step result.
fn run_rebase_step(
    repo_path: &Path,
    args: &[&str],
    command: &str,
    failure_message: impl FnOnce(&str) -> String,
) -> Result<RebaseStepResult, GitError> {
    let output = run_git_command_with_index_lock_retry(repo_path, args, &[])?;

    if output.status.success() {
        return Ok(RebaseStepResult::Completed);
    }

    let detail = command_output_detail(&output.stdout, &output.stderr);
    if is_rebase_conflict(&detail) {
        return Ok(RebaseStepResult::Conflict { detail });
    }

    Err(GitError::CommandFailed {
        command: command.to_string(),
        stderr: failure_message(&detail),
    })
}

/// Aborts one rebase through injected process and retry boundaries.
fn abort_rebase_with_dependencies(
    repo_path: &Path,
    command_runner: &dyn GitCommandRunner,
    sleeper: &dyn Sleeper,
    metadata_cleaner: &dyn RebaseMetadataCleaner,
) -> Result<(), GitError> {
    let output = run_git_command_with_index_lock_retry_with_dependencies(
        repo_path,
        &["rebase", "--abort"],
        &[],
        command_runner,
        sleeper,
    )?;
    if !output.status.success() {
        let detail = command_output_detail(&output.stdout, &output.stderr);
        if is_stale_or_inactive_rebase_error(&detail) {
            match metadata_cleaner.clean_stale_metadata(repo_path) {
                Ok(true) => return Ok(()),
                Ok(false) => {}
                Err(cleanup_error) => {
                    return Err(GitError::CommandFailed {
                        command: "git rebase --abort".to_string(),
                        stderr: format!(
                            "Failed to abort rebase: {detail}. Stale rebase metadata cleanup \
                             failed: {cleanup_error}."
                        ),
                    });
                }
            }
        }

        return Err(GitError::CommandFailed {
            command: "git rebase --abort".to_string(),
            stderr: format!("Failed to abort rebase: {detail}."),
        });
    }

    Ok(())
}

/// Returns whether abort output identifies a known stale or inactive rebase.
fn is_stale_or_inactive_rebase_error(detail: &str) -> bool {
    let normalized_detail = detail.to_ascii_lowercase();

    normalized_detail.contains("no rebase in progress")
        || normalized_detail.contains("already a rebase-merge directory")
        || normalized_detail.contains("already a rebase-apply directory")
        || normalized_detail.contains("middle of another rebase")
}

/// Removes exact stale rebase metadata entries from the resolved git directory.
fn clean_stale_rebase_metadata(repo_path: &Path) -> Result<bool, GitError> {
    let git_dir = resolve_git_dir(repo_path)
        .ok_or_else(|| GitError::OutputParse("Failed to resolve git directory".to_string()))?;
    let removed_rebase_merge = remove_stale_rebase_metadata_path(&git_dir.join("rebase-merge"))?;
    let removed_rebase_apply = remove_stale_rebase_metadata_path(&git_dir.join("rebase-apply"))?;

    Ok(removed_rebase_merge || removed_rebase_apply)
}

/// Removes one exact metadata path without following directory symlinks.
fn remove_stale_rebase_metadata_path(path: &Path) -> Result<bool, GitError> {
    let metadata = match fs::symlink_metadata(path) {
        Ok(metadata) => metadata,
        Err(error) if error.kind() == ErrorKind::NotFound => return Ok(false),
        Err(error) => return Err(error.into()),
    };

    if metadata.file_type().is_dir() {
        fs::remove_dir_all(path)?;
    } else {
        fs::remove_file(path)?;
    }

    Ok(true)
}

/// Runs a git command and retries when `index.lock` contention occurs.
pub(super) fn run_git_command_with_index_lock_retry(
    repo_path: &Path,
    args: &[&str],
    environment: &[(&str, &str)],
) -> Result<Output, GitError> {
    let command_runner = ProcessGitCommandRunner;
    let sleeper = ThreadSleeper;

    run_git_command_with_index_lock_retry_with_dependencies(
        repo_path,
        args,
        environment,
        &command_runner,
        &sleeper,
    )
}

/// Runs a git command with retries using injected command and sleep
/// dependencies.
fn run_git_command_with_index_lock_retry_with_dependencies(
    repo_path: &Path,
    args: &[&str],
    environment: &[(&str, &str)],
    command_runner: &dyn GitCommandRunner,
    sleeper: &dyn Sleeper,
) -> Result<Output, GitError> {
    let args = args
        .iter()
        .map(|arg| String::from(*arg))
        .collect::<Vec<_>>();
    let environment = environment
        .iter()
        .map(|(key, value)| (String::from(*key), String::from(*value)))
        .collect::<Vec<_>>();

    for attempt in 0..GIT_INDEX_LOCK_RETRY_ATTEMPTS {
        let output =
            command_runner.run_git_command_output_with_env(repo_path, &args, &environment)?;
        if output.status.success() {
            return Ok(output);
        }

        let detail = command_output_detail(&output.stdout, &output.stderr);
        let is_last_attempt = attempt + 1 == GIT_INDEX_LOCK_RETRY_ATTEMPTS;
        if !is_git_index_lock_error(&detail) || is_last_attempt {
            return Ok(output);
        }

        sleeper.sleep(GIT_INDEX_LOCK_RETRY_DELAY);
    }

    unreachable!("index lock retry loop should always return an output")
}

/// Returns whether git output detail indicates a rebase conflict state.
///
/// Matches all known git messages that signal a conflict requiring manual
/// resolution, including messages emitted when staging partially-resolved
/// files and attempting `git rebase --continue` prematurely.
pub(super) fn is_rebase_conflict(detail: &str) -> bool {
    detail.contains("CONFLICT")
        || detail.contains("Resolve all conflicts manually")
        || detail.contains("could not apply")
        || detail.contains("mark them as resolved")
        || detail.contains("unresolved conflict")
        || detail.contains("Committing is not possible")
}

/// Returns whether git output indicates transient index lock contention.
pub(super) fn is_git_index_lock_error(detail: &str) -> bool {
    let normalized_detail = detail.to_ascii_lowercase();

    normalized_detail.contains("index.lock")
        && (normalized_detail.contains("file exists")
            || normalized_detail.contains("unable to create")
            || normalized_detail.contains("another git process"))
}

#[cfg(test)]
mod tests {
    use std::fs;
    #[cfg(unix)]
    use std::os::unix::fs as unix_fs;
    use std::process::{Command, Output};

    use mockall::predicate::eq;
    use tempfile::tempdir;

    use super::*;
    use crate::MockSleeper;

    #[test]
    fn test_run_git_command_with_index_lock_retry_retries_and_sleeps_before_success() {
        // Arrange
        let mut command_runner = MockGitCommandRunner::new();
        let mut sleeper = MockSleeper::new();
        let repo_path = Path::new(".");
        let args = ["rebase", "main"];
        let environment: [(&str, &str); 0] = [];

        command_runner
            .expect_run_git_command_output_with_env()
            .times(1)
            .returning(|_, _, _| Ok(git_index_lock_output()));
        command_runner
            .expect_run_git_command_output_with_env()
            .times(1)
            .returning(|_, _, _| Ok(success_output()));

        sleeper
            .expect_sleep()
            .with(eq(GIT_INDEX_LOCK_RETRY_DELAY))
            .times(1)
            .return_once(|_| {});

        // Act
        let output = run_git_command_with_index_lock_retry_with_dependencies(
            repo_path,
            &args,
            &environment,
            &command_runner,
            &sleeper,
        )
        .expect("retry helper should return command output");

        // Assert
        assert!(output.status.success());
    }

    #[test]
    fn test_run_git_command_with_index_lock_retry_passes_owned_args_and_environment() {
        // Arrange
        let mut command_runner = MockGitCommandRunner::new();
        let mut sleeper = MockSleeper::new();
        let repo_path = Path::new(".");
        let args = ["-c", "core.editor=true", "rebase", "main"];
        let environment = [("GIT_EDITOR", "true")];

        command_runner
            .expect_run_git_command_output_with_env()
            .withf(|repo_path, args, environment| {
                repo_path == Path::new(".")
                    && args.iter().map(String::as_str).eq([
                        "-c",
                        "core.editor=true",
                        "rebase",
                        "main",
                    ])
                    && environment
                        .iter()
                        .map(|(key, value)| (key.as_str(), value.as_str()))
                        .eq([("GIT_EDITOR", "true")])
            })
            .times(1)
            .returning(|_, _, _| Ok(success_output()));
        sleeper.expect_sleep().times(0);

        // Act
        let output = run_git_command_with_index_lock_retry_with_dependencies(
            repo_path,
            &args,
            &environment,
            &command_runner,
            &sleeper,
        )
        .expect("retry helper should return command output");

        // Assert
        assert!(output.status.success());
    }

    #[test]
    fn test_run_git_command_with_index_lock_retry_returns_last_lock_failure() {
        // Arrange
        let mut command_runner = MockGitCommandRunner::new();
        let mut sleeper = MockSleeper::new();
        let repo_path = Path::new(".");
        let args = ["rebase", "main"];
        let environment: [(&str, &str); 0] = [];

        command_runner
            .expect_run_git_command_output_with_env()
            .times(GIT_INDEX_LOCK_RETRY_ATTEMPTS)
            .returning(|_, _, _| Ok(git_index_lock_output()));
        sleeper
            .expect_sleep()
            .with(eq(GIT_INDEX_LOCK_RETRY_DELAY))
            .times(GIT_INDEX_LOCK_RETRY_ATTEMPTS - 1)
            .returning(|_| {});

        // Act
        let output = run_git_command_with_index_lock_retry_with_dependencies(
            repo_path,
            &args,
            &environment,
            &command_runner,
            &sleeper,
        )
        .expect("retry helper should return command output");

        // Assert
        assert!(!output.status.success());
        assert!(command_output_detail(&output.stdout, &output.stderr).contains("index.lock"));
    }

    #[test]
    fn test_run_git_command_with_index_lock_retry_returns_command_error_without_sleeping() {
        // Arrange
        let mut command_runner = MockGitCommandRunner::new();
        let mut sleeper = MockSleeper::new();
        let repo_path = Path::new(".");
        let args = ["rebase", "main"];
        let environment: [(&str, &str); 0] = [];

        command_runner
            .expect_run_git_command_output_with_env()
            .times(1)
            .return_once(|_, _, _| {
                Err(GitError::CommandFailed {
                    command: "git".to_string(),
                    stderr: "git execution failed".to_string(),
                })
            });
        sleeper.expect_sleep().times(0);

        // Act
        let error = run_git_command_with_index_lock_retry_with_dependencies(
            repo_path,
            &args,
            &environment,
            &command_runner,
            &sleeper,
        )
        .expect_err("retry helper should surface command execution errors");

        // Assert
        assert_eq!(error.to_string(), "git: git execution failed");
    }

    #[test]
    fn test_run_git_command_with_index_lock_retry_does_not_sleep_for_non_lock_errors() {
        // Arrange
        let mut command_runner = MockGitCommandRunner::new();
        let mut sleeper = MockSleeper::new();
        let repo_path = Path::new(".");
        let args = ["rebase", "main"];
        let environment: [(&str, &str); 0] = [];

        command_runner
            .expect_run_git_command_output_with_env()
            .times(1)
            .returning(|_, _, _| Ok(non_lock_failure_output()));
        sleeper.expect_sleep().times(0);

        // Act
        let output = run_git_command_with_index_lock_retry_with_dependencies(
            repo_path,
            &args,
            &environment,
            &command_runner,
            &sleeper,
        )
        .expect("retry helper should return command output");

        // Assert
        assert!(!output.status.success());
    }

    #[test]
    fn test_is_rebase_conflict_matches_unmerged_files_message() {
        // Arrange
        let detail = "Committing is not possible because you have unmerged files.";

        // Act
        let is_conflict = is_rebase_conflict(detail);

        // Assert
        assert!(is_conflict);
    }

    #[test]
    fn abort_rebase_succeeds_through_injected_boundaries() {
        // Arrange
        let mut command_runner = MockGitCommandRunner::new();
        let metadata_cleaner = MockRebaseMetadataCleaner::new();
        let mut sleeper = MockSleeper::new();
        command_runner
            .expect_run_git_command_output_with_env()
            .withf(|repo_path, args, environment| {
                repo_path == Path::new("session-worktree")
                    && args == ["rebase", "--abort"]
                    && environment.is_empty()
            })
            .once()
            .returning(|_, _, _| Ok(success_output()));
        sleeper.expect_sleep().times(0);

        // Act
        let result = abort_rebase_with_dependencies(
            Path::new("session-worktree"),
            &command_runner,
            &sleeper,
            &metadata_cleaner,
        );

        // Assert
        assert!(result.is_ok());
    }

    #[test]
    fn abort_rebase_preserves_command_runner_error() {
        // Arrange
        let mut command_runner = MockGitCommandRunner::new();
        let metadata_cleaner = MockRebaseMetadataCleaner::new();
        let mut sleeper = MockSleeper::new();
        command_runner
            .expect_run_git_command_output_with_env()
            .once()
            .return_once(|_, _, _| {
                Err(GitError::CommandFailed {
                    command: "git rebase --abort".to_string(),
                    stderr: "failed to spawn git".to_string(),
                })
            });
        sleeper.expect_sleep().times(0);

        // Act
        let error = abort_rebase_with_dependencies(
            Path::new("session-worktree"),
            &command_runner,
            &sleeper,
            &metadata_cleaner,
        )
        .expect_err("command runner failure should be preserved");

        // Assert
        assert!(matches!(
            error,
            GitError::CommandFailed { command, stderr }
                if command == "git rebase --abort" && stderr == "failed to spawn git"
        ));
    }

    #[test]
    fn abort_rebase_preserves_actionable_command_failure() {
        // Arrange
        let mut command_runner = MockGitCommandRunner::new();
        let metadata_cleaner = MockRebaseMetadataCleaner::new();
        let mut sleeper = MockSleeper::new();
        command_runner
            .expect_run_git_command_output_with_env()
            .once()
            .returning(|_, _, _| {
                let mut output = non_lock_failure_output();
                output.stderr = b"fatal: cannot open .git/rebase-merge/head-name".to_vec();

                Ok(output)
            });
        sleeper.expect_sleep().times(0);

        // Act
        let error = abort_rebase_with_dependencies(
            Path::new("session-worktree"),
            &command_runner,
            &sleeper,
            &metadata_cleaner,
        )
        .expect_err("failed abort should preserve the git error");

        // Assert
        assert!(matches!(
            error,
            GitError::CommandFailed { command, stderr }
                if command == "git rebase --abort"
                    && stderr.contains(".git/rebase-merge/head-name")
        ));
    }

    #[test]
    fn abort_rebase_recovers_when_stale_metadata_is_removed() {
        // Arrange
        let temp_dir = tempdir().expect("tempdir should be created");
        let git_dir = temp_dir.path().join(".git");
        let stale_metadata = git_dir.join("rebase-merge");
        fs::create_dir(&git_dir).expect("git dir should be created");
        fs::create_dir(&stale_metadata).expect("stale metadata should be created");
        let mut command_runner = MockGitCommandRunner::new();
        let metadata_cleaner = FilesystemRebaseMetadataCleaner;
        let mut sleeper = MockSleeper::new();
        command_runner
            .expect_run_git_command_output_with_env()
            .once()
            .returning(|_, _, _| Ok(stale_rebase_failure_output()));
        sleeper.expect_sleep().times(0);

        // Act
        let result = abort_rebase_with_dependencies(
            temp_dir.path(),
            &command_runner,
            &sleeper,
            &metadata_cleaner,
        );

        // Assert
        assert!(result.is_ok());
        assert!(!stale_metadata.exists());
    }

    #[test]
    fn abort_rebase_preserves_stale_error_when_no_metadata_is_removed() {
        // Arrange
        let mut command_runner = MockGitCommandRunner::new();
        let mut metadata_cleaner = MockRebaseMetadataCleaner::new();
        let mut sleeper = MockSleeper::new();
        command_runner
            .expect_run_git_command_output_with_env()
            .once()
            .returning(|_, _, _| Ok(stale_rebase_failure_output()));
        metadata_cleaner
            .expect_clean_stale_metadata()
            .once()
            .returning(|_| Ok(false));
        sleeper.expect_sleep().times(0);

        // Act
        let error = abort_rebase_with_dependencies(
            Path::new("session-worktree"),
            &command_runner,
            &sleeper,
            &metadata_cleaner,
        )
        .expect_err("missing stale metadata should preserve the abort failure");

        // Assert
        assert!(matches!(
            error,
            GitError::CommandFailed { command, stderr }
                if command == "git rebase --abort" && stderr.contains("No rebase in progress")
        ));
    }

    #[test]
    fn abort_rebase_appends_stale_metadata_cleanup_failure() {
        // Arrange
        let mut command_runner = MockGitCommandRunner::new();
        let mut metadata_cleaner = MockRebaseMetadataCleaner::new();
        let mut sleeper = MockSleeper::new();
        command_runner
            .expect_run_git_command_output_with_env()
            .once()
            .returning(|_, _, _| Ok(stale_rebase_failure_output()));
        metadata_cleaner
            .expect_clean_stale_metadata()
            .once()
            .returning(|_| {
                Err(GitError::Io(std::io::Error::new(
                    ErrorKind::PermissionDenied,
                    "metadata is read-only",
                )))
            });
        sleeper.expect_sleep().times(0);

        // Act
        let error = abort_rebase_with_dependencies(
            Path::new("session-worktree"),
            &command_runner,
            &sleeper,
            &metadata_cleaner,
        )
        .expect_err("cleanup failure should preserve both error contexts");

        // Assert
        assert!(matches!(
            error,
            GitError::CommandFailed { command, stderr }
                if command == "git rebase --abort"
                    && stderr.contains("No rebase in progress")
                    && stderr.contains("metadata is read-only")
        ));
    }

    #[test]
    fn stale_rebase_error_detection_matches_only_known_diagnostics() {
        // Arrange
        let stale_diagnostics = [
            "fatal: No rebase in progress?",
            "fatal: It seems that there is already a rebase-merge directory",
            "fatal: It seems that there is already a rebase-apply directory",
            "fatal: It seems that I cannot tell whether you are in the middle of another rebase",
        ];

        // Act
        let stale_results = stale_diagnostics.map(is_stale_or_inactive_rebase_error);
        let unrelated_result =
            is_stale_or_inactive_rebase_error("fatal: cannot read rebase-merge/head-name");

        // Assert
        assert!(stale_results.into_iter().all(|is_stale| is_stale));
        assert!(!unrelated_result);
    }

    #[test]
    fn filesystem_metadata_cleaner_removes_exact_rebase_entries() {
        // Arrange
        let temp_dir = tempdir().expect("tempdir should be created");
        let git_dir = temp_dir.path().join(".git");
        let rebase_merge = git_dir.join("rebase-merge");
        let rebase_apply = git_dir.join("rebase-apply");
        let unrelated_metadata = git_dir.join("MERGE_HEAD");
        fs::create_dir(&git_dir).expect("git dir should be created");
        fs::create_dir(&rebase_merge).expect("rebase-merge should be created");
        fs::write(rebase_merge.join("head-name"), "refs/heads/main")
            .expect("rebase-merge metadata should be written");
        fs::write(&rebase_apply, "apply state").expect("rebase-apply should be written");
        fs::write(&unrelated_metadata, "merge state").expect("merge metadata should be written");
        let metadata_cleaner = FilesystemRebaseMetadataCleaner;

        // Act
        let removed = metadata_cleaner
            .clean_stale_metadata(temp_dir.path())
            .expect("stale metadata cleanup should succeed");

        // Assert
        assert!(removed);
        assert!(!rebase_merge.exists());
        assert!(!rebase_apply.exists());
        assert!(unrelated_metadata.exists());
    }

    #[test]
    fn filesystem_metadata_cleaner_reports_no_change_without_rebase_entries() {
        // Arrange
        let temp_dir = tempdir().expect("tempdir should be created");
        fs::create_dir(temp_dir.path().join(".git")).expect("git dir should be created");
        let metadata_cleaner = FilesystemRebaseMetadataCleaner;

        // Act
        let removed = metadata_cleaner
            .clean_stale_metadata(temp_dir.path())
            .expect("empty metadata cleanup should succeed");

        // Assert
        assert!(!removed);
    }

    #[test]
    fn filesystem_metadata_cleaner_rejects_missing_git_directory() {
        // Arrange
        let temp_dir = tempdir().expect("tempdir should be created");
        let metadata_cleaner = FilesystemRebaseMetadataCleaner;

        // Act
        let error = metadata_cleaner
            .clean_stale_metadata(temp_dir.path())
            .expect_err("repository without git metadata should fail");

        // Assert
        assert!(matches!(
            error,
            GitError::OutputParse(message) if message == "Failed to resolve git directory"
        ));
    }

    #[test]
    fn remove_stale_metadata_preserves_non_not_found_io_error() {
        // Arrange
        let temp_dir = tempdir().expect("tempdir should be created");
        let parent_file = temp_dir.path().join("parent-file");
        fs::write(&parent_file, "not a directory").expect("parent file should be written");

        // Act
        let error = remove_stale_rebase_metadata_path(&parent_file.join("rebase-merge"))
            .expect_err("non-directory parent should remain an I/O error");

        // Assert
        assert!(matches!(error, GitError::Io(_)));
    }

    #[cfg(unix)]
    #[test]
    fn filesystem_metadata_cleaner_does_not_follow_directory_symlink() {
        // Arrange
        let temp_dir = tempdir().expect("tempdir should be created");
        let git_dir = temp_dir.path().join(".git");
        let external_dir = temp_dir.path().join("external-rebase-data");
        let external_marker = external_dir.join("marker");
        fs::create_dir(&git_dir).expect("git dir should be created");
        fs::create_dir(&external_dir).expect("external directory should be created");
        fs::write(&external_marker, "preserve").expect("external marker should be written");
        unix_fs::symlink(&external_dir, git_dir.join("rebase-merge"))
            .expect("metadata symlink should be created");
        let metadata_cleaner = FilesystemRebaseMetadataCleaner;

        // Act
        let removed = metadata_cleaner
            .clean_stale_metadata(temp_dir.path())
            .expect("symlink cleanup should succeed");

        // Assert
        assert!(removed);
        assert!(!git_dir.join("rebase-merge").exists());
        assert!(external_marker.exists());
    }

    #[test]
    fn test_in_progress_operation_detects_rebase_metadata() {
        // Arrange
        let temp_dir = tempdir().expect("tempdir should be created");
        let git_dir = temp_dir.path().join(".git");
        fs::create_dir(&git_dir).expect("git dir should be created");
        fs::create_dir(git_dir.join("rebase-merge")).expect("rebase metadata should be created");

        // Act
        let operation =
            in_progress_operation_sync(temp_dir.path()).expect("operation should be detected");

        // Assert
        assert_eq!(operation, Some(InProgressGitOperation::Rebase));
    }

    #[test]
    fn test_in_progress_operation_detects_merge_metadata() {
        // Arrange
        let temp_dir = tempdir().expect("tempdir should be created");
        let git_dir = temp_dir.path().join(".git");
        fs::create_dir(&git_dir).expect("git dir should be created");
        fs::write(git_dir.join("MERGE_HEAD"), "merge").expect("merge metadata should be created");

        // Act
        let operation =
            in_progress_operation_sync(temp_dir.path()).expect("operation should be detected");

        // Assert
        assert_eq!(operation, Some(InProgressGitOperation::Merge));
    }

    #[test]
    fn test_in_progress_operation_detects_cherry_pick_metadata() {
        // Arrange
        let temp_dir = tempdir().expect("tempdir should be created");
        let git_dir = temp_dir.path().join(".git");
        fs::create_dir(&git_dir).expect("git dir should be created");
        fs::write(git_dir.join("CHERRY_PICK_HEAD"), "cherry-pick")
            .expect("cherry-pick metadata should be created");

        // Act
        let operation =
            in_progress_operation_sync(temp_dir.path()).expect("operation should be detected");

        // Assert
        assert_eq!(operation, Some(InProgressGitOperation::CherryPick));
    }

    #[test]
    fn test_in_progress_operation_detects_revert_metadata() {
        // Arrange
        let temp_dir = tempdir().expect("tempdir should be created");
        let git_dir = temp_dir.path().join(".git");
        fs::create_dir(&git_dir).expect("git dir should be created");
        fs::write(git_dir.join("REVERT_HEAD"), "revert")
            .expect("revert metadata should be created");

        // Act
        let operation =
            in_progress_operation_sync(temp_dir.path()).expect("operation should be detected");

        // Assert
        assert_eq!(operation, Some(InProgressGitOperation::Revert));
    }

    #[test]
    fn test_in_progress_operation_returns_none_for_clean_git_dir() {
        // Arrange
        let temp_dir = tempdir().expect("tempdir should be created");
        fs::create_dir(temp_dir.path().join(".git")).expect("git dir should be created");

        // Act
        let operation =
            in_progress_operation_sync(temp_dir.path()).expect("operation should be detected");

        // Assert
        assert_eq!(operation, None);
    }

    /// Returns a successful git command output.
    fn success_output() -> Output {
        Command::new("git")
            .arg("--version")
            .output()
            .expect("failed to run git --version")
    }

    /// Returns a failing git command output that matches index lock contention.
    fn git_index_lock_output() -> Output {
        let mut output = Command::new("git")
            .arg("definitely-invalid-subcommand")
            .output()
            .expect("failed to run git invalid command");
        output.stdout = vec![];
        output.stderr = b"fatal: Unable to create '.git/index.lock': File exists.".to_vec();

        output
    }

    /// Returns a failing git command output that is unrelated to index locking.
    fn non_lock_failure_output() -> Output {
        let mut output = Command::new("git")
            .arg("definitely-invalid-subcommand")
            .output()
            .expect("failed to run git invalid command");
        output.stdout = vec![];
        output.stderr = b"fatal: not a git repository".to_vec();

        output
    }

    /// Returns a failing git command output for known stale rebase metadata.
    fn stale_rebase_failure_output() -> Output {
        let mut output = non_lock_failure_output();
        output.stderr = b"fatal: No rebase in progress?".to_vec();

        output
    }
}