gg-cli 0.41.0

GG - Gui for JJ
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
mod change;
mod r#ref;
mod revision;

use std::collections::HashMap;
use std::sync::Arc;

use anyhow::{Context, Error, Result};
use async_trait::async_trait;
use futures_util::TryStreamExt;
use itertools::Itertools;
use jj_cli::{
    git_util::load_git_import_options,
    merge_tools::{self, ConflictResolveError, ExternalMergeTool, MergeEditor},
    ui::Ui,
};
use jj_lib::{
    backend::{CommitId, TreeValue},
    conflicts::{self, ConflictMarkerStyle, ConflictMaterializeOptions, MaterializedTreeValue},
    files::FileMergeHunkLevel,
    git::{
        self, GitFetchRefExpression, GitPushOptions, GitPushRefTargets, GitSettings,
        GitSubprocessOptions, REMOTE_NAME_FOR_LOCAL_GIT_REPO,
    },
    merge::{Diff, SameChange},
    merged_tree::MergedTree,
    object_id::ObjectId as ObjectIdTrait,
    op_walk,
    ref_name::WorkspaceNameBuf,
    ref_name::{GitRefNameBuf, RefNameBuf, RemoteName, RemoteNameBuf, RemoteRefSymbol},
    refs::{self, LocalAndRemoteRef, RefPushAction},
    repo::Repo,
    repo_path::RepoPath,
    revset::{self, RevsetStreamExt},
    rewrite::{self},
    store::Store,
    str_util::StringExpression,
    tree_merge::MergeOptions,
    workspace_store::{SimpleWorkspaceStore, WorkspaceStore as _},
};
use pollster::FutureExt as _;
use tokio::io::AsyncReadExt;

use super::{
    Mutation,
    git_util::{AuthContext, get_git_remote_names},
    gui_util::WorkspaceSession,
};

use crate::messages::mutations::{
    ExternalDiff, ExternalResolve, ForgetWorkspace, GitFetch, GitPush, GitRefspec, MutationOptions,
    MutationResult, RenameWorkspace, UndoOperation,
};

macro_rules! precondition {
    ($($args:tt)*) => {
        return Ok(MutationResult::PreconditionError { message: format!($($args)*) })
    }
}

#[async_trait(?Send)]
impl Mutation for ExternalDiff {
    async fn execute(
        self: Box<Self>,
        ws: &mut WorkspaceSession,
        _options: &MutationOptions,
    ) -> Result<MutationResult> {
        let commit = ws.resolve_change_id(&self.id)?;
        let parents = commit.parents().await?;
        let from_tree = rewrite::merge_commit_trees(ws.repo(), &parents).await?;
        let to_tree = commit.tree();

        let tool_args: jj_cli::config::CommandNameAndArgs =
            ws.data.workspace_settings.get("ui.diff-formatter")?;
        let tool = if let Some(name) = tool_args.as_str() {
            merge_tools::get_external_tool_config(&ws.data.workspace_settings, name)?
                .unwrap_or_else(|| ExternalMergeTool::with_program(name))
        } else {
            ExternalMergeTool::with_diff_args(&tool_args)
        };

        let repo_path = RepoPath::from_internal_string(&self.path.repo_path)?;
        let store = ws.repo().store().clone();
        let left_content = read_file_content(&store, &from_tree, repo_path).await?;
        let right_content = read_file_content(&store, &to_tree, repo_path).await?;
        let relative_path = repo_path.to_fs_path_unchecked(std::path::Path::new(""));

        std::thread::spawn(move || {
            let run = || -> Result<()> {
                let temp_dir = tempfile::Builder::new().prefix("jj-diff-").tempdir()?;

                let left_dir = temp_dir.path().join("left");
                let right_dir = temp_dir.path().join("right");
                let left_path = left_dir.join(&relative_path);
                let right_path = right_dir.join(&relative_path);

                if let Some(parent) = left_path.parent() {
                    std::fs::create_dir_all(parent)?;
                }
                if let Some(parent) = right_path.parent() {
                    std::fs::create_dir_all(parent)?;
                }
                std::fs::write(&left_path, &left_content)?;
                std::fs::write(&right_path, &right_content)?;

                let left_rel = left_path
                    .strip_prefix(temp_dir.path())
                    .unwrap()
                    .to_str()
                    .unwrap()
                    .to_owned();
                let right_rel = right_path
                    .strip_prefix(temp_dir.path())
                    .unwrap()
                    .to_str()
                    .unwrap()
                    .to_owned();
                let patterns = HashMap::from([
                    ("left", left_rel),
                    ("right", right_rel),
                    ("width", "80".to_owned()),
                ]);

                let ui = Ui::null();
                merge_tools::invoke_external_diff(
                    &ui,
                    &mut std::io::sink(),
                    &tool,
                    temp_dir.path(),
                    &patterns,
                )?;
                Ok(())
            };
            if let Err(e) = run() {
                log::error!("external diff tool: {e:#}");
            }
        });

        Ok(MutationResult::Unchanged)
    }
}

