git-pair 0.3.0

A Git extension for managing pair programming sessions with per-branch co-author configuration
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
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

pub fn get_git_pair_dir() -> Result<PathBuf, String> {
    let current_dir =
        env::current_dir().map_err(|e| format!("Error getting current directory: {}", e))?;
    let git_dir = current_dir.join(".git");

    if !git_dir.exists() {
        return Err("Not in a git repository. Please run 'git init' first.".to_string());
    }

    Ok(git_dir.join("git-pair"))
}

fn get_current_branch() -> Result<String, String> {
    let output = Command::new("git")
        .args(["branch", "--show-current"])
        .output()
        .map_err(|e| format!("Error running git command: {}", e))?;

    if !output.status.success() {
        return Err("Failed to get current branch name".to_string());
    }

    let branch_name = String::from_utf8(output.stdout)
        .map_err(|e| format!("Error parsing branch name: {}", e))?
        .trim()
        .to_string();

    if branch_name.is_empty() {
        return Err("No branch name found (detached HEAD?)".to_string());
    }

    Ok(branch_name)
}

fn get_branch_config_file() -> Result<PathBuf, String> {
    let git_pair_dir = get_git_pair_dir()?;
    let branch_name = get_current_branch()?;

    // Sanitize branch name for filename (replace problematic characters)
    let safe_branch_name = branch_name.replace(['/', '\\', ':'], "_");

    Ok(git_pair_dir.join(format!("config-{}", safe_branch_name)))
}

// Global roster management functions
fn get_global_config_dir() -> Result<PathBuf, String> {
    let home_dir = env::var("HOME").map_err(|_| "HOME environment variable not set".to_string())?;
    let config_dir = PathBuf::from(home_dir).join(".config").join("git-pair");
    Ok(config_dir)
}

fn get_global_roster_file() -> Result<PathBuf, String> {
    // Check for environment variable override (useful for testing)
    if let Ok(custom_path) = env::var("GIT_PAIR_ROSTER_FILE") {
        return Ok(PathBuf::from(custom_path));
    }

    let config_dir = get_global_config_dir()?;
    Ok(config_dir.join("roster"))
}

pub fn add_global_coauthor(alias: &str, name: &str, email: &str) -> Result<String, String> {
    let roster_file = get_global_roster_file()?;

    // Create parent directory if it doesn't exist (handle both default and custom paths)
    if let Some(parent) = roster_file.parent() {
        fs::create_dir_all(parent)
            .map_err(|e| format!("Error creating roster directory: {}", e))?;
    }

    // Read existing roster or create default content
    let content = if roster_file.exists() {
        fs::read_to_string(&roster_file)
            .map_err(|e| format!("Error reading global roster: {}", e))?
    } else {
        "# Global git-pair roster\n# Format: alias|name|email\n".to_string()
    };

    // Check if alias already exists
    if content
        .lines()
        .any(|line| line.starts_with(&format!("{}|", alias)))
    {
        return Err(format!("Alias '{}' already exists in global roster", alias));
    }

    // Add new entry
    let new_entry = format!("{}|{}|{}\n", alias, name, email);
    let new_content = content + &new_entry;

    fs::write(&roster_file, new_content)
        .map_err(|e| format!("Error writing to global roster: {}", e))?;

    Ok(format!(
        "Added '{}' ({} <{}>) to global roster",
        alias, name, email
    ))
}

pub fn get_global_roster() -> Result<Vec<(String, String, String)>, String> {
    let roster_file = get_global_roster_file()?;

    if !roster_file.exists() {
        return Ok(Vec::new());
    }

    let content = fs::read_to_string(&roster_file)
        .map_err(|e| format!("Error reading global roster: {}", e))?;

    let mut roster = Vec::new();
    for line in content.lines() {
        if line.starts_with('#') || line.trim().is_empty() {
            continue;
        }

        let parts: Vec<&str> = line.split('|').collect();
        if parts.len() == 3 {
            roster.push((
                parts[0].to_string(),
                parts[1].to_string(),
                parts[2].to_string(),
            ));
        }
    }

    Ok(roster)
}

pub fn add_coauthor_from_global(alias: &str) -> Result<String, String> {
    let roster = get_global_roster()?;

    // Find the alias in the roster
    if let Some((_, name, email)) = roster.iter().find(|(a, _, _)| a == alias) {
        // Split name into first and last name for the existing add_coauthor function
        let name_parts: Vec<&str> = name.split_whitespace().collect();
        if name_parts.len() >= 2 {
            let first_name = name_parts[0];
            let last_name = name_parts[1..].join(" ");
            add_coauthor(first_name, &last_name, email)
        } else {
            // If only one name, use it as first name and empty last name
            add_coauthor(name, "", email)
        }
    } else {
        Err(format!("Alias '{}' not found in global roster. Use 'git pair list --global' to see available aliases.", alias))
    }
}

pub fn init_pair_config() -> Result<String, String> {
    let _current_dir =
        env::current_dir().map_err(|e| format!("Error getting current directory: {}", e))?;
    let git_pair_dir = get_git_pair_dir()?;
    let branch_name = get_current_branch()?;

    // Create .git/git-pair directory
    fs::create_dir_all(&git_pair_dir)
        .map_err(|e| format!("Error creating git-pair directory: {}", e))?;

    // Create branch-specific config file
    let config_file = get_branch_config_file()?;
    let default_config = format!(
        "# git-pair configuration file for branch '{}'\n# Co-authors will be listed here\n",
        branch_name
    );

    if config_file.exists() {
        Ok(format!(
            "git-pair already initialized for branch '{}'",
            branch_name
        ))
    } else {
        fs::write(&config_file, default_config)
            .map_err(|e| format!("Error creating config file: {}", e))?;
        Ok(format!(
            "Successfully initialized git-pair for branch '{}'!\nConfiguration file created at: {}",
            branch_name,
            config_file.display()
        ))
    }
}

pub fn add_coauthor(name: &str, surname: &str, email: &str) -> Result<String, String> {
    let config_file = get_branch_config_file()?;
    let branch_name = get_current_branch()?;

    // Check if git-pair is initialized for this branch
    if !config_file.exists() {
        return Err(format!(
            "git-pair not initialized for branch '{}'. Please run 'git-pair init' first.",
            branch_name
        ));
    }

    // Read existing config
    let existing_content = fs::read_to_string(&config_file)
        .map_err(|e| format!("Error reading config file: {}", e))?;

    // Create the co-author entry
    let full_name = format!("{} {}", name, surname);
    let coauthor_line = format!("Co-authored-by: {} <{}>\n", full_name, email);

    // Check if this co-author already exists
    if existing_content.contains(coauthor_line.trim()) {
        return Ok(format!(
            "Co-author '{}' <{}> already exists on branch '{}'",
            full_name, email, branch_name
        ));
    }

    // Append the new co-author
    let new_content = existing_content + &coauthor_line;

    fs::write(&config_file, new_content)
        .map_err(|e| format!("Error writing to config file: {}", e))?;

    update_commit_template()?;
    Ok(format!(
        "Added co-author: {} <{}> to branch '{}'",
        full_name, email, branch_name
    ))
}

