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 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
|
// Copyright 2023 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use testutils::git;
use crate::common::create_commit;
use crate::common::CommandOutput;
use crate::common::TestEnvironment;
use crate::common::TestWorkDir;
fn add_commit_to_branch(git_repo: &gix::Repository, branch: &str) -> gix::ObjectId {
git::add_commit(
git_repo,
&format!("refs/heads/{branch}"),
branch, // filename
branch.as_bytes(), // content
"message",
&[],
)
.commit_id
}
/// Creates a remote Git repo containing a bookmark with the same name
fn init_git_remote(test_env: &TestEnvironment, remote: &str) -> gix::Repository {
let git_repo_path = test_env.env_root().join(remote);
let git_repo = git::init(git_repo_path);
add_commit_to_branch(&git_repo, remote);
git_repo
}
/// Add a remote containing a bookmark with the same name
fn add_git_remote(
test_env: &TestEnvironment,
work_dir: &TestWorkDir,
remote: &str,
) -> gix::Repository {
let repo = init_git_remote(test_env, remote);
work_dir
.run_jj(["git", "remote", "add", remote, &format!("../{remote}")])
.success();
repo
}
#[must_use]
fn get_bookmark_output(work_dir: &TestWorkDir) -> CommandOutput {
// --quiet to suppress deleted bookmarks hint
work_dir.run_jj(["bookmark", "list", "--all-remotes", "--quiet"])
}
#[must_use]
fn get_log_output(work_dir: &TestWorkDir) -> CommandOutput {
let template =
r#"commit_id.short() ++ " \"" ++ description.first_line() ++ "\" " ++ bookmarks"#;
work_dir.run_jj(["log", "-T", template, "-r", "all()"])
}
fn clone_git_remote_into(
test_env: &TestEnvironment,
upstream: &str,
fork: &str,
) -> gix::Repository {
let upstream_path = test_env.env_root().join(upstream);
let fork_path = test_env.env_root().join(fork);
let fork_repo = git::clone(&fork_path, upstream_path.to_str().unwrap(), Some(upstream));
// create local branch mirroring the upstream
let upstream_head = fork_repo
.find_reference(&format!("refs/remotes/{upstream}/{upstream}"))
.unwrap()
.peel_to_id_in_place()
.unwrap()
.detach();
fork_repo
.reference(
format!("refs/heads/{upstream}"),
upstream_head,
gix::refs::transaction::PreviousValue::MustNotExist,
"create tracking head",
)
.unwrap();
fork_repo
}
#[test]
fn test_git_fetch_with_default_config() {
let test_env = TestEnvironment::default();
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "origin");
work_dir.run_jj(["git", "fetch"]).success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
origin@origin: qmyrypzk ab8b299e message
[EOF]
");
}
#[test]
fn test_git_fetch_default_remote() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "origin");
work_dir.run_jj(["git", "fetch"]).success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
origin: qmyrypzk ab8b299e message
@origin: qmyrypzk ab8b299e message
[EOF]
");
}
#[test]
fn test_git_fetch_single_remote() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "rem1");
let output = work_dir.run_jj(["git", "fetch"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Hint: Fetching from the only existing remote: rem1
bookmark: rem1@rem1 [new] tracked
[EOF]
");
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
rem1: ppspxspk 4acd0343 message
@rem1: ppspxspk 4acd0343 message
[EOF]
");
}
#[test]
fn test_git_fetch_single_remote_all_remotes_flag() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "rem1");
work_dir.run_jj(["git", "fetch", "--all-remotes"]).success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
rem1: ppspxspk 4acd0343 message
@rem1: ppspxspk 4acd0343 message
[EOF]
");
}
#[test]
fn test_git_fetch_single_remote_from_arg() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "rem1");
work_dir
.run_jj(["git", "fetch", "--remote", "rem1"])
.success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
rem1: ppspxspk 4acd0343 message
@rem1: ppspxspk 4acd0343 message
[EOF]
");
}
#[test]
fn test_git_fetch_single_remote_from_config() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "rem1");
test_env.add_config(r#"git.fetch = "rem1""#);
work_dir.run_jj(["git", "fetch"]).success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
rem1: ppspxspk 4acd0343 message
@rem1: ppspxspk 4acd0343 message
[EOF]
");
}
#[test]
fn test_git_fetch_multiple_remotes() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "rem1");
add_git_remote(&test_env, &work_dir, "rem2");
work_dir
.run_jj(["git", "fetch", "--remote", "rem1", "--remote", "rem2"])
.success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
rem1: ppspxspk 4acd0343 message
@rem1: ppspxspk 4acd0343 message
rem2: pzqqpnpo 44c57802 message
@rem2: pzqqpnpo 44c57802 message
[EOF]
");
}
#[test]
fn test_git_fetch_with_glob() {
let test_env = TestEnvironment::default();
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "rem1");
add_git_remote(&test_env, &work_dir, "rem2");
let output = work_dir.run_jj(["git", "fetch", "--remote", "glob:*"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: rem1@rem1 [new] untracked
bookmark: rem2@rem2 [new] untracked
[EOF]
");
}
#[test]
fn test_git_fetch_with_glob_and_exact_match() {
let test_env = TestEnvironment::default();
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "rem1");
add_git_remote(&test_env, &work_dir, "rem2");
add_git_remote(&test_env, &work_dir, "upstream1");
add_git_remote(&test_env, &work_dir, "upstream2");
add_git_remote(&test_env, &work_dir, "origin");
let output = work_dir.run_jj(["git", "fetch", "--remote=glob:rem*", "--remote=origin"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: origin@origin [new] untracked
bookmark: rem1@rem1 [new] untracked
bookmark: rem2@rem2 [new] untracked
[EOF]
");
}
#[test]
fn test_git_fetch_with_glob_from_config() {
let test_env = TestEnvironment::default();
test_env.add_config(r#"git.fetch = "glob:rem*""#);
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "rem1");
add_git_remote(&test_env, &work_dir, "rem2");
add_git_remote(&test_env, &work_dir, "upstream");
let output = work_dir.run_jj(["git", "fetch"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: rem1@rem1 [new] untracked
bookmark: rem2@rem2 [new] untracked
[EOF]
");
}
#[test]
fn test_git_fetch_with_glob_with_no_matching_remotes() {
let test_env = TestEnvironment::default();
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "upstream");
let output = work_dir.run_jj(["git", "fetch", "--remote=glob:rem*"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Warning: No git remotes matching 'rem*'
Error: No git remotes to push
[EOF]
[exit status: 1]
");
// No remote should have been fetched as part of the failing transaction
insta::assert_snapshot!(get_bookmark_output(&work_dir), @"");
}
#[test]
fn test_git_fetch_all_remotes() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "rem1");
add_git_remote(&test_env, &work_dir, "rem2");
// add empty [remote "rem3"] section to .git/config, which should be ignored
work_dir
.run_jj(["git", "remote", "add", "rem3", "../unknown"])
.success();
work_dir
.run_jj(["git", "remote", "remove", "rem3"])
.success();
work_dir.run_jj(["git", "fetch", "--all-remotes"]).success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
rem1: ppspxspk 4acd0343 message
@rem1: ppspxspk 4acd0343 message
rem2: pzqqpnpo 44c57802 message
@rem2: pzqqpnpo 44c57802 message
[EOF]
");
}
#[test]
fn test_git_fetch_multiple_remotes_from_config() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "rem1");
add_git_remote(&test_env, &work_dir, "rem2");
test_env.add_config(r#"git.fetch = ["rem1", "rem2"]"#);
work_dir.run_jj(["git", "fetch"]).success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
rem1: ppspxspk 4acd0343 message
@rem1: ppspxspk 4acd0343 message
rem2: pzqqpnpo 44c57802 message
@rem2: pzqqpnpo 44c57802 message
[EOF]
");
}
#[test]
fn test_git_fetch_no_matching_remote() {
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", "fetch", "--remote", "rem1"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Warning: No git remotes matching 'rem1'
Error: No git remotes to push
[EOF]
[exit status: 1]
");
insta::assert_snapshot!(get_bookmark_output(&work_dir), @"");
}
#[test]
fn test_git_fetch_nonexistent_remote() {
let test_env = TestEnvironment::default();
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "rem1");
let output = work_dir.run_jj(["git", "fetch", "--remote", "rem1", "--remote", "rem2"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Warning: No git remotes matching 'rem2'
bookmark: rem1@rem1 [new] untracked
[EOF]
");
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
rem1@rem1: ppspxspk 4acd0343 message
[EOF]
");
}
#[test]
fn test_git_fetch_nonexistent_remote_from_config() {
let test_env = TestEnvironment::default();
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "rem1");
test_env.add_config(r#"git.fetch = ["rem1", "rem2"]"#);
let output = work_dir.run_jj(["git", "fetch"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Warning: No git remotes matching 'rem2'
bookmark: rem1@rem1 [new] untracked
[EOF]
");
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
rem1@rem1: ppspxspk 4acd0343 message
[EOF]
");
}
#[test]
fn test_git_fetch_from_remote_named_git() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
let work_dir = test_env.work_dir("repo");
init_git_remote(&test_env, "git");
git::init(work_dir.root());
git::add_remote(work_dir.root(), "git", "../git");
// Existing remote named 'git' shouldn't block the repo initialization.
work_dir.run_jj(["git", "init", "--git-repo=."]).success();
// Try fetching from the remote named 'git'.
let output = work_dir.run_jj(["git", "fetch", "--remote=git"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Error: Git remote named 'git' is reserved for local Git repository
Hint: Run `jj git remote rename` to give a different name.
[EOF]
[exit status: 1]
");
// Fetch remote refs by using the git CLI.
git::fetch(work_dir.root(), "git");
// Implicit import shouldn't fail because of the remote ref.
let output = work_dir.run_jj(["bookmark", "list", "--all-remotes"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Warning: Failed to import some Git refs:
refs/remotes/git/git
Hint: Git remote named 'git' is reserved for local Git repository.
Use `jj git remote rename` to give a different name.
[EOF]
");
// Explicit import also works. Warnings are printed twice because this is a
// colocated repo. That should be fine since "jj git import" wouldn't be
// used in colocated environment.
insta::assert_snapshot!(work_dir.run_jj(["git", "import"]), @r"
------- stderr -------
Warning: Failed to import some Git refs:
refs/remotes/git/git
Hint: Git remote named 'git' is reserved for local Git repository.
Use `jj git remote rename` to give a different name.
Warning: Failed to import some Git refs:
refs/remotes/git/git
Hint: Git remote named 'git' is reserved for local Git repository.
Use `jj git remote rename` to give a different name.
Nothing changed.
[EOF]
");
// The remote can be renamed, and the ref can be imported.
work_dir
.run_jj(["git", "remote", "rename", "git", "bar"])
.success();
let output = work_dir.run_jj(["bookmark", "list", "--all-remotes"]);
insta::assert_snapshot!(output, @r"
git: vkponlun 400c483d message
@bar: vkponlun 400c483d message
@git: vkponlun 400c483d message
[EOF]
------- stderr -------
Done importing changes from the underlying Git repo.
[EOF]
");
}
#[test]
fn test_git_fetch_from_remote_with_slashes() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
let work_dir = test_env.work_dir("repo");
init_git_remote(&test_env, "source");
git::init(work_dir.root());
git::add_remote(work_dir.root(), "slash/origin", "../source");
// Existing remote with slash shouldn't block the repo initialization.
work_dir.run_jj(["git", "init", "--git-repo=."]).success();
// Try fetching from the remote named 'git'.
let output = work_dir.run_jj(["git", "fetch", "--remote=slash/origin"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Error: Git remotes with slashes are incompatible with jj: slash/origin
Hint: Run `jj git remote rename` to give a different name.
[EOF]
[exit status: 1]
");
}
#[test]
fn test_git_fetch_prune_before_updating_tips() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
let git_repo = add_git_remote(&test_env, &work_dir, "origin");
work_dir.run_jj(["git", "fetch"]).success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
origin: qmyrypzk ab8b299e message
@origin: qmyrypzk ab8b299e message
[EOF]
");
// Remove origin bookmark in git repo and create origin/subname
let mut origin_reference = git_repo.find_reference("refs/heads/origin").unwrap();
let commit_id = origin_reference.peel_to_commit().unwrap().id().detach();
origin_reference.delete().unwrap();
git_repo
.reference(
"refs/heads/origin/subname",
commit_id,
gix::refs::transaction::PreviousValue::MustNotExist,
"create new reference",
)
.unwrap();
work_dir.run_jj(["git", "fetch"]).success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
origin/subname: qmyrypzk ab8b299e message
@origin: qmyrypzk ab8b299e message
[EOF]
");
}
#[test]
fn test_git_fetch_conflicting_bookmarks() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "rem1");
// Create a rem1 bookmark locally
work_dir.run_jj(["new", "root()"]).success();
work_dir
.run_jj(["bookmark", "create", "-r@", "rem1"])
.success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
rem1: kkmpptxz 2b17ac71 (empty) (no description set)
[EOF]
");
work_dir
.run_jj(["git", "fetch", "--remote", "rem1", "--branch", "glob:*"])
.success();
// This should result in a CONFLICTED bookmark
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
rem1 (conflicted):
+ kkmpptxz 2b17ac71 (empty) (no description set)
+ ppspxspk 4acd0343 message
@rem1 (behind by 1 commits): ppspxspk 4acd0343 message
[EOF]
");
}
#[test]
fn test_git_fetch_conflicting_bookmarks_colocated() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
let work_dir = test_env.work_dir("repo");
git::init(work_dir.root());
// create_colocated_repo_and_bookmarks_from_trunk1(&test_env, &repo_path);
work_dir
.run_jj(["git", "init", "--git-repo", "."])
.success();
add_git_remote(&test_env, &work_dir, "rem1");
insta::assert_snapshot!(get_bookmark_output(&work_dir), @"");
// Create a rem1 bookmark locally
work_dir.run_jj(["new", "root()"]).success();
work_dir
.run_jj(["bookmark", "create", "-r@", "rem1"])
.success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
rem1: zsuskuln c2934cfb (empty) (no description set)
@git: zsuskuln c2934cfb (empty) (no description set)
[EOF]
");
work_dir
.run_jj(["git", "fetch", "--remote", "rem1", "--branch", "rem1"])
.success();
// This should result in a CONFLICTED bookmark
// See https://github.com/jj-vcs/jj/pull/1146#discussion_r1112372340 for the bug this tests for.
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
rem1 (conflicted):
+ zsuskuln c2934cfb (empty) (no description set)
+ ppspxspk 4acd0343 message
@git (behind by 1 commits): zsuskuln c2934cfb (empty) (no description set)
@rem1 (behind by 1 commits): ppspxspk 4acd0343 message
[EOF]
");
}
// Helper functions to test obtaining multiple bookmarks at once and changed
// bookmarks
fn create_colocated_repo_and_bookmarks_from_trunk1(work_dir: &TestWorkDir) -> String {
// Create a colocated repo in `source` to populate it more easily
work_dir
.run_jj(["git", "init", "--git-repo", "."])
.success();
create_commit(work_dir, "trunk1", &[]);
create_commit(work_dir, "a1", &["trunk1"]);
create_commit(work_dir, "a2", &["trunk1"]);
create_commit(work_dir, "b", &["trunk1"]);
format!(
" ===== Source git repo contents =====\n{}",
get_log_output(work_dir)
)
}
fn create_trunk2_and_rebase_bookmarks(work_dir: &TestWorkDir) -> String {
create_commit(work_dir, "trunk2", &["trunk1"]);
for br in ["a1", "a2", "b"] {
work_dir
.run_jj(["rebase", "-b", br, "-d", "trunk2"])
.success();
}
format!(
" ===== Source git repo contents =====\n{}",
get_log_output(work_dir)
)
}
#[test]
fn test_git_fetch_all() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
let source_dir = test_env.work_dir("source");
git::init(source_dir.root());
// Clone an empty repo. The target repo is a normal `jj` repo, *not* colocated
let output = test_env.run_jj_in(".", ["git", "clone", "source", "target"]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Fetching into new repo in "$TEST_ENV/target"
Nothing changed.
[EOF]
"#);
let target_dir = test_env.work_dir("target");
let source_log = create_colocated_repo_and_bookmarks_from_trunk1(&source_dir);
insta::assert_snapshot!(source_log, @r#"
===== Source git repo contents =====
@ bc83465a3090 "b" b
│ ○ d4d535f1d579 "a2" a2
├─╯
│ ○ c8303692b8e2 "a1" a1
├─╯
○ 382881770501 "trunk1" trunk1
◆ 000000000000 ""
[EOF]
"#);
// Nothing in our repo before the fetch
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
◆ 000000000000 ""
[EOF]
"#);
insta::assert_snapshot!(get_bookmark_output(&target_dir), @"");
let output = target_dir.run_jj(["git", "fetch"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: a1@origin [new] tracked
bookmark: a2@origin [new] tracked
bookmark: b@origin [new] tracked
bookmark: trunk1@origin [new] tracked
[EOF]
");
insta::assert_snapshot!(get_bookmark_output(&target_dir), @r"
a1: mzvwutvl c8303692 a1
@origin: mzvwutvl c8303692 a1
a2: yqosqzyt d4d535f1 a2
@origin: yqosqzyt d4d535f1 a2
b: yostqsxw bc83465a b
@origin: yostqsxw bc83465a b
trunk1: kkmpptxz 38288177 trunk1
@origin: kkmpptxz 38288177 trunk1
[EOF]
");
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ bc83465a3090 "b" b
│ │ ○ d4d535f1d579 "a2" a2
│ ├─╯
│ │ ○ c8303692b8e2 "a1" a1
│ ├─╯
│ ○ 382881770501 "trunk1" trunk1
├─╯
◆ 000000000000 ""
[EOF]
"#);
// ==== Change both repos ====
// First, change the target repo:
let source_log = create_trunk2_and_rebase_bookmarks(&source_dir);
insta::assert_snapshot!(source_log, @r#"
===== Source git repo contents =====
○ 6fc6fe17dbee "b" b
│ ○ baad96fead6c "a2" a2
├─╯
│ ○ 798c5e2435e1 "a1" a1
├─╯
@ e80d998ab04b "trunk2" trunk2
○ 382881770501 "trunk1" trunk1
◆ 000000000000 ""
[EOF]
"#);
// Change a bookmark in the source repo as well, so that it becomes conflicted.
target_dir
.run_jj(["describe", "b", "-m=new_descr_for_b_to_create_conflict"])
.success();
// Our repo before and after fetch
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ 0fbbc495357c "new_descr_for_b_to_create_conflict" b*
│ │ ○ d4d535f1d579 "a2" a2
│ ├─╯
│ │ ○ c8303692b8e2 "a1" a1
│ ├─╯
│ ○ 382881770501 "trunk1" trunk1
├─╯
◆ 000000000000 ""
[EOF]
"#);
insta::assert_snapshot!(get_bookmark_output(&target_dir), @r"
a1: mzvwutvl c8303692 a1
@origin: mzvwutvl c8303692 a1
a2: yqosqzyt d4d535f1 a2
@origin: yqosqzyt d4d535f1 a2
b: yostqsxw 0fbbc495 new_descr_for_b_to_create_conflict
@origin (ahead by 1 commits, behind by 1 commits): yostqsxw hidden bc83465a b
trunk1: kkmpptxz 38288177 trunk1
@origin: kkmpptxz 38288177 trunk1
[EOF]
");
let output = target_dir.run_jj(["git", "fetch"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: a1@origin [updated] tracked
bookmark: a2@origin [updated] tracked
bookmark: b@origin [updated] tracked
bookmark: trunk2@origin [new] tracked
Abandoned 2 commits that are no longer reachable.
[EOF]
");
insta::assert_snapshot!(get_bookmark_output(&target_dir), @r"
a1: mzvwutvl 798c5e24 a1
@origin: mzvwutvl 798c5e24 a1
a2: yqosqzyt baad96fe a2
@origin: yqosqzyt baad96fe a2
b (conflicted):
- yostqsxw hidden bc83465a b
+ yostqsxw?? 0fbbc495 new_descr_for_b_to_create_conflict
+ yostqsxw?? 6fc6fe17 b
@origin (behind by 1 commits): yostqsxw?? 6fc6fe17 b
trunk1: kkmpptxz 38288177 trunk1
@origin: kkmpptxz 38288177 trunk1
trunk2: uyznsvlq e80d998a trunk2
@origin: uyznsvlq e80d998a trunk2
[EOF]
");
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ 6fc6fe17dbee "b" b?? b@origin
│ │ ○ baad96fead6c "a2" a2
│ ├─╯
│ │ ○ 798c5e2435e1 "a1" a1
│ ├─╯
│ ○ e80d998ab04b "trunk2" trunk2
│ │ ○ 0fbbc495357c "new_descr_for_b_to_create_conflict" b??
│ ├─╯
│ ○ 382881770501 "trunk1" trunk1
├─╯
◆ 000000000000 ""
[EOF]
"#);
}
#[test]
fn test_git_fetch_some_of_many_bookmarks() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
let source_dir = test_env.work_dir("source");
git::init(source_dir.root());
// Clone an empty repo. The target repo is a normal `jj` repo, *not* colocated
let output = test_env.run_jj_in(".", ["git", "clone", "source", "target"]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Fetching into new repo in "$TEST_ENV/target"
Nothing changed.
[EOF]
"#);
let target_dir = test_env.work_dir("target");
let source_log = create_colocated_repo_and_bookmarks_from_trunk1(&source_dir);
insta::assert_snapshot!(source_log, @r#"
===== Source git repo contents =====
@ bc83465a3090 "b" b
│ ○ d4d535f1d579 "a2" a2
├─╯
│ ○ c8303692b8e2 "a1" a1
├─╯
○ 382881770501 "trunk1" trunk1
◆ 000000000000 ""
[EOF]
"#);
// Test an error message
let output = target_dir.run_jj(["git", "fetch", "--branch", "glob:^:a*"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Error: Invalid branch pattern provided. When fetching, branch names and globs may not contain the characters `:`, `^`, `?`, `[`, `]`
[EOF]
[exit status: 1]
");
let output = target_dir.run_jj(["git", "fetch", "--branch", "a*"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Error: Branch names may not include `*`.
Hint: Prefix the pattern with `glob:` to expand `*` as a glob
[EOF]
[exit status: 1]
");
// Nothing in our repo before the fetch
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
◆ 000000000000 ""
[EOF]
"#);
// Fetch one bookmark...
let output = target_dir.run_jj(["git", "fetch", "--branch", "b"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: b@origin [new] tracked
[EOF]
");
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ bc83465a3090 "b" b
│ ○ 382881770501 "trunk1"
├─╯
◆ 000000000000 ""
[EOF]
"#);
// ...check what the intermediate state looks like...
insta::assert_snapshot!(get_bookmark_output(&target_dir), @r"
b: yostqsxw bc83465a b
@origin: yostqsxw bc83465a b
[EOF]
");
// ...then fetch two others with a glob.
let output = target_dir.run_jj(["git", "fetch", "--branch", "glob:a*"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: a1@origin [new] tracked
bookmark: a2@origin [new] tracked
[EOF]
");
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ d4d535f1d579 "a2" a2
│ │ ○ c8303692b8e2 "a1" a1
│ ├─╯
│ │ ○ bc83465a3090 "b" b
│ ├─╯
│ ○ 382881770501 "trunk1"
├─╯
◆ 000000000000 ""
[EOF]
"#);
// Fetching the same bookmark again
let output = target_dir.run_jj(["git", "fetch", "--branch", "a1"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Nothing changed.
[EOF]
");
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ d4d535f1d579 "a2" a2
│ │ ○ c8303692b8e2 "a1" a1
│ ├─╯
│ │ ○ bc83465a3090 "b" b
│ ├─╯
│ ○ 382881770501 "trunk1"
├─╯
◆ 000000000000 ""
[EOF]
"#);
// ==== Change both repos ====
// First, change the target repo:
let source_log = create_trunk2_and_rebase_bookmarks(&source_dir);
insta::assert_snapshot!(source_log, @r#"
===== Source git repo contents =====
○ 2b30dbc93959 "b" b
│ ○ 841140b152fc "a2" a2
├─╯
│ ○ bc7e74c21d43 "a1" a1
├─╯
@ 756be1d31c41 "trunk2" trunk2
○ 382881770501 "trunk1" trunk1
◆ 000000000000 ""
[EOF]
"#);
// Change a bookmark in the source repo as well, so that it becomes conflicted.
target_dir
.run_jj(["describe", "b", "-m=new_descr_for_b_to_create_conflict"])
.success();
// Our repo before and after fetch of two bookmarks
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ c62db3119722 "new_descr_for_b_to_create_conflict" b*
│ │ ○ d4d535f1d579 "a2" a2
│ ├─╯
│ │ ○ c8303692b8e2 "a1" a1
│ ├─╯
│ ○ 382881770501 "trunk1"
├─╯
◆ 000000000000 ""
[EOF]
"#);
let output = target_dir.run_jj(["git", "fetch", "--branch", "b", "--branch", "a1"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: a1@origin [updated] tracked
bookmark: b@origin [updated] tracked
Abandoned 1 commits that are no longer reachable.
[EOF]
");
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ 2b30dbc93959 "b" b?? b@origin
│ │ ○ bc7e74c21d43 "a1" a1
│ ├─╯
│ ○ 756be1d31c41 "trunk2"
│ │ ○ c62db3119722 "new_descr_for_b_to_create_conflict" b??
│ ├─╯
│ │ ○ d4d535f1d579 "a2" a2
│ ├─╯
│ ○ 382881770501 "trunk1"
├─╯
◆ 000000000000 ""
[EOF]
"#);
// We left a2 where it was before, let's see how `jj bookmark list` sees this.
insta::assert_snapshot!(get_bookmark_output(&target_dir), @r"
a1: mzvwutvl bc7e74c2 a1
@origin: mzvwutvl bc7e74c2 a1
a2: yqosqzyt d4d535f1 a2
@origin: yqosqzyt d4d535f1 a2
b (conflicted):
- yostqsxw hidden bc83465a b
+ yostqsxw?? c62db311 new_descr_for_b_to_create_conflict
+ yostqsxw?? 2b30dbc9 b
@origin (behind by 1 commits): yostqsxw?? 2b30dbc9 b
[EOF]
");
// Now, let's fetch a2 and double-check that fetching a1 and b again doesn't do
// anything.
let output = target_dir.run_jj(["git", "fetch", "--branch", "b", "--branch", "glob:a*"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: a2@origin [updated] tracked
Abandoned 1 commits that are no longer reachable.
[EOF]
");
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ 841140b152fc "a2" a2
│ │ ○ 2b30dbc93959 "b" b?? b@origin
│ ├─╯
│ │ ○ bc7e74c21d43 "a1" a1
│ ├─╯
│ ○ 756be1d31c41 "trunk2"
│ │ ○ c62db3119722 "new_descr_for_b_to_create_conflict" b??
│ ├─╯
│ ○ 382881770501 "trunk1"
├─╯
◆ 000000000000 ""
[EOF]
"#);
insta::assert_snapshot!(get_bookmark_output(&target_dir), @r"
a1: mzvwutvl bc7e74c2 a1
@origin: mzvwutvl bc7e74c2 a1
a2: yqosqzyt 841140b1 a2
@origin: yqosqzyt 841140b1 a2
b (conflicted):
- yostqsxw hidden bc83465a b
+ yostqsxw?? c62db311 new_descr_for_b_to_create_conflict
+ yostqsxw?? 2b30dbc9 b
@origin (behind by 1 commits): yostqsxw?? 2b30dbc9 b
[EOF]
");
}
#[test]
fn test_git_fetch_bookmarks_some_missing() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "origin");
add_git_remote(&test_env, &work_dir, "rem1");
add_git_remote(&test_env, &work_dir, "rem2");
add_git_remote(&test_env, &work_dir, "rem3");
// single missing bookmark, implicit remotes (@origin)
let output = work_dir.run_jj(["git", "fetch", "--branch", "noexist"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Warning: No branch matching `noexist` found on any specified/configured remote
Nothing changed.
[EOF]
");
insta::assert_snapshot!(get_bookmark_output(&work_dir), @"");
// multiple missing bookmarks, implicit remotes (@origin)
let output = work_dir.run_jj([
"git", "fetch", "--branch", "noexist1", "--branch", "noexist2",
]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Warning: No branch matching `noexist1` found on any specified/configured remote
Warning: No branch matching `noexist2` found on any specified/configured remote
Nothing changed.
[EOF]
");
insta::assert_snapshot!(get_bookmark_output(&work_dir), @"");
// single existing bookmark, implicit remotes (@origin)
let output = work_dir.run_jj(["git", "fetch", "--branch", "origin"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: origin@origin [new] tracked
[EOF]
");
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
origin: qmyrypzk ab8b299e message
@origin: qmyrypzk ab8b299e message
[EOF]
");
// multiple existing bookmark, explicit remotes, each bookmark is only in one
// remote.
let output = work_dir.run_jj([
"git", "fetch", "--branch", "rem1", "--branch", "rem2", "--branch", "rem3", "--remote",
"rem1", "--remote", "rem2", "--remote", "rem3",
]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: rem1@rem1 [new] tracked
bookmark: rem2@rem2 [new] tracked
bookmark: rem3@rem3 [new] tracked
[EOF]
");
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
origin: qmyrypzk ab8b299e message
@origin: qmyrypzk ab8b299e message
rem1: ppspxspk 4acd0343 message
@rem1: ppspxspk 4acd0343 message
rem2: pzqqpnpo 44c57802 message
@rem2: pzqqpnpo 44c57802 message
rem3: wrzwlmys 45a3faef message
@rem3: wrzwlmys 45a3faef message
[EOF]
");
// multiple bookmarks, one exists, one doesn't
let output = work_dir.run_jj([
"git", "fetch", "--branch", "rem1", "--branch", "notexist", "--remote", "rem1",
]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Warning: No branch matching `notexist` found on any specified/configured remote
Nothing changed.
[EOF]
");
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
origin: qmyrypzk ab8b299e message
@origin: qmyrypzk ab8b299e message
rem1: ppspxspk 4acd0343 message
@rem1: ppspxspk 4acd0343 message
rem2: pzqqpnpo 44c57802 message
@rem2: pzqqpnpo 44c57802 message
rem3: wrzwlmys 45a3faef message
@rem3: wrzwlmys 45a3faef message
[EOF]
");
}
#[test]
fn test_git_fetch_bookmarks_missing_with_subprocess_localized_message() {
let test_env = TestEnvironment::default();
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "origin");
// "fatal: couldn't find remote ref %s" shouldn't be localized.
let output = work_dir.run_jj_with(|cmd| {
cmd.args(["git", "fetch", "--branch=unknown"])
// Initialize locale as "en_US" which is the most common.
.env("LC_ALL", "en_US.UTF-8")
// Set some other locale variables for testing.
.env("LC_MESSAGES", "en_US.UTF-8")
.env("LANG", "en_US.UTF-8")
// GNU gettext prioritizes LANGUAGE if translation is enabled. It works
// no matter if system locale exists or not.
.env("LANGUAGE", "zh_TW")
});
insta::assert_snapshot!(output, @r"
------- stderr -------
Warning: No branch matching `unknown` found on any specified/configured remote
Nothing changed.
[EOF]
");
}
// See `test_undo_restore_commands.rs` for fetch-undo-push and fetch-undo-fetch
// of the same bookmarks for various kinds of undo.
#[test]
fn test_git_fetch_undo() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
let source_dir = test_env.work_dir("source");
git::init(source_dir.root());
// Clone an empty repo. The target repo is a normal `jj` repo, *not* colocated
let output = test_env.run_jj_in(".", ["git", "clone", "source", "target"]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Fetching into new repo in "$TEST_ENV/target"
Nothing changed.
[EOF]
"#);
let target_dir = test_env.work_dir("target");
let source_log = create_colocated_repo_and_bookmarks_from_trunk1(&source_dir);
insta::assert_snapshot!(source_log, @r#"
===== Source git repo contents =====
@ bc83465a3090 "b" b
│ ○ d4d535f1d579 "a2" a2
├─╯
│ ○ c8303692b8e2 "a1" a1
├─╯
○ 382881770501 "trunk1" trunk1
◆ 000000000000 ""
[EOF]
"#);
// Fetch 2 bookmarks
let output = target_dir.run_jj(["git", "fetch", "--branch", "b", "--branch", "a1"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: a1@origin [new] tracked
bookmark: b@origin [new] tracked
[EOF]
");
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ bc83465a3090 "b" b
│ │ ○ c8303692b8e2 "a1" a1
│ ├─╯
│ ○ 382881770501 "trunk1"
├─╯
◆ 000000000000 ""
[EOF]
"#);
let output = target_dir.run_jj(["undo"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Undid operation: 158b589e0e15 (2001-02-03 08:05:18) fetch from git remote(s) origin
[EOF]
");
// The undo works as expected
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
◆ 000000000000 ""
[EOF]
"#);
// Now try to fetch just one bookmark
let output = target_dir.run_jj(["git", "fetch", "--branch", "b"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: b@origin [new] tracked
[EOF]
");
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ bc83465a3090 "b" b
│ ○ 382881770501 "trunk1"
├─╯
◆ 000000000000 ""
[EOF]
"#);
}
// Compare to `test_git_import_undo` in test_git_import_export
// TODO: Explain why these behaviors are useful
#[test]
fn test_fetch_undo_what() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
let source_dir = test_env.work_dir("source");
git::init(source_dir.root());
// Clone an empty repo. The target repo is a normal `jj` repo, *not* colocated
let output = test_env.run_jj_in(".", ["git", "clone", "source", "target"]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Fetching into new repo in "$TEST_ENV/target"
Nothing changed.
[EOF]
"#);
let work_dir = test_env.work_dir("target");
let source_log = create_colocated_repo_and_bookmarks_from_trunk1(&source_dir);
insta::assert_snapshot!(source_log, @r#"
===== Source git repo contents =====
@ bc83465a3090 "b" b
│ ○ d4d535f1d579 "a2" a2
├─╯
│ ○ c8303692b8e2 "a1" a1
├─╯
○ 382881770501 "trunk1" trunk1
◆ 000000000000 ""
[EOF]
"#);
// Initial state we will try to return to after `op restore`. There are no
// bookmarks.
insta::assert_snapshot!(get_bookmark_output(&work_dir), @"");
let base_operation_id = work_dir.current_operation_id();
// Fetch a bookmark
let output = work_dir.run_jj(["git", "fetch", "--branch", "b"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: b@origin [new] tracked
[EOF]
");
insta::assert_snapshot!(get_log_output(&work_dir), @r#"
@ e8849ae12c70 ""
│ ○ bc83465a3090 "b" b
│ ○ 382881770501 "trunk1"
├─╯
◆ 000000000000 ""
[EOF]
"#);
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
b: yostqsxw bc83465a b
@origin: yostqsxw bc83465a b
[EOF]
");
// We can undo the change in the repo without moving the remote-tracking
// bookmark
let output = work_dir.run_jj(["op", "restore", "--what", "repo", &base_operation_id]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Restored to operation: 8f47435a3990 (2001-02-03 08:05:07) add workspace 'default'
[EOF]
");
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
b (deleted)
@origin: yostqsxw hidden bc83465a b
[EOF]
");
// Now, let's demo restoring just the remote-tracking bookmark. First, let's
// change our local repo state...
work_dir
.run_jj(["bookmark", "c", "-r@", "newbookmark"])
.success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
b (deleted)
@origin: yostqsxw hidden bc83465a b
newbookmark: qpvuntsm e8849ae1 (empty) (no description set)
[EOF]
");
// Restoring just the remote-tracking state will not affect `newbookmark`, but
// will eliminate `b@origin`.
let output = work_dir.run_jj([
"op",
"restore",
"--what",
"remote-tracking",
&base_operation_id,
]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Restored to operation: 8f47435a3990 (2001-02-03 08:05:07) add workspace 'default'
[EOF]
");
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
newbookmark: qpvuntsm e8849ae1 (empty) (no description set)
[EOF]
");
}
#[test]
fn test_git_fetch_remove_fetch() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "origin");
work_dir
.run_jj(["bookmark", "create", "-r@", "origin"])
.success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
origin: qpvuntsm e8849ae1 (empty) (no description set)
[EOF]
");
work_dir.run_jj(["git", "fetch"]).success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
origin (conflicted):
+ qpvuntsm e8849ae1 (empty) (no description set)
+ qmyrypzk ab8b299e message
@origin (behind by 1 commits): qmyrypzk ab8b299e message
[EOF]
");
work_dir
.run_jj(["git", "remote", "remove", "origin"])
.success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
origin (conflicted):
+ qpvuntsm e8849ae1 (empty) (no description set)
+ qmyrypzk ab8b299e message
[EOF]
");
work_dir
.run_jj(["git", "remote", "add", "origin", "../origin"])
.success();
// Check that origin@origin is properly recreated
let output = work_dir.run_jj(["git", "fetch"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: origin@origin [new] tracked
[EOF]
");
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
origin (conflicted):
+ qpvuntsm e8849ae1 (empty) (no description set)
+ qmyrypzk ab8b299e message
@origin (behind by 1 commits): qmyrypzk ab8b299e message
[EOF]
");
}
#[test]
fn test_git_fetch_rename_fetch() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
add_git_remote(&test_env, &work_dir, "origin");
work_dir
.run_jj(["bookmark", "create", "-r@", "origin"])
.success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
origin: qpvuntsm e8849ae1 (empty) (no description set)
[EOF]
");
work_dir.run_jj(["git", "fetch"]).success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
origin (conflicted):
+ qpvuntsm e8849ae1 (empty) (no description set)
+ qmyrypzk ab8b299e message
@origin (behind by 1 commits): qmyrypzk ab8b299e message
[EOF]
");
work_dir
.run_jj(["git", "remote", "rename", "origin", "upstream"])
.success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
origin (conflicted):
+ qpvuntsm e8849ae1 (empty) (no description set)
+ qmyrypzk ab8b299e message
@upstream (behind by 1 commits): qmyrypzk ab8b299e message
[EOF]
");
// Check that jj indicates that nothing has changed
let output = work_dir.run_jj(["git", "fetch", "--remote", "upstream"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Nothing changed.
[EOF]
");
}
#[test]
fn test_git_fetch_removed_bookmark() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
let source_dir = test_env.work_dir("source");
git::init(source_dir.root());
// Clone an empty repo. The target repo is a normal `jj` repo, *not* colocated
let output = test_env.run_jj_in(".", ["git", "clone", "source", "target"]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Fetching into new repo in "$TEST_ENV/target"
Nothing changed.
[EOF]
"#);
let target_dir = test_env.work_dir("target");
let source_log = create_colocated_repo_and_bookmarks_from_trunk1(&source_dir);
insta::assert_snapshot!(source_log, @r#"
===== Source git repo contents =====
@ bc83465a3090 "b" b
│ ○ d4d535f1d579 "a2" a2
├─╯
│ ○ c8303692b8e2 "a1" a1
├─╯
○ 382881770501 "trunk1" trunk1
◆ 000000000000 ""
[EOF]
"#);
// Fetch all bookmarks
let output = target_dir.run_jj(["git", "fetch"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: a1@origin [new] tracked
bookmark: a2@origin [new] tracked
bookmark: b@origin [new] tracked
bookmark: trunk1@origin [new] tracked
[EOF]
");
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ bc83465a3090 "b" b
│ │ ○ d4d535f1d579 "a2" a2
│ ├─╯
│ │ ○ c8303692b8e2 "a1" a1
│ ├─╯
│ ○ 382881770501 "trunk1" trunk1
├─╯
◆ 000000000000 ""
[EOF]
"#);
// Remove a2 bookmark in origin
source_dir
.run_jj(["bookmark", "forget", "--include-remotes", "a2"])
.success();
// Fetch bookmark a1 from origin and check that a2 is still there
let output = target_dir.run_jj(["git", "fetch", "--branch", "a1"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Nothing changed.
[EOF]
");
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ bc83465a3090 "b" b
│ │ ○ d4d535f1d579 "a2" a2
│ ├─╯
│ │ ○ c8303692b8e2 "a1" a1
│ ├─╯
│ ○ 382881770501 "trunk1" trunk1
├─╯
◆ 000000000000 ""
[EOF]
"#);
// Fetch bookmarks a2 from origin, and check that it has been removed locally
let output = target_dir.run_jj(["git", "fetch", "--branch", "a2"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: a2@origin [deleted] untracked
Abandoned 1 commits that are no longer reachable.
[EOF]
");
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ bc83465a3090 "b" b
│ │ ○ c8303692b8e2 "a1" a1
│ ├─╯
│ ○ 382881770501 "trunk1" trunk1
├─╯
◆ 000000000000 ""
[EOF]
"#);
}
#[test]
fn test_git_fetch_removed_parent_bookmark() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
let source_dir = test_env.work_dir("source");
git::init(source_dir.root());
// Clone an empty repo. The target repo is a normal `jj` repo, *not* colocated
let output = test_env.run_jj_in(".", ["git", "clone", "source", "target"]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Fetching into new repo in "$TEST_ENV/target"
Nothing changed.
[EOF]
"#);
let target_dir = test_env.work_dir("target");
let source_log = create_colocated_repo_and_bookmarks_from_trunk1(&source_dir);
insta::assert_snapshot!(source_log, @r#"
===== Source git repo contents =====
@ bc83465a3090 "b" b
│ ○ d4d535f1d579 "a2" a2
├─╯
│ ○ c8303692b8e2 "a1" a1
├─╯
○ 382881770501 "trunk1" trunk1
◆ 000000000000 ""
[EOF]
"#);
// Fetch all bookmarks
let output = target_dir.run_jj(["git", "fetch"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: a1@origin [new] tracked
bookmark: a2@origin [new] tracked
bookmark: b@origin [new] tracked
bookmark: trunk1@origin [new] tracked
[EOF]
");
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ bc83465a3090 "b" b
│ │ ○ d4d535f1d579 "a2" a2
│ ├─╯
│ │ ○ c8303692b8e2 "a1" a1
│ ├─╯
│ ○ 382881770501 "trunk1" trunk1
├─╯
◆ 000000000000 ""
[EOF]
"#);
// Remove all bookmarks in origin.
source_dir
.run_jj(["bookmark", "forget", "--include-remotes", "glob:*"])
.success();
// Fetch bookmarks master, trunk1 and a1 from origin and check that only those
// bookmarks have been removed and that others were not rebased because of
// abandoned commits.
let output = target_dir.run_jj([
"git", "fetch", "--branch", "master", "--branch", "trunk1", "--branch", "a1",
]);
insta::assert_snapshot!(output, @r"
------- stderr -------
bookmark: a1@origin [deleted] untracked
bookmark: trunk1@origin [deleted] untracked
Abandoned 1 commits that are no longer reachable.
Warning: No branch matching `master` found on any specified/configured remote
[EOF]
");
insta::assert_snapshot!(get_log_output(&target_dir), @r#"
@ e8849ae12c70 ""
│ ○ bc83465a3090 "b" b
│ │ ○ d4d535f1d579 "a2" a2
│ ├─╯
│ ○ 382881770501 "trunk1"
├─╯
◆ 000000000000 ""
[EOF]
"#);
}
#[test]
fn test_git_fetch_remote_only_bookmark() {
let test_env = TestEnvironment::default();
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
// Create non-empty git repo to add as a remote
let git_repo_path = test_env.env_root().join("git-repo");
let git_repo = git::init(git_repo_path);
work_dir
.run_jj(["git", "remote", "add", "origin", "../git-repo"])
.success();
// Create a commit and a bookmark in the git repo
let commit_result = git::add_commit(
&git_repo,
"refs/heads/feature1",
"file",
b"content",
"message",
&[],
);
// Fetch using git.auto_local_bookmark = true
test_env.add_config("git.auto-local-bookmark = true");
work_dir
.run_jj(["git", "fetch", "--remote=origin"])
.success();
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
feature1: qomsplrm ebeb70d8 message
@origin: qomsplrm ebeb70d8 message
[EOF]
");
git::write_commit(
&git_repo,
"refs/heads/feature2",
commit_result.tree_id,
"message",
&[],
);
// Fetch using git.auto_local_bookmark = false
test_env.add_config("git.auto-local-bookmark = false");
work_dir
.run_jj(["git", "fetch", "--remote=origin"])
.success();
insta::assert_snapshot!(get_log_output(&work_dir), @r#"
@ e8849ae12c70 ""
│ ◆ ebeb70d8c5f9 "message" feature1 feature2@origin
├─╯
◆ 000000000000 ""
[EOF]
"#);
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
feature1: qomsplrm ebeb70d8 message
@origin: qomsplrm ebeb70d8 message
feature2@origin: qomsplrm ebeb70d8 message
[EOF]
");
}
#[test]
fn test_git_fetch_preserve_commits_across_repos() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-bookmark = true");
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
let upstream_repo = add_git_remote(&test_env, &work_dir, "upstream");
let fork_path = test_env.env_root().join("fork");
let fork_repo = clone_git_remote_into(&test_env, "upstream", "fork");
work_dir
.run_jj(["git", "remote", "add", "fork", "../fork"])
.success();
// add commit to fork remote in another branch
add_commit_to_branch(&fork_repo, "feature");
// fetch remote bookmarks
work_dir
.run_jj(["git", "fetch", "--remote=fork", "--remote=upstream"])
.success();
insta::assert_snapshot!(get_log_output(&work_dir), @r#"
@ e8849ae12c70 ""
│ ○ bcd7cd779791 "message" upstream
├─╯
│ ○ 16ec9ef2877a "message" feature
├─╯
◆ 000000000000 ""
[EOF]
"#);
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
feature: srwrtuky 16ec9ef2 message
@fork: srwrtuky 16ec9ef2 message
upstream: zkvzklqn bcd7cd77 message
@fork: zkvzklqn bcd7cd77 message
@upstream: zkvzklqn bcd7cd77 message
[EOF]
");
// merge fork/feature into the upstream/upstream
git::add_remote(upstream_repo.git_dir(), "fork", fork_path.to_str().unwrap());
git::fetch(upstream_repo.git_dir(), "fork");
let base_id = upstream_repo
.find_reference("refs/heads/upstream")
.unwrap()
.peel_to_commit()
.unwrap()
.id()
.detach();
let fork_id = upstream_repo
.find_reference("refs/remotes/fork/feature")
.unwrap()
.peel_to_commit()
.unwrap()
.id()
.detach();
git::write_commit(
&upstream_repo,
"refs/heads/upstream",
upstream_repo.empty_tree().id().detach(),
"merge",
&[base_id, fork_id],
);
// remove branch on the fork
fork_repo
.find_reference("refs/heads/feature")
.unwrap()
.delete()
.unwrap();
// fetch again on the jj repo, first looking at fork and then at upstream
work_dir
.run_jj(["git", "fetch", "--remote=fork", "--remote=upstream"])
.success();
insta::assert_snapshot!(get_log_output(&work_dir), @r#"
@ e8849ae12c70 ""
│ ○ f3e9250bd003 "merge" upstream*
│ ├─╮
│ │ ○ 16ec9ef2877a "message"
├───╯
│ ○ bcd7cd779791 "message" upstream@fork
├─╯
◆ 000000000000 ""
[EOF]
"#);
insta::assert_snapshot!(get_bookmark_output(&work_dir), @r"
upstream: trrkvuqr f3e9250b merge
@fork (behind by 2 commits): zkvzklqn bcd7cd77 message
@upstream: trrkvuqr f3e9250b merge
[EOF]
");
}
|