File: test_git_remotes.rs

package info (click to toggle)
jujutsu 0.37.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,740 kB
  • sloc: sh: 283; makefile: 34
file content (802 lines) | stat: -rw-r--r-- 24,577 bytes parent folder | download
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
// Copyright 2022 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::fs;
use std::io::Write as _;
use std::path::Path;
use std::path::PathBuf;

use indoc::indoc;
use testutils::git;

use crate::common::TestEnvironment;

fn read_git_config(repo_path: &Path) -> String {
    let git_config = fs::read_to_string(repo_path.join(".jj/repo/store/git/config"))
        .or_else(|_| fs::read_to_string(repo_path.join(".git/config")))
        .unwrap();
    git_config
        .split_inclusive('\n')
        .filter(|line| {
            // Filter out non‐portable values.
            [
                "\tfilemode =",
                "\tsymlinks =",
                "\tignorecase =",
                "\tprecomposeunicode =",
            ]
            .iter()
            .all(|prefix| !line.to_ascii_lowercase().starts_with(prefix))
        })
        .collect()
}

#[test]
fn test_git_remotes() {
    let test_env = TestEnvironment::default();

    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");

    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @"");
    let output = work_dir.run_jj(["git", "remote", "add", "foo", "http://example.com/repo/foo"]);
    insta::assert_snapshot!(output, @"");
    let output = work_dir.run_jj(["git", "remote", "add", "bar", "http://example.com/repo/bar"]);
    insta::assert_snapshot!(output, @"");
    let output = work_dir.run_jj([
        "git",
        "remote",
        "add",
        "baz",
        "http://example.com/repo/baz",
        "--push-url",
        "git@example.com:repo/baz",
    ]);
    insta::assert_snapshot!(output, @"");
    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @r"
    bar http://example.com/repo/bar
    baz http://example.com/repo/baz (push: git@example.com:repo/baz)
    foo http://example.com/repo/foo
    [EOF]
    ");
    let output = work_dir.run_jj(["git", "remote", "remove", "foo"]);
    insta::assert_snapshot!(output, @"");
    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @r"
    bar http://example.com/repo/bar
    baz http://example.com/repo/baz (push: git@example.com:repo/baz)
    [EOF]
    ");
    let output = work_dir.run_jj(["git", "remote", "remove", "nonexistent"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Error: No git remote named 'nonexistent'
    [EOF]
    [exit status: 1]
    ");
    insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
    [core]
    	repositoryformatversion = 0
    	bare = true
    	logallrefupdates = false
    [remote "bar"]
    	url = http://example.com/repo/bar
    	fetch = +refs/heads/*:refs/remotes/bar/*
    [remote "baz"]
    	url = http://example.com/repo/baz
    	pushurl = git@example.com:repo/baz
    	fetch = +refs/heads/*:refs/remotes/baz/*
    "#);

    // named remote that cannot be parsed
    work_dir.write_file(
        ".jj/repo/store/git/config",
        indoc! {r#"
            [remote "foo"]
                url = invalid\
        "#},
    );
    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @r#"
    ------- stderr -------
    Error: Failed to load configured remote foo
    Caused by:
    1: The fetch url under `remote.foo` was invalid
    2: The url at "remote.<name>.url=" could not be parsed
    3: local path "" does not specify a path to a repository
    [EOF]
    [exit status: 1]
    "#);
}

#[test]
fn test_git_remote_add() {
    let test_env = TestEnvironment::default();

    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");
    work_dir
        .run_jj(["git", "remote", "add", "foo", "http://example.com/repo/foo"])
        .success();
    let output = work_dir.run_jj([
        "git",
        "remote",
        "add",
        "foo",
        "http://example.com/repo/foo2",
    ]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Error: Git remote named 'foo' already exists
    [EOF]
    [exit status: 1]
    ");
    let output = work_dir.run_jj(["git", "remote", "add", "git", "http://example.com/repo/git"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Error: Git remote named 'git' is reserved for local Git repository
    [EOF]
    [exit status: 1]
    ");
    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @r"
    foo http://example.com/repo/foo
    [EOF]
    ");
}

#[test]
fn test_git_remote_with_fetch_tags() {
    let test_env = TestEnvironment::default();

    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");

    let output = work_dir.run_jj(["git", "remote", "add", "foo", "http://example.com/repo"]);
    insta::assert_snapshot!(output, @"");

    let output = work_dir.run_jj([
        "git",
        "remote",
        "add",
        "foo-included",
        "http://example.com/repo",
        "--fetch-tags",
        "included",
    ]);
    insta::assert_snapshot!(output, @"");

    let output = work_dir.run_jj([
        "git",
        "remote",
        "add",
        "foo-all",
        "http://example.com/repo",
        "--fetch-tags",
        "all",
    ]);
    insta::assert_snapshot!(output, @"");

    let output = work_dir.run_jj([
        "git",
        "remote",
        "add",
        "foo-none",
        "http://example.com/repo",
        "--fetch-tags",
        "none",
    ]);
    insta::assert_snapshot!(output, @"");

    insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
    [core]
    	repositoryformatversion = 0
    	bare = true
    	logallrefupdates = false
    [remote "foo"]
    	url = http://example.com/repo
    	fetch = +refs/heads/*:refs/remotes/foo/*
    [remote "foo-included"]
    	url = http://example.com/repo
    	fetch = +refs/heads/*:refs/remotes/foo-included/*
    [remote "foo-all"]
    	url = http://example.com/repo
    	tagOpt = --tags
    	fetch = +refs/heads/*:refs/remotes/foo-all/*
    [remote "foo-none"]
    	url = http://example.com/repo
    	tagOpt = --no-tags
    	fetch = +refs/heads/*:refs/remotes/foo-none/*
    "#);
}

#[test]
fn test_git_remote_set_url() {
    let test_env = TestEnvironment::default();

    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");
    work_dir
        .run_jj(["git", "remote", "add", "foo", "http://example.com/repo/foo"])
        .success();
    let output = work_dir.run_jj([
        "git",
        "remote",
        "set-url",
        "bar",
        "http://example.com/repo/bar",
    ]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Error: No git remote named 'bar'
    [EOF]
    [exit status: 1]
    ");
    let output = work_dir.run_jj([
        "git",
        "remote",
        "set-url",
        "git",
        "http://example.com/repo/git",
    ]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Error: Git remote named 'git' is reserved for local Git repository
    [EOF]
    [exit status: 1]
    ");
    let output = work_dir.run_jj([
        "git",
        "remote",
        "set-url",
        "foo",
        "http://example.com/repo/bar",
    ]);
    insta::assert_snapshot!(output, @"");
    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @r"
    foo http://example.com/repo/bar
    [EOF]
    ");
    insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
    [core]
    	repositoryformatversion = 0
    	bare = true
    	logallrefupdates = false
    [remote "foo"]
    	url = http://example.com/repo/bar
    	fetch = +refs/heads/*:refs/remotes/foo/*
    "#);
    // explicitly set the push url to the same value as fetch works.
    let output = work_dir.run_jj([
        "git",
        "remote",
        "set-url",
        "foo",
        "--push",
        "https://example.com/repo/bar",
    ]);
    insta::assert_snapshot!(output, @"");
    insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
    [core]
    	repositoryformatversion = 0
    	bare = true
    	logallrefupdates = false
    [remote "foo"]
    	url = http://example.com/repo/bar
    	pushurl = https://example.com/repo/bar
    	fetch = +refs/heads/*:refs/remotes/foo/*
    "#);
    let output = work_dir.run_jj([
        "git",
        "remote",
        "set-url",
        "foo",
        "--push",
        "git@example.com:repo/bar",
    ]);
    insta::assert_snapshot!(output, @"");
    insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
    [core]
    	repositoryformatversion = 0
    	bare = true
    	logallrefupdates = false
    [remote "foo"]
    	url = http://example.com/repo/bar
    	pushurl = git@example.com:repo/bar
    	fetch = +refs/heads/*:refs/remotes/foo/*
    "#);
    let output = work_dir.run_jj([
        "git",
        "remote",
        "set-url",
        "foo",
        "--fetch",
        "http://example.com/repo/bar2",
    ]);
    insta::assert_snapshot!(output, @"");
    insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
    [core]
    	repositoryformatversion = 0
    	bare = true
    	logallrefupdates = false
    [remote "foo"]
    	url = http://example.com/repo/bar2
    	pushurl = git@example.com:repo/bar
    	fetch = +refs/heads/*:refs/remotes/foo/*
    "#);
    let output = work_dir.run_jj([
        "git",
        "remote",
        "set-url",
        "foo",
        "http://example.com/repo/bar",
    ]);
    insta::assert_snapshot!(output, @"");
    insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
    [core]
    	repositoryformatversion = 0
    	bare = true
    	logallrefupdates = false
    [remote "foo"]
    	url = http://example.com/repo/bar
    	pushurl = git@example.com:repo/bar
    	fetch = +refs/heads/*:refs/remotes/foo/*
    "#);
    let output = work_dir.run_jj([
        "git",
        "remote",
        "set-url",
        "foo",
        "https://example.com/repo/baz",
        "--fetch",
        "https://example.com/repo/bar2",
    ]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    error: the argument '[URL]' cannot be used with '--fetch <FETCH>'

    Usage: jj git remote set-url <REMOTE> <URL>

    For more information, try '--help'.
    [EOF]
    [exit status: 2]
    ");
    let output = work_dir.run_jj([
        "git",
        "remote",
        "set-url",
        "foo",
        "https://example.com/repo/baz",
        "--push",
        "git@example.com:/repo/baz",
    ]);
    insta::assert_snapshot!(output, @"");
    insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
    [core]
    	repositoryformatversion = 0
    	bare = true
    	logallrefupdates = false
    [remote "foo"]
    	url = https://example.com/repo/baz
    	pushurl = git@example.com:/repo/baz
    	fetch = +refs/heads/*:refs/remotes/foo/*
    "#);
    let output = work_dir.run_jj([
        "git",
        "remote",
        "set-url",
        "foo",
        "--fetch",
        "https://example.com/repo/bar",
        "--push",
        "git@example.com:/repo/bar",
    ]);
    insta::assert_snapshot!(output, @"");
    insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
    [core]
    	repositoryformatversion = 0
    	bare = true
    	logallrefupdates = false
    [remote "foo"]
    	url = https://example.com/repo/bar
    	pushurl = git@example.com:/repo/bar
    	fetch = +refs/heads/*:refs/remotes/foo/*
    "#);
}

#[test]
fn test_git_remote_relative_path() {
    let test_env = TestEnvironment::default();
    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");

    // Relative path using OS-native separator
    let path = PathBuf::from_iter(["..", "native", "sep"]);
    work_dir
        .run_jj(["git", "remote", "add", "foo", path.to_str().unwrap()])
        .success();
    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @r"
    foo $TEST_ENV/native/sep
    [EOF]
    ");

    // Relative path using UNIX separator
    test_env
        .run_jj_in(
            ".",
            ["-Rrepo", "git", "remote", "set-url", "foo", "unix/sep"],
        )
        .success();
    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @r"
    foo $TEST_ENV/unix/sep
    [EOF]
    ");
}

#[test]
fn test_git_remote_rename() {
    let test_env = TestEnvironment::default();

    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");
    work_dir
        .run_jj(["git", "remote", "add", "foo", "http://example.com/repo/foo"])
        .success();
    work_dir
        .run_jj(["git", "remote", "add", "baz", "http://example.com/repo/baz"])
        .success();
    let output = work_dir.run_jj(["git", "remote", "rename", "bar", "foo"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Error: No git remote named 'bar'
    [EOF]
    [exit status: 1]
    ");
    let output = work_dir.run_jj(["git", "remote", "rename", "foo", "baz"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Error: Git remote named 'baz' already exists
    [EOF]
    [exit status: 1]
    ");
    let output = work_dir.run_jj(["git", "remote", "rename", "foo", "git"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Error: Git remote named 'git' is reserved for local Git repository
    [EOF]
    [exit status: 1]
    ");
    let output = work_dir.run_jj(["git", "remote", "rename", "foo", "bar"]);
    insta::assert_snapshot!(output, @"");
    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @r"
    bar http://example.com/repo/foo
    baz http://example.com/repo/baz
    [EOF]
    ");
    insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
    [core]
    	repositoryformatversion = 0
    	bare = true
    	logallrefupdates = false
    [remote "baz"]
    	url = http://example.com/repo/baz
    	fetch = +refs/heads/*:refs/remotes/baz/*
    [remote "bar"]
    	url = http://example.com/repo/foo
    	fetch = +refs/heads/*:refs/remotes/bar/*
    "#);
}

#[test]
fn test_git_remote_named_git() {
    let test_env = TestEnvironment::default();

    // Existing remote named 'git' shouldn't block the repo initialization.
    let work_dir = test_env.work_dir("repo");
    git::init(work_dir.root());
    git::add_remote(work_dir.root(), "git", "http://example.com/repo/repo");
    work_dir.run_jj(["git", "init", "--git-repo=."]).success();
    work_dir
        .run_jj(["bookmark", "create", "-r@", "main"])
        .success();

    // The remote can be renamed.
    let output = work_dir.run_jj(["git", "remote", "rename", "git", "bar"]);
    insta::assert_snapshot!(output, @"");
    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @r"
    bar http://example.com/repo/repo
    [EOF]
    ------- stderr -------
    Done importing changes from the underlying Git repo.
    [EOF]
    ");
    insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
    [core]
    	repositoryformatversion = 0
    	bare = false
    	logallrefupdates = true
    [remote "bar"]
    	url = http://example.com/repo/repo
    	fetch = +refs/heads/*:refs/remotes/bar/*
    "#);
    // @git bookmark shouldn't be renamed.
    let output = work_dir.run_jj(["log", "-rmain@git", "-Tbookmarks"]);
    insta::assert_snapshot!(output, @r"
    @  main
    ~
    [EOF]
    ");

    // The remote cannot be renamed back by jj.
    let output = work_dir.run_jj(["git", "remote", "rename", "bar", "git"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Error: Git remote named 'git' is reserved for local Git repository
    [EOF]
    [exit status: 1]
    ");

    // Reinitialize the repo with remote named 'git'.
    work_dir.remove_dir_all(".jj");
    git::rename_remote(work_dir.root(), "bar", "git");
    work_dir.run_jj(["git", "init", "--git-repo=."]).success();
    insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
    [core]
    	repositoryformatversion = 0
    	bare = false
    	logallrefupdates = true
    [remote "git"]
    	url = http://example.com/repo/repo
    	fetch = +refs/heads/*:refs/remotes/git/*
    "#);

    // The remote can also be removed.
    let output = work_dir.run_jj(["git", "remote", "remove", "git"]);
    insta::assert_snapshot!(output, @"");
    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @"");
    insta::assert_snapshot!(read_git_config(work_dir.root()), @r"
    [core]
    	repositoryformatversion = 0
    	bare = false
    	logallrefupdates = true
    ");
    // @git bookmark shouldn't be removed.
    let output = work_dir.run_jj(["log", "-rmain@git", "-Tbookmarks"]);
    insta::assert_snapshot!(output, @r"
    ○  main
    ~
    [EOF]
    ");
}

#[test]
fn test_git_remote_with_slashes() {
    let test_env = TestEnvironment::default();

    // Existing remote with slashes shouldn't block the repo initialization.
    let work_dir = test_env.work_dir("repo");
    git::init(work_dir.root());
    git::add_remote(
        work_dir.root(),
        "slash/origin",
        "http://example.com/repo/repo",
    );
    work_dir.run_jj(["git", "init", "--git-repo=."]).success();
    work_dir
        .run_jj(["bookmark", "create", "-r@", "main"])
        .success();

    // Cannot add remote with a slash via `jj`
    let output = work_dir.run_jj([
        "git",
        "remote",
        "add",
        "another/origin",
        "http://examples.org/repo/repo",
    ]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Error: Git remotes with slashes are incompatible with jj: another/origin
    [EOF]
    [exit status: 1]
    ");
    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @r"
    slash/origin http://example.com/repo/repo
    [EOF]
    ");

    // The remote can be renamed.
    let output = work_dir.run_jj(["git", "remote", "rename", "slash/origin", "origin"]);
    insta::assert_snapshot!(output, @"");
    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @r"
    origin http://example.com/repo/repo
    [EOF]
    ");

    // The remote cannot be renamed back by jj.
    let output = work_dir.run_jj(["git", "remote", "rename", "origin", "slash/origin"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Error: Git remotes with slashes are incompatible with jj: slash/origin
    [EOF]
    [exit status: 1]
    ");

    // Reinitialize the repo with remote with slashes
    work_dir.remove_dir_all(".jj");
    git::rename_remote(work_dir.root(), "origin", "slash/origin");
    work_dir.run_jj(["git", "init", "--git-repo=."]).success();

    // The remote can also be removed.
    let output = work_dir.run_jj(["git", "remote", "remove", "slash/origin"]);
    insta::assert_snapshot!(output, @"");
    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @"");
    // @git bookmark shouldn't be removed.
    let output = work_dir.run_jj(["log", "-rmain@git", "-Tbookmarks"]);
    insta::assert_snapshot!(output, @r"
    ○  main
    ~
    [EOF]
    ");
}

#[test]
fn test_git_remote_with_branch_config() {
    let test_env = TestEnvironment::default();

    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");

    let output = work_dir.run_jj(["git", "remote", "add", "foo", "http://example.com/repo"]);
    insta::assert_snapshot!(output, @"");

    let mut config_file = fs::OpenOptions::new()
        .append(true)
        .open(work_dir.root().join(".jj/repo/store/git/config"))
        .unwrap();
    // `git clone` adds branch configuration like this.
    let eol = if cfg!(windows) { "\r\n" } else { "\n" };
    write!(config_file, "[branch \"test\"]{eol}").unwrap();
    write!(config_file, "\tremote = foo{eol}").unwrap();
    write!(config_file, "\tmerge = refs/heads/test{eol}").unwrap();
    drop(config_file);

    let output = work_dir.run_jj(["git", "remote", "rename", "foo", "bar"]);
    insta::assert_snapshot!(output, @"");

    insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
    [core]
    	repositoryformatversion = 0
    	bare = true
    	logallrefupdates = false
    [branch "test"]
    	remote = bar
    	merge = refs/heads/test
    [remote "bar"]
    	url = http://example.com/repo
    	fetch = +refs/heads/*:refs/remotes/bar/*
    "#);
}

#[test]
fn test_git_remote_with_global_git_remote_config() {
    let mut test_env = TestEnvironment::default();
    test_env.work_dir("").write_file(
        "git-config",
        indoc! {r#"
            [remote "origin"]
                prune = true
            [remote "foo"]
                url = htps://example.com/repo/foo
                fetch = +refs/heads/*:refs/remotes/foo/*
        "#},
    );
    test_env.add_env_var("GIT_CONFIG_GLOBAL", test_env.env_root().join("git-config"));

    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");

    let output = work_dir.run_jj(["git", "remote", "list"]);
    // Complete remotes from the global configuration are listed.
    //
    // `git remote -v` lists all remotes from the global configuration,
    // even incomplete ones like `origin`. This is inconsistent with
    // the other `git remote` commands, which ignore the global
    // configuration (even `git remote get-url`).
    insta::assert_snapshot!(output, @r"
    foo htps://example.com/repo/foo
    [EOF]
    ");

    let output = work_dir.run_jj(["git", "remote", "rename", "foo", "bar"]);
    // Divergence from Git: we read the remote from the global
    // configuration and write it back out. Git will use the global
    // configuration for commands like `git remote -v`, `git fetch`,
    // and `git push`, but `git remote rename`, `git remote remove`,
    // `git remote set-url`, etc., will ignore it.
    //
    // This behavior applies to `jj git remote remove` and
    // `jj git remote set-url` as well. It would be hard to change due
    // to gitoxide’s model, but hopefully it’s relatively harmless.
    insta::assert_snapshot!(output, @"");
    insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
    [core]
    	repositoryformatversion = 0
    	bare = true
    	logallrefupdates = false
    [remote "bar"]
    	url = htps://example.com/repo/foo
    	fetch = +refs/heads/*:refs/remotes/bar/*
    "#);
    // This has the unfortunate consequence that the original remote
    // still exists after renaming.
    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @r"
    bar htps://example.com/repo/foo
    foo htps://example.com/repo/foo
    [EOF]
    ");

    let output = work_dir.run_jj([
        "git",
        "remote",
        "add",
        "origin",
        "http://example.com/repo/origin/1",
    ]);
    insta::assert_snapshot!(output, @"");

    let output = work_dir.run_jj([
        "git",
        "remote",
        "set-url",
        "origin",
        "https://example.com/repo/origin/2",
    ]);
    insta::assert_snapshot!(output, @"");

    let output = work_dir.run_jj(["git", "remote", "list"]);
    insta::assert_snapshot!(output, @r"
    bar htps://example.com/repo/foo
    foo htps://example.com/repo/foo
    origin https://example.com/repo/origin/2
    [EOF]
    ");
    insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
    [core]
    	repositoryformatversion = 0
    	bare = true
    	logallrefupdates = false
    [remote "bar"]
    	url = htps://example.com/repo/foo
    	fetch = +refs/heads/*:refs/remotes/bar/*
    [remote "origin"]
    	url = https://example.com/repo/origin/2
    	fetch = +refs/heads/*:refs/remotes/origin/*
    "#);
}