#[async_trait(?Send)]
impl Mutation for ExternalResolve {
    async fn execute(
        self: Box<Self>,
        ws: &mut WorkspaceSession,
        _options: &MutationOptions,
    ) -> Result<MutationResult> {
        let commit = ws.resolve_change_id(&self.id)?;

        if ws.check_immutable(vec![commit.id().clone()])? {
            precondition!("Revision is immutable");
        }

        let tree = commit.tree();
        let repo_path = RepoPath::from_internal_string(&self.path.repo_path)?;

        let ui = Ui::null();
        let merge_editor = MergeEditor::from_settings(
            &ui,
            &ws.data.workspace_settings,
            ws.data.path_converter.clone(),
            ConflictMarkerStyle::Git,
        )
        .context("failed to load merge editor config")?;

        let (new_tree, _partial) = match merge_editor.edit_files(&ui, &tree, &[repo_path]).await {
            Ok(result) => result,
            Err(ConflictResolveError::EmptyOrUnchanged) => {
                return Ok(MutationResult::Unchanged);
            }
            Err(err) => return Err(err.into()),
        };

        if new_tree.tree_ids() == tree.tree_ids() {
            return Ok(MutationResult::Unchanged);
        }

        let mut tx = ws.start_transaction().await?;
        tx.repo_mut()
            .rewrite_commit(&commit)
            .set_tree(new_tree)
            .write()
            .await?;

        match ws
            .finish_transaction(tx, format!("resolve conflicts in {}", commit.id().hex()))
            .await?
        {
            Some(new_status) => Ok(MutationResult::Updated {
                new_status,
                new_selection: None,
            }),
            None => Ok(MutationResult::Unchanged),
        }
    }
}

#[async_trait(?Send)]
impl Mutation for GitFetch {
    async fn execute(
        self: Box<Self>,
        ws: &mut WorkspaceSession,
        _options: &MutationOptions,
    ) -> Result<MutationResult> {
        let mut tx = ws.start_transaction().await?;

        let git_repo = match ws.git_repo() {
            Some(git_repo) => git_repo,
            None => precondition!("No git backend"),
        };

        let mut remote_patterns = Vec::new();
        match &self.refspec {
            GitRefspec::AllBookmarks { remote_name } => {
                remote_patterns.push((remote_name.clone(), None));
            }
            GitRefspec::AllRemotes { bookmark_ref } => {
                let bookmark_name = bookmark_ref.as_bookmark()?;
                for remote_name in get_git_remote_names(&git_repo) {
                    remote_patterns.push((remote_name, Some(bookmark_name.to_owned())));
                }
            }
            GitRefspec::RemoteBookmark {
                remote_name,
                bookmark_ref,
            } => {
                let bookmark_name = bookmark_ref.as_bookmark()?;
                remote_patterns.push((remote_name.clone(), Some(bookmark_name.to_owned())));
            }
        }

        // accumulate input requirements
        let mut auth_ctx = AuthContext::new(self.input);
        let progress_sender = ws.sink();
        let git_settings = GitSettings::from_settings(&ws.data.workspace_settings)?;
        let remote_settings = ws.data.workspace_settings.remote_settings()?;
        let import_options = load_git_import_options(&Ui::null(), &git_settings, &remote_settings)
            .map_err(|e| Error::new(e.error))?;

        for (remote_name, pattern) in &remote_patterns {
            let bookmark_expr = pattern
                .clone()
                .map(StringExpression::exact)
                .unwrap_or_else(StringExpression::all);
            let refspecs = git::expand_fetch_refspecs(
                RemoteName::new(remote_name),
                GitFetchRefExpression {
                    bookmark: bookmark_expr,
                    tag: StringExpression::none(),
                },
            )?;

            let result = auth_ctx.with_callbacks(
                Some(progress_sender.clone()),
                ws.session.enable_askpass,
                |cb, env| {
                    let mut subprocess_options = git_settings.to_subprocess_options();
                    subprocess_options.environment = env;

                    let mut fetcher =
                        git::GitFetch::new(tx.repo_mut(), subprocess_options, &import_options)?;

                    fetcher
                        .fetch(RemoteName::new(remote_name), refspecs, cb, None, None)
                        .context("failed to fetch")?;

                    pollster::block_on(fetcher.import_refs()).context("failed to import refs")?;

                    Ok(())
                },
            );

            if let Err(err) = result {
                return Ok(auth_ctx.into_result(err));
            }
        }

        match ws
            .finish_transaction(tx, "fetch from git remote(s)".to_string())
            .await?
        {
            Some(new_status) => Ok(MutationResult::Updated {
                new_status,
                new_selection: None,
            }),
            None => Ok(MutationResult::Unchanged),
        }
    }
}

