Debian Codegraph @

cli/tests/test_git_remotes.rs

raw
1 // Copyright 2022 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use std::fs;
16 use std::io::Write as _;
17 use std::path::Path;
18 use std::path::PathBuf;
19
20 use indoc::indoc;
21 use testutils::TestResult;
22 use testutils::git;
23
24 use crate::common::TestEnvironment;
25
26 fn read_git_config(repo_path: &Path) -> String {
27 let git_config = fs::read_to_string(repo_path.join(".jj/repo/store/git/config"))
28 .or_else(|_| fs::read_to_string(repo_path.join(".git/config")))
29 .unwrap();
30 git_config
31 .split_inclusive('\n')
32 .filter(|line| {
33 // Filter out non‐portable values.
34 [
35 "\tfilemode =",
36 "\tsymlinks =",
37 "\tignorecase =",
38 "\tprecomposeunicode =",
39 ]
40 .iter()
41 .all(|prefix| !line.to_ascii_lowercase().starts_with(prefix))
42 })
43 .collect()
44 }
45
46 #[test]
47 fn test_git_remotes() {
48 let test_env = TestEnvironment::default();
49
50 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
51 let work_dir = test_env.work_dir("repo");
52
53 let output = work_dir.run_jj(["git", "remote", "list"]);
54 insta::assert_snapshot!(output, @"");
55 let output = work_dir.run_jj(["git", "remote", "add", "foo", "http://example.com/repo/foo"]);
56 insta::assert_snapshot!(output, @"");
57 let output = work_dir.run_jj(["git", "remote", "add", "bar", "http://example.com/repo/bar"]);
58 insta::assert_snapshot!(output, @"");
59 let output = work_dir.run_jj([
60 "git",
61 "remote",
62 "add",
63 "baz",
64 "http://example.com/repo/baz",
65 "--push-url",
66 "[email protected]:repo/baz",
67 ]);
68 insta::assert_snapshot!(output, @"");
69 let output = work_dir.run_jj(["git", "remote", "list"]);
70 insta::assert_snapshot!(output, @"
71 bar http://example.com/repo/bar
72 baz http://example.com/repo/baz (push: [email protected]:repo/baz)
73 foo http://example.com/repo/foo
74 [EOF]
75 ");
76 let output = work_dir.run_jj(["git", "remote", "remove", "foo"]);
77 insta::assert_snapshot!(output, @"");
78 let output = work_dir.run_jj(["git", "remote", "list"]);
79 insta::assert_snapshot!(output, @"
80 bar http://example.com/repo/bar
81 baz http://example.com/repo/baz (push: [email protected]:repo/baz)
82 [EOF]
83 ");
84 let output = work_dir.run_jj(["git", "remote", "remove", "nonexistent"]);
85 insta::assert_snapshot!(output, @"
86 ------- stderr -------
87 Error: No git remote named 'nonexistent'
88 [EOF]
89 [exit status: 1]
90 ");
91 insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
92 [core]
93 bare = true
94 logallrefupdates = false
95 repositoryformatversion = 0
96 [remote "bar"]
97 url = http://example.com/repo/bar
98 fetch = +refs/heads/*:refs/remotes/bar/*
99 [remote "baz"]
100 url = http://example.com/repo/baz
101 pushurl = [email protected]:repo/baz
102 fetch = +refs/heads/*:refs/remotes/baz/*
103 "#);
104
105 // named remote that cannot be parsed
106 work_dir.write_file(
107 ".jj/repo/store/git/config",
108 indoc! {r#"
109 [remote "foo"]
110 url = https://
111 "#},
112 );
113 let output = work_dir.run_jj(["git", "remote", "list"]);
114 insta::assert_snapshot!(output, @r#"
115 ------- stderr -------
116 Error: Unexpected Git error when managing remotes
117 Caused by:
118 1: The fetch url under `remote.foo` was invalid
119 2: The url at "remote.<name>.url=https://" could not be parsed
120 3: URL "https://" can not be parsed as valid URL
121 4: Scheme requires host
122 [EOF]
123 [exit status: 1]
124 "#);
125 }
126
127 #[test]
128 fn test_git_remote_add() {
129 let test_env = TestEnvironment::default();
130
131 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
132 let work_dir = test_env.work_dir("repo");
133 work_dir
134 .run_jj(["git", "remote", "add", "foo", "http://example.com/repo/foo"])
135 .success();
136 let output = work_dir.run_jj([
137 "git",
138 "remote",
139 "add",
140 "foo",
141 "http://example.com/repo/foo2",
142 ]);
143 insta::assert_snapshot!(output, @"
144 ------- stderr -------
145 Error: Git remote named 'foo' already exists
146 [EOF]
147 [exit status: 1]
148 ");
149 let output = work_dir.run_jj(["git", "remote", "add", "git", "http://example.com/repo/git"]);
150 insta::assert_snapshot!(output, @"
151 ------- stderr -------
152 Error: Git remote named 'git' is reserved for local Git repository
153 [EOF]
154 [exit status: 1]
155 ");
156 let output = work_dir.run_jj(["git", "remote", "list"]);
157 insta::assert_snapshot!(output, @"
158 foo http://example.com/repo/foo
159 [EOF]
160 ");
161 }
162
163 #[test]
164 fn test_git_remote_add_duplicate_url_warning() {
165 let test_env = TestEnvironment::default();
166
167 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
168 let work_dir = test_env.work_dir("repo");
169 work_dir
170 .run_jj(["git", "remote", "add", "foo", "http://example.com/repo/foo"])
171 .success();
172 let output = work_dir.run_jj(["git", "remote", "add", "bar", "http://example.com/repo/foo"]);
173 insta::assert_snapshot!(output, @"
174 ------- stderr -------
175 Warning: Remote foo already uses the same URL.
176 Hint: If this was a mistake, run `jj git remote remove bar`.
177 [EOF]
178 ");
179 let output = work_dir.run_jj(["git", "remote", "list"]);
180 insta::assert_snapshot!(output, @"
181 bar http://example.com/repo/foo
182 foo http://example.com/repo/foo
183 [EOF]
184 ");
185 }
186
187 #[test]
188 fn test_git_remote_add_duplicate_url_warning_with_url_rewrite() {
189 let test_env = TestEnvironment::default();
190
191 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
192 let work_dir = test_env.work_dir("repo");
193 let mut config_file = fs::OpenOptions::new()
194 .append(true)
195 .open(work_dir.root().join(".jj/repo/store/git/config"))
196 .unwrap();
197 // The warning is about exact configured URL strings. Git rewrite rules can
198 // make different strings resolve to the same URL, so they should not affect
199 // whether this warning fires.
200 writeln!(
201 config_file,
202 r#"[url "https://example.com/"]
203 insteadOf = gh:"#
204 )
205 .unwrap();
206 drop(config_file);
207
208 work_dir
209 .run_jj(["git", "remote", "add", "foo", "gh:org/repo"])
210 .success();
211 let output = work_dir.run_jj(["git", "remote", "add", "bar", "gh:org/repo"]);
212 insta::assert_snapshot!(output, @"
213 ------- stderr -------
214 Warning: Remote foo already uses the same URL.
215 Hint: If this was a mistake, run `jj git remote remove bar`.
216 [EOF]
217 ");
218 let output = work_dir.run_jj([
219 "git",
220 "remote",
221 "add",
222 "baz",
223 "https://example.com/org/repo",
224 ]);
225 insta::assert_snapshot!(output, @"");
226 }
227
228 #[test]
229 fn test_git_remote_add_duplicate_url_warning_omits_url() {
230 let test_env = TestEnvironment::default();
231
232 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
233 let work_dir = test_env.work_dir("repo");
234 // Remote URLs can contain embedded credentials. The warning should identify
235 // the duplicate remote without echoing secrets into stderr or logs.
236 work_dir
237 .run_jj([
238 "git",
239 "remote",
240 "add",
241 "foo",
242 "https://user:[email protected]/repo",
243 ])
244 .success();
245 let output = work_dir.run_jj([
246 "git",
247 "remote",
248 "add",
249 "bar",
250 "https://user:[email protected]/repo",
251 ]);
252 insta::assert_snapshot!(output, @"
253 ------- stderr -------
254 Warning: Remote foo already uses the same URL.
255 Hint: If this was a mistake, run `jj git remote remove bar`.
256 [EOF]
257 ");
258 }
259
260 #[test]
261 fn test_git_remote_add_duplicate_url_warning_cross_direction() {
262 let test_env = TestEnvironment::default();
263
264 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
265 let work_dir = test_env.work_dir("repo");
266 work_dir
267 .run_jj([
268 "git",
269 "remote",
270 "add",
271 "foo",
272 "http://example.com/repo/fetch",
273 "--push-url",
274 "http://example.com/repo/push",
275 ])
276 .success();
277
278 let output = work_dir.run_jj([
279 "git",
280 "remote",
281 "add",
282 "bar",
283 "http://example.com/repo/new-fetch",
284 "--push-url",
285 "http://example.com/repo/fetch",
286 ]);
287 insta::assert_snapshot!(output, @"
288 ------- stderr -------
289 Warning: Remote foo already uses the same URL.
290 Hint: If this was a mistake, run `jj git remote remove bar`.
291 [EOF]
292 ");
293 let output = work_dir.run_jj([
294 "git",
295 "remote",
296 "add",
297 "baz",
298 "http://example.com/repo/push",
299 "--push-url",
300 "http://example.com/repo/new-push",
301 ]);
302 insta::assert_snapshot!(output, @"
303 ------- stderr -------
304 Warning: Remote foo already uses the same URL.
305 Hint: If this was a mistake, run `jj git remote remove baz`.
306 [EOF]
307 ");
308 }
309
310 #[test]
311 fn test_git_remote_set_url() {
312 let test_env = TestEnvironment::default();
313
314 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
315 let work_dir = test_env.work_dir("repo");
316 work_dir
317 .run_jj(["git", "remote", "add", "foo", "http://example.com/repo/foo"])
318 .success();
319 let output = work_dir.run_jj([
320 "git",
321 "remote",
322 "set-url",
323 "bar",
324 "http://example.com/repo/bar",
325 ]);
326 insta::assert_snapshot!(output, @"
327 ------- stderr -------
328 Error: No git remote named 'bar'
329 [EOF]
330 [exit status: 1]
331 ");
332 let output = work_dir.run_jj([
333 "git",
334 "remote",
335 "set-url",
336 "git",
337 "http://example.com/repo/git",
338 ]);
339 insta::assert_snapshot!(output, @"
340 ------- stderr -------
341 Error: Git remote named 'git' is reserved for local Git repository
342 [EOF]
343 [exit status: 1]
344 ");
345 let output = work_dir.run_jj([
346 "git",
347 "remote",
348 "set-url",
349 "foo",
350 "http://example.com/repo/bar",
351 ]);
352 insta::assert_snapshot!(output, @"");
353 let output = work_dir.run_jj(["git", "remote", "list"]);
354 insta::assert_snapshot!(output, @"
355 foo http://example.com/repo/bar
356 [EOF]
357 ");
358 insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
359 [core]
360 bare = true
361 logallrefupdates = false
362 repositoryformatversion = 0
363 [remote "foo"]
364 url = http://example.com/repo/bar
365 fetch = +refs/heads/*:refs/remotes/foo/*
366 "#);
367 // explicitly set the push url to the same value as fetch works.
368 let output = work_dir.run_jj([
369 "git",
370 "remote",
371 "set-url",
372 "foo",
373 "--push",
374 "https://example.com/repo/bar",
375 ]);
376 insta::assert_snapshot!(output, @"");
377 insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
378 [core]
379 bare = true
380 logallrefupdates = false
381 repositoryformatversion = 0
382 [remote "foo"]
383 url = http://example.com/repo/bar
384 pushurl = https://example.com/repo/bar
385 fetch = +refs/heads/*:refs/remotes/foo/*
386 "#);
387 let output = work_dir.run_jj([
388 "git",
389 "remote",
390 "set-url",
391 "foo",
392 "--push",
393 "[email protected]:repo/bar",
394 ]);
395 insta::assert_snapshot!(output, @"");
396 insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
397 [core]
398 bare = true
399 logallrefupdates = false
400 repositoryformatversion = 0
401 [remote "foo"]
402 url = http://example.com/repo/bar
403 pushurl = [email protected]:repo/bar
404 fetch = +refs/heads/*:refs/remotes/foo/*
405 "#);
406 let output = work_dir.run_jj([
407 "git",
408 "remote",
409 "set-url",
410 "foo",
411 "--fetch",
412 "http://example.com/repo/bar2",
413 ]);
414 insta::assert_snapshot!(output, @"");
415 insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
416 [core]
417 bare = true
418 logallrefupdates = false
419 repositoryformatversion = 0
420 [remote "foo"]
421 url = http://example.com/repo/bar2
422 pushurl = [email protected]:repo/bar
423 fetch = +refs/heads/*:refs/remotes/foo/*
424 "#);
425 let output = work_dir.run_jj([
426 "git",
427 "remote",
428 "set-url",
429 "foo",
430 "http://example.com/repo/bar",
431 ]);
432 insta::assert_snapshot!(output, @"");
433 insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
434 [core]
435 bare = true
436 logallrefupdates = false
437 repositoryformatversion = 0
438 [remote "foo"]
439 url = http://example.com/repo/bar
440 pushurl = [email protected]:repo/bar
441 fetch = +refs/heads/*:refs/remotes/foo/*
442 "#);
443 let output = work_dir.run_jj([
444 "git",
445 "remote",
446 "set-url",
447 "foo",
448 "https://example.com/repo/baz",
449 "--fetch",
450 "https://example.com/repo/bar2",
451 ]);
452 insta::assert_snapshot!(output, @"
453 ------- stderr -------
454 error: the argument '[URL]' cannot be used with '--fetch <FETCH>'
455
456 Usage: jj git remote set-url <REMOTE> <URL>
457
458 For more information, try '--help'.
459 [EOF]
460 [exit status: 2]
461 ");
462 let output = work_dir.run_jj([
463 "git",
464 "remote",
465 "set-url",
466 "foo",
467 "https://example.com/repo/baz",
468 "--push",
469 "[email protected]:/repo/baz",
470 ]);
471 insta::assert_snapshot!(output, @"");
472 insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
473 [core]
474 bare = true
475 logallrefupdates = false
476 repositoryformatversion = 0
477 [remote "foo"]
478 url = https://example.com/repo/baz
479 pushurl = [email protected]:/repo/baz
480 fetch = +refs/heads/*:refs/remotes/foo/*
481 "#);
482 let output = work_dir.run_jj([
483 "git",
484 "remote",
485 "set-url",
486 "foo",
487 "--fetch",
488 "https://example.com/repo/bar",
489 "--push",
490 "[email protected]:/repo/bar",
491 ]);
492 insta::assert_snapshot!(output, @"");
493 insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
494 [core]
495 bare = true
496 logallrefupdates = false
497 repositoryformatversion = 0
498 [remote "foo"]
499 url = https://example.com/repo/bar
500 pushurl = [email protected]:/repo/bar
501 fetch = +refs/heads/*:refs/remotes/foo/*
502 "#);
503 }
504
505 #[test]
506 fn test_git_remote_relative_path() {
507 let test_env = TestEnvironment::default();
508 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
509 let work_dir = test_env.work_dir("repo");
510
511 // Relative path using OS-native separator
512 let path = PathBuf::from_iter(["..", "native", "sep"]);
513 work_dir
514 .run_jj(["git", "remote", "add", "foo", path.to_str().unwrap()])
515 .success();
516 let output = work_dir.run_jj(["git", "remote", "list"]);
517 insta::assert_snapshot!(output, @"
518 foo $TEST_ENV/native/sep
519 [EOF]
520 ");
521
522 // Relative path using UNIX separator
523 test_env
524 .run_jj_in(
525 ".",
526 ["-Rrepo", "git", "remote", "set-url", "foo", "unix/sep"],
527 )
528 .success();
529 let output = work_dir.run_jj(["git", "remote", "list"]);
530 insta::assert_snapshot!(output, @"
531 foo $TEST_ENV/unix/sep
532 [EOF]
533 ");
534 }
535
536 #[test]
537 fn test_git_remote_rename() {
538 let test_env = TestEnvironment::default();
539
540 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
541 let work_dir = test_env.work_dir("repo");
542 work_dir
543 .run_jj(["git", "remote", "add", "foo", "http://example.com/repo/foo"])
544 .success();
545 work_dir
546 .run_jj(["git", "remote", "add", "baz", "http://example.com/repo/baz"])
547 .success();
548 let output = work_dir.run_jj(["git", "remote", "rename", "bar", "foo"]);
549 insta::assert_snapshot!(output, @"
550 ------- stderr -------
551 Error: No git remote named 'bar'
552 [EOF]
553 [exit status: 1]
554 ");
555 let output = work_dir.run_jj(["git", "remote", "rename", "foo", "baz"]);
556 insta::assert_snapshot!(output, @"
557 ------- stderr -------
558 Error: Git remote named 'baz' already exists
559 [EOF]
560 [exit status: 1]
561 ");
562 let output = work_dir.run_jj(["git", "remote", "rename", "foo", "git"]);
563 insta::assert_snapshot!(output, @"
564 ------- stderr -------
565 Error: Git remote named 'git' is reserved for local Git repository
566 [EOF]
567 [exit status: 1]
568 ");
569 let output = work_dir.run_jj(["git", "remote", "rename", "foo", "bar"]);
570 insta::assert_snapshot!(output, @"");
571 let output = work_dir.run_jj(["git", "remote", "list"]);
572 insta::assert_snapshot!(output, @"
573 bar http://example.com/repo/foo
574 baz http://example.com/repo/baz
575 [EOF]
576 ");
577 insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
578 [core]
579 bare = true
580 logallrefupdates = false
581 repositoryformatversion = 0
582 [remote "baz"]
583 url = http://example.com/repo/baz
584 fetch = +refs/heads/*:refs/remotes/baz/*
585 [remote "bar"]
586 url = http://example.com/repo/foo
587 fetch = +refs/heads/*:refs/remotes/bar/*
588 "#);
589 }
590
591 #[test]
592 fn test_git_remote_rename_updates_trunk() {
593 // Verify trunk() resolves correctly after renaming the remote it references.
594 let test_env = TestEnvironment::default();
595
596 let remote_repo = git::init(test_env.env_root().join("remote"));
597 git::add_commit(&remote_repo, "refs/heads/main", "file", b"", "init", &[]);
598 git::set_symbolic_reference(&remote_repo, "HEAD", "refs/heads/main");
599 test_env
600 .run_jj_in(".", ["git", "clone", "--branch=main", "remote", "local"])
601 .success();
602 let local_dir = test_env.work_dir("local");
603
604 let output = local_dir.run_jj(["git", "remote", "rename", "origin", "upstream"]);
605 insta::assert_snapshot!(output, @"
606 ------- stderr -------
607 Updating the revset alias `trunk()` to `main@upstream`.
608 [EOF]
609 ");
610
611 // trunk() should resolve to the correct commit after the rename
612 let output = local_dir.run_jj(["log", "-r", "trunk()", "-T", "description"]);
613 insta::assert_snapshot!(output, @"
614 ◆ init
615 │
616 ~
617 [EOF]
618 ");
619 }
620
621 #[test]
622 fn test_git_remote_with_preset_config() {
623 let test_env = TestEnvironment::default();
624 // Add user-level config which shouldn't be renamed
625 test_env.add_config(indoc! {r#"
626 remotes.origin.fetch-bookmarks = "user-origin"
627 remotes.foo.fetch-bookmarks = "user-foo"
628 remotes.bar.fetch-tags = "user-bar"
629 "#});
630
631 // Set up default branch at remote
632 let remote_repo = git::init(test_env.env_root().join("remote"));
633 git::add_commit(&remote_repo, "refs/heads/main", "file", b"", "init", &[]);
634 git::set_symbolic_reference(&remote_repo, "HEAD", "refs/heads/main");
635
636 // Clone the repo, add another remote to ensure that only the target remote
637 // settings will be updated
638 test_env
639 .run_jj_in(
640 ".",
641 [
642 "git",
643 "clone",
644 "--branch=main",
645 "--tag=~*",
646 "remote",
647 "local",
648 ],
649 )
650 .success();
651 let local_dir = test_env.work_dir("local");
652 local_dir
653 .run_jj(["git", "remote", "add", "bar", "../remote"])
654 .success();
655 local_dir
656 .run_jj([
657 "config",
658 "set",
659 "--repo",
660 "remotes.bar.fetch-bookmarks",
661 "repo-bar",
662 ])
663 .success();
664
665 let list_remotes_config =
666 || local_dir.run_jj(["config", "list", "--include-overridden", "remotes"]);
667 let list_trunk_config = || local_dir.run_jj(["config", "list", "revset-aliases.'trunk()'"]);
668 insta::assert_snapshot!(list_remotes_config(), @r#"
669 # remotes.origin.fetch-bookmarks = "user-origin"
670 remotes.foo.fetch-bookmarks = "user-foo"
671 remotes.bar.fetch-tags = "user-bar"
672 remotes.origin.fetch-bookmarks = "main"
673 remotes.origin.fetch-tags = "~*"
674 remotes.bar.fetch-bookmarks = "repo-bar"
675 [EOF]
676 "#);
677 insta::assert_snapshot!(list_trunk_config(), @r#"
678 revset-aliases.'trunk()' = "main@origin"
679 [EOF]
680 "#);
681
682 // Preset repo-level config should be updated automatically
683 let output = local_dir.run_jj(["git", "remote", "rename", "origin", "foo"]);
684 insta::assert_snapshot!(output, @"
685 ------- stderr -------
686 Updating the revset alias `trunk()` to `main@foo`.
687 [EOF]
688 ");
689 insta::assert_snapshot!(list_remotes_config(), @r#"
690 remotes.origin.fetch-bookmarks = "user-origin"
691 # remotes.foo.fetch-bookmarks = "user-foo"
692 remotes.bar.fetch-tags = "user-bar"
693 remotes.foo.fetch-bookmarks = "main"
694 remotes.foo.fetch-tags = "~*"
695 remotes.bar.fetch-bookmarks = "repo-bar"
696 [EOF]
697 "#);
698 insta::assert_snapshot!(list_trunk_config(), @r#"
699 revset-aliases.'trunk()' = "main@foo"
700 [EOF]
701 "#);
702
703 // Preset repo-level config should be removed automatically
704 // TODO: suppress warning about unresolvable trunk()
705 let output = local_dir.run_jj(["git", "remote", "remove", "foo"]);
706 insta::assert_snapshot!(output, @"
707 ------- stderr -------
708 Resetting the revset alias `trunk()` to default value.
709 [EOF]
710 ");
711 insta::assert_snapshot!(list_remotes_config(), @r#"
712 remotes.origin.fetch-bookmarks = "user-origin"
713 remotes.foo.fetch-bookmarks = "user-foo"
714 remotes.bar.fetch-tags = "user-bar"
715 remotes.bar.fetch-bookmarks = "repo-bar"
716 [EOF]
717 "#);
718 insta::assert_snapshot!(list_trunk_config(), @r"
719 ------- stderr -------
720 Warning: No matching config key for: revset-aliases.'trunk()'
721 [EOF]
722 ");
723
724 // Set trunk to non-default value, which shouldn't be updated automatically
725 local_dir
726 .run_jj([
727 "config",
728 "set",
729 "--repo",
730 "revset-aliases.'trunk()'",
731 "main@custom-remote",
732 ])
733 .success();
734 let output = local_dir.run_jj(["git", "remote", "rename", "bar", "foo"]);
735 insta::assert_snapshot!(output, @"
736 ------- stderr -------
737 Warning: Failed to resolve `revset-aliases.trunk()`: Revision `main@custom-remote` doesn't exist
738 The `trunk()` alias is temporarily set to `root()`.
739 Hint: Use `jj config edit --repo` to adjust the `trunk()` alias.
740 [EOF]
741 ");
742 insta::assert_snapshot!(list_remotes_config(), @r#"
743 remotes.origin.fetch-bookmarks = "user-origin"
744 # remotes.foo.fetch-bookmarks = "user-foo"
745 remotes.bar.fetch-tags = "user-bar"
746 remotes.foo.fetch-bookmarks = "repo-bar"
747 [EOF]
748 "#);
749 insta::assert_snapshot!(list_trunk_config(), @r#"
750 revset-aliases.'trunk()' = "main@custom-remote"
751 [EOF]
752 "#);
753
754 let output = local_dir.run_jj(["git", "remote", "remove", "foo"]);
755 insta::assert_snapshot!(output, @"
756 ------- stderr -------
757 Warning: Failed to resolve `revset-aliases.trunk()`: Revision `main@custom-remote` doesn't exist
758 The `trunk()` alias is temporarily set to `root()`.
759 Hint: Use `jj config edit --repo` to adjust the `trunk()` alias.
760 [EOF]
761 ");
762 insta::assert_snapshot!(list_remotes_config(), @r#"
763 remotes.origin.fetch-bookmarks = "user-origin"
764 remotes.foo.fetch-bookmarks = "user-foo"
765 remotes.bar.fetch-tags = "user-bar"
766 [EOF]
767 "#);
768 insta::assert_snapshot!(list_trunk_config(), @r#"
769 revset-aliases.'trunk()' = "main@custom-remote"
770 [EOF]
771 "#);
772 }
773
774 #[test]
775 fn test_git_remote_named_git() {
776 let test_env = TestEnvironment::default();
777
778 // Existing remote named 'git' shouldn't block the repo initialization.
779 let work_dir = test_env.work_dir("repo");
780 git::init(work_dir.root());
781 git::add_remote(work_dir.root(), "git", "http://example.com/repo/repo");
782 work_dir.run_jj(["git", "init", "--git-repo=."]).success();
783 work_dir
784 .run_jj(["bookmark", "create", "-r@", "main"])
785 .success();
786
787 // The remote can be renamed.
788 let output = work_dir.run_jj(["git", "remote", "rename", "git", "bar"]);
789 insta::assert_snapshot!(output, @"");
790 let output = work_dir.run_jj(["git", "remote", "list"]);
791 insta::assert_snapshot!(output, @"
792 bar http://example.com/repo/repo
793 [EOF]
794 ------- stderr -------
795 Done importing changes from the underlying Git repo.
796 [EOF]
797 ");
798 insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
799 [core]
800 bare = false
801 logallrefupdates = true
802 repositoryformatversion = 0
803 [remote "bar"]
804 url = http://example.com/repo/repo
805 fetch = +refs/heads/*:refs/remotes/bar/*
806 "#);
807 // @git bookmark shouldn't be renamed.
808 let output = work_dir.run_jj(["log", "-rmain@git", "-Tbookmarks"]);
809 insta::assert_snapshot!(output, @"
810 @ main
811 │
812 ~
813 [EOF]
814 ");
815
816 // The remote cannot be renamed back by jj.
817 let output = work_dir.run_jj(["git", "remote", "rename", "bar", "git"]);
818 insta::assert_snapshot!(output, @"
819 ------- stderr -------
820 Error: Git remote named 'git' is reserved for local Git repository
821 [EOF]
822 [exit status: 1]
823 ");
824
825 // Reinitialize the repo with remote named 'git'.
826 work_dir.remove_dir_all(".jj");
827 git::rename_remote(work_dir.root(), "bar", "git");
828 work_dir.run_jj(["git", "init", "--git-repo=."]).success();
829 insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
830 [core]
831 bare = false
832 logallrefupdates = true
833 repositoryformatversion = 0
834 [remote "git"]
835 url = http://example.com/repo/repo
836 fetch = +refs/heads/*:refs/remotes/git/*
837 "#);
838
839 // The remote can also be removed.
840 let output = work_dir.run_jj(["git", "remote", "remove", "git"]);
841 insta::assert_snapshot!(output, @"");
842 let output = work_dir.run_jj(["git", "remote", "list"]);
843 insta::assert_snapshot!(output, @"");
844 insta::assert_snapshot!(read_git_config(work_dir.root()), @"
845 [core]
846 bare = false
847 logallrefupdates = true
848 repositoryformatversion = 0
849 ");
850 // @git bookmark shouldn't be removed.
851 let output = work_dir.run_jj(["log", "-rmain@git", "-Tbookmarks"]);
852 insta::assert_snapshot!(output, @"
853 ○ main
854 │
855 ~
856 [EOF]
857 ");
858 }
859
860 #[test]
861 fn test_git_remote_with_slashes() {
862 let test_env = TestEnvironment::default();
863
864 // Existing remote with slashes shouldn't block the repo initialization.
865 let work_dir = test_env.work_dir("repo");
866 git::init(work_dir.root());
867 git::add_remote(
868 work_dir.root(),
869 "slash/origin",
870 "http://example.com/repo/repo",
871 );
872 work_dir.run_jj(["git", "init", "--git-repo=."]).success();
873 work_dir
874 .run_jj(["bookmark", "create", "-r@", "main"])
875 .success();
876
877 // Cannot add remote with a slash via `jj`
878 let output = work_dir.run_jj([
879 "git",
880 "remote",
881 "add",
882 "another/origin",
883 "http://examples.org/repo/repo",
884 ]);
885 insta::assert_snapshot!(output, @"
886 ------- stderr -------
887 Error: Git remotes with slashes are incompatible with jj: another/origin
888 [EOF]
889 [exit status: 1]
890 ");
891 let output = work_dir.run_jj(["git", "remote", "list"]);
892 insta::assert_snapshot!(output, @"
893 slash/origin http://example.com/repo/repo
894 [EOF]
895 ");
896
897 // The remote can be renamed.
898 let output = work_dir.run_jj(["git", "remote", "rename", "slash/origin", "origin"]);
899 insta::assert_snapshot!(output, @"");
900 let output = work_dir.run_jj(["git", "remote", "list"]);
901 insta::assert_snapshot!(output, @"
902 origin http://example.com/repo/repo
903 [EOF]
904 ");
905
906 // The remote cannot be renamed back by jj.
907 let output = work_dir.run_jj(["git", "remote", "rename", "origin", "slash/origin"]);
908 insta::assert_snapshot!(output, @"
909 ------- stderr -------
910 Error: Git remotes with slashes are incompatible with jj: slash/origin
911 [EOF]
912 [exit status: 1]
913 ");
914
915 // Reinitialize the repo with remote with slashes
916 work_dir.remove_dir_all(".jj");
917 git::rename_remote(work_dir.root(), "origin", "slash/origin");
918 work_dir.run_jj(["git", "init", "--git-repo=."]).success();
919
920 // The remote can also be removed.
921 let output = work_dir.run_jj(["git", "remote", "remove", "slash/origin"]);
922 insta::assert_snapshot!(output, @"");
923 let output = work_dir.run_jj(["git", "remote", "list"]);
924 insta::assert_snapshot!(output, @"");
925 // @git bookmark shouldn't be removed.
926 let output = work_dir.run_jj(["log", "-rmain@git", "-Tbookmarks"]);
927 insta::assert_snapshot!(output, @"
928 ○ main
929 │
930 ~
931 [EOF]
932 ");
933 }
934
935 #[test]
936 fn test_git_remote_with_branch_config() -> TestResult {
937 let test_env = TestEnvironment::default();
938
939 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
940 let work_dir = test_env.work_dir("repo");
941
942 let output = work_dir.run_jj(["git", "remote", "add", "foo", "http://example.com/repo"]);
943 insta::assert_snapshot!(output, @"");
944
945 let mut config_file = fs::OpenOptions::new()
946 .append(true)
947 .open(work_dir.root().join(".jj/repo/store/git/config"))?;
948 // `git clone` adds branch configuration like this.
949 let eol = if cfg!(windows) { "\r\n" } else { "\n" };
950 write!(config_file, "[branch \"test\"]{eol}")?;
951 write!(config_file, "\tremote = foo{eol}")?;
952 write!(config_file, "\tmerge = refs/heads/test{eol}")?;
953 drop(config_file);
954
955 let output = work_dir.run_jj(["git", "remote", "rename", "foo", "bar"]);
956 insta::assert_snapshot!(output, @"");
957
958 insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
959 [core]
960 bare = true
961 logallrefupdates = false
962 repositoryformatversion = 0
963 [branch "test"]
964 remote = bar
965 merge = refs/heads/test
966 [remote "bar"]
967 url = http://example.com/repo
968 fetch = +refs/heads/*:refs/remotes/bar/*
969 "#);
970 Ok(())
971 }
972
973 #[test]
974 fn test_git_remote_with_global_git_remote_config() {
975 let mut test_env = TestEnvironment::default();
976 test_env.work_dir("").write_file(
977 "git-config",
978 indoc! {r#"
979 [remote "origin"]
980 prune = true
981 [remote "foo"]
982 url = htps://example.com/repo/foo
983 fetch = +refs/heads/*:refs/remotes/foo/*
984 "#},
985 );
986 test_env.add_env_var("GIT_CONFIG_GLOBAL", test_env.env_root().join("git-config"));
987
988 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
989 let work_dir = test_env.work_dir("repo");
990
991 let output = work_dir.run_jj(["git", "remote", "list"]);
992 // Complete remotes from the global configuration are listed.
993 //
994 // `git remote -v` lists all remotes from the global configuration,
995 // even incomplete ones like `origin`. This is inconsistent with
996 // the other `git remote` commands, which ignore the global
997 // configuration (even `git remote get-url`).
998 insta::assert_snapshot!(output, @"
999 foo htps://example.com/repo/foo
1000 [EOF]
1001 ");
1002
1003 let output = work_dir.run_jj(["git", "remote", "rename", "foo", "bar"]);
1004 // Divergence from Git: we read the remote from the global
1005 // configuration and write it back out. Git will use the global
1006 // configuration for commands like `git remote -v`, `git fetch`,
1007 // and `git push`, but `git remote rename`, `git remote remove`,
1008 // `git remote set-url`, etc., will ignore it.
1009 //
1010 // This behavior applies to `jj git remote remove` and
1011 // `jj git remote set-url` as well. It would be hard to change due
1012 // to gitoxide’s model, but hopefully it’s relatively harmless.
1013 insta::assert_snapshot!(output, @"");
1014 insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
1015 [core]
1016 bare = true
1017 logallrefupdates = false
1018 repositoryformatversion = 0
1019 [remote "bar"]
1020 url = htps://example.com/repo/foo
1021 fetch = +refs/heads/*:refs/remotes/bar/*
1022 "#);
1023 // This has the unfortunate consequence that the original remote
1024 // still exists after renaming.
1025 let output = work_dir.run_jj(["git", "remote", "list"]);
1026 insta::assert_snapshot!(output, @"
1027 bar htps://example.com/repo/foo
1028 foo htps://example.com/repo/foo
1029 [EOF]
1030 ");
1031
1032 let output = work_dir.run_jj([
1033 "git",
1034 "remote",
1035 "add",
1036 "origin",
1037 "http://example.com/repo/origin/1",
1038 ]);
1039 insta::assert_snapshot!(output, @"");
1040
1041 let output = work_dir.run_jj([
1042 "git",
1043 "remote",
1044 "set-url",
1045 "origin",
1046 "https://example.com/repo/origin/2",
1047 ]);
1048 insta::assert_snapshot!(output, @"");
1049
1050 let output = work_dir.run_jj(["git", "remote", "list"]);
1051 insta::assert_snapshot!(output, @"
1052 bar htps://example.com/repo/foo
1053 foo htps://example.com/repo/foo
1054 origin https://example.com/repo/origin/2
1055 [EOF]
1056 ");
1057 insta::assert_snapshot!(read_git_config(work_dir.root()), @r#"
1058 [core]
1059 bare = true
1060 logallrefupdates = false
1061 repositoryformatversion = 0
1062 [remote "bar"]
1063 url = htps://example.com/repo/foo
1064 fetch = +refs/heads/*:refs/remotes/bar/*
1065 [remote "origin"]
1066 url = https://example.com/repo/origin/2
1067 fetch = +refs/heads/*:refs/remotes/origin/*
1068 "#);
1069 }
1070
1071 #[test]
1072 fn test_git_remote_name_validation() {
1073 let test_env = TestEnvironment::default();
1074 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1075 let work_dir = test_env.work_dir("repo");
1076
1077 // Invalid remote name is rejected (detailed validation tested in jj-lib)
1078 let output = work_dir.run_jj([
1079 "git",
1080 "remote",
1081 "add",
1082 "my remote",
1083 "http://example.com/repo",
1084 ]);
1085 insta::assert_snapshot!(output, @r#"
1086 ------- stderr -------
1087 Error: Invalid Git remote name
1088 Caused by:
1089 1: remote names must be valid within refspecs for fetching: "my remote"
1090 2: Reference name contains invalid byte: " "
1091 [EOF]
1092 [exit status: 1]
1093 "#);
1094 }