jgl 1.3.0

Multi-repo manager for jujutsu (jj)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#![allow(clippy::unwrap_used, clippy::expect_used)]

mod harness;

use harness::TestRepo;
use tempfile::TempDir;

// --- jgl add ---

#[test]
fn add_real_jj_repo_registers_in_config() {
    let tmp = TempDir::new().unwrap();
    let repo = TestRepo::new(tmp.path().join("repo"))
        .with_commit("initial", &[("README.md", "# Hello")])
        .build();

    let config_path = tmp.path().join("config.toml");
    jgl::commands::add::run(
        &config_path,
        repo.path().to_str().unwrap(),
        &mut std::io::sink(),
    )
    .unwrap();

    let config = jgl::config::Config::load(&config_path).unwrap();
    assert_eq!(config.repos.len(), 1);
    assert_eq!(config.repos[0].path, repo.path().to_str().unwrap());
}

#[test]
fn add_repo_with_remote_registers_correctly() {
    let tmp = TempDir::new().unwrap();
    let repo = TestRepo::new(tmp.path().join("repo"))
        .with_remote("origin")
        .with_commit("initial", &[("README.md", "# Hello")])
        .build();

    let config_path = tmp.path().join("config.toml");
    jgl::commands::add::run(
        &config_path,
        repo.path().to_str().unwrap(),
        &mut std::io::sink(),
    )
    .unwrap();

    let config = jgl::config::Config::load(&config_path).unwrap();
    assert_eq!(config.repos.len(), 1);
    // The remote path should exist and be a bare repo
    assert!(repo.remote_path("origin").join("HEAD").exists());
}

// --- jgl fetch ---

#[test]
fn fetch_pulls_commits_pushed_by_clone() {
    let tmp = TempDir::new().unwrap();
    let repo = TestRepo::new(tmp.path().join("repo"))
        .with_remote("origin")
        .with_commit("initial", &[("README.md", "# Hello")])
        .build();

    // Register repo in config
    let config_path = tmp.path().join("config.toml");
    jgl::commands::add::run(
        &config_path,
        repo.path().to_str().unwrap(),
        &mut std::io::sink(),
    )
    .unwrap();

    // A second client clones, commits, and pushes
    let clone = repo.clone_as(tmp.path().join("clone"));
    clone.commit("feat: add feature", &[("src/lib.rs", "pub fn foo() {}")]);
    clone.push("origin");

    // Before fetch: repo does not see the new commit
    assert!(!repo
        .log_messages()
        .contains(&"feat: add feature".to_owned()));

    // Run jgl fetch
    jgl::commands::fetch::run(
        &config_path,
        &jgl::commands::fetch::FetchOptions {
            verbose: false,
            rebase: false,
            with_conflicts: false,
            idle_timeout: std::time::Duration::from_secs(
                jgl::commands::fetch::DEFAULT_IDLE_TIMEOUT_SECS,
            ),
        },
        &mut std::io::sink(),
    )
    .unwrap();

    // After fetch: repo sees the new commit
    assert!(repo
        .log_messages()
        .contains(&"feat: add feature".to_owned()));
}

#[test]
fn fetch_multiple_repos_all_updated() {
    let tmp = TempDir::new().unwrap();

    let repo_a = TestRepo::new(tmp.path().join("repo_a"))
        .with_remote("origin")
        .with_commit("repo-a initial", &[("a.txt", "a")])
        .build();

    let repo_b = TestRepo::new(tmp.path().join("repo_b"))
        .with_remote("origin")
        .with_commit("repo-b initial", &[("b.txt", "b")])
        .build();

    let config_path = tmp.path().join("config.toml");
    jgl::commands::add::run(
        &config_path,
        repo_a.path().to_str().unwrap(),
        &mut std::io::sink(),
    )
    .unwrap();
    jgl::commands::add::run(
        &config_path,
        repo_b.path().to_str().unwrap(),
        &mut std::io::sink(),
    )
    .unwrap();

    // Push new commits from clones
    let clone_a = repo_a.clone_as(tmp.path().join("clone_a"));
    clone_a.commit("feat: from clone a", &[("new_a.txt", "x")]);
    clone_a.push("origin");

    let clone_b = repo_b.clone_as(tmp.path().join("clone_b"));
    clone_b.commit("feat: from clone b", &[("new_b.txt", "y")]);
    clone_b.push("origin");

    jgl::commands::fetch::run(
        &config_path,
        &jgl::commands::fetch::FetchOptions {
            verbose: false,
            rebase: false,
            with_conflicts: false,
            idle_timeout: std::time::Duration::from_secs(
                jgl::commands::fetch::DEFAULT_IDLE_TIMEOUT_SECS,
            ),
        },
        &mut std::io::sink(),
    )
    .unwrap();

    assert!(repo_a
        .log_messages()
        .contains(&"feat: from clone a".to_owned()));
    assert!(repo_b
        .log_messages()
        .contains(&"feat: from clone b".to_owned()));
}