#[async_trait(?Send)]
impl Mutation for GitPush {
    async fn execute(
        self: Box<Self>,
        ws: &mut WorkspaceSession,
        _options: &MutationOptions,
    ) -> Result<MutationResult> {
        let mut tx = ws.start_transaction().await?;

        // determine bookmarks to push, recording the old and new commits
        let mut remote_bookmark_updates: Vec<(&str, Vec<(RefNameBuf, Diff<Option<CommitId>>)>)> =
            Vec::new();
        let remote_bookmark_refs: Vec<_> = match &self.refspec {
            GitRefspec::AllBookmarks { remote_name } => {
                let remote_name_ref = RemoteNameBuf::from(remote_name);
                let mut bookmark_updates = Vec::new();
                for (bookmark_name, targets) in ws.view().local_remote_bookmarks(&remote_name_ref) {
                    if !targets.remote_ref.is_tracked() {
                        continue;
                    }

                    match classify_bookmark_push(bookmark_name.as_str(), remote_name, targets) {
                        Err(message) => return Ok(MutationResult::PreconditionError { message }),
                        Ok(None) => (),
                        Ok(Some(update)) => {
                            bookmark_updates.push((bookmark_name.to_owned(), update))
                        }
                    }
                }
                remote_bookmark_updates.push((remote_name, bookmark_updates));

                ws.view()
                    .remote_bookmarks(&remote_name_ref)
                    .map(|(name, remote_ref)| (name.to_owned(), remote_ref))
                    .collect()
            }
            GitRefspec::AllRemotes { bookmark_ref } => {
                let bookmark_name = bookmark_ref.as_bookmark()?;
                let bookmark_name_ref = RefNameBuf::from(bookmark_name);

                let mut remote_bookmark_refs = Vec::new();
                for (remote_name, group) in ws
                    .view()
                    .all_remote_bookmarks()
                    .filter_map(|(remote_ref_symbol, remote_ref)| {
                        if remote_ref.is_tracked()
                            && remote_ref_symbol.name == bookmark_name_ref
                            && remote_ref_symbol.remote != REMOTE_NAME_FOR_LOCAL_GIT_REPO
                        {
                            Some((remote_ref_symbol.remote, remote_ref))
                        } else {
                            None
                        }
                    })
                    .chunk_by(|(remote_name, _)| *remote_name)
                    .into_iter()
                {
                    let mut bookmark_updates = Vec::new();
                    for (_, remote_ref) in group {
                        let targets = LocalAndRemoteRef {
                            local_target: ws.view().get_local_bookmark(&bookmark_name_ref),
                            remote_ref,
                        };
                        match classify_bookmark_push(bookmark_name, remote_name.as_str(), targets) {
                            Err(message) => {
                                return Ok(MutationResult::PreconditionError { message });
                            }
                            Ok(None) => (),
                            Ok(Some(update)) => {
                                bookmark_updates.push((RefNameBuf::from(bookmark_name), update))
                            }
                        }
                        remote_bookmark_refs.push((RefNameBuf::from(bookmark_name), remote_ref));
                    }
                    remote_bookmark_updates.push((remote_name.as_str(), bookmark_updates));
                }

                remote_bookmark_refs
            }
            GitRefspec::RemoteBookmark {
                remote_name,
                bookmark_ref,
            } => {
                let bookmark_name = bookmark_ref.as_bookmark()?;
                let bookmark_name_ref = RefNameBuf::from(bookmark_name);
                let local_target = ws.view().get_local_bookmark(&bookmark_name_ref);
                let remote_name_ref = RemoteNameBuf::from(remote_name);
                let remote_ref_symbol = RemoteRefSymbol {
                    name: &bookmark_name_ref,
                    remote: &remote_name_ref,
                };
                let remote_ref = ws.view().get_remote_bookmark(remote_ref_symbol);

                match classify_bookmark_push(
                    bookmark_name,
                    remote_name,
                    LocalAndRemoteRef {
                        local_target,
                        remote_ref,
                    },
                ) {
                    Err(message) => return Ok(MutationResult::PreconditionError { message }),
                    Ok(None) => (),
                    Ok(Some(update)) => {
                        remote_bookmark_updates
                            .push((remote_name, vec![(RefNameBuf::from(bookmark_name), update)]));
                    }
                }

                vec![(
                    RefNameBuf::from(bookmark_name),
                    ws.view().get_remote_bookmark(remote_ref_symbol),
                )]
            }
        };

        // check for conflicts
        let mut new_heads = vec![];
        for (_, bookmark_updates) in &mut remote_bookmark_updates {
            for (_, update) in bookmark_updates {
                if let Some(new_target) = &update.after {
                    new_heads.push(new_target.clone());
                }
            }
        }

        let mut old_heads = remote_bookmark_refs
            .into_iter()
            .flat_map(|(_, old_head)| old_head.target.added_ids())
            .cloned()
            .collect_vec();
        if old_heads.is_empty() {
            old_heads.push(ws.repo().store().root_commit_id().clone());
        }

        let store = ws.repo().store().clone();
        let commits: Vec<_> = revset::walk_revs(ws.repo(), &new_heads, &old_heads)?
            .stream()
            .commits(&store)
            .try_collect()
            .block_on()?;
        for commit in commits {
            let mut reasons = vec![];
            if commit.description().is_empty() {
                reasons.push("it has no description");
            }
            if commit.author().name.is_empty()
                || commit.author().email.is_empty()
                || commit.committer().name.is_empty()
                || commit.committer().email.is_empty()
            {
                reasons.push("it has no author and/or committer set");
            }
            if commit.has_conflict() {
                reasons.push("it has conflicts");
            }
            if !reasons.is_empty() {
                precondition!(
                    "Won't push revision {} since {}",
                    ws.format_change_id(commit.id(), commit.change_id()).prefix,
                    reasons.join(" and ")
                );
            }
        }

        // check if there are any actual updates to push
        let has_updates = remote_bookmark_updates
            .iter()
            .any(|(_, updates)| !updates.is_empty());
        if !has_updates {
            match &self.refspec {
                GitRefspec::AllBookmarks { remote_name } => {
                    precondition!(
                        "No tracked bookmarks to push to remote '{remote_name}'. Track or push a bookmark first."
                    );
                }
                GitRefspec::AllRemotes { bookmark_ref } => {
                    let bookmark_name = bookmark_ref.as_bookmark()?;
                    precondition!("Bookmark '{bookmark_name}' is not tracked at any remote.");
                }
                GitRefspec::RemoteBookmark {
                    remote_name,
                    bookmark_ref,
                } => {
                    let bookmark_name = bookmark_ref.as_bookmark()?;
                    precondition!(
                        "Bookmark '{bookmark_name}' is not tracked at remote '{remote_name}'. Track it first."
                    );
                }
            }
        }

        // accumulate input requirements
        let mut auth_ctx = AuthContext::new(self.input);
        let event_sink = ws.sink();
        let subprocess_options = GitSubprocessOptions::from_settings(&ws.data.workspace_settings)?;

        // push to each remote
        for (remote_name, branch_updates) in remote_bookmark_updates.into_iter() {
            let targets = GitPushRefTargets {
                bookmarks: branch_updates,
                tags: Vec::new(),
            };

            let result = auth_ctx.with_callbacks(
                Some(event_sink.clone()),
                ws.session.enable_askpass,
                |cb, env| {
                    let mut subprocess_options = subprocess_options.clone();
                    subprocess_options.environment = env;

                    git::push_refs(
                        tx.repo_mut(),
                        subprocess_options,
                        RemoteName::new(remote_name),
                        &targets,
                        cb,
                        &GitPushOptions::default(),
                    )
                },
            );

            match result {
                Err(err) => return Ok(auth_ctx.into_result(err.into())),
                Ok(stats) if !stats.all_ok() => {
                    let format_refs = |refs: &[(_, Option<String>)]| {
                        refs.iter()
                            .map(|(ref_name, reason): &(GitRefNameBuf, _)| match reason {
                                Some(msg) if msg.as_str() != "stale info" => {
                                    format!("{} (reason: {})", ref_name.as_str(), msg)
                                }
                                _ => ref_name.as_str().to_string(),
                            })
                            .join(", ")
                    };

                    let mut message = String::new();

                    if !stats.rejected.is_empty() {
                        message += &format!(
                            "The following references unexpectedly moved on the remote: {}. Try fetching first.",
                            format_refs(&stats.rejected)
                        );
                    }

                    if !stats.remote_rejected.is_empty() {
                        if !message.is_empty() {
                            message += "\n\n";
                        }
                        message += &format!(
                            "The remote rejected the following updates: {}. Check if you have permission to push.",
                            format_refs(&stats.remote_rejected)
                        );
                    }

                    return Ok(MutationResult::PreconditionError { message });
                }
                Ok(_) => (),
            }
        }

        match ws
            .finish_transaction(
                tx,
                match &self.refspec {
                    GitRefspec::AllBookmarks { remote_name } => {
                        format!("push all tracked bookmarks to git remote {}", remote_name)
                    }
                    GitRefspec::AllRemotes { bookmark_ref } => {
                        format!(
                            "push {} to all tracked git remotes",
                            bookmark_ref.as_bookmark()?
                        )
                    }
                    GitRefspec::RemoteBookmark {
                        remote_name,
                        bookmark_ref,
                    } => {
                        format!(
                            "push {} to git remote {}",
                            bookmark_ref.as_bookmark()?,
                            remote_name
                        )
                    }
                },
            )
            .await?
        {
            Some(new_status) => Ok(MutationResult::Updated {
                new_status,
                new_selection: None,
            }),
            None => Ok(MutationResult::Unchanged),
        }
    }
}