pub fn update_commit_template() -> Result<(), String> {
    let config_file = get_branch_config_file()?;

    // Read the config file to get co-authors
    let config_content = fs::read_to_string(&config_file)
        .map_err(|e| format!("Error reading config file: {}", e))?;

    // Extract co-author lines
    let coauthor_lines: Vec<&str> = config_content
        .lines()
        .filter(|line| line.starts_with("Co-authored-by:"))
        .collect();

    if coauthor_lines.is_empty() {
        // No co-authors, remove the hook
        remove_git_hook()?;
    } else {
        // Install or update the hook with current co-authors
        let current_dir =
            env::current_dir().map_err(|e| format!("Error getting current directory: {}", e))?;
        install_git_hook_in(&current_dir)?;
    }

    Ok(())
}

fn remove_git_hook_in(working_dir: &Path) -> Result<(), String> {
    let hook_file = working_dir
        .join(".git")
        .join("hooks")
        .join("prepare-commit-msg");

    if hook_file.exists() {
        let hook_content = fs::read_to_string(&hook_file)
            .map_err(|e| format!("Error reading hook file: {}", e))?;

        // Check if our section exists
        if let Some(new_content) = remove_git_pair_section(&hook_content) {
            if is_effectively_empty(&new_content) {
                // If only whitespace/comments/shebang remain, remove the entire file
                fs::remove_file(&hook_file)
                    .map_err(|e| format!("Error removing git hook: {}", e))?;
            } else {
                // Write back the content without our section
                fs::write(&hook_file, new_content)
                    .map_err(|e| format!("Error updating git hook: {}", e))?;
            }
        }
    }

    Ok(())
}

fn remove_git_hook() -> Result<(), String> {
    let current_dir =
        env::current_dir().map_err(|e| format!("Error getting current directory: {}", e))?;
    remove_git_hook_in(&current_dir)
}

pub fn remove_coauthor(identifier: &str) -> Result<String, String> {
    let config_file = get_branch_config_file()?;
    let branch_name = get_current_branch()?;

    // Check if git-pair is initialized for this branch
    if !config_file.exists() {
        return Err(format!(
            "git-pair not initialized for branch '{}'. Please run 'git-pair init' first.",
            branch_name
        ));
    }

    // Read existing config
    let existing_content = fs::read_to_string(&config_file)
        .map_err(|e| format!("Error reading config file: {}", e))?;

    // Get current co-authors
    let mut coauthor_lines: Vec<String> = existing_content
        .lines()
        .filter(|line| line.starts_with("Co-authored-by:"))
        .map(|line| line.to_string())
        .collect();

    // Store original count for comparison
    let original_count = coauthor_lines.len();

    // Try to match by different criteria
    coauthor_lines.retain(|line| !matches_coauthor(line, identifier));

    if coauthor_lines.len() == original_count {
        // No co-author was removed, check if it might be a global alias
        if let Ok(roster) = get_global_roster() {
            if let Some((_, name, email)) = roster.iter().find(|(alias, _, _)| alias == identifier)
            {
                // Try to remove by the actual name/email from the global roster
                let full_name_pattern = name;
                let email_pattern = email;

                coauthor_lines.retain(|line| {
                    !line.contains(full_name_pattern) && !line.contains(email_pattern)
                });

                if coauthor_lines.len() == original_count {
                    return Err(format!(
                        "Co-author matching alias '{}' ({} <{}>) not found on branch '{}'",
                        identifier, name, email, branch_name
                    ));
                }
            } else {
                return Err(format!("Co-author '{}' not found on branch '{}'. Use 'git-pair status' to see current co-authors.", identifier, branch_name));
            }
        } else {
            return Err(format!("Co-author '{}' not found on branch '{}'. Use 'git-pair status' to see current co-authors.", identifier, branch_name));
        }
    }

    // Reconstruct the config file content
    let mut new_content = String::new();

    // Add header
    new_content.push_str(&format!(
        "# git-pair configuration file for branch '{}'\n# Co-authors will be listed here\n",
        branch_name
    ));

    // Add remaining co-authors
    for coauthor in &coauthor_lines {
        new_content.push_str(coauthor);
        new_content.push('\n');
    }

    // Write back the updated content
    fs::write(&config_file, new_content)
        .map_err(|e| format!("Error writing to config file: {}", e))?;

    // Update the commit template
    update_commit_template()?;

    let removed_count = original_count - coauthor_lines.len();
    if removed_count == 1 {
        Ok(format!(
            "Removed 1 co-author matching '{}' from branch '{}'",
            identifier, branch_name
        ))
    } else {
        Ok(format!(
            "Removed {} co-authors matching '{}' from branch '{}'",
            removed_count, identifier, branch_name
        ))
    }
}

fn matches_coauthor(coauthor_line: &str, identifier: &str) -> bool {
    // Match by full name (case-insensitive)
    if coauthor_line
        .to_lowercase()
        .contains(&identifier.to_lowercase())
    {
        return true;
    }

    // Match by email
    if coauthor_line.contains(identifier) {
        return true;
    }

    false
}

pub fn clear_coauthors() -> Result<String, String> {
    let config_file = get_branch_config_file()?;
    let branch_name = get_current_branch()?;

    // Check if git-pair is initialized for this branch
    if !config_file.exists() {
        return Err(format!(
            "git-pair not initialized for branch '{}'. Please run 'git-pair init' first.",
            branch_name
        ));
    }

    // Reset config file to default content
    let default_config = format!(
        "# git-pair configuration file for branch '{}'\n# Co-authors will be listed here\n",
        branch_name
    );
    fs::write(&config_file, default_config)
        .map_err(|e| format!("Error clearing config file: {}", e))?;

    // Remove git hook
    remove_git_hook()?;

    Ok(format!(
        "Cleared all co-authors for branch '{}' and uninstalled git hook",
        branch_name
    ))
}

