forked from martinvonz/jj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
3537 lines (3356 loc) · 127 KB
/
mod.rs
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
// Copyright 2020 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#[cfg(feature = "bench")]
mod bench;
mod branch;
mod git;
mod operation;
use std::collections::{BTreeMap, HashSet};
use std::fmt::Debug;
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::PathBuf;
use std::sync::Arc;
use std::{fs, io};
use clap::builder::NonEmptyStringValueParser;
use clap::{ArgGroup, ArgMatches, Command, CommandFactory, FromArgMatches, Subcommand};
use indexmap::{IndexMap, IndexSet};
use itertools::Itertools;
use jujutsu_lib::backend::{CommitId, ObjectId, TreeValue};
use jujutsu_lib::commit::Commit;
use jujutsu_lib::dag_walk::topo_order_reverse;
use jujutsu_lib::default_index_store::{DefaultIndexStore, ReadonlyIndexWrapper};
use jujutsu_lib::matchers::EverythingMatcher;
use jujutsu_lib::op_store::{RefTarget, WorkspaceId};
use jujutsu_lib::repo::{ReadonlyRepo, Repo};
use jujutsu_lib::repo_path::RepoPath;
use jujutsu_lib::revset::{
ReverseRevsetGraphIterator, RevsetAliasesMap, RevsetExpression, RevsetFilterPredicate,
RevsetGraphEdge, RevsetGraphEdgeType, RevsetIteratorExt,
};
use jujutsu_lib::rewrite::{back_out_commit, merge_commit_trees, rebase_commit, DescendantRebaser};
use jujutsu_lib::settings::UserSettings;
use jujutsu_lib::tree::{merge_trees, Tree};
use jujutsu_lib::workspace::Workspace;
use jujutsu_lib::{conflicts, file_util, revset};
use maplit::{hashmap, hashset};
use crate::cli_util::{
check_stale_working_copy, get_config_file_path, print_checkout_stats,
resolve_multiple_nonempty_revsets, resolve_mutliple_nonempty_revsets_flag_guarded,
run_ui_editor, serialize_config_value, short_commit_hash, user_error, user_error_with_hint,
write_config_value_to_file, Args, CommandError, CommandHelper, DescriptionArg,
LogContentFormat, RevisionArg, WorkspaceCommandHelper,
};
use crate::config::{AnnotatedValue, ConfigSource};
use crate::diff_util::{self, DiffFormat, DiffFormatArgs};
use crate::formatter::{Formatter, PlainTextFormatter};
use crate::graphlog::{get_graphlog, Edge};
use crate::ui::Ui;
use crate::{template_parser, text_util};
#[derive(clap::Parser, Clone, Debug)]
enum Commands {
Abandon(AbandonArgs),
Backout(BackoutArgs),
#[cfg(feature = "bench")]
#[command(subcommand)]
Bench(bench::BenchCommands),
#[command(subcommand)]
Branch(branch::BranchSubcommand),
#[command(alias = "print")]
Cat(CatArgs),
Checkout(CheckoutArgs),
Commit(CommitArgs),
#[command(subcommand)]
Config(ConfigSubcommand),
#[command(subcommand)]
Debug(DebugCommands),
Describe(DescribeArgs),
Diff(DiffArgs),
Diffedit(DiffeditArgs),
Duplicate(DuplicateArgs),
Edit(EditArgs),
Files(FilesArgs),
#[command(subcommand)]
Git(git::GitCommands),
Init(InitArgs),
Interdiff(InterdiffArgs),
Log(LogArgs),
/// Merge work from multiple branches
///
/// Unlike most other VCSs, `jj merge` does not implicitly include the
/// working copy revision's parent as one of the parents of the merge;
/// you need to explicitly list all revisions that should become parents
/// of the merge.
///
/// This is the same as `jj new`, except that it requires at least two
/// arguments.
Merge(NewArgs),
Move(MoveArgs),
New(NewArgs),
Obslog(ObslogArgs),
#[command(subcommand)]
#[command(visible_alias = "op")]
Operation(operation::OperationCommands),
Rebase(RebaseArgs),
Resolve(ResolveArgs),
Restore(RestoreArgs),
Show(ShowArgs),
Sparse(SparseArgs),
Split(SplitArgs),
Squash(SquashArgs),
Status(StatusArgs),
#[command(subcommand)]
Util(UtilCommands),
/// Undo an operation (shortcut for `jj op undo`)
Undo(operation::OperationUndoArgs),
Unsquash(UnsquashArgs),
Untrack(UntrackArgs),
Version(VersionArgs),
#[command(subcommand)]
Workspace(WorkspaceCommands),
}
/// Display version information
#[derive(clap::Args, Clone, Debug)]
struct VersionArgs {}
/// Create a new repo in the given directory
///
/// If the given directory does not exist, it will be created. If no directory
/// is given, the current directory is used.
#[derive(clap::Args, Clone, Debug)]
#[command(group(ArgGroup::new("backend").args(&["git", "git_repo"])))]
struct InitArgs {
/// The destination directory
#[arg(default_value = ".", value_hint = clap::ValueHint::DirPath)]
destination: String,
/// Use the Git backend, creating a jj repo backed by a Git repo
#[arg(long)]
git: bool,
/// Path to a git repo the jj repo will be backed by
#[arg(long, value_hint = clap::ValueHint::DirPath)]
git_repo: Option<String>,
}
#[derive(clap::Args, Clone, Debug)]
#[command(group = clap::ArgGroup::new("config_level").multiple(false).required(true))]
struct ConfigArgs {
/// Target the user-level config
#[arg(long, group = "config_level")]
user: bool,
/// Target the repo-level config
#[arg(long, group = "config_level")]
repo: bool,
}
impl ConfigArgs {
fn get_source_kind(&self) -> ConfigSource {
if self.user {
ConfigSource::User
} else if self.repo {
ConfigSource::Repo
} else {
// Shouldn't be reachable unless clap ArgGroup is broken.
panic!("No config_level provided");
}
}
}
/// Manage config options
///
/// Operates on jj configuration, which comes from the config file and
/// environment variables. Uses the config file at ~/.jjconfig.toml or
/// $XDG_CONFIG_HOME/jj/config.toml, unless overridden with the JJ_CONFIG
/// environment variable, combined with repo config at .jj/repo/config.toml
/// if present.
///
/// For supported config options and more details about jj config, see
/// https://github.com/martinvonz/jj/blob/main/docs/config.md.
#[derive(clap::Subcommand, Clone, Debug)]
enum ConfigSubcommand {
#[command(visible_alias("l"))]
List(ConfigListArgs),
#[command(visible_alias("s"))]
Set(ConfigSetArgs),
#[command(visible_alias("e"))]
Edit(ConfigEditArgs),
}
/// List variables set in config file, along with their values.
#[derive(clap::Args, Clone, Debug)]
struct ConfigListArgs {
/// An optional name of a specific config option to look up.
#[arg(value_parser = NonEmptyStringValueParser::new())]
pub name: Option<String>,
/// Whether to explicitly include built-in default values in the list.
#[arg(long)]
pub include_defaults: bool,
// TODO(#1047): Support --show-origin using LayeredConfigs.
// TODO(#1047): Support ConfigArgs (--user or --repo).
}
/// Update config file to set the given option to a given value.
#[derive(clap::Args, Clone, Debug)]
struct ConfigSetArgs {
#[arg(required = true)]
name: String,
#[arg(required = true)]
value: String,
#[clap(flatten)]
config_args: ConfigArgs,
}
/// Start an editor on a jj config file.
#[derive(clap::Args, Clone, Debug)]
struct ConfigEditArgs {
#[clap(flatten)]
pub config_args: ConfigArgs,
}
/// Create a new, empty change and edit it in the working copy
///
/// For more information, see
/// https://github.com/martinvonz/jj/blob/main/docs/working-copy.md.
#[derive(clap::Args, Clone, Debug)]
#[command(visible_aliases = &["co"])]
struct CheckoutArgs {
/// The revision to update to
revision: RevisionArg,
/// Ignored (but lets you pass `-r` for consistency with other commands)
#[arg(short = 'r', hide = true)]
unused_revision: bool,
/// The change description to use
#[arg(long, short, default_value = "")]
message: DescriptionArg,
}
/// Stop tracking specified paths in the working copy
#[derive(clap::Args, Clone, Debug)]
struct UntrackArgs {
/// Paths to untrack
#[arg(required = true, value_hint = clap::ValueHint::AnyPath)]
paths: Vec<String>,
}
/// List files in a revision
#[derive(clap::Args, Clone, Debug)]
struct FilesArgs {
/// The revision to list files in
#[arg(long, short, default_value = "@")]
revision: RevisionArg,
/// Only list files matching these prefixes (instead of all files)
#[arg(value_hint = clap::ValueHint::AnyPath)]
paths: Vec<String>,
}
/// Print contents of a file in a revision
#[derive(clap::Args, Clone, Debug)]
struct CatArgs {
/// The revision to get the file contents from
#[arg(long, short, default_value = "@")]
revision: RevisionArg,
/// The file to print
#[arg(value_hint = clap::ValueHint::FilePath)]
path: String,
}
/// Show changes in a revision
///
/// With the `-r` option, which is the default, shows the changes compared to
/// the parent revision. If there are several parent revisions (i.e., the given
/// revision is a merge), then they will be merged and the changes from the
/// result to the given revision will be shown.
///
/// With the `--from` and/or `--to` options, shows the difference from/to the
/// given revisions. If either is left out, it defaults to the working-copy
/// commit. For example, `jj diff --from main` shows the changes from "main"
/// (perhaps a branch name) to the working-copy commit.
#[derive(clap::Args, Clone, Debug)]
struct DiffArgs {
/// Show changes in this revision, compared to its parent(s)
#[arg(long, short)]
revision: Option<RevisionArg>,
/// Show changes from this revision
#[arg(long, conflicts_with = "revision")]
from: Option<RevisionArg>,
/// Show changes to this revision
#[arg(long, conflicts_with = "revision")]
to: Option<RevisionArg>,
/// Restrict the diff to these paths
#[arg(value_hint = clap::ValueHint::AnyPath)]
paths: Vec<String>,
#[command(flatten)]
format: DiffFormatArgs,
}
/// Show commit description and changes in a revision
#[derive(clap::Args, Clone, Debug)]
struct ShowArgs {
/// Show changes in this revision, compared to its parent(s)
#[arg(default_value = "@")]
revision: RevisionArg,
/// Ignored (but lets you pass `-r` for consistency with other commands)
#[arg(short = 'r', hide = true)]
unused_revision: bool,
#[command(flatten)]
format: DiffFormatArgs,
}
/// Show high-level repo status
///
/// This includes:
///
/// * The working copy commit and its (first) parent, and a summary of the
/// changes between them
///
/// * Conflicted branches (see https://github.com/martinvonz/jj/blob/main/docs/branches.md)
#[derive(clap::Args, Clone, Debug)]
#[command(visible_alias = "st")]
struct StatusArgs {}
/// Show commit history
#[derive(clap::Args, Clone, Debug)]
struct LogArgs {
/// Which revisions to show. Defaults to the `ui.default-revset` setting,
/// or `@ | (remote_branches() | tags()).. | ((remote_branches() |
/// tags())..)-` if it is not set.
#[arg(long, short)]
revisions: Option<RevisionArg>,
/// Show commits modifying the given paths
#[arg(value_hint = clap::ValueHint::AnyPath)]
paths: Vec<String>,
/// Show revisions in the opposite order (older revisions first)
#[arg(long)]
reversed: bool,
/// Don't show the graph, show a flat list of revisions
#[arg(long)]
no_graph: bool,
/// Render each revision using the given template
///
/// For the syntax, see https://github.com/martinvonz/jj/blob/main/docs/templates.md
#[arg(long, short = 'T')]
template: Option<String>,
/// Show patch
#[arg(long, short = 'p')]
patch: bool,
#[command(flatten)]
diff_format: DiffFormatArgs,
}
/// Show how a change has evolved
///
/// Show how a change has evolved as it's been updated, rebased, etc.
#[derive(clap::Args, Clone, Debug)]
struct ObslogArgs {
#[arg(long, short, default_value = "@")]
revision: RevisionArg,
/// Don't show the graph, show a flat list of revisions
#[arg(long)]
no_graph: bool,
/// Render each revision using the given template
///
/// For the syntax, see https://github.com/martinvonz/jj/blob/main/docs/templates.md
#[arg(long, short = 'T')]
template: Option<String>,
/// Show patch compared to the previous version of this change
///
/// If the previous version has different parents, it will be temporarily
/// rebased to the parents of the new version, so the diff is not
/// contaminated by unrelated changes.
#[arg(long, short = 'p')]
patch: bool,
#[command(flatten)]
diff_format: DiffFormatArgs,
}
/// Compare the changes of two commits
///
/// This excludes changes from other commits by temporarily rebasing `--from`
/// onto `--to`'s parents. If you wish to compare the same change across
/// versions, consider `jj obslog -p` instead.
#[derive(clap::Args, Clone, Debug)]
#[command(group(ArgGroup::new("to_diff").args(&["from", "to"]).multiple(true).required(true)))]
struct InterdiffArgs {
/// Show changes from this revision
#[arg(long)]
from: Option<RevisionArg>,
/// Show changes to this revision
#[arg(long)]
to: Option<RevisionArg>,
/// Restrict the diff to these paths
#[arg(value_hint = clap::ValueHint::AnyPath)]
paths: Vec<String>,
#[command(flatten)]
format: DiffFormatArgs,
}
/// Update the change description or other metadata
///
/// Starts an editor to let you edit the description of a change. The editor
/// will be $EDITOR, or `pico` if that's not defined.
#[derive(clap::Args, Clone, Debug)]
struct DescribeArgs {
/// The revision whose description to edit
#[arg(default_value = "@")]
revision: RevisionArg,
/// Ignored (but lets you pass `-r` for consistency with other commands)
#[arg(short = 'r', hide = true)]
unused_revision: bool,
/// The change description to use (don't open editor)
#[arg(long, short)]
message: Option<DescriptionArg>,
/// Read the change description from stdin
#[arg(long)]
stdin: bool,
/// Don't open an editor
///
/// This is mainly useful in combination with e.g. `--reset-author`.
#[arg(long)]
no_edit: bool,
/// Reset the author to the configured user
///
/// This resets the author name, email, and timestamp.
#[arg(long)]
reset_author: bool,
}
/// Update the description and create a new change on top.
#[derive(clap::Args, Clone, Debug)]
#[command(visible_aliases=&["ci"])]
struct CommitArgs {
/// The change description to use (don't open editor)
#[arg(long, short)]
message: Option<DescriptionArg>,
}
/// Create a new change with the same content as an existing one
#[derive(clap::Args, Clone, Debug)]
struct DuplicateArgs {
/// The revision(s) to duplicate
#[arg(default_value = "@")]
revisions: Vec<RevisionArg>,
/// Ignored (but lets you pass `-r` for consistency with other commands)
#[arg(short = 'r', hide = true)]
unused_revision: bool,
}
/// Abandon a revision
///
/// Abandon a revision, rebasing descendants onto its parent(s). The behavior is
/// similar to `jj restore`; the difference is that `jj abandon` gives you a new
/// change, while `jj restore` updates the existing change.
#[derive(clap::Args, Clone, Debug)]
struct AbandonArgs {
/// The revision(s) to abandon
#[arg(default_value = "@")]
revisions: Vec<RevisionArg>,
/// Do not print every abandoned commit on a separate line
#[arg(long, short)]
summary: bool,
/// Ignored (but lets you pass `-r` for consistency with other commands)
#[arg(short = 'r', hide = true)]
unused_revision: bool,
}
/// Edit a commit in the working copy
///
/// Puts the contents of a commit in the working copy for editing. Any changes
/// you make in the working copy will update (amend) the commit.
#[derive(clap::Args, Clone, Debug)]
struct EditArgs {
/// The commit to edit
revision: RevisionArg,
/// Ignored (but lets you pass `-r` for consistency with other commands)
#[arg(short = 'r', hide = true)]
unused_revision: bool,
}
/// Create a new, empty change and edit it in the working copy
///
/// Note that you can create a merge commit by specifying multiple revisions as
/// argument. For example, `jj new main @` will create a new commit with the
/// `main` branch and the working copy as parents.
///
/// For more information, see
/// https://github.com/martinvonz/jj/blob/main/docs/working-copy.md.
#[derive(clap::Args, Clone, Debug)]
#[command(group(ArgGroup::new("order").args(&["insert_after", "insert_before"])))]
struct NewArgs {
/// Parent(s) of the new change
#[arg(default_value = "@")]
revisions: Vec<RevisionArg>,
/// Ignored (but lets you pass `-r` for consistency with other commands)
#[arg(short = 'r', hide = true)]
unused_revision: bool,
/// The change description to use
#[arg(long, short, default_value = "")]
message: DescriptionArg,
/// Allow revsets expanding to multiple commits in a single argument
#[arg(long, short = 'L')]
allow_large_revsets: bool,
/// Insert the new change between the target commit(s) and their children
#[arg(long, short = 'A', visible_alias = "after")]
insert_after: bool,
/// Insert the new change between the target commit(s) and their parents
#[arg(long, short = 'B', visible_alias = "before")]
insert_before: bool,
}
/// Move changes from one revision into another
///
/// Use `--interactive` to move only part of the source revision into the
/// destination. The selected changes (or all the changes in the source revision
/// if not using `--interactive`) will be moved into the destination. The
/// changes will be removed from the source. If that means that the source is
/// now empty compared to its parent, it will be abandoned. Without
/// `--interactive`, the source change will always be empty.
///
/// If the source became empty and both the source and destination had a
/// non-empty description, you will be asked for the combined description. If
/// either was empty, then the other one will be used.
#[derive(clap::Args, Clone, Debug)]
#[command(group(ArgGroup::new("to_move").args(&["from", "to"]).multiple(true).required(true)))]
struct MoveArgs {
/// Move part of this change into the destination
#[arg(long)]
from: Option<RevisionArg>,
/// Move part of the source into this change
#[arg(long)]
to: Option<RevisionArg>,
/// Interactively choose which parts to move
#[arg(long, short)]
interactive: bool,
/// Move only changes to these paths (instead of all paths)
#[arg(conflicts_with = "interactive", value_hint = clap::ValueHint::AnyPath)]
paths: Vec<String>,
}
/// Move changes from a revision into its parent
///
/// After moving the changes into the parent, the child revision will have the
/// same content state as before. If that means that the change is now empty
/// compared to its parent, it will be abandoned.
/// Without `--interactive`, the child change will always be empty.
///
/// If the source became empty and both the source and destination had a
/// non-empty description, you will be asked for the combined description. If
/// either was empty, then the other one will be used.
#[derive(clap::Args, Clone, Debug)]
#[command(visible_alias = "amend")]
struct SquashArgs {
#[arg(long, short, default_value = "@")]
revision: RevisionArg,
/// The description to use for squashed revision (don't open editor)
#[arg(long, short)]
message: Option<DescriptionArg>,
/// Interactively choose which parts to squash
#[arg(long, short)]
interactive: bool,
/// Move only changes to these paths (instead of all paths)
#[arg(conflicts_with = "interactive", value_hint = clap::ValueHint::AnyPath)]
paths: Vec<String>,
}
/// Move changes from a revision's parent into the revision
///
/// After moving the changes out of the parent, the child revision will have the
/// same content state as before. If moving the change out of the parent change
/// made it empty compared to its parent, it will be abandoned. Without
/// `--interactive`, the parent change will always become empty.
///
/// If the source became empty and both the source and destination had a
/// non-empty description, you will be asked for the combined description. If
/// either was empty, then the other one will be used.
#[derive(clap::Args, Clone, Debug)]
#[command(visible_alias = "unamend")]
struct UnsquashArgs {
#[arg(long, short, default_value = "@")]
revision: RevisionArg,
/// Interactively choose which parts to unsquash
// TODO: It doesn't make much sense to run this without -i. We should make that
// the default.
#[arg(long, short)]
interactive: bool,
}
/// Resolve a conflicted file with an external merge tool
///
/// Only conflicts that can be resolved with a 3-way merge are supported. See
/// docs for merge tool configuration instructions.
///
/// Note that conflicts can also be resolved without using this command. You may
/// edit the conflict markers in the conflicted file directly with a text
/// editor.
// TODOs:
// - `jj resolve --editor` to resolve a conflict in the default text editor. Should work for
// conflicts with 3+ adds. Useful to resolve conflicts in a commit other than the current one.
// - A way to help split commits with conflicts that are too complicated (more than two sides)
// into commits with simpler conflicts. In case of a tree with many merges, we could for example
// point to existing commits with simpler conflicts where resolving those conflicts would help
// simplify the present one.
#[derive(clap::Args, Clone, Debug)]
struct ResolveArgs {
#[arg(long, short, default_value = "@")]
revision: String,
/// Instead of resolving one conflict, list all the conflicts
// TODO: Also have a `--summary` option. `--list` currently acts like
// `diff --summary`, but should be more verbose.
#[arg(long, short)]
list: bool,
/// Do not print the list of remaining conflicts (if any) after resolving a
/// conflict
#[arg(long, short, conflicts_with = "list")]
quiet: bool,
/// Restrict to these paths when searching for a conflict to resolve. We
/// will attempt to resolve the first conflict we can find. You can use
/// the `--list` argument to find paths to use here.
// TODO: Find the conflict we can resolve even if it's not the first one.
#[arg(value_hint = clap::ValueHint::AnyPath)]
paths: Vec<String>,
}
/// Restore paths from another revision
///
/// That means that the paths get the same content in the destination (`--to`)
/// as they had in the source (`--from`). This is typically used for undoing
/// changes to some paths in the working copy (`jj restore <paths>`).
///
/// When neither `--from` nor `--to` is specified, the command restores into the
/// working copy from its parent. If one of `--from` or `--to` is specified, the
/// other one defaults to the working copy.
///
/// See `jj diffedit` if you'd like to restore portions of files rather than
/// entire files.
#[derive(clap::Args, Clone, Debug)]
struct RestoreArgs {
/// Revision to restore from (source)
#[arg(long)]
from: Option<RevisionArg>,
/// Revision to restore into (destination)
#[arg(long)]
to: Option<RevisionArg>,
/// Restore only these paths (instead of all paths)
#[arg(value_hint = clap::ValueHint::AnyPath)]
paths: Vec<String>,
}
/// Touch up the content changes in a revision with a diff editor
///
/// With the `-r` option, which is the default, starts a diff editor (`meld` by
/// default) on the changes in the revision.
///
/// With the `--from` and/or `--to` options, starts a diff editor comparing the
/// "from" revision to the "to" revision.
///
/// Edit the right side of the diff until it looks the way you want. Once you
/// close the editor, the revision specified with `-r` or `--to` will be
/// updated. Descendants will be rebased on top as usual, which may result in
/// conflicts.
///
/// See `jj restore` if you want to move entire files from one revision to
/// another. See `jj squash -i` or `jj unsquash -i` if you instead want to move
/// changes into or out of the parent revision.
#[derive(clap::Args, Clone, Debug)]
struct DiffeditArgs {
/// The revision to touch up. Defaults to @ if --to/--from are not
/// specified.
#[arg(long, short)]
revision: Option<RevisionArg>,
/// Show changes from this revision. Defaults to @ if --to is specified.
#[arg(long, conflicts_with = "revision")]
from: Option<RevisionArg>,
/// Edit changes in this revision. Defaults to @ if --from is specified.
#[arg(long, conflicts_with = "revision")]
to: Option<RevisionArg>,
}
/// Split a revision in two
///
/// Starts a diff editor (`meld` by default) on the changes in the revision.
/// Edit the right side of the diff until it has the content you want in the
/// first revision. Once you close the editor, your edited content will replace
/// the previous revision. The remaining changes will be put in a new revision
/// on top. You will be asked to enter a change description for each.
#[derive(clap::Args, Clone, Debug)]
struct SplitArgs {
/// The revision to split
#[arg(long, short, default_value = "@")]
revision: RevisionArg,
/// Put these paths in the first commit and don't run the diff editor
#[arg(value_hint = clap::ValueHint::AnyPath)]
paths: Vec<String>,
}
/// Move revisions to different parent(s)
///
/// There are three different ways of specifying which revisions to rebase:
/// `-b` to rebase a whole branch, `-s` to rebase a revision and its
/// descendants, and `-r` to rebase a single commit. If none of them is
/// specified, it defaults to `-b @`.
///
/// With `-s`, the command rebases the specified revision and its descendants
/// onto the destination. For example, `jj rebase -s M -d O` would transform
/// your history like this (letters followed by an apostrophe are post-rebase
/// versions):
///
/// O N'
/// | |
/// | N M'
/// | | |
/// | M O
/// | | => |
/// | | L | L
/// | |/ | |
/// | K | K
/// |/ |/
/// J J
///
/// With `-b`, the command rebases the whole "branch" containing the specified
/// revision. A "branch" is the set of commits that includes:
///
/// * the specified revision and ancestors that are not also ancestors of the
/// destination
/// * all descendants of those commits
///
/// In other words, `jj rebase -b X -d Y` rebases commits in the revset
/// `(Y..X):` (which is equivalent to `jj rebase -s 'roots(Y..X)' -d Y` for a
/// single root). For example, either `jj rebase -b L -d O` or `jj rebase -b M
/// -d O` would transform your history like this (because `L` and `M` are on the
/// same "branch", relative to the destination):
///
/// O N'
/// | |
/// | N M'
/// | | |
/// | M | L'
/// | | => |/
/// | | L K'
/// | |/ |
/// | K O
/// |/ |
/// J J
///
/// With `-r`, the command rebases only the specified revision onto the
/// destination. Any "hole" left behind will be filled by rebasing descendants
/// onto the specified revision's parent(s). For example, `jj rebase -r K -d M`
/// would transform your history like this:
///
/// M K'
/// | |
/// | L M
/// | | => |
/// | K | L'
/// |/ |/
/// J J
///
/// Note that you can create a merge commit by repeating the `-d` argument.
/// For example, if you realize that commit L actually depends on commit M in
/// order to work (in addition to its current parent K), you can run `jj rebase
/// -s L -d K -d M`:
///
/// M L'
/// | |\
/// | L M |
/// | | => | |
/// | K | K
/// |/ |/
/// J J
#[derive(clap::Args, Clone, Debug)]
#[command(verbatim_doc_comment)]
#[command(group(ArgGroup::new("to_rebase").args(&["branch", "source", "revision"])))]
struct RebaseArgs {
/// Rebase the whole branch relative to destination's ancestors (can be
/// repeated)
///
/// `jj rebase -b=br -d=dst` is equivalent to `jj rebase '-s=roots(dst..br)'
/// -d=dst`.
#[arg(long, short)]
branch: Vec<RevisionArg>,
/// Rebase specified revision(s) together their tree of descendants (can be
/// repeated)
///
/// Each specified revision will become a direct child of the destination
/// revision(s), even if some of the source revisions are descendants
/// of others.
#[arg(long, short)]
source: Vec<RevisionArg>,
/// Rebase only this revision, rebasing descendants onto this revision's
/// parent(s)
#[arg(long, short)]
revision: Option<RevisionArg>,
/// The revision(s) to rebase onto (can be repeated)
#[arg(long, short, required = true)]
destination: Vec<RevisionArg>,
/// Allow revsets expanding to multiple commits in a single argument (for
/// those options that can be repeated)
#[arg(long, short = 'L')]
allow_large_revsets: bool,
}
/// Apply the reverse of a revision on top of another revision
#[derive(clap::Args, Clone, Debug)]
struct BackoutArgs {
/// The revision to apply the reverse of
#[arg(long, short, default_value = "@")]
revision: RevisionArg,
/// The revision to apply the reverse changes on top of
// TODO: It seems better to default this to `@-`. Maybe the working
// copy should be rebased on top?
#[arg(long, short, default_value = "@")]
destination: Vec<RevisionArg>,
}
/// Commands for working with workspaces
#[derive(Subcommand, Clone, Debug)]
enum WorkspaceCommands {
Add(WorkspaceAddArgs),
Forget(WorkspaceForgetArgs),
List(WorkspaceListArgs),
Root(WorkspaceRootArgs),
UpdateStale(WorkspaceUpdateStaleArgs),
}
/// Add a workspace
#[derive(clap::Args, Clone, Debug)]
struct WorkspaceAddArgs {
/// Where to create the new workspace
destination: String,
/// A name for the workspace
///
/// To override the default, which is the basename of the destination
/// directory.
#[arg(long)]
name: Option<String>,
}
/// Stop tracking a workspace's working-copy commit in the repo
///
/// The workspace will not be touched on disk. It can be deleted from disk
/// before or after running this command.
#[derive(clap::Args, Clone, Debug)]
struct WorkspaceForgetArgs {
/// Name of the workspace to forget (the current workspace by default)
workspace: Option<String>,
}
/// List workspaces
#[derive(clap::Args, Clone, Debug)]
struct WorkspaceListArgs {}
/// Show the current workspace root directory
#[derive(clap::Args, Clone, Debug)]
struct WorkspaceRootArgs {}
/// Update a workspace that has become stale
///
/// For information about stale working copies, see
/// https://github.com/martinvonz/jj/blob/main/docs/working-copy.md.
#[derive(clap::Args, Clone, Debug)]
struct WorkspaceUpdateStaleArgs {}
/// Manage which paths from the working-copy commit are present in the working
/// copy
#[derive(clap::Args, Clone, Debug)]
struct SparseArgs {
/// Patterns to add to the working copy
#[arg(long, value_hint = clap::ValueHint::AnyPath)]
add: Vec<String>,
/// Patterns to remove from the working copy
#[arg(long, conflicts_with = "clear", value_hint = clap::ValueHint::AnyPath)]
remove: Vec<String>,
/// Include no files in the working copy (combine with --add)
#[arg(long)]
clear: bool,
/// Include all files in the working copy
#[arg(long, conflicts_with_all = &["add", "remove", "clear"])]
reset: bool,
/// List patterns
#[arg(long, conflicts_with_all = &["add", "remove", "clear", "reset"])]
list: bool,
}
/// Infrequently used commands such as for generating shell completions
#[derive(Subcommand, Clone, Debug)]
enum UtilCommands {
Completion(UtilCompletionArgs),
Mangen(UtilMangenArgs),
ConfigSchema(UtilConfigSchemaArgs),
}
/// Print a command-line-completion script
#[derive(clap::Args, Clone, Debug)]
struct UtilCompletionArgs {
/// Print a completion script for Bash
///
/// Apply it by running this:
///
/// source <(jj util completion)
#[arg(long, verbatim_doc_comment)]
bash: bool,
/// Print a completion script for Fish
///
/// Apply it by running this:
///
/// jj util completion --fish | source
#[arg(long, verbatim_doc_comment)]
fish: bool,
/// Print a completion script for Zsh
///
/// Apply it by running this:
///
/// autoload -U compinit
/// compinit
/// source <(jj util completion --zsh | sed '$d') # remove the last line
/// compdef _jj jj
#[arg(long, verbatim_doc_comment)]
zsh: bool,
}
/// Print a ROFF (manpage)
#[derive(clap::Args, Clone, Debug)]
struct UtilMangenArgs {}
/// Print the JSON schema for the jj TOML config format.
#[derive(clap::Args, Clone, Debug)]
struct UtilConfigSchemaArgs {}
/// Low-level commands not intended for users
#[derive(Subcommand, Clone, Debug)]
#[command(hide = true)]
enum DebugCommands {
Revset(DebugRevsetArgs),
#[command(name = "workingcopy")]
WorkingCopy(DebugWorkingCopyArgs),
Template(DebugTemplateArgs),
Index(DebugIndexArgs),
#[command(name = "reindex")]
ReIndex(DebugReIndexArgs),
Operation(DebugOperationArgs),
}
/// Evaluate revset to full commit IDs
#[derive(clap::Args, Clone, Debug)]
struct DebugRevsetArgs {
revision: String,
}
/// Show information about the working copy state
#[derive(clap::Args, Clone, Debug)]
struct DebugWorkingCopyArgs {}
/// Parse a template
#[derive(clap::Args, Clone, Debug)]
struct DebugTemplateArgs {
template: String,
}
/// Show commit index stats
#[derive(clap::Args, Clone, Debug)]
struct DebugIndexArgs {}
/// Rebuild commit index
#[derive(clap::Args, Clone, Debug)]
struct DebugReIndexArgs {}
/// Show information about an operation and its view
#[derive(clap::Args, Clone, Debug)]
struct DebugOperationArgs {
#[arg(default_value = "@")]
operation: String,
}
fn add_to_git_exclude(ui: &mut Ui, git_repo: &git2::Repository) -> Result<(), CommandError> {
let exclude_file_path = git_repo.path().join("info").join("exclude");
if exclude_file_path.exists() {
match fs::OpenOptions::new()
.read(true)
.write(true)
.open(&exclude_file_path)
{
Ok(mut exclude_file) => {
let mut buf = vec![];
exclude_file.read_to_end(&mut buf)?;
let pattern = b"\n/.jj/\n";
if !buf.windows(pattern.len()).any(|window| window == pattern) {
exclude_file.seek(SeekFrom::End(0))?;
if !buf.ends_with(b"\n") {
exclude_file.write_all(b"\n")?;
}
exclude_file.write_all(b"/.jj/\n")?;
}
}
Err(err) => {
writeln!(