// this is another case where it would be nice if we could reuse jj-cli's error messages
#[async_trait(?Send)]
impl Mutation for UndoOperation {
    async fn execute(
        self: Box<Self>,
        ws: &mut WorkspaceSession,
        _options: &MutationOptions,
    ) -> Result<MutationResult> {
        let head_op = op_walk::resolve_op_with_repo(ws.repo(), "@").await?; // XXX this should be behind an abstraction, maybe reused in snapshot
        let mut parent_ops = head_op.parents().await?.into_iter();

        let Some(parent_op) = parent_ops.next() else {
            precondition!("Cannot undo repo initialization");
        };

        if parent_ops.next().is_some() {
            precondition!("Cannot undo a merge operation");
        };

        let mut tx = ws.start_transaction().await?;
        let repo_loader = tx.base_repo().loader();
        let head_repo = repo_loader.load_at(&head_op).await?;
        let parent_repo = repo_loader.load_at(&parent_op).await?;
        tx.repo_mut().merge(&head_repo, &parent_repo).await?;
        let restored_view = tx.repo().view().store_view().clone();
        tx.repo_mut().set_view(restored_view);

        match ws
            .finish_transaction(tx, format!("undo operation {}", head_op.id().hex()))
            .await?
        {
            Some(new_status) => {
                let working_copy = ws.get_commit(ws.wc_id())?;
                let new_selection = Some(ws.format_header(&working_copy, None)?);
                Ok(MutationResult::Updated {
                    new_status,
                    new_selection,
                })
            }
            None => Ok(MutationResult::Unchanged),
        }
    }
}