pub fn get_coauthors() -> Result<Vec<String>, String> {
    let config_file = get_branch_config_file()?;
    let branch_name = get_current_branch()?;

    if !config_file.exists() {
        return Err(format!(
            "git-pair not initialized for branch '{}'. Please run 'git-pair init' first.",
            branch_name
        ));
    }

    let config_content = fs::read_to_string(&config_file)
        .map_err(|e| format!("Error reading config file: {}", e))?;

    let coauthors: Vec<String> = config_content
        .lines()
        .filter(|line| line.starts_with("Co-authored-by:"))
        .map(|line| line.to_string())
        .collect();

    Ok(coauthors)
}

// Helper functions for hook management

/// Checks if hook content is effectively empty (only shebang, whitespace, or comments)
fn is_effectively_empty(content: &str) -> bool {
    for line in content.lines() {
        let trimmed = line.trim();
        if !trimmed.is_empty() && !trimmed.starts_with('#') {
            return false;
        }
    }
    true
}

/// Merges git-pair section into existing hook content
fn merge_git_pair_section(
    existing_content: &str,
    git_pair_section: &str,
) -> Result<String, String> {
    const BEGIN_MARKER: &str = "# BEGIN git-pair";
    const END_MARKER: &str = "# END git-pair";

    // Check if git-pair section already exists
    if let Some(begin_pos) = existing_content.find(BEGIN_MARKER) {
        if let Some(end_pos) = existing_content.find(END_MARKER) {
            // Replace existing git-pair section
            let before = &existing_content[..begin_pos];
            let after = &existing_content[end_pos + END_MARKER.len()..];

            // Remove trailing newline from 'before' if it exists, and ensure proper spacing
            let before_trimmed = before.trim_end();
            let after_trimmed = after.trim_start();

            let mut result = String::new();

            // Add shebang if this is a new file and before section is empty
            if before_trimmed.is_empty() && !existing_content.starts_with("#!") {
                result.push_str("#!/bin/sh\n");
            }

            if !before_trimmed.is_empty() {
                result.push_str(before_trimmed);
                result.push('\n');
            }

            result.push_str(git_pair_section);

            if !after_trimmed.is_empty() {
                result.push('\n');
                result.push_str(after_trimmed);
            }

            Ok(result)
        } else {
            Err("Found BEGIN marker but no END marker in existing hook".to_string())
        }
    } else {
        // Append git-pair section to existing content
        let mut result = String::new();

        if existing_content.trim().is_empty() {
            // New file, add shebang
            result.push_str("#!/bin/sh\n");
            result.push_str(git_pair_section);
        } else {
            // Existing content, append our section
            result.push_str(existing_content.trim_end());
            result.push_str("\n\n");
            result.push_str(git_pair_section);
        }

        Ok(result)
    }
}

/// Removes git-pair section from hook content, returns None if no section found
fn remove_git_pair_section(content: &str) -> Option<String> {
    const BEGIN_MARKER: &str = "# BEGIN git-pair";
    const END_MARKER: &str = "# END git-pair";

    if let Some(begin_pos) = content.find(BEGIN_MARKER) {
        if let Some(end_pos) = content.find(END_MARKER) {
            let before = &content[..begin_pos];
            let after = &content[end_pos + END_MARKER.len()..];

            // Clean up spacing - remove extra newlines around the removed section
            let before_trimmed = before.trim_end();
            let after_trimmed = after.trim_start();

            let mut result = String::new();

            if !before_trimmed.is_empty() {
                result.push_str(before_trimmed);
                if !after_trimmed.is_empty() {
                    result.push('\n');
                }
            }

            if !after_trimmed.is_empty() {
                result.push_str(after_trimmed);
            }

            Some(result)
        } else {
            // Found BEGIN but no END, don't modify
            None
        }
    } else {
        // No git-pair section found
        None
    }
}