#[test]
fn fetch_fails_when_repo_is_deleted() {
    let tmp = TempDir::new().unwrap();
    let repo = TestRepo::new(tmp.path().join("repo"))
        .with_remote("origin")
        .with_commit("initial", &[("README.md", "# Hello")])
        .build();

    let config_path = tmp.path().join("config.toml");
    jgl::commands::add::run(
        &config_path,
        repo.path().to_str().unwrap(),
        &mut std::io::sink(),
    )
    .unwrap();

    std::fs::remove_dir_all(repo.path()).unwrap();

    let err = jgl::commands::fetch::run(
        &config_path,
        &jgl::commands::fetch::FetchOptions {
            verbose: false,
            rebase: false,
            with_conflicts: false,
            idle_timeout: std::time::Duration::from_secs(
                jgl::commands::fetch::DEFAULT_IDLE_TIMEOUT_SECS,
            ),
        },
        &mut std::io::sink(),
    )
    .unwrap_err();
    assert!(err.to_string().contains("failed"));
}

#[test]
fn fetch_fails_when_remote_is_deleted() {
    let tmp = TempDir::new().unwrap();
    let repo = TestRepo::new(tmp.path().join("repo"))
        .with_remote("origin")
        .with_commit("initial", &[("README.md", "# Hello")])
        .build();

    let config_path = tmp.path().join("config.toml");
    jgl::commands::add::run(
        &config_path,
        repo.path().to_str().unwrap(),
        &mut std::io::sink(),
    )
    .unwrap();

    // Remove the bare remote so fetch has nowhere to pull from
    std::fs::remove_dir_all(repo.remote_path("origin")).unwrap();

    let err = jgl::commands::fetch::run(
        &config_path,
        &jgl::commands::fetch::FetchOptions {
            verbose: false,
            rebase: false,
            with_conflicts: false,
            idle_timeout: std::time::Duration::from_secs(
                jgl::commands::fetch::DEFAULT_IDLE_TIMEOUT_SECS,
            ),
        },
        &mut std::io::sink(),
    )
    .unwrap_err();
    assert!(err.to_string().contains("failed"));
}

// --- new behaviour tests ---

#[test]
fn fetch_continues_after_partial_failure() {
    let tmp = TempDir::new().unwrap();

    let repo_a = TestRepo::new(tmp.path().join("repo_a"))
        .with_remote("origin")
        .with_commit("initial a", &[("a.txt", "a")])
        .build();

    let repo_b = TestRepo::new(tmp.path().join("repo_b"))
        .with_remote("origin")
        .with_commit("initial b", &[("b.txt", "b")])
        .build();

    let config_path = tmp.path().join("config.toml");
    jgl::commands::add::run(
        &config_path,
        repo_a.path().to_str().unwrap(),
        &mut std::io::sink(),
    )
    .unwrap();
    jgl::commands::add::run(
        &config_path,
        repo_b.path().to_str().unwrap(),
        &mut std::io::sink(),
    )
    .unwrap();

    // Push a new commit to repo_b's remote
    let clone_b = repo_b.clone_as(tmp.path().join("clone_b"));
    clone_b.commit("feat: new in b", &[("new.txt", "x")]);
    clone_b.push("origin");

    // Delete repo_a so its fetch fails
    std::fs::remove_dir_all(repo_a.path()).unwrap();

    // run() should report failure (repo_a errored)
    let err = jgl::commands::fetch::run(
        &config_path,
        &jgl::commands::fetch::FetchOptions {
            verbose: false,
            rebase: false,
            with_conflicts: false,
            idle_timeout: std::time::Duration::from_secs(
                jgl::commands::fetch::DEFAULT_IDLE_TIMEOUT_SECS,
            ),
        },
        &mut std::io::sink(),
    )
    .unwrap_err();
    assert!(err.to_string().contains("failed"));

    // repo_b must still have been fetched despite repo_a failing
    assert!(
        repo_b.log_messages().contains(&"feat: new in b".to_owned()),
        "repo_b should have been fetched even though repo_a failed"
    );
}