#[async_trait(?Send)]
impl Mutation for ForgetWorkspace {
    async fn execute(
        self: Box<Self>,
        ws: &mut WorkspaceSession,
        _options: &MutationOptions,
    ) -> Result<MutationResult> {
        let workspace_name: WorkspaceNameBuf = self.name.into();

        let Some(wc_id) = ws.view().get_wc_commit_id(&workspace_name) else {
            precondition!("Workspace '{}' not found", workspace_name.as_symbol());
        };
        if *workspace_name == *ws.name() {
            precondition!("Cannot forget the current workspace");
        }

        let wc_commit = ws.get_commit(wc_id)?;

        let mut tx = ws.start_transaction().await?;
        tx.repo_mut().remove_wc_commit(&workspace_name).await?;

        // abandon the old WC commit if it's empty (same tree as parent merge)
        let parents = wc_commit.parents().await?;
        let parent_tree = rewrite::merge_commit_trees(tx.repo(), &parents).await?;
        if wc_commit.tree().tree_ids() == parent_tree.tree_ids() {
            tx.repo_mut().record_abandoned_commit(&wc_commit);
            tx.repo_mut().rebase_descendants().await?;
        }

        let workspace_store = SimpleWorkspaceStore::load(ws.workspace.repo_path())?;
        workspace_store.forget(&[&*workspace_name])?;

        match ws
            .finish_transaction(
                tx,
                format!("forget workspace '{}'", workspace_name.as_symbol()),
            )
            .await?
        {
            Some(new_status) => Ok(MutationResult::Updated {
                new_status,
                new_selection: None,
            }),
            None => Ok(MutationResult::Unchanged),
        }
    }
}

#[async_trait(?Send)]
impl Mutation for RenameWorkspace {
    async fn execute(
        self: Box<Self>,
        ws: &mut WorkspaceSession,
        _options: &MutationOptions,
    ) -> Result<MutationResult> {
        if self.new_name.is_empty() {
            precondition!("New workspace name cannot be empty");
        }

        let old_name: WorkspaceNameBuf = self.name.into();
        let new_name: WorkspaceNameBuf = self.new_name.into();

        if *old_name == *new_name {
            return Ok(MutationResult::Unchanged);
        }

        if ws.view().get_wc_commit_id(&old_name).is_none() {
            precondition!("Workspace '{}' not found", old_name.as_symbol());
        }
        if ws.view().get_wc_commit_id(&new_name).is_some() {
            precondition!("Workspace '{}' already exists", new_name.as_symbol());
        }
        if *old_name != *ws.name() {
            precondition!("Can only rename the current workspace");
        }

        match ws.rename_workspace(old_name, new_name).await? {
            Some(new_status) => Ok(MutationResult::Updated {
                new_status,
                new_selection: None,
            }),
            None => Ok(MutationResult::Unchanged),
        }
    }
}

