jj-cli 0.14.0

Jujutsu - an experimental version control system
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
// Copyright 2023 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.

use std::path::Path;

use crate::common::TestEnvironment;

fn create_commit(test_env: &TestEnvironment, repo_path: &Path, name: &str, parents: &[&str]) {
    if parents.is_empty() {
        test_env.jj_cmd_ok(repo_path, &["new", "root()", "-m", name]);
    } else {
        let mut args = vec!["new", "-m", name];
        args.extend(parents);
        test_env.jj_cmd_ok(repo_path, &args);
    }
    std::fs::write(repo_path.join(name), format!("{name}\n")).unwrap();
    test_env.jj_cmd_ok(repo_path, &["branch", "create", name]);
}

#[test]
fn test_basics() {
    let test_env = TestEnvironment::default();
    test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
    let repo_path = test_env.env_root().join("repo");

    create_commit(&test_env, &repo_path, "a", &[]);
    create_commit(&test_env, &repo_path, "b", &["a"]);
    create_commit(&test_env, &repo_path, "c", &[]);
    create_commit(&test_env, &repo_path, "d", &["c"]);
    create_commit(&test_env, &repo_path, "e", &["a", "d"]);
    // Test the setup
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @    [znk] e
    ├─╮
    │ ◉  [vru] d
    │ ◉  [roy] c
    │ │ ◉  [zsu] b
    ├───╯
    ◉ │  [rlv] a
    ├─╯
    ◉  [zzz]
    "###);

    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", "d"]);
    insta::assert_snapshot!(stdout, @"");
    insta::assert_snapshot!(stderr, @r###"
    Abandoned commit vruxwmqv b7c62f28 d | d
    Rebased 1 descendant commits onto parents of abandoned commits
    Working copy now at: znkkpsqq 11a2e10e e | e
    Parent commit      : rlvkpnrz 2443ea76 a | a
    Parent commit      : royxmykx fe2e8e8b c d | c
    Added 0 files, modified 0 files, removed 1 files
    "###);
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @    [znk] e
    ├─╮
    │ ◉  [roy] c d
    │ │ ◉  [zsu] b
    ├───╯
    ◉ │  [rlv] a
    ├─╯
    ◉  [zzz]
    "###);

    test_env.jj_cmd_ok(&repo_path, &["undo"]);
    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon"] /* abandons `e` */);
    insta::assert_snapshot!(stdout, @"");
    insta::assert_snapshot!(stderr, @r###"
    Abandoned commit znkkpsqq 5557ece3 e | e
    Working copy now at: nkmrtpmo 6b527513 (empty) (no description set)
    Parent commit      : rlvkpnrz 2443ea76 a e?? | a
    Added 0 files, modified 0 files, removed 3 files
    "###);
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @  [nkm]
    │ ◉  [zsu] b
    ├─╯
    ◉  [rlv] a e??
    │ ◉  [vru] d e??
    │ ◉  [roy] c
    ├─╯
    ◉  [zzz]
    "###);

    // Abandoning `a` would normally result in its descendant merge commit, `e`,
    // still having two parents. However, since one of those parents (the root
    // commit) would be the ancestor of another, only one of the parents is kept.
    test_env.jj_cmd_ok(&repo_path, &["undo"]);
    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", "a"]);
    insta::assert_snapshot!(stdout, @"");
    insta::assert_snapshot!(stderr, @r###"
    Abandoned commit rlvkpnrz 2443ea76 a | a
    Rebased 2 descendant commits onto parents of abandoned commits
    Working copy now at: znkkpsqq b0af79c3 e | e
    Parent commit      : vruxwmqv b7c62f28 d | d
    Added 0 files, modified 0 files, removed 1 files
    "###);
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @  [znk] e
    ◉  [vru] d
    ◉  [roy] c
    │ ◉  [zsu] b
    ├─╯
    ◉  [zzz] a
    "###);

    test_env.jj_cmd_ok(&repo_path, &["undo"]);
    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", "descendants(c)"]);
    insta::assert_snapshot!(stdout, @"");
    insta::assert_snapshot!(stderr, @r###"
    Abandoned the following commits:
      znkkpsqq 5557ece3 e | e
      vruxwmqv b7c62f28 d | d
      royxmykx fe2e8e8b c | c
    Working copy now at: wvuyspvk 3f93e69f (empty) (no description set)
    Parent commit      : rlvkpnrz 2443ea76 a e?? | a
    Added 0 files, modified 0 files, removed 3 files
    "###);
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @  [wvu]
    │ ◉  [zsu] b
    ├─╯
    ◉  [rlv] a e??
    ◉  [zzz] c d e??
    "###);

    // Test abandoning the same commit twice directly
    test_env.jj_cmd_ok(&repo_path, &["undo"]);
    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", "b", "b"]);
    insta::assert_snapshot!(stdout, @"");
    insta::assert_snapshot!(stderr, @r###"
    Abandoned commit zsuskuln 1394f625 b | b
    "###);
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @    [znk] e
    ├─╮
    │ ◉  [vru] d
    │ ◉  [roy] c
    ◉ │  [rlv] a b
    ├─╯
    ◉  [zzz]
    "###);

    // Test abandoning the same commit twice indirectly
    test_env.jj_cmd_ok(&repo_path, &["undo"]);
    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", "d::", "a::"]);
    insta::assert_snapshot!(stdout, @"");
    insta::assert_snapshot!(stderr, @r###"
    Abandoned the following commits:
      znkkpsqq 5557ece3 e | e
      vruxwmqv b7c62f28 d | d
      zsuskuln 1394f625 b | b
      rlvkpnrz 2443ea76 a | a
    Working copy now at: oupztwtk 304ae338 (empty) (no description set)
    Parent commit      : zzzzzzzz 00000000 a b e?? | (empty) (no description set)
    Added 0 files, modified 0 files, removed 4 files
    "###);
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @  [oup]
    │ ◉  [roy] c d e??
    ├─╯
    ◉  [zzz] a b e??
    "###);
}

// TODO(#2600): Make sure the results here become saner as #2600 is fixed. There
// is an simpler demo of #2600 at https://github.com/martinvonz/jj/pull/2655.
// However, fixing #2600 will likely change how `abandon` works. This test
// exists to track how that happens. See also the corresponding test in
// `test_rebase_command`
#[test]
fn test_bug_2600() {
    let test_env = TestEnvironment::default();
    test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
    let repo_path = test_env.env_root().join("repo");

    // We will not touch "nottherootcommit". See the
    // `test_bug_2600_rootcommit_special_case` for the one case where base being the
    // child of the root commit changes the expected behavior.
    create_commit(&test_env, &repo_path, "nottherootcommit", &[]);
    create_commit(&test_env, &repo_path, "base", &["nottherootcommit"]);
    create_commit(&test_env, &repo_path, "a", &["base"]);
    create_commit(&test_env, &repo_path, "b", &["base", "a"]);
    create_commit(&test_env, &repo_path, "c", &["b"]);

    // Test the setup
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @  [znk] c
    ◉    [vru] b
    ├─╮
    │ ◉  [roy] a
    ├─╯
    ◉  [zsu] base
    ◉  [rlv] nottherootcommit
    ◉  [zzz]
    "###);
    let setup_opid = test_env.current_operation_id(&repo_path);

    test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]);
    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", "base"]);
    insta::assert_snapshot!(stdout, @"");
    insta::assert_snapshot!(stderr, @r###"
    Abandoned commit zsuskuln 73c929fc base | base
    Rebased 3 descendant commits onto parents of abandoned commits
    Working copy now at: znkkpsqq 510f8756 c | c
    Parent commit      : vruxwmqv 7301d9ab b | b
    Added 0 files, modified 0 files, removed 1 files
    "###);
    // BUG. The user would expect
    // @  c
    // ├─╮
    // │ ◉  a
    // ├─╯
    // ◉  base nottherootcommit
    //    // This is likely caused by DescendantRebaser
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @  [znk] c
    ◉  [vru] b
    ◉  [roy] a
    ◉  [rlv] base nottherootcommit
    ◉  [zzz]
    "###);

    test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]);
    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", "a"]);
    insta::assert_snapshot!(stdout, @"");
    insta::assert_snapshot!(stderr, @r###"
    Abandoned commit royxmykx 98f3b9ba a | a
    Rebased 2 descendant commits onto parents of abandoned commits
    Working copy now at: znkkpsqq 683b9435 c | c
    Parent commit      : vruxwmqv c10cb7b4 b | b
    Added 0 files, modified 0 files, removed 1 files
    "###);
    // This is likely what the user will expect.
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @  [znk] c
    ◉  [vru] b
    ◉  [zsu] a base
    ◉  [rlv] nottherootcommit
    ◉  [zzz]
    "###);

    test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]);
    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", "b"]);
    insta::assert_snapshot!(stdout, @"");
    insta::assert_snapshot!(stderr, @r###"
    Abandoned commit vruxwmqv 8c0dced0 b | b
    Rebased 1 descendant commits onto parents of abandoned commits
    Working copy now at: znkkpsqq 924bdd1c c | c
    Parent commit      : royxmykx 98f3b9ba a b?? | a
    Added 0 files, modified 0 files, removed 1 files
    "###);
    // BUG. The user would expect
    // @  c
    // ├─╮
    // │ ◉  a
    // ├─╯
    // ◉  base
    // ◉  nottherootcommit
    //    // This is likely caused by logic in `cmd_abandon`, not DescendantRebaser
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @  [znk] c
    ◉  [roy] a b??
    ◉  [zsu] b?? base
    ◉  [rlv] nottherootcommit
    ◉  [zzz]
    "###);

    test_env.jj_cmd_ok(&repo_path, &["op", "restore", &setup_opid]);
    // ========= Reminder of the setup ===========
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @  [znk] c
    ◉    [vru] b
    ├─╮
    │ ◉  [roy] a
    ├─╯
    ◉  [zsu] base
    ◉  [rlv] nottherootcommit
    ◉  [zzz]
    "###);
    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", "a", "b"]);
    insta::assert_snapshot!(stdout, @"");
    insta::assert_snapshot!(stderr, @r###"
    Abandoned the following commits:
      royxmykx 98f3b9ba a | a
      vruxwmqv 8c0dced0 b | b
    Rebased 1 descendant commits onto parents of abandoned commits
    Working copy now at: znkkpsqq 84fac1f8 c | c
    Parent commit      : zsuskuln 73c929fc a b base | base
    Added 0 files, modified 0 files, removed 2 files
    "###);
    // This is likely what the user would expect
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @  [znk] c
    ◉  [zsu] a b base
    ◉  [rlv] nottherootcommit
    ◉  [zzz]
    "###);
    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["branch", "list", "b"]);
    insta::assert_snapshot!(stdout, @r###"
    b: zsuskuln 73c929fc base
    "###);
    insta::assert_snapshot!(stderr, @"");
}

#[test]
fn test_bug_2600_rootcommit_special_case() {
    let test_env = TestEnvironment::default();
    test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
    let repo_path = test_env.env_root().join("repo");

    // Set up like `test_bug_2600`, but without the `nottherootcommit` commit.
    create_commit(&test_env, &repo_path, "base", &[]);
    create_commit(&test_env, &repo_path, "a", &["base"]);
    create_commit(&test_env, &repo_path, "b", &["base", "a"]);
    create_commit(&test_env, &repo_path, "c", &["b"]);

    // Setup
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @  [vru] c
    ◉    [roy] b
    ├─╮
    │ ◉  [zsu] a
    ├─╯
    ◉  [rlv] base
    ◉  [zzz]
    "###);

    // Now, the test
    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", "base"]);
    insta::assert_snapshot!(stdout, @"");
    insta::assert_snapshot!(stderr, @r###"
    Abandoned commit rlvkpnrz 0c61db1b base | base
    Rebased 3 descendant commits onto parents of abandoned commits
    Working copy now at: vruxwmqv 73e9185c c | c
    Parent commit      : royxmykx 80dd9cba b | b
    Added 0 files, modified 0 files, removed 1 files
    "###);
    // The current behavior is either correct or should be replaced with an error
    // message. Even though the user would expect `b` to still be a descendant of
    // `base`, it is impossible in the Git backend.
    // See also https://github.com/martinvonz/jj/issues/2600#issuecomment-1835418824
    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
    @  [vru] c
    ◉  [roy] b
    ◉  [zsu] a
    ◉  [zzz] base
    "###);
}

#[test]
fn test_double_abandon() {
    let test_env = TestEnvironment::default();
    test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
    let repo_path = test_env.env_root().join("repo");

    create_commit(&test_env, &repo_path, "a", &[]);
    // Test the setup
    insta::assert_snapshot!(
    test_env.jj_cmd_success(&repo_path, &["log", "--no-graph", "-r", "a"])
        , @r###"
    rlvkpnrz test.user@example.com 2001-02-03 04:05:09.000 +07:00 a 2443ea76
    a
    "###);

    let commit_id = test_env.jj_cmd_success(
        &repo_path,
        &["log", "--no-graph", "--color=never", "-T=commit_id", "-r=a"],
    );

    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", &commit_id]);
    insta::assert_snapshot!(stdout, @"");
    insta::assert_snapshot!(stderr, @r###"
    Abandoned commit rlvkpnrz 2443ea76 a | a
    Working copy now at: royxmykx f37b4afd (empty) (no description set)
    Parent commit      : zzzzzzzz 00000000 a | (empty) (no description set)
    Added 0 files, modified 0 files, removed 1 files
    "###);
    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["abandon", &commit_id]);
    insta::assert_snapshot!(stdout, @"");
    insta::assert_snapshot!(stderr, @r###"
    Abandoned commit rlvkpnrz hidden 2443ea76 a
    Nothing changed.
    "###);
}

fn get_log_output(test_env: &TestEnvironment, repo_path: &Path) -> String {
    test_env.jj_cmd_success(
        repo_path,
        &[
            "log",
            "-T",
            r#"separate(" ", "[" ++ change_id.short(3) ++ "]", branches)"#,
        ],
    )
}