#[test]
fn fetch_result_shows_changed_and_unchanged() {
    let tmp = TempDir::new().unwrap();

    let repo_a = TestRepo::new(tmp.path().join("repo_a"))
        .with_remote("origin")
        .with_commit("initial a", &[("a.txt", "a")])
        .build();

    let repo_b = TestRepo::new(tmp.path().join("repo_b"))
        .with_remote("origin")
        .with_commit("initial b", &[("b.txt", "b")])
        .build();

    let config_path = tmp.path().join("config.toml");
    jgl::commands::add::run(
        &config_path,
        repo_a.path().to_str().unwrap(),
        &mut std::io::sink(),
    )
    .unwrap();
    jgl::commands::add::run(
        &config_path,
        repo_b.path().to_str().unwrap(),
        &mut std::io::sink(),
    )
    .unwrap();

    // Push a new commit only to repo_a's remote
    let clone_a = repo_a.clone_as(tmp.path().join("clone_a"));
    clone_a.commit("feat: new in a", &[("new.txt", "x")]);
    clone_a.push("origin");

    let results = jgl::commands::fetch::run_with_results(
        &config_path,
        &jgl::commands::fetch::ProcessRunner {
            idle_timeout: std::time::Duration::from_secs(
                jgl::commands::fetch::DEFAULT_IDLE_TIMEOUT_SECS,
            ),
        },
        &jgl::commands::fetch::FetchOptions {
            verbose: false,
            rebase: false,
            with_conflicts: false,
            idle_timeout: std::time::Duration::from_secs(
                jgl::commands::fetch::DEFAULT_IDLE_TIMEOUT_SECS,
            ),
        },
        None,
    )
    .unwrap();

    let result_a = results.iter().find(|r| r.path == *repo_a.path()).unwrap();
    let result_b = results.iter().find(|r| r.path == *repo_b.path()).unwrap();

    assert!(
        matches!(result_a.status, jgl::commands::fetch::FetchStatus::Changed),
        "repo_a should be Changed"
    );
    assert!(
        matches!(
            result_b.status,
            jgl::commands::fetch::FetchStatus::Unchanged
        ),
        "repo_b should be Unchanged"
    );
}

#[test]
fn fetch_labels_repos_by_dirname() {
    let tmp = TempDir::new().unwrap();

    let repo_a = TestRepo::new(tmp.path().join("my_project"))
        .with_remote("origin")
        .with_commit("initial", &[("a.txt", "a")])
        .build();

    let config_path = tmp.path().join("config.toml");
    jgl::commands::add::run(
        &config_path,
        repo_a.path().to_str().unwrap(),
        &mut std::io::sink(),
    )
    .unwrap();

    let results = jgl::commands::fetch::run_with_results(
        &config_path,
        &jgl::commands::fetch::ProcessRunner {
            idle_timeout: std::time::Duration::from_secs(
                jgl::commands::fetch::DEFAULT_IDLE_TIMEOUT_SECS,
            ),
        },
        &jgl::commands::fetch::FetchOptions {
            verbose: false,
            rebase: false,
            with_conflicts: false,
            idle_timeout: std::time::Duration::from_secs(
                jgl::commands::fetch::DEFAULT_IDLE_TIMEOUT_SECS,
            ),
        },
        None,
    )
    .unwrap();

    assert_eq!(results[0].label, "my_project");
}