async fn read_file_content(
    store: &Arc<Store>,
    tree: &MergedTree,
    path: &RepoPath,
) -> Result<Vec<u8>> {
    let entry = tree.path_value(path).await?;
    match entry.into_resolved() {
        Ok(Some(TreeValue::File { id, .. })) => {
            let mut reader = store.read_file(path, &id).await?;
            let mut content = Vec::new();
            reader.read_to_end(&mut content).await?;
            Ok(content)
        }
        Ok(Some(_)) => Ok(Vec::new()),
        Ok(None) => Ok(Vec::new()),
        Err(_) => {
            // handle conflicts by materializing them
            match conflicts::materialize_tree_value(
                store,
                path,
                tree.path_value(path).await?,
                tree.labels(),
            )
            .await?
            {
                MaterializedTreeValue::FileConflict(file) => {
                    let mut content = Vec::new();
                    conflicts::materialize_merge_result(
                        &file.contents,
                        &file.labels,
                        &mut content,
                        &ConflictMaterializeOptions {
                            marker_style: ConflictMarkerStyle::Git,
                            marker_len: None,
                            merge: MergeOptions {
                                hunk_level: FileMergeHunkLevel::Line,
                                same_change: SameChange::Accept,
                            },
                        },
                    )?;
                    Ok(content)
                }
                _ => Ok(Vec::new()),
            }
        }
    }
}

fn classify_bookmark_push(
    bookmark_name: &str,
    remote_name: &str,
    targets: LocalAndRemoteRef,
) -> Result<Option<Diff<Option<CommitId>>>, String> {
    let push_action = refs::classify_ref_push_action(targets);
    match push_action {
        RefPushAction::AlreadyMatches => Ok(None),
        RefPushAction::Update(update) => Ok(Some(update)),
        RefPushAction::LocalConflicted => Err(format!("Bookmark {} is conflicted.", bookmark_name)),
        RefPushAction::RemoteConflicted => Err(format!(
            "Bookmark {}@{} is conflicted. Try fetching first.",
            bookmark_name, remote_name
        )),
        RefPushAction::RemoteUntracked => Err(format!(
            "Non-tracking remote bookmark {}@{} exists. Try tracking it first.",
            bookmark_name, remote_name
        )),
    }
}

pub(crate) use precondition;

#[cfg(all(test, not(feature = "ts-rs")))]
mod tests {
    use super::*;
    use crate::{
        messages::{
            RevSet, StoreRef,
            mutations::{CreateRevision, DescribeRevision, MoveRef},
            queries::RevsResult,
        },
        worker::{
            WorkerSession, queries,
            tests::{mkrepo, query_by_id, revs},
        },
    };
    use anyhow::Result;
    use assert_matches::assert_matches;
    use jj_lib::ref_name::WorkspaceName;
    use jj_lib::str_util::StringMatcher;
    use std::fs;

    #[tokio::test]
    async fn immutability_of_bookmark() -> Result<()> {
        let repo = mkrepo();

        let mut session = WorkerSession::default();
        let mut ws = session.load_workspace(repo.path()).await?;

        let immutable_matcher = StringMatcher::Exact("immutable_bookmark".to_string());
        let (_, ref_at_start) = ws
            .view()
            .local_bookmarks_matching(&immutable_matcher)
            .next()
            .unwrap();
        let ref_at_start = ref_at_start.as_normal().unwrap().clone();
        assert_matches!(ws.check_immutable([ref_at_start.clone()]), Ok(true));

        let header = queries::query_revision(&ws, &revs::immutable_bookmark())?
            .expect("immutable_bookmark exists");
        let immutable_bm = header
            .refs
            .iter()
            .find(|r| {
                matches!(
                    r,
                    StoreRef::LocalBookmark {
                        bookmark_name,
                        ..
                        } if bookmark_name == "immutable_bookmark"
                )
            })
            .unwrap();

        let MutationResult::Updated {
            new_selection: Some(new_selection),
            ..
        } = CreateRevision {
            set: RevSet::singleton(revs::working_copy()),
        }
        .execute_unboxed(&mut ws)
        .await?
        else {
            panic!("Creating new revision didn't update the selection");
        };

        MoveRef {
            r#ref: immutable_bm.clone(),
            to_id: new_selection.id.clone(),
        }
        .execute_unboxed(&mut ws)
        .await?;

        let (_, after_change) = ws
            .view()
            .local_bookmarks_matching(&immutable_matcher)
            .next()
            .unwrap();
        let after_change = after_change.as_normal().unwrap().clone();
        assert_ne!(ref_at_start, after_change);

        assert_matches!(ws.check_immutable([after_change]), Ok(true));

        Ok(())
    }