fn install_git_hook_in(working_dir: &Path) -> Result<(), String> {
    let hooks_dir = working_dir.join(".git").join("hooks");
    let hook_file = hooks_dir.join("prepare-commit-msg");

    // Create hooks directory if it doesn't exist
    fs::create_dir_all(&hooks_dir).map_err(|e| format!("Error creating hooks directory: {}", e))?;

    // Read existing hook content if it exists
    let existing_content = if hook_file.exists() {
        fs::read_to_string(&hook_file)
            .map_err(|e| format!("Error reading existing hook file: {}", e))?
    } else {
        String::new()
    };

    // Generate our git-pair hook section
    let git_pair_section = r#"# BEGIN git-pair
# git-pair hook to automatically add co-authors

COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2

# Only add co-authors for regular commits (not merges, rebases, etc.)
if [ -z "$COMMIT_SOURCE" ] || [ "$COMMIT_SOURCE" = "message" ]; then
  # Check if co-authors are already present
  if ! grep -q "Co-authored-by:" "$COMMIT_MSG_FILE"; then
    # Get current branch and config file
    CURRENT_BRANCH=$(git branch --show-current)
    SAFE_BRANCH=$(echo "$CURRENT_BRANCH" | sed 's/[/\\:]/_/g')
    CONFIG_FILE=".git/git-pair/config-$SAFE_BRANCH"

    # Add co-authors from branch-specific config if it exists
    if [ -f "$CONFIG_FILE" ]; then
      COAUTHORS=$(grep '^Co-authored-by:' "$CONFIG_FILE")
      if [ -n "$COAUTHORS" ]; then
        echo "" >> "$COMMIT_MSG_FILE"
        echo "$COAUTHORS" >> "$COMMIT_MSG_FILE"
      fi
    fi
  fi
fi
# END git-pair"#;

    // Create the new hook content
    let new_content = merge_git_pair_section(&existing_content, git_pair_section)?;

    // Write the hook file
    fs::write(&hook_file, new_content).map_err(|e| format!("Error writing git hook: {}", e))?;

    // Make the hook executable
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&hook_file)
            .map_err(|e| format!("Error getting hook file permissions: {}", e))?
            .permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&hook_file, perms)
            .map_err(|e| format!("Error setting hook file permissions: {}", e))?;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env;
    use std::fs;
    use std::path::{Path, PathBuf};
    use std::process::Command;
    use std::sync::Mutex;
    use std::time::{SystemTime, UNIX_EPOCH};

    // Import the helper function for tests
    use super::matches_coauthor;

    // Mutex to ensure global roster tests don't interfere with each other
    static GLOBAL_ROSTER_TEST_LOCK: Mutex<()> = Mutex::new(());

    // Simple RAII wrapper for temporary directories
    struct TempDir {
        path: PathBuf,
    }

    impl TempDir {
        fn new() -> std::io::Result<Self> {
            let timestamp = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_nanos();

            let mut temp_path = env::temp_dir();
            temp_path.push(format!(
                "git-pair-test-{}-{}",
                std::process::id(),
                timestamp
            ));

            fs::create_dir_all(&temp_path)?;
            Ok(TempDir { path: temp_path })
        }

        fn path(&self) -> &Path {
            &self.path
        }
    }

    impl Drop for TempDir {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(&self.path);
        }
    }

    // Simple temporary file helper
    fn create_temp_file() -> std::io::Result<PathBuf> {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();

        let mut temp_path = env::temp_dir();
        temp_path.push(format!(
            "git-pair-test-{}-{}",
            std::process::id(),
            timestamp
        ));

        // Create empty file
        fs::write(&temp_path, "")?;
        Ok(temp_path)
    }

    // Test helper functions that work with a specific directory instead of changing global cwd
    fn get_git_pair_dir_in(working_dir: &Path) -> Result<PathBuf, String> {
        let git_dir = working_dir.join(".git");

        if !git_dir.exists() {
            return Err("Not in a git repository. Please run 'git init' first.".to_string());
        }

        Ok(git_dir.join("git-pair"))
    }

    fn get_current_branch_in(working_dir: &Path) -> Result<String, String> {
        let output = Command::new("git")
            .args(["branch", "--show-current"])
            .current_dir(working_dir)
            .output()
            .map_err(|e| format!("Error running git command: {}", e))?;

        if !output.status.success() {
            return Err("Error getting current branch".to_string());
        }

        let branch = String::from_utf8_lossy(&output.stdout).trim().to_string();
        if branch.is_empty() {
            return Err("No current branch found".to_string());
        }

        Ok(branch)
    }

    fn get_branch_config_file_in(working_dir: &Path) -> Result<PathBuf, String> {
        let git_pair_dir = get_git_pair_dir_in(working_dir)?;
        let branch_name = get_current_branch_in(working_dir)?;

        // Sanitize branch name for filename (replace problematic characters)
        let safe_branch_name = branch_name.replace(['/', '\\', ':'], "_");

        Ok(git_pair_dir.join(format!("config-{}", safe_branch_name)))
    }

    fn init_pair_config_in(working_dir: &Path) -> Result<String, String> {
        let git_pair_dir = get_git_pair_dir_in(working_dir)?;
        let branch_name = get_current_branch_in(working_dir)?;

        // Create .git/git-pair directory
        fs::create_dir_all(&git_pair_dir)
            .map_err(|e| format!("Error creating git-pair directory: {}", e))?;

        // Create branch-specific config file
        let config_file = get_branch_config_file_in(working_dir)?;
        let default_config = format!(
            "# git-pair configuration file for branch '{}'\n# Co-authors will be listed here\n",
            branch_name
        );

        if config_file.exists() {
            Ok(format!(
                "git-pair already initialized for branch '{}'",
                branch_name
            ))
        } else {
            fs::write(&config_file, default_config)
                .map_err(|e| format!("Error creating config file: {}", e))?;
            Ok(format!("Successfully initialized git-pair for branch '{}'!\nConfiguration file created at: {}", branch_name, config_file.display()))
        }
    }

    fn add_coauthor_in(
        working_dir: &Path,
        name: &str,
        surname: &str,
        email: &str,
    ) -> Result<String, String> {
        let config_file = get_branch_config_file_in(working_dir)?;
        let branch_name = get_current_branch_in(working_dir)?;

        // Check if git-pair is initialized for this branch
        if !config_file.exists() {
            return Err(format!(
                "git-pair not initialized for branch '{}'. Please run 'git-pair init' first.",
                branch_name
            ));
        }

        // Read existing config
        let existing_content = fs::read_to_string(&config_file)
            .map_err(|e| format!("Error reading config file: {}", e))?;

        // Create the co-author entry
        let full_name = format!("{} {}", name, surname);
        let coauthor_line = format!("Co-authored-by: {} <{}>\n", full_name, email);

        // Check if this co-author already exists
        if existing_content.contains(coauthor_line.trim()) {
            return Ok(format!(
                "Co-author '{}' <{}> already exists on branch '{}'",
                full_name, email, branch_name
            ));
        }

        // Append the new co-author
        let new_content = existing_content + &coauthor_line;

        fs::write(&config_file, new_content)
            .map_err(|e| format!("Error writing to config file: {}", e))?;

        // Install/update hook
        install_git_hook_in(working_dir)?;
        Ok(format!(
            "Added co-author: {} <{}> to branch '{}'",
            full_name, email, branch_name
        ))
    }

    fn get_coauthors_in(working_dir: &Path) -> Result<Vec<String>, String> {
        let config_file = get_branch_config_file_in(working_dir)?;
        let branch_name = get_current_branch_in(working_dir)?;

        if !config_file.exists() {
            return Err(format!(
                "git-pair not initialized for branch '{}'. Please run 'git-pair init' first.",
                branch_name
            ));
        }

        let content = fs::read_to_string(&config_file)
            .map_err(|e| format!("Error reading config file: {}", e))?;

        let coauthors: Vec<String> = content
            .lines()
            .filter(|line| line.starts_with("Co-authored-by:"))
            .map(|line| line.to_string())
            .collect();

        Ok(coauthors)
    }

    fn remove_coauthor_in(working_dir: &Path, identifier: &str) -> Result<String, String> {
        let config_file = get_branch_config_file_in(working_dir)?;
        let branch_name = get_current_branch_in(working_dir)?;

        // Check if git-pair is initialized for this branch
        if !config_file.exists() {
            return Err(format!(
                "git-pair not initialized for branch '{}'. Please run 'git-pair init' first.",
                branch_name
            ));
        }

        // Read existing config
        let existing_content = fs::read_to_string(&config_file)
            .map_err(|e| format!("Error reading config file: {}", e))?;

        // Get current co-authors
        let mut coauthor_lines: Vec<String> = existing_content
            .lines()
            .filter(|line| line.starts_with("Co-authored-by:"))
            .map(|line| line.to_string())
            .collect();

        // Store original count for comparison
        let original_count = coauthor_lines.len();

        // Try to match by different criteria
        coauthor_lines.retain(|line| !matches_coauthor(line, identifier));

        if coauthor_lines.len() == original_count {
            // No co-author was removed, check if it might be a global alias
            if let Ok(roster) = get_global_roster() {
                if let Some((_, name, email)) =
                    roster.iter().find(|(alias, _, _)| alias == identifier)
                {
                    // Try to remove by the actual name/email from the global roster
                    let full_name_pattern = name;
                    let email_pattern = email;

                    coauthor_lines.retain(|line| {
                        !line.contains(full_name_pattern) && !line.contains(email_pattern)
                    });

                    if coauthor_lines.len() == original_count {
                        return Err(format!(
                            "Co-author matching alias '{}' ({} <{}>) not found on branch '{}'",
                            identifier, name, email, branch_name
                        ));
                    }
                } else {
                    return Err(format!("Co-author '{}' not found on branch '{}'. Use 'git-pair status' to see current co-authors.", identifier, branch_name));
                }
            } else {
                return Err(format!("Co-author '{}' not found on branch '{}'. Use 'git-pair status' to see current co-authors.", identifier, branch_name));
            }
        }

        // Reconstruct the config file content
        let mut new_content = String::new();

        // Add header
        new_content.push_str(&format!(
            "# git-pair configuration file for branch '{}'\n# Co-authors will be listed here\n",
            branch_name
        ));

        // Add remaining co-authors
        for coauthor in &coauthor_lines {
            new_content.push_str(coauthor);
            new_content.push('\n');
        }

        // Write back the updated content
        fs::write(&config_file, new_content)
            .map_err(|e| format!("Error writing to config file: {}", e))?;

        // Update git hook
        if coauthor_lines.is_empty() {
            remove_git_hook_in(working_dir)?;
        } else {
            install_git_hook_in(working_dir)?;
        }

        let removed_count = original_count - coauthor_lines.len();
        if removed_count == 1 {
            Ok(format!(
                "Removed 1 co-author matching '{}' from branch '{}'",
                identifier, branch_name
            ))
        } else {
            Ok(format!(
                "Removed {} co-authors matching '{}' from branch '{}'",
                removed_count, identifier, branch_name
            ))
        }
    }

    fn clear_coauthors_in(working_dir: &Path) -> Result<String, String> {
        let config_file = get_branch_config_file_in(working_dir)?;
        let branch_name = get_current_branch_in(working_dir)?;

        // Check if git-pair is initialized for this branch
        if !config_file.exists() {
            return Err(format!(
                "git-pair not initialized for branch '{}'. Please run 'git-pair init' first.",
                branch_name
            ));
        }

        // Reset config file to default content
        let default_config = format!(
            "# git-pair configuration file for branch '{}'\n# Co-authors will be listed here\n",
            branch_name
        );
        fs::write(&config_file, default_config)
            .map_err(|e| format!("Error clearing config file: {}", e))?;

        // Remove git hook
        remove_git_hook_in(working_dir)?;

        Ok(format!(
            "Cleared all co-authors for branch '{}' and uninstalled git hook",
            branch_name
        ))
    }

    fn add_coauthor_from_global_in(working_dir: &Path, alias: &str) -> Result<String, String> {
        // Check if git-pair is initialized
        let config_file = get_branch_config_file_in(working_dir)?;
        let branch_name = get_current_branch_in(working_dir)?;

        if !config_file.exists() {
            return Err(format!(
                "git-pair not initialized for branch '{}'. Please run 'git-pair init' first.",
                branch_name
            ));
        }

        // Get from global roster
        let roster = get_global_roster()?;
        let (_, name, email) = roster
            .iter()
            .find(|(a, _, _)| a == alias)
            .ok_or_else(|| format!("Alias '{}' not found in global roster", alias))?;

        // Split name into first and last name
        let name_parts: Vec<&str> = name.split_whitespace().collect();
        let first_name = name_parts.first().map_or("", |v| v).to_string();
        let last_name = if name_parts.len() > 1 {
            name_parts[1..].join(" ")
        } else {
            String::new()
        };

        // Add the coauthor
        add_coauthor_in(working_dir, &first_name, &last_name, email)
    }

    // Test helper to create a temporary git repository without changing global cwd
    fn setup_test_repo() -> std::io::Result<TempDir> {
        use std::process::Command;
        let temp_dir = TempDir::new()?;
        let repo_path = temp_dir.path();

        // Initialize git repo in the temp directory (without changing global cwd)
        Command::new("git")
            .args(["init"])
            .current_dir(repo_path)
            .output()?;

        // Configure git user (required for commits)
        Command::new("git")
            .args(["config", "user.name", "Test User"])
            .current_dir(repo_path)
            .output()?;

        Command::new("git")
            .args(["config", "user.email", "test@example.com"])
            .current_dir(repo_path)
            .output()?;

        Ok(temp_dir)
    }

    #[test]
    fn test_init_pair_config_success() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");

        let result = init_pair_config_in(temp_dir.path()).expect("Init should succeed");
        assert!(result.contains("Successfully initialized git-pair for branch"));

        // Check that files were created
        assert!(temp_dir.path().join(".git/git-pair").exists());

        // Check branch-specific config file exists (should be config-main for default branch)
        let branch_config =
            get_branch_config_file_in(temp_dir.path()).expect("Should get branch config file");
        assert!(branch_config.exists());

        // Check config file content
        let config_content = fs::read_to_string(&branch_config).expect("Config file should exist");
        assert!(config_content.contains("# git-pair configuration file for branch"));
    }

    #[test]
    fn test_init_pair_config_already_initialized() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");

        // Initialize once
        init_pair_config_in(temp_dir.path()).expect("First init should succeed");

        // Initialize again
        let result = init_pair_config_in(temp_dir.path()).expect("Second init should succeed");
        assert!(result.contains("git-pair already initialized"));
    }

    #[test]
    fn test_init_pair_config_not_git_repo() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");

        let result = init_pair_config_in(temp_dir.path());
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Not in a git repository"));
    }

    #[test]
    fn test_add_coauthor_success() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();
        init_pair_config_in(test_dir).expect("Init should succeed");

        let result = add_coauthor_in(test_dir, "John", "Doe", "john.doe@example.com")
            .expect("Add should succeed");
        assert!(result.contains("Added co-author: John Doe"));

        // Check branch-specific config file was updated
        let branch_name = get_current_branch_in(test_dir).expect("Should get current branch");
        let config_dir = test_dir.join(".git/git-pair");
        let config_file = config_dir.join(format!("config-{}", branch_name));
        let config_content = fs::read_to_string(&config_file).expect("Config file should exist");
        assert!(config_content.contains("Co-authored-by: John Doe <john.doe@example.com>"));

        // Check git hook was installed
        assert!(test_dir.join(".git/hooks/prepare-commit-msg").exists());
    }

    #[test]
    fn test_add_coauthor_duplicate() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();
        init_pair_config_in(test_dir).expect("Init should succeed");

        // Add first time
        add_coauthor_in(test_dir, "John", "Doe", "john.doe@example.com")
            .expect("First add should succeed");

        // Add same person again
        let result = add_coauthor_in(test_dir, "John", "Doe", "john.doe@example.com")
            .expect("Duplicate add should succeed");
        assert!(result.contains("already exists"));
    }

    #[test]
    fn test_add_coauthor_not_initialized() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();

        let result = add_coauthor_in(test_dir, "John", "Doe", "john.doe@example.com");
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("git-pair not initialized"));
    }

    #[test]
    fn test_multiple_coauthors() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();
        init_pair_config_in(test_dir).expect("Init should succeed");

        // Add multiple co-authors
        add_coauthor_in(test_dir, "John", "Doe", "john.doe@example.com")
            .expect("First add should succeed");
        add_coauthor_in(test_dir, "Jane", "Smith", "jane.smith@example.com")
            .expect("Second add should succeed");

        let coauthors = get_coauthors_in(test_dir).expect("Get coauthors should succeed");
        assert_eq!(coauthors.len(), 2);
        assert!(coauthors.iter().any(|c| c.contains("John Doe")));
        assert!(coauthors.iter().any(|c| c.contains("Jane Smith")));
    }

    #[test]
    fn test_clear_coauthors_success() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();
        init_pair_config_in(test_dir).expect("Init should succeed");
        add_coauthor_in(test_dir, "John", "Doe", "john.doe@example.com")
            .expect("Add should succeed");

        let result = clear_coauthors_in(test_dir).expect("Clear should succeed");
        assert!(result.contains("Cleared all co-authors"));

        // Check that co-authors were cleared
        let coauthors = get_coauthors_in(test_dir).expect("Get coauthors should succeed");
        assert!(coauthors.is_empty());

        // Check that git hook was removed
        assert!(!test_dir.join(".git/hooks/prepare-commit-msg").exists());
    }

    #[test]
    fn test_clear_coauthors_not_initialized() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();

        let result = clear_coauthors_in(test_dir);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("git-pair not initialized"));
    }

    #[test]
    fn test_get_coauthors_empty() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();
        init_pair_config_in(test_dir).expect("Init should succeed");

        let coauthors = get_coauthors_in(test_dir).expect("Get coauthors should succeed");
        assert!(coauthors.is_empty());
    }

    #[test]
    fn test_get_coauthors_not_initialized() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();

        let result = get_coauthors_in(test_dir);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("git-pair not initialized"));
    }

    #[test]
    fn test_git_hook_functionality() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();
        init_pair_config_in(test_dir).expect("Init should succeed");
        add_coauthor_in(test_dir, "Alice", "Johnson", "alice@example.com")
            .expect("Add should succeed");

        // Create a test file and commit with -m flag
        let test_file = test_dir.join("test.txt");
        fs::write(&test_file, "test content").expect("Should write test file");

        Command::new("git")
            .args(["add", "test.txt"])
            .current_dir(test_dir)
            .output()
            .expect("Git add should succeed");

        let output = Command::new("git")
            .args(["commit", "-m", "Test commit message"])
            .current_dir(test_dir)
            .output()
            .expect("Git commit should succeed");

        assert!(output.status.success());

        // Check that the commit message includes co-author
        let log_output = Command::new("git")
            .args(["log", "--pretty=format:%B", "-1"])
            .current_dir(test_dir)
            .output()
            .expect("Git log should succeed");

        let commit_message =
            String::from_utf8(log_output.stdout).expect("Log output should be valid UTF-8");
        assert!(commit_message.contains("Test commit message"));
        assert!(commit_message.contains("Co-authored-by: Alice Johnson <alice@example.com>"));
    }

    #[test]
    fn test_git_config_integration() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();

        init_pair_config_in(test_dir).expect("Init should succeed");
        add_coauthor_in(test_dir, "John", "Doe", "john.doe@example.com")
            .expect("Add should succeed");

        // Check that git hook was installed
        assert!(test_dir.join(".git/hooks/prepare-commit-msg").exists());

        let hook_content = fs::read_to_string(test_dir.join(".git/hooks/prepare-commit-msg"))
            .expect("Hook file should exist");
        assert!(hook_content.contains("git-pair hook"));
        // With per-branch config, co-author names are read dynamically from config files
        // so they won't be hard-coded in the hook
        assert!(hook_content.contains("CONFIG_FILE"));
        assert!(hook_content.contains("grep '^Co-authored-by:'"));

        // Check that the branch-specific config file contains the co-author
        let branch_config =
            get_branch_config_file_in(test_dir).expect("Should get branch config file");
        let config_content = fs::read_to_string(&branch_config).expect("Config file should exist");
        assert!(config_content.contains("John Doe"));

        // Clear and check hook was removed
        clear_coauthors_in(test_dir).expect("Clear should succeed");

        // Hook should be removed
        assert!(!test_dir.join(".git/hooks/prepare-commit-msg").exists());
    }

    #[test]
    fn test_global_roster_add_and_list() {
        let _lock = GLOBAL_ROSTER_TEST_LOCK.lock().unwrap();

        // Use a temporary roster file for testing
        let temp_path = create_temp_file().expect("Failed to create temp file");
        env::set_var("GIT_PAIR_ROSTER_FILE", temp_path.to_str().unwrap());

        // Test adding to global roster
        let result = add_global_coauthor("alice", "Alice Johnson", "alice@example.com")
            .expect("Should add to global roster");
        assert!(result.contains("Added 'alice'"));

        // Test listing global roster
        let roster = get_global_roster().expect("Should get global roster");
        assert_eq!(roster.len(), 1);
        assert_eq!(
            roster[0],
            (
                "alice".to_string(),
                "Alice Johnson".to_string(),
                "alice@example.com".to_string()
            )
        );

        // Test duplicate alias
        let result = add_global_coauthor("alice", "Alice Smith", "alice.smith@example.com");
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("already exists"));

        // Clean up
        env::remove_var("GIT_PAIR_ROSTER_FILE");
    }

    #[test]
    fn test_add_coauthor_from_global() {
        let _lock = GLOBAL_ROSTER_TEST_LOCK.lock().unwrap();

        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();
        let temp_path = create_temp_file().expect("Failed to create temp file");
        env::set_var("GIT_PAIR_ROSTER_FILE", temp_path.to_str().unwrap());

        // Initialize branch config
        init_pair_config_in(test_dir).expect("Init should succeed");

        // Add to global roster
        add_global_coauthor("bob", "Bob Wilson", "bob@example.com")
            .expect("Should add to global roster");

        // Test adding from global roster
        let result =
            add_coauthor_from_global_in(test_dir, "bob").expect("Should add from global roster");
        assert!(result.contains("Added co-author: Bob Wilson"));

        // Verify it was added to branch config
        let coauthors = get_coauthors_in(test_dir).expect("Should get coauthors");
        assert_eq!(coauthors.len(), 1);
        assert!(coauthors[0].contains("Bob Wilson"));

        // Test non-existent alias
        let result = add_coauthor_from_global_in(test_dir, "charlie");
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("not found in global roster"));

        // Clean up
        env::remove_var("GIT_PAIR_ROSTER_FILE");
    }

    #[test]
    fn test_global_roster_empty() {
        let _lock = GLOBAL_ROSTER_TEST_LOCK.lock().unwrap();

        let temp_path = create_temp_file().expect("Failed to create temp file");
        env::set_var("GIT_PAIR_ROSTER_FILE", temp_path.to_str().unwrap());

        // Remove the file to test truly empty state
        fs::remove_file(&temp_path).ok();

        // Test empty global roster
        let roster = get_global_roster().expect("Should get empty global roster");
        assert!(roster.is_empty());

        // Clean up
        env::remove_var("GIT_PAIR_ROSTER_FILE");
    }

    #[test]
    fn test_remove_coauthor_by_name() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();
        init_pair_config_in(test_dir).expect("Init should succeed");

        // Add multiple co-authors
        add_coauthor_in(test_dir, "John", "Doe", "john.doe@example.com")
            .expect("Add should succeed");
        add_coauthor_in(test_dir, "Jane", "Smith", "jane.smith@example.com")
            .expect("Add should succeed");

        // Remove by name
        let result = remove_coauthor_in(test_dir, "John Doe").expect("Remove should succeed");
        assert!(result.contains("Removed 1 co-author matching 'John Doe'"));

        // Check remaining co-authors
        let coauthors = get_coauthors_in(test_dir).expect("Get coauthors should succeed");
        assert_eq!(coauthors.len(), 1);
        assert!(coauthors[0].contains("Jane Smith"));
        assert!(!coauthors[0].contains("John Doe"));
    }

    #[test]
    fn test_remove_coauthor_by_email() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();
        init_pair_config_in(test_dir).expect("Init should succeed");

        // Add multiple co-authors
        add_coauthor_in(test_dir, "John", "Doe", "john.doe@example.com")
            .expect("Add should succeed");
        add_coauthor_in(test_dir, "Jane", "Smith", "jane.smith@example.com")
            .expect("Add should succeed");

        // Remove by email
        let result =
            remove_coauthor_in(test_dir, "jane.smith@example.com").expect("Remove should succeed");
        assert!(result.contains("Removed 1 co-author matching 'jane.smith@example.com'"));

        // Check remaining co-authors
        let coauthors = get_coauthors_in(test_dir).expect("Get coauthors should succeed");
        assert_eq!(coauthors.len(), 1);
        assert!(coauthors[0].contains("John Doe"));
        assert!(!coauthors[0].contains("Jane Smith"));
    }

    #[test]
    fn test_remove_coauthor_by_global_alias() {
        let _lock = GLOBAL_ROSTER_TEST_LOCK.lock().unwrap();

        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();
        let temp_path = create_temp_file().expect("Failed to create temp file");
        env::set_var("GIT_PAIR_ROSTER_FILE", temp_path.to_str().unwrap());

        // Setup global roster
        add_global_coauthor("alice", "Alice Johnson", "alice@example.com")
            .expect("Should add to global roster");
        add_global_coauthor("bob", "Bob Wilson", "bob@example.com")
            .expect("Should add to global roster");

        // Initialize and add co-authors
        init_pair_config_in(test_dir).expect("Init should succeed");
        add_coauthor_from_global_in(test_dir, "alice").expect("Add alice should succeed");
        add_coauthor_from_global_in(test_dir, "bob").expect("Add bob should succeed");

        // Remove by alias
        let result = remove_coauthor_in(test_dir, "alice").expect("Remove should succeed");
        assert!(result.contains("Removed 1 co-author matching 'alice'"));

        // Check remaining co-authors
        let coauthors = get_coauthors_in(test_dir).expect("Get coauthors should succeed");
        assert_eq!(coauthors.len(), 1);
        assert!(coauthors[0].contains("Bob Wilson"));
        assert!(!coauthors[0].contains("Alice Johnson"));

        // Clean up
        env::remove_var("GIT_PAIR_ROSTER_FILE");
    }

    #[test]
    fn test_remove_coauthor_not_found() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();
        init_pair_config_in(test_dir).expect("Init should succeed");

        // Add one co-author
        add_coauthor_in(test_dir, "John", "Doe", "john.doe@example.com")
            .expect("Add should succeed");

        // Try to remove non-existent co-author
        let result = remove_coauthor_in(test_dir, "Jane Smith");
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("not found on branch"));

        // Verify original co-author is still there
        let coauthors = get_coauthors_in(test_dir).expect("Get coauthors should succeed");
        assert_eq!(coauthors.len(), 1);
        assert!(coauthors[0].contains("John Doe"));
    }

    #[test]
    fn test_remove_coauthor_not_initialized() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();

        let result = remove_coauthor_in(test_dir, "John Doe");
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("git-pair not initialized"));
    }

    #[test]
    fn test_remove_last_coauthor_removes_hook() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();
        init_pair_config_in(test_dir).expect("Init should succeed");

        // Add one co-author
        add_coauthor_in(test_dir, "John", "Doe", "john.doe@example.com")
            .expect("Add should succeed");

        // Verify hook exists
        assert!(test_dir.join(".git/hooks/prepare-commit-msg").exists());

        // Remove the only co-author
        remove_coauthor_in(test_dir, "John Doe").expect("Remove should succeed");

        // Verify hook was removed since no co-authors remain
        assert!(!test_dir.join(".git/hooks/prepare-commit-msg").exists());

        // Verify no co-authors remain
        let coauthors = get_coauthors_in(test_dir).expect("Get coauthors should succeed");
        assert!(coauthors.is_empty());
    }

    #[test]
    fn test_remove_coauthor_case_insensitive() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();
        init_pair_config_in(test_dir).expect("Init should succeed");

        // Add co-author
        add_coauthor_in(test_dir, "John", "Doe", "john.doe@example.com")
            .expect("Add should succeed");

        // Remove with different case
        let result = remove_coauthor_in(test_dir, "john doe").expect("Remove should succeed");
        assert!(result.contains("Removed 1 co-author matching 'john doe'"));

        // Verify co-author was removed
        let coauthors = get_coauthors_in(test_dir).expect("Get coauthors should succeed");
        assert!(coauthors.is_empty());
    }

    // Tests for improved hook management

    #[test]
    fn test_is_effectively_empty() {
        assert!(is_effectively_empty(""));
        assert!(is_effectively_empty("   \n  \t  "));
        assert!(is_effectively_empty("#!/bin/sh"));
        assert!(is_effectively_empty("#!/bin/sh\n# comment"));
        assert!(is_effectively_empty("#!/bin/sh\n\n# comment\n   "));
        assert!(!is_effectively_empty("#!/bin/sh\necho 'something'"));
        assert!(!is_effectively_empty("echo 'test'"));
    }

    #[test]
    fn test_merge_git_pair_section_new_file() {
        let existing = "";
        let git_pair_section = "# BEGIN git-pair\necho 'git-pair'\n# END git-pair";

        let result = merge_git_pair_section(existing, git_pair_section).unwrap();
        assert!(result.starts_with("#!/bin/sh\n"));
        assert!(result.contains("# BEGIN git-pair"));
        assert!(result.contains("# END git-pair"));
    }

    #[test]
    fn test_merge_git_pair_section_existing_content() {
        let existing = "#!/bin/sh\necho 'existing hook'";
        let git_pair_section = "# BEGIN git-pair\necho 'git-pair'\n# END git-pair";

        let result = merge_git_pair_section(existing, git_pair_section).unwrap();
        assert!(result.contains("echo 'existing hook'"));
        assert!(result.contains("# BEGIN git-pair"));
        assert!(result.ends_with("# END git-pair"));
    }

    #[test]
    fn test_merge_git_pair_section_replace_existing() {
        let existing =
            "#!/bin/sh\necho 'before'\n# BEGIN git-pair\necho 'old'\n# END git-pair\necho 'after'";
        let git_pair_section = "# BEGIN git-pair\necho 'new'\n# END git-pair";

        let result = merge_git_pair_section(existing, git_pair_section).unwrap();
        assert!(result.contains("echo 'before'"));
        assert!(result.contains("echo 'new'"));
        assert!(result.contains("echo 'after'"));
        assert!(!result.contains("echo 'old'"));
    }

    #[test]
    fn test_remove_git_pair_section_success() {
        let content = "#!/bin/sh\necho 'before'\n# BEGIN git-pair\necho 'git-pair'\n# END git-pair\necho 'after'";

        let result = remove_git_pair_section(content).unwrap();
        assert!(result.contains("echo 'before'"));
        assert!(result.contains("echo 'after'"));
        assert!(!result.contains("git-pair"));
        assert!(!result.contains("BEGIN"));
    }

    #[test]
    fn test_remove_git_pair_section_not_found() {
        let content = "#!/bin/sh\necho 'no git-pair here'";

        let result = remove_git_pair_section(content);
        assert!(result.is_none());
    }

    #[test]
    fn test_remove_git_pair_section_only_content() {
        let content = "#!/bin/sh\n# BEGIN git-pair\necho 'git-pair'\n# END git-pair";

        let result = remove_git_pair_section(content).unwrap();
        assert_eq!(result.trim(), "#!/bin/sh");
    }

    #[test]
    fn test_hook_preservation_workflow() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();

        // Create an existing hook
        let hooks_dir = test_dir.join(".git/hooks");
        fs::create_dir_all(&hooks_dir).expect("Should create hooks dir");
        let hook_file = hooks_dir.join("prepare-commit-msg");
        let existing_hook = "#!/bin/sh\necho 'existing hook logic'";
        fs::write(&hook_file, existing_hook).expect("Should write existing hook");

        // Initialize git-pair and add co-author
        init_pair_config_in(test_dir).expect("Init should succeed");
        add_coauthor_in(test_dir, "John", "Doe", "john@example.com").expect("Add should succeed");

        // Check that existing hook content is preserved
        let hook_content = fs::read_to_string(&hook_file).expect("Hook should exist");
        assert!(hook_content.contains("existing hook logic"));
        assert!(hook_content.contains("# BEGIN git-pair"));
        assert!(hook_content.contains("# END git-pair"));

        // Clear co-authors and check that only git-pair section is removed
        clear_coauthors_in(test_dir).expect("Clear should succeed");

        let remaining_content = fs::read_to_string(&hook_file).expect("Hook should still exist");
        assert!(remaining_content.contains("existing hook logic"));
        assert!(!remaining_content.contains("git-pair"));
    }

    #[test]
    fn test_hook_complete_removal() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();

        // Initialize git-pair and add co-author (no existing hook)
        init_pair_config_in(test_dir).expect("Init should succeed");
        add_coauthor_in(test_dir, "John", "Doe", "john@example.com").expect("Add should succeed");

        let hook_file = test_dir.join(".git/hooks/prepare-commit-msg");
        assert!(hook_file.exists());

        // Clear co-authors - should remove entire hook file since it only contains git-pair content
        clear_coauthors_in(test_dir).expect("Clear should succeed");

        assert!(!hook_file.exists());
    }

    #[test]
    fn test_hook_update_preserves_existing() {
        let temp_dir = setup_test_repo().expect("Failed to setup test repo");
        let test_dir = temp_dir.path();

        // Create existing hook
        let hooks_dir = test_dir.join(".git/hooks");
        fs::create_dir_all(&hooks_dir).expect("Should create hooks dir");
        let hook_file = hooks_dir.join("prepare-commit-msg");
        fs::write(&hook_file, "#!/bin/sh\necho 'original'").expect("Should write hook");

        // Initialize and add first co-author
        init_pair_config_in(test_dir).expect("Init should succeed");
        add_coauthor_in(test_dir, "John", "Doe", "john@example.com").expect("Add should succeed");

        // Add second co-author (should update hook)
        add_coauthor_in(test_dir, "Jane", "Smith", "jane@example.com").expect("Add should succeed");

        // Original content should still be there
        let hook_content = fs::read_to_string(&hook_file).expect("Hook should exist");
        assert!(hook_content.contains("echo 'original'"));
        assert!(hook_content.contains("git-pair"));

        // Should only have one git-pair section
        assert_eq!(hook_content.matches("# BEGIN git-pair").count(), 1);
        assert_eq!(hook_content.matches("# END git-pair").count(), 1);
    }
}