#[test]
fn fetch_rebase_fails_when_working_change_on_immutable_not_in_main() {
    // Scenario:
    //   - origin/main is advanced by a clone (so fetch sees changes)
    //   - local repo has a "feature" commit pushed to origin/feature
    //     (making it immutable via untracked_remote_bookmarks())
    //   - @ (working change) sits on top of that immutable feature commit
    //   - jgl fetch --rebase calls `jj rebase -o trunk()` which must
    //     traverse the immutable feature commit → jj refuses → Failed
    let tmp = TempDir::new().unwrap();
    let repo = TestRepo::new(tmp.path().join("repo"))
        .with_remote("origin")
        .with_commit("initial", &[("README.md", "# Hello")])
        .build();

    let config_path = tmp.path().join("config.toml");
    jgl::commands::add::run(
        &config_path,
        repo.path().to_str().unwrap(),
        &mut std::io::sink(),
    )
    .unwrap();

    // Advance origin/main via a clone so trunk() will move after fetch
    let clone = repo.clone_as(tmp.path().join("clone"));
    clone.commit("feat: remote progress", &[("remote.txt", "x")]);
    clone.push("origin");

    // In the local repo: create a commit, push it as "feature" bookmark, then
    // untrack it so feature@origin becomes an untracked remote bookmark and the
    // commit falls into immutable_heads() via untracked_remote_bookmarks().
    std::fs::write(repo.path().join("feature.txt"), "feature work").unwrap();
    repo.run_jj(&["commit", "-m", "feat: local feature"]);
    repo.run_jj(&["bookmark", "create", "feature", "-r", "@-"]);
    repo.run_jj(&["git", "push", "--remote", "origin", "-b", "feature"]);
    repo.run_jj(&["bookmark", "untrack", "feature", "--remote=origin"]);

    // Add a working change on top of the now-immutable feature commit
    std::fs::write(repo.path().join("wip.txt"), "wip").unwrap();

    // fetch --rebase: fetch succeeds (origin/main advanced), then
    // `jj rebase -b @ -o trunk()` fails because @- is immutable and not in main
    let results = jgl::commands::fetch::run_with_results(
        &config_path,
        &jgl::commands::fetch::ProcessRunner {
            idle_timeout: std::time::Duration::from_secs(
                jgl::commands::fetch::DEFAULT_IDLE_TIMEOUT_SECS,
            ),
        },
        &jgl::commands::fetch::FetchOptions {
            verbose: false,
            rebase: true,
            with_conflicts: false,
            idle_timeout: std::time::Duration::from_secs(
                jgl::commands::fetch::DEFAULT_IDLE_TIMEOUT_SECS,
            ),
        },
        None,
    )
    .unwrap();

    assert_eq!(results.len(), 1);
    assert!(
        matches!(
            results[0].rebase_status,
            jgl::commands::fetch::RebaseStatus::Failed(_)
        ),
        "expected rebase to fail when @ is on top of immutable commit not in main, got {:?}",
        results[0].rebase_status
    );

    let mut out = Vec::<u8>::new();
    jgl::commands::fetch::display_results(&results, true, false, &mut out).unwrap();
    let stdout = String::from_utf8(out).unwrap();

    assert!(
        stdout.contains("rebase error"),
        "stdout should contain 'rebase error': {stdout:?}"
    );
}

#[test]
fn fetch_disambiguates_same_dirname() {
    let tmp = TempDir::new().unwrap();

    // Two repos with the same directory name but under different parents
    let dir_a = tmp.path().join("team_a").join("myrepo");
    let dir_b = tmp.path().join("team_b").join("myrepo");
    std::fs::create_dir_all(&dir_a).unwrap();
    std::fs::create_dir_all(&dir_b).unwrap();

    let repo_a = TestRepo::new(dir_a)
        .with_remote("origin")
        .with_commit("initial a", &[("a.txt", "a")])
        .build();

    let repo_b = TestRepo::new(dir_b)
        .with_remote("origin")
        .with_commit("initial b", &[("b.txt", "b")])
        .build();

    let config_path = tmp.path().join("config.toml");
    jgl::commands::add::run(
        &config_path,
        repo_a.path().to_str().unwrap(),
        &mut std::io::sink(),
    )
    .unwrap();
    jgl::commands::add::run(
        &config_path,
        repo_b.path().to_str().unwrap(),
        &mut std::io::sink(),
    )
    .unwrap();

    let results = jgl::commands::fetch::run_with_results(
        &config_path,
        &jgl::commands::fetch::ProcessRunner {
            idle_timeout: std::time::Duration::from_secs(
                jgl::commands::fetch::DEFAULT_IDLE_TIMEOUT_SECS,
            ),
        },
        &jgl::commands::fetch::FetchOptions {
            verbose: false,
            rebase: false,
            with_conflicts: false,
            idle_timeout: std::time::Duration::from_secs(
                jgl::commands::fetch::DEFAULT_IDLE_TIMEOUT_SECS,
            ),
        },
        None,
    )
    .unwrap();

    let labels: Vec<&str> = results.iter().map(|r| r.label.as_str()).collect();
    assert!(
        labels.contains(&"team_a/myrepo"),
        "expected team_a/myrepo in {labels:?}"
    );
    assert!(
        labels.contains(&"team_b/myrepo"),
        "expected team_b/myrepo in {labels:?}"
    );
}