    #[tokio::test]
    async fn immutable_workspace_head() -> Result<()> {
        let repo = mkrepo();

        let mut session = WorkerSession::default();
        let mut ws = session.load_workspace(repo.path()).await?;

        let immutable_matcher = StringMatcher::Exact("immutable_bookmark".to_string());

        let header = queries::query_revision(&ws, &revs::immutable_bookmark())?
            .expect("immutable_bookmark exists");
        let immutable_bm = header
            .refs
            .iter()
            .find(|r| {
                matches!(
                    r,
                    StoreRef::LocalBookmark {
                        bookmark_name,
                        ..
                        } if bookmark_name == "immutable_bookmark"
                )
            })
            .unwrap();

        let working_copy = revs::working_copy();
        MoveRef {
            r#ref: immutable_bm.clone(),
            to_id: working_copy,
        }
        .execute_unboxed(&mut ws)
        .await?;

        let (_, after_change) = ws
            .view()
            .local_bookmarks_matching(&immutable_matcher)
            .next()
            .unwrap();
        let after_change = after_change.as_normal().unwrap().clone();

        // rev containing the bookmark is now immutable:
        assert_matches!(ws.check_immutable([after_change]), Ok(true));

        // checked-out rev is not immutable (because we made a new one):
        let current_ws_heads: Vec<jj_lib::backend::CommitId> = ws
            .repo()
            .view()
            .wc_commit_ids()
            .iter()
            .map(|(_, id)| id.clone())
            .collect();
        assert_matches!(ws.check_immutable(current_ws_heads), Ok(false));

        Ok(())
    }

    // XXX possibly this should be a session test using the ExecuteSnapshot event
    #[tokio::test]
    async fn snapshot_respects_auto_track_config() -> Result<()> {
        let repo = mkrepo();

        // Configure snapshot.auto-track to only track .txt files
        let config_path = repo.path().join(".jj/repo/config.toml");
        let config_content = r#"
[snapshot]
auto-track = "glob:*.txt"
"#;
        fs::write(&config_path, config_content).unwrap();

        let mut session = WorkerSession::default();
        let mut ws = session.load_workspace(repo.path()).await?;

        // Write two new files, one tracked and one untracked
        fs::write(repo.path().join("tracked.txt"), "tracked content").unwrap();
        fs::write(repo.path().join("untracked.dat"), "untracked content").unwrap();

        // Trigger a snapshot by describing the revision
        DescribeRevision {
            id: revs::working_copy(),
            new_description: "test auto-track".to_owned(),
            reset_author: false,
        }
        .execute_unboxed(&mut ws)
        .await?;

        // Verify: only the .txt file should have been tracked
        let rev = query_by_id(&ws, revs::working_copy()).await?;
        match rev {
            RevsResult::Detail { changes, .. } => {
                assert_eq!(changes.len(), 1);
                assert_eq!(changes[0].path.repo_path, "tracked.txt");
            }
            _ => panic!("Expected working copy to exist"),
        }

        // Verify: the .dat file should exist, but be untracked
        assert!(repo.path().join("untracked.dat").exists());

        Ok(())
    }

    #[tokio::test]
    async fn forget_workspace_via_mutation() -> Result<()> {
        let repo = mkrepo();

        let mut session = WorkerSession::default();
        let mut ws = session.load_workspace(repo.path()).await?;

        ws.add_workspace("second".to_owned(), repo.path().join("second-workspace"))
            .await?;
        assert!(
            ws.view()
                .get_wc_commit_id(WorkspaceName::new("second"))
                .is_some()
        );

        let result = ForgetWorkspace {
            name: "second".to_owned(),
        }
        .execute_unboxed(&mut ws)
        .await?;

        assert_matches!(result, MutationResult::Updated { .. });
        assert!(
            ws.view()
                .get_wc_commit_id(WorkspaceName::new("second"))
                .is_none()
        );

        Ok(())
    }

