1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
|
// Copyright 2024 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 std::path::PathBuf;
use indoc::formatdoc;
use test_case::test_case;
use testutils::git;
use crate::common::to_toml_value;
use crate::common::CommandOutput;
use crate::common::TestEnvironment;
use crate::common::TestWorkDir;
fn init_git_repo(git_repo_path: &Path, bare: bool) -> gix::Repository {
let git_repo = if bare {
git::init_bare(git_repo_path)
} else {
git::init(git_repo_path)
};
let git::CommitResult { commit_id, .. } = git::add_commit(
&git_repo,
"refs/heads/my-bookmark",
"some-file",
b"some content",
"My commit message",
&[],
);
git::set_head_to_id(&git_repo, commit_id);
git_repo
}
#[must_use]
fn get_bookmark_output(work_dir: &TestWorkDir) -> CommandOutput {
work_dir.run_jj(["bookmark", "list", "--all-remotes"])
}
#[must_use]
fn get_log_output(work_dir: &TestWorkDir) -> CommandOutput {
let template = r#"
separate(" ",
commit_id.short(),
bookmarks,
if(git_head, "git_head()"),
description,
)"#;
work_dir.run_jj(["log", "-T", template, "-r=all()"])
}
fn read_git_target(work_dir: &TestWorkDir) -> String {
String::from_utf8(work_dir.read_file(".jj/repo/store/git_target").into()).unwrap()
}
#[test]
fn test_git_init_internal() {
let test_env = TestEnvironment::default();
let output = test_env.run_jj_in(".", ["git", "init", "repo"]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Initialized repo in "repo"
[EOF]
"#);
let work_dir = test_env.work_dir("repo");
let jj_path = work_dir.root().join(".jj");
let repo_path = jj_path.join("repo");
let store_path = repo_path.join("store");
assert!(work_dir.root().is_dir());
assert!(jj_path.is_dir());
assert!(jj_path.join("working_copy").is_dir());
assert!(repo_path.is_dir());
assert!(store_path.is_dir());
assert!(store_path.join("git").is_dir());
assert_eq!(read_git_target(&work_dir), "git");
}
#[test]
fn test_git_init_internal_ignore_working_copy() {
let test_env = TestEnvironment::default();
let work_dir = test_env.work_dir("").create_dir("repo");
work_dir.write_file("file1", "");
let output = work_dir.run_jj(["git", "init", "--ignore-working-copy"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Error: --ignore-working-copy is not respected
[EOF]
[exit status: 2]
");
}
#[test]
fn test_git_init_internal_at_operation() {
let test_env = TestEnvironment::default();
let work_dir = test_env.work_dir("").create_dir("repo");
let output = work_dir.run_jj(["git", "init", "--at-op=@-"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Error: --at-op is not respected
[EOF]
[exit status: 2]
");
}
#[test_case(false; "full")]
#[test_case(true; "bare")]
fn test_git_init_external(bare: bool) {
let test_env = TestEnvironment::default();
let git_repo_path = test_env.env_root().join("git-repo");
init_git_repo(&git_repo_path, bare);
let output = test_env.run_jj_in(
".",
[
"git",
"init",
"repo",
"--git-repo",
git_repo_path.to_str().unwrap(),
],
);
insta::allow_duplicates! {
insta::assert_snapshot!(output, @r#"
------- stderr -------
Done importing changes from the underlying Git repo.
Working copy (@) now at: sqpuoqvx ed6b5138 (empty) (no description set)
Parent commit (@-) : nntyzxmz e80a42cc my-bookmark | My commit message
Added 1 files, modified 0 files, removed 0 files
Initialized repo in "repo"
[EOF]
"#);
}
let work_dir = test_env.work_dir("repo");
let jj_path = work_dir.root().join(".jj");
let repo_path = jj_path.join("repo");
let store_path = repo_path.join("store");
assert!(work_dir.root().is_dir());
assert!(jj_path.is_dir());
assert!(jj_path.join("working_copy").is_dir());
assert!(repo_path.is_dir());
assert!(store_path.is_dir());
let unix_git_target_file_contents = read_git_target(&work_dir).replace('\\', "/");
if bare {
assert!(unix_git_target_file_contents.ends_with("/git-repo"));
} else {
assert!(unix_git_target_file_contents.ends_with("/git-repo/.git"));
}
// Check that the Git repo's HEAD got checked out
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output(&work_dir), @r"
@ ed6b513890ae
○ e80a42cccd06 my-bookmark git_head() My commit message
◆ 000000000000
[EOF]
");
}
}
#[test_case(false; "full")]
#[test_case(true; "bare")]
fn test_git_init_external_import_trunk(bare: bool) {
let test_env = TestEnvironment::default();
let git_repo_path = test_env.env_root().join("git-repo");
let git_repo = init_git_repo(&git_repo_path, bare);
// Add remote bookmark "trunk" for remote "origin", and set it as "origin/HEAD"
let oid = git_repo
.find_reference("refs/heads/my-bookmark")
.unwrap()
.id();
git_repo
.reference(
"refs/remotes/origin/trunk",
oid.detach(),
gix::refs::transaction::PreviousValue::MustNotExist,
"create remote ref",
)
.unwrap();
git::set_symbolic_reference(
&git_repo,
"refs/remotes/origin/HEAD",
"refs/remotes/origin/trunk",
);
let output = test_env.run_jj_in(
".",
[
"git",
"init",
"repo",
"--git-repo",
git_repo_path.to_str().unwrap(),
],
);
insta::allow_duplicates! {
insta::assert_snapshot!(output, @r#"
------- stderr -------
Done importing changes from the underlying Git repo.
Setting the revset alias `trunk()` to `trunk@origin`
Working copy (@) now at: sqpuoqvx ed6b5138 (empty) (no description set)
Parent commit (@-) : nntyzxmz e80a42cc my-bookmark trunk@origin | My commit message
Added 1 files, modified 0 files, removed 0 files
Initialized repo in "repo"
[EOF]
"#);
}
// "trunk()" alias should be set to remote "origin"'s default bookmark "trunk"
let work_dir = test_env.work_dir("repo");
let output = work_dir.run_jj(["config", "list", "--repo", "revset-aliases.\"trunk()\""]);
insta::allow_duplicates! {
insta::assert_snapshot!(output, @r#"
revset-aliases."trunk()" = "trunk@origin"
[EOF]
"#);
}
}
#[test]
fn test_git_init_external_ignore_working_copy() {
let test_env = TestEnvironment::default();
let git_repo_path = test_env.env_root().join("git-repo");
init_git_repo(&git_repo_path, false);
let work_dir = test_env.work_dir("").create_dir("repo");
work_dir.write_file("file1", "");
// No snapshot should be taken
let output = work_dir.run_jj([
"git",
"init",
"--ignore-working-copy",
"--git-repo",
git_repo_path.to_str().unwrap(),
]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Error: --ignore-working-copy is not respected
[EOF]
[exit status: 2]
");
}
#[test]
fn test_git_init_external_at_operation() {
let test_env = TestEnvironment::default();
let git_repo_path = test_env.env_root().join("git-repo");
init_git_repo(&git_repo_path, false);
let work_dir = test_env.work_dir("").create_dir("repo");
let output = work_dir.run_jj([
"git",
"init",
"--at-op=@-",
"--git-repo",
git_repo_path.to_str().unwrap(),
]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Error: --at-op is not respected
[EOF]
[exit status: 2]
");
}
#[test]
fn test_git_init_external_non_existent_directory() {
let test_env = TestEnvironment::default();
let output = test_env.run_jj_in(".", ["git", "init", "repo", "--git-repo", "non-existent"]);
insta::assert_snapshot!(output.strip_stderr_last_line(), @r"
------- stderr -------
Error: Failed to access the repository
Caused by:
1: Cannot access $TEST_ENV/non-existent
[EOF]
[exit status: 1]
");
}
#[test]
fn test_git_init_external_non_existent_git_directory() {
let test_env = TestEnvironment::default();
let work_dir = test_env.work_dir("repo");
let output = test_env.run_jj_in(".", ["git", "init", "repo", "--git-repo", "repo"]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Error: Failed to access the repository
Caused by:
1: Failed to open git repository
2: "$TEST_ENV/repo" does not appear to be a git repository
3: Missing HEAD at '.git/HEAD'
[EOF]
[exit status: 1]
"#);
let jj_path = work_dir.root().join(".jj");
assert!(!jj_path.exists());
}
#[test]
fn test_git_init_colocated_via_git_repo_path() {
let test_env = TestEnvironment::default();
let work_dir = test_env.work_dir("repo");
init_git_repo(work_dir.root(), false);
let output = work_dir.run_jj(["git", "init", "--git-repo", "."]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Done importing changes from the underlying Git repo.
Initialized repo in "."
[EOF]
"#);
let jj_path = work_dir.root().join(".jj");
let repo_path = jj_path.join("repo");
let store_path = repo_path.join("store");
assert!(work_dir.root().is_dir());
assert!(jj_path.is_dir());
assert!(jj_path.join("working_copy").is_dir());
assert!(repo_path.is_dir());
assert!(store_path.is_dir());
assert!(read_git_target(&work_dir)
.replace('\\', "/")
.ends_with("../../../.git"));
// Check that the Git repo's HEAD got checked out
insta::assert_snapshot!(get_log_output(&work_dir), @r"
@ f3fe58bc88cc
○ e80a42cccd06 my-bookmark git_head() My commit message
◆ 000000000000
[EOF]
");
// Check that the Git repo's HEAD moves
work_dir.run_jj(["new"]).success();
insta::assert_snapshot!(get_log_output(&work_dir), @r"
@ 0c77f9e21b55
○ f3fe58bc88cc git_head()
○ e80a42cccd06 my-bookmark My commit message
◆ 000000000000
[EOF]
");
}
#[test]
fn test_git_init_colocated_via_git_repo_path_gitlink() {
let test_env = TestEnvironment::default();
// <jj_work_dir>/.git -> <git_repo_path>
let git_repo_path = test_env.env_root().join("git-repo");
let git_repo = init_git_repo(&git_repo_path, false);
let jj_work_dir = test_env.work_dir("").create_dir("repo");
git::create_gitlink(jj_work_dir.root(), git_repo.path());
assert!(jj_work_dir.root().join(".git").is_file());
let output = jj_work_dir.run_jj(["git", "init", "--git-repo", "."]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Done importing changes from the underlying Git repo.
Initialized repo in "."
[EOF]
"#);
insta::assert_snapshot!(read_git_target(&jj_work_dir), @"../../../.git");
// Check that the Git repo's HEAD got checked out
insta::assert_snapshot!(get_log_output(&jj_work_dir), @r"
@ f3fe58bc88cc
○ e80a42cccd06 my-bookmark git_head() My commit message
◆ 000000000000
[EOF]
");
// Check that the Git repo's HEAD moves
jj_work_dir.run_jj(["new"]).success();
insta::assert_snapshot!(get_log_output(&jj_work_dir), @r"
@ 0c77f9e21b55
○ f3fe58bc88cc git_head()
○ e80a42cccd06 my-bookmark My commit message
◆ 000000000000
[EOF]
");
}
#[cfg(unix)]
#[test]
fn test_git_init_colocated_via_git_repo_path_symlink_directory() {
let test_env = TestEnvironment::default();
// <jj_work_dir>/.git -> <git_repo_path>
let git_repo_path = test_env.env_root().join("git-repo");
init_git_repo(&git_repo_path, false);
let jj_work_dir = test_env.work_dir("").create_dir("repo");
std::os::unix::fs::symlink(git_repo_path.join(".git"), jj_work_dir.root().join(".git"))
.unwrap();
let output = jj_work_dir.run_jj(["git", "init", "--git-repo", "."]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Done importing changes from the underlying Git repo.
Initialized repo in "."
[EOF]
"#);
insta::assert_snapshot!(read_git_target(&jj_work_dir), @"../../../.git");
// Check that the Git repo's HEAD got checked out
insta::assert_snapshot!(get_log_output(&jj_work_dir), @r"
@ f3fe58bc88cc
○ e80a42cccd06 my-bookmark git_head() My commit message
◆ 000000000000
[EOF]
");
// Check that the Git repo's HEAD moves
jj_work_dir.run_jj(["new"]).success();
insta::assert_snapshot!(get_log_output(&jj_work_dir), @r"
@ 0c77f9e21b55
○ f3fe58bc88cc git_head()
○ e80a42cccd06 my-bookmark My commit message
◆ 000000000000
[EOF]
");
}
#[cfg(unix)]
#[test]
fn test_git_init_colocated_via_git_repo_path_symlink_directory_without_bare_config() {
let test_env = TestEnvironment::default();
// <jj_work_dir>/.git -> <git_repo_path>
let git_repo_path = test_env.env_root().join("git-repo.git");
let jj_work_dir = test_env.work_dir("repo");
// Set up git repo without core.bare set (as the "repo" tool would do.)
// The core.bare config is deduced from the directory name.
let git_repo = init_git_repo(jj_work_dir.root(), false);
git::remove_config_value(git_repo, "config", "bare");
std::fs::rename(jj_work_dir.root().join(".git"), &git_repo_path).unwrap();
std::os::unix::fs::symlink(&git_repo_path, jj_work_dir.root().join(".git")).unwrap();
let output = jj_work_dir.run_jj(["git", "init", "--git-repo", "."]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Done importing changes from the underlying Git repo.
Initialized repo in "."
[EOF]
"#);
insta::assert_snapshot!(read_git_target(&jj_work_dir), @"../../../.git");
// Check that the Git repo's HEAD got checked out
insta::assert_snapshot!(get_log_output(&jj_work_dir), @r"
@ f3fe58bc88cc
○ e80a42cccd06 my-bookmark git_head() My commit message
◆ 000000000000
[EOF]
");
// Check that the Git repo's HEAD moves
jj_work_dir.run_jj(["new"]).success();
insta::assert_snapshot!(get_log_output(&jj_work_dir), @r"
@ 0c77f9e21b55
○ f3fe58bc88cc git_head()
○ e80a42cccd06 my-bookmark My commit message
◆ 000000000000
[EOF]
");
}
#[cfg(unix)]
#[test]
fn test_git_init_colocated_via_git_repo_path_symlink_gitlink() {
let test_env = TestEnvironment::default();
// <jj_work_dir>/.git -> <git_workdir_path>/.git -> <git_repo_path>
let git_repo_path = test_env.env_root().join("git-repo");
let git_workdir_path = test_env.env_root().join("git-workdir");
let git_repo = init_git_repo(&git_repo_path, false);
std::fs::create_dir(&git_workdir_path).unwrap();
git::create_gitlink(&git_workdir_path, git_repo.path());
assert!(git_workdir_path.join(".git").is_file());
let jj_work_dir = test_env.work_dir("").create_dir("repo");
std::os::unix::fs::symlink(
git_workdir_path.join(".git"),
jj_work_dir.root().join(".git"),
)
.unwrap();
let output = jj_work_dir.run_jj(["git", "init", "--git-repo", "."]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Done importing changes from the underlying Git repo.
Initialized repo in "."
[EOF]
"#);
insta::assert_snapshot!(read_git_target(&jj_work_dir), @"../../../.git");
// Check that the Git repo's HEAD got checked out
insta::assert_snapshot!(get_log_output(&jj_work_dir), @r"
@ f3fe58bc88cc
○ e80a42cccd06 my-bookmark git_head() My commit message
◆ 000000000000
[EOF]
");
// Check that the Git repo's HEAD moves
jj_work_dir.run_jj(["new"]).success();
insta::assert_snapshot!(get_log_output(&jj_work_dir), @r"
@ 0c77f9e21b55
○ f3fe58bc88cc git_head()
○ e80a42cccd06 my-bookmark My commit message
◆ 000000000000
[EOF]
");
}
#[test]
fn test_git_init_colocated_via_git_repo_path_imported_refs() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
// Set up remote refs
test_env.run_jj_in(".", ["git", "init", "remote"]).success();
let remote_dir = test_env.work_dir("remote");
remote_dir
.run_jj(["bookmark", "create", "-r@", "local-remote", "remote-only"])
.success();
remote_dir.run_jj(["new"]).success();
remote_dir.run_jj(["git", "export"]).success();
let remote_git_path = remote_dir
.root()
.join(PathBuf::from_iter([".jj", "repo", "store", "git"]));
let set_up_local_repo = |local_path: &Path| {
let git_repo = git::clone(local_path, remote_git_path.to_str().unwrap(), None);
let git_ref = git_repo
.find_reference("refs/remotes/origin/local-remote")
.unwrap();
git_repo
.reference(
"refs/heads/local-remote",
git_ref.target().id().to_owned(),
gix::refs::transaction::PreviousValue::MustNotExist,
"move local-remote bookmark",
)
.unwrap();
};
// With git.auto-local-bookmark = true
let local_dir = test_env.work_dir("local1");
set_up_local_repo(local_dir.root());
let output = local_dir.run_jj(["git", "init", "--git-repo=."]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Done importing changes from the underlying Git repo.
Initialized repo in "."
[EOF]
"#);
insta::assert_snapshot!(get_bookmark_output(&local_dir), @r"
local-remote: qpvuntsm e8849ae1 (empty) (no description set)
@git: qpvuntsm e8849ae1 (empty) (no description set)
@origin: qpvuntsm e8849ae1 (empty) (no description set)
remote-only: qpvuntsm e8849ae1 (empty) (no description set)
@git: qpvuntsm e8849ae1 (empty) (no description set)
@origin: qpvuntsm e8849ae1 (empty) (no description set)
[EOF]
");
// With git.auto-local-bookmark = false
test_env.add_config("git.auto-local-bookmark = false");
let local_dir = test_env.work_dir("local2");
set_up_local_repo(local_dir.root());
let output = local_dir.run_jj(["git", "init", "--git-repo=."]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Done importing changes from the underlying Git repo.
Hint: The following remote bookmarks aren't associated with the existing local bookmarks:
local-remote@origin
Hint: Run the following command to keep local bookmarks updated on future pulls:
jj bookmark track local-remote@origin
Initialized repo in "."
[EOF]
"#);
insta::assert_snapshot!(get_bookmark_output(&local_dir), @r"
local-remote: qpvuntsm e8849ae1 (empty) (no description set)
@git: qpvuntsm e8849ae1 (empty) (no description set)
local-remote@origin: qpvuntsm e8849ae1 (empty) (no description set)
remote-only@origin: qpvuntsm e8849ae1 (empty) (no description set)
[EOF]
");
}
#[test]
fn test_git_init_colocated_dirty_working_copy() {
let test_env = TestEnvironment::default();
let work_dir = test_env.work_dir("repo");
let git_repo = init_git_repo(work_dir.root(), false);
let mut index_manager = git::IndexManager::new(&git_repo);
index_manager.add_file("new-staged-file", b"new content");
index_manager.add_file("some-file", b"new content");
index_manager.sync_index();
work_dir.write_file("unstaged-file", "new content");
insta::assert_debug_snapshot!(git::status(&git_repo), @r#"
[
GitStatus {
path: "new-staged-file",
status: Index(
Addition,
),
},
GitStatus {
path: "some-file",
status: Index(
Modification,
),
},
GitStatus {
path: "unstaged-file",
status: Worktree(
Added,
),
},
]
"#);
let output = work_dir.run_jj(["git", "init", "--git-repo", "."]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Done importing changes from the underlying Git repo.
Initialized repo in "."
[EOF]
"#);
// Working-copy changes should have been snapshotted.
let output = work_dir.run_jj(["log", "-s", "--ignore-working-copy"]);
insta::assert_snapshot!(output, @r"
@ sqpuoqvx test.user@example.com 2001-02-03 08:05:07 6efc2a53
│ (no description set)
│ C {some-file => new-staged-file}
│ M some-file
│ C {some-file => unstaged-file}
○ nntyzxmz someone@example.org 1970-01-01 11:00:00 my-bookmark git_head() e80a42cc
│ My commit message
│ A some-file
◆ zzzzzzzz root() 00000000
[EOF]
");
// Git index should be consistent with the working copy parent. With the
// current implementation, the index is unchanged. Since jj created new
// working copy commit, it's also okay to update the index reflecting the
// working copy commit or the working copy parent.
insta::assert_debug_snapshot!(git::status(&git_repo), @r#"
[
GitStatus {
path: ".jj/.gitignore",
status: Worktree(
Ignored,
),
},
GitStatus {
path: ".jj/repo",
status: Worktree(
Ignored,
),
},
GitStatus {
path: ".jj/working_copy",
status: Worktree(
Ignored,
),
},
GitStatus {
path: "new-staged-file",
status: Index(
Addition,
),
},
GitStatus {
path: "some-file",
status: Index(
Modification,
),
},
GitStatus {
path: "unstaged-file",
status: Worktree(
IntentToAdd,
),
},
]
"#);
}
#[test]
fn test_git_init_colocated_ignore_working_copy() {
let test_env = TestEnvironment::default();
let work_dir = test_env.work_dir("repo");
init_git_repo(work_dir.root(), false);
work_dir.write_file("file1", "");
let output = work_dir.run_jj(["git", "init", "--ignore-working-copy", "--colocate"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Error: --ignore-working-copy is not respected
[EOF]
[exit status: 2]
");
}
#[test]
fn test_git_init_colocated_at_operation() {
let test_env = TestEnvironment::default();
let work_dir = test_env.work_dir("repo");
init_git_repo(work_dir.root(), false);
let output = work_dir.run_jj(["git", "init", "--at-op=@-", "--colocate"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Error: --at-op is not respected
[EOF]
[exit status: 2]
");
}
#[test]
fn test_git_init_external_but_git_dir_exists() {
let test_env = TestEnvironment::default();
let git_repo_path = test_env.env_root().join("git-repo");
let work_dir = test_env.work_dir("repo");
git::init(&git_repo_path);
init_git_repo(work_dir.root(), false);
let output = work_dir.run_jj(["git", "init", "--git-repo", git_repo_path.to_str().unwrap()]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Initialized repo in "."
[EOF]
"#);
// The local ".git" repository is unrelated, so no commits should be imported
insta::assert_snapshot!(get_log_output(&work_dir), @r"
@ e8849ae12c70
◆ 000000000000
[EOF]
");
// Check that Git HEAD is not set because this isn't a colocated repo
work_dir.run_jj(["new"]).success();
insta::assert_snapshot!(get_log_output(&work_dir), @r"
@ 1c1c95df80e5
○ e8849ae12c70
◆ 000000000000
[EOF]
");
}
#[test]
fn test_git_init_colocated_via_flag_git_dir_exists() {
let test_env = TestEnvironment::default();
let work_dir = test_env.work_dir("repo");
init_git_repo(work_dir.root(), false);
let output = test_env.run_jj_in(".", ["git", "init", "--colocate", "repo"]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Done importing changes from the underlying Git repo.
Initialized repo in "repo"
[EOF]
"#);
// Check that the Git repo's HEAD got checked out
insta::assert_snapshot!(get_log_output(&work_dir), @r"
@ f3fe58bc88cc
○ e80a42cccd06 my-bookmark git_head() My commit message
◆ 000000000000
[EOF]
");
// Check that the Git repo's HEAD moves
work_dir.run_jj(["new"]).success();
insta::assert_snapshot!(get_log_output(&work_dir), @r"
@ 0c77f9e21b55
○ f3fe58bc88cc git_head()
○ e80a42cccd06 my-bookmark My commit message
◆ 000000000000
[EOF]
");
}
#[test]
fn test_git_init_colocated_via_flag_git_dir_not_exists() {
let test_env = TestEnvironment::default();
let work_dir = test_env.work_dir("repo");
let output = test_env.run_jj_in(".", ["git", "init", "--colocate", "repo"]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Initialized repo in "repo"
[EOF]
"#);
// No HEAD ref is available yet
insta::assert_snapshot!(get_log_output(&work_dir), @r"
@ e8849ae12c70
◆ 000000000000
[EOF]
");
// Create the default bookmark (create both in case we change the default)
work_dir
.run_jj(["bookmark", "create", "-r@", "main", "master"])
.success();
// If .git/HEAD pointed to the default bookmark, new working-copy commit would
// be created on top.
insta::assert_snapshot!(get_log_output(&work_dir), @r"
@ e8849ae12c70 main master
◆ 000000000000
[EOF]
");
}
#[test]
fn test_git_init_conditional_config() {
let test_env = TestEnvironment::default();
let old_workspace_dir = test_env.work_dir("old");
let new_workspace_dir = test_env.work_dir("new");
let run_jj = |work_dir: &TestWorkDir, args: &[&str]| {
work_dir.run_jj_with(|cmd| {
cmd.args(args)
.env_remove("JJ_EMAIL")
.env_remove("JJ_OP_HOSTNAME")
.env_remove("JJ_OP_USERNAME")
})
};
let log_template = r#"separate(' ', author.email(), description.first_line()) ++ "\n""#;
let op_log_template = r#"separate(' ', user, description.first_line()) ++ "\n""#;
// Override user.email and operation.username conditionally
test_env.add_config(formatdoc! {"
user.email = 'base@example.org'
operation.hostname = 'base'
operation.username = 'base'
[[--scope]]
--when.repositories = [{new_workspace_root}]
user.email = 'new-repo@example.org'
operation.username = 'new-repo'
",
new_workspace_root = to_toml_value(new_workspace_dir.root().to_str().unwrap()),
});
// Override operation.hostname by repo config, which should be loaded into
// the command settings, but shouldn't be copied to the new repo.
run_jj(&test_env.work_dir(""), &["git", "init", "old"]).success();
run_jj(
&old_workspace_dir,
&["config", "set", "--repo", "operation.hostname", "old-repo"],
)
.success();
run_jj(&old_workspace_dir, &["new"]).success();
let output = run_jj(&old_workspace_dir, &["op", "log", "-T", op_log_template]);
insta::assert_snapshot!(output, @r"
@ base@old-repo new empty commit
○ base@base add workspace 'default'
○ @
[EOF]
");
// Create new repo at the old workspace directory.
let output = run_jj(&old_workspace_dir, &["git", "init", "../new"]);
insta::assert_snapshot!(output.normalize_backslash(), @r#"
------- stderr -------
Initialized repo in "../new"
[EOF]
"#);
run_jj(&new_workspace_dir, &["new"]).success();
let output = run_jj(&new_workspace_dir, &["log", "-T", log_template]);
insta::assert_snapshot!(output, @r"
@ new-repo@example.org
○ new-repo@example.org
◆
[EOF]
");
let output = run_jj(&new_workspace_dir, &["op", "log", "-T", op_log_template]);
insta::assert_snapshot!(output, @r"
@ new-repo@base new empty commit
○ new-repo@base add workspace 'default'
○ @
[EOF]
");
}
#[test]
fn test_git_init_bad_wc_path() {
let test_env = TestEnvironment::default();
std::fs::write(test_env.env_root().join("existing-file"), b"").unwrap();
let output = test_env.run_jj_in(".", ["git", "init", "existing-file"]);
insta::assert_snapshot!(output.strip_stderr_last_line(), @r"
------- stderr -------
Error: Failed to create workspace
[EOF]
[exit status: 1]
");
}
|