    #[tokio::test]
    async fn forget_workspace_abandons_empty_wc() -> Result<()> {
        let repo = mkrepo();

        let mut session = WorkerSession::default();
        let mut ws = session.load_workspace(repo.path()).await?;

        ws.add_workspace("second".to_owned(), repo.path().join("second-workspace"))
            .await?;

        let count_before = queries::query_log(&ws, "all()", 100)?.rows.len();

        ForgetWorkspace {
            name: "second".to_owned(),
        }
        .execute_unboxed(&mut ws)
        .await?;

        let count_after = queries::query_log(&ws, "all()", 100)?.rows.len();
        assert_eq!(
            count_before - 1,
            count_after,
            "empty WC commit should be abandoned"
        );

        Ok(())
    }

    #[tokio::test]
    async fn forget_workspace_rejects_current() -> Result<()> {
        let repo = mkrepo();

        let mut session = WorkerSession::default();
        let mut ws = session.load_workspace(repo.path()).await?;

        let result = ForgetWorkspace {
            name: ws.name().as_str().to_owned(),
        }
        .execute_unboxed(&mut ws)
        .await?;

        assert_matches!(result, MutationResult::PreconditionError { message } if message.contains("current workspace"));

        Ok(())
    }

    #[tokio::test]
    async fn forget_workspace_rejects_nonexistent() -> Result<()> {
        let repo = mkrepo();

        let mut session = WorkerSession::default();
        let mut ws = session.load_workspace(repo.path()).await?;

        let result = ForgetWorkspace {
            name: "nonexistent".to_owned(),
        }
        .execute_unboxed(&mut ws)
        .await?;

        assert_matches!(result, MutationResult::PreconditionError { message } if message.contains("not found"));

        Ok(())
    }

    #[tokio::test]
    async fn rename_workspace_via_mutation() -> Result<()> {
        let repo = mkrepo();

        let mut session = WorkerSession::default();
        let mut ws = session.load_workspace(repo.path()).await?;

        let old_name = ws.name().as_str().to_owned();

        let result = RenameWorkspace {
            name: old_name.clone(),
            new_name: "renamed".to_owned(),
        }
        .execute_unboxed(&mut ws)
        .await?;

        assert_matches!(result, MutationResult::Updated { .. });
        assert_eq!(ws.name().as_str(), "renamed");
        assert!(
            ws.view()
                .get_wc_commit_id(WorkspaceName::new(&old_name))
                .is_none()
        );
        assert!(
            ws.view()
                .get_wc_commit_id(WorkspaceName::new("renamed"))
                .is_some()
        );

        Ok(())
    }

    #[tokio::test]
    async fn rename_workspace_same_name_is_unchanged() -> Result<()> {
        let repo = mkrepo();

        let mut session = WorkerSession::default();
        let mut ws = session.load_workspace(repo.path()).await?;

        let name = ws.name().as_str().to_owned();

        let result = RenameWorkspace {
            name: name.clone(),
            new_name: name,
        }
        .execute_unboxed(&mut ws)
        .await?;

        assert_matches!(result, MutationResult::Unchanged);

        Ok(())
    }

    #[tokio::test]
    async fn rename_workspace_rejects_empty_name() -> Result<()> {
        let repo = mkrepo();

        let mut session = WorkerSession::default();
        let mut ws = session.load_workspace(repo.path()).await?;

        let result = RenameWorkspace {
            name: ws.name().as_str().to_owned(),
            new_name: "".to_owned(),
        }
        .execute_unboxed(&mut ws)
        .await?;

        assert_matches!(result, MutationResult::PreconditionError { message } if message.contains("cannot be empty"));

        Ok(())
    }

    #[tokio::test]
    async fn rename_workspace_rejects_duplicate_name() -> Result<()> {
        let repo = mkrepo();

        let mut session = WorkerSession::default();
        let mut ws = session.load_workspace(repo.path()).await?;

        ws.add_workspace("other".to_owned(), repo.path().join("other-workspace"))
            .await?;

        let result = RenameWorkspace {
            name: ws.name().as_str().to_owned(),
            new_name: "other".to_owned(),
        }
        .execute_unboxed(&mut ws)
        .await?;

        assert_matches!(result, MutationResult::PreconditionError { message } if message.contains("already exists"));

        Ok(())
    }

    #[tokio::test]
    async fn rename_workspace_rejects_non_current() -> Result<()> {
        let repo = mkrepo();

        let mut session = WorkerSession::default();
        let mut ws = session.load_workspace(repo.path()).await?;

        ws.add_workspace("other".to_owned(), repo.path().join("other-workspace"))
            .await?;

        let result = RenameWorkspace {
            name: "other".to_owned(),
            new_name: "renamed".to_owned(),
        }
        .execute_unboxed(&mut ws)
        .await?;

        assert_matches!(result, MutationResult::PreconditionError { message } if message.contains("current workspace"));

        Ok(())
    }
}