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
// This file is part of radicle-surf
// <https://github.com/radicle-dev/radicle-surf>
//
// Copyright (C) 2019-2020 The Radicle Team <dev@radicle.xyz>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 or
// later as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! ```
//! use nonempty::NonEmpty;
//! use radicle_surf::file_system::{Directory, File, Label, Path, SystemType};
//! use radicle_surf::file_system::unsound;
//! use radicle_surf::vcs::git::*;
//! use std::collections::HashMap;
//! # use std::error::Error;
//!
//! # fn main() -> Result<(), Box<dyn Error>> {
//! let repo = Repository::new("./data/git-platinum")?;
//!
//! // Pin the browser to a parituclar commit.
//! let pin_commit = Oid::from_str("3873745c8f6ffb45c990eb23b491d4b4b6182f95")?;
//! let mut browser = Browser::new(&repo, Branch::local("master"))?;
//! browser.commit(pin_commit)?;
//!
//! let directory = browser.get_directory()?;
//! let mut directory_contents = directory.list_directory();
//! directory_contents.sort();
//!
//! assert_eq!(directory_contents, vec![
//! SystemType::file(unsound::label::new(".i-am-well-hidden")),
//! SystemType::file(unsound::label::new(".i-too-am-hidden")),
//! SystemType::file(unsound::label::new("README.md")),
//! SystemType::directory(unsound::label::new("bin")),
//! SystemType::directory(unsound::label::new("src")),
//! SystemType::directory(unsound::label::new("text")),
//! SystemType::directory(unsound::label::new("this")),
//! ]);
//!
//! // find src directory in the Git directory and the in-memory directory
//! let src_directory = directory
//! .find_directory(Path::new(unsound::label::new("src")))
//! .expect("failed to find src");
//! let mut src_directory_contents = src_directory.list_directory();
//! src_directory_contents.sort();
//!
//! assert_eq!(src_directory_contents, vec![
//! SystemType::file(unsound::label::new("Eval.hs")),
//! SystemType::file(unsound::label::new("Folder.svelte")),
//! SystemType::file(unsound::label::new("memory.rs")),
//! ]);
//! #
//! # Ok(())
//! # }
//! ```
// Re-export git2 as sub-module
pub use git2::{self, Error as Git2Error, Oid, Time};
/// Provides ways of selecting a particular reference/revision.
mod reference;
pub use reference::{Ref, Rev};
mod repo;
pub use repo::{History, Repository, RepositoryRef};
pub mod error;
mod ext;
/// Provides the data for talking about branches.
pub mod branch;
pub use branch::{Branch, BranchName, BranchType};
/// Provides the data for talking about tags.
pub mod tag;
pub use tag::{Tag, TagName};
/// Provides the data for talking about commits.
pub mod commit;
pub use commit::{Author, Commit};
/// Provides the data for talking about namespaces.
pub mod namespace;
pub use namespace::Namespace;
/// Provides the data for talking about repository statistics.
pub mod stats;
pub use stats::Stats;
pub use crate::diff::Diff;
use crate::{
file_system,
file_system::directory,
vcs,
vcs::{git::error::*, Vcs},
};
use nonempty::NonEmpty;
use std::{
collections::{BTreeSet, HashMap},
convert::TryFrom,
str,
};
/// The signature of a commit
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Signature(Vec<u8>);
impl From<git2::Buf> for Signature {
fn from(other: git2::Buf) -> Self {
Signature((*other).into())
}
}
/// Determines whether to look for local or remote references or both.
pub enum RefScope {
/// List all branches by default.
All,
/// List only local branches.
Local,
/// List only remote branches.
Remote {
/// Name of the remote. If `None`, then get the reference from all
/// remotes.
name: Option<String>,
},
}
/// Turn an `Option<P>` into a [`RefScope`]. If the `P` is present then
/// this is set as the remote of the `RefScope`. Otherwise, it's local
/// branch.
impl<P> From<Option<P>> for RefScope
where
P: ToString,
{
fn from(peer_id: Option<P>) -> Self {
peer_id.map_or(RefScope::Local, |peer_id| RefScope::Remote {
// We qualify the remotes as the PeerId + heads, otherwise we would grab the tags too.
name: Some(format!("{}/heads", peer_id.to_string())),
})
}
}
/// A [`crate::vcs::Browser`] that uses [`Repository`] as the underlying
/// repository backend, [`git2::Commit`] as the artifact, and [`Error`] for
/// error reporting.
pub type Browser<'a> = vcs::Browser<RepositoryRef<'a>, Commit, Error>;
impl<'a> Browser<'a> {
/// Create a new browser to interact with.
///
/// The `revspec` provided will be used to kick off the [`History`] for this
/// `Browser`.
///
/// # Errors
///
/// * [`error::Error::Git`]
///
/// # Examples
///
/// ```
/// use radicle_surf::vcs::git::{Browser, Branch, Repository};
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let browser = Browser::new(&repo, Branch::local("master"))?;
/// #
/// # Ok(())
/// # }
/// ```
pub fn new(
repository: impl Into<RepositoryRef<'a>>,
rev: impl Into<Rev>,
) -> Result<Self, Error> {
let repository = repository.into();
let history = repository.get_history(rev.into())?;
Ok(Self::init(repository, history))
}
/// Create a new `Browser` that starts in a given `namespace`.
///
/// # Errors
///
/// * [`error::Error::Git`]
///
/// # Examples
///
/// ```
/// use radicle_surf::vcs::git::{Browser, Repository, Branch, RefScope, BranchName, Namespace};
/// use std::convert::TryFrom;
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let browser = Browser::new_with_namespace(
/// &repo,
/// &Namespace::try_from("golden")?,
/// Branch::local("master")
/// )?;
///
/// let mut branches = browser.list_branches(RefScope::Local)?;
/// branches.sort();
///
/// assert_eq!(
/// branches,
/// vec![
/// Branch::local("banana"),
/// Branch::local("master"),
/// ]
/// );
/// #
/// # Ok(())
/// # }
/// ```
pub fn new_with_namespace(
repository: impl Into<RepositoryRef<'a>>,
namespace: &Namespace,
rev: impl Into<Rev>,
) -> Result<Self, Error> {
let repository = repository.into();
// This is a bit weird, the references don't seem to all be present unless we
// make a call to `references` o_O.
let _ = repository.repo_ref.references()?;
repository.switch_namespace(&namespace.to_string())?;
let history = repository.get_history(rev.into())?;
Ok(Self::init(repository, history))
}
fn init(repository: RepositoryRef<'a>, history: History) -> Self {
let snapshot = Box::new(|repository: &RepositoryRef<'a>, history: &History| {
let tree = Self::get_tree(repository.repo_ref, history.0.first())?;
Ok(directory::Directory::from_hash_map(tree))
});
vcs::Browser {
snapshot,
history,
repository,
}
}
/// Switch the namespace you are browsing in. This will consume the previous
/// `Browser` and give you back a new `Browser` for that particular
/// namespace. The `revision` provided will kick-off the history for
/// this `Browser`.
pub fn switch_namespace(
self,
namespace: &Namespace,
rev: impl Into<Ref>,
) -> Result<Self, Error> {
self.repository.switch_namespace(&namespace.to_string())?;
let history = self.get_history(Rev::from(rev))?;
Ok(Browser {
snapshot: self.snapshot,
repository: self.repository,
history,
})
}
/// What is the current namespace we're browsing in.
pub fn which_namespace(&self) -> Result<Option<Namespace>, Error> {
self.repository
.repo_ref
.namespace_bytes()
.map(Namespace::try_from)
.transpose()
}
/// Set the current `Browser` history to the `HEAD` commit of the underlying
/// repository.
///
/// # Errors
///
/// * [`error::Error::Git`]
///
/// # Examples
///
/// ```
/// use radicle_surf::vcs::git::{Browser, Repository, Branch};
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let mut browser = Browser::new(&repo, Branch::local("master"))?;
///
/// // ensure we're at HEAD
/// browser.head();
///
/// let directory = browser.get_directory();
///
/// // We are able to render the directory
/// assert!(directory.is_ok());
/// #
/// # Ok(())
/// # }
/// ```
pub fn head(&mut self) -> Result<(), Error> {
let history = self.repository.head()?;
self.set(history);
Ok(())
}
/// Set the current `Browser`'s [`History`] to the given [`BranchName`]
/// provided.
///
/// # Errors
///
/// * [`error::Error::Git`]
/// * [`error::Error::NotBranch`]
///
/// # Examples
///
/// ```
/// use radicle_surf::vcs::git::{Branch, Browser, Repository};
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let mut browser = Browser::new(&repo, Branch::local("master"))?;
///
/// // ensure we're on 'master'
/// browser.branch(Branch::local("master"));
///
/// let directory = browser.get_directory();
///
/// // We are able to render the directory
/// assert!(directory.is_ok());
/// #
/// # Ok(())
/// # }
/// ```
///
/// ```
/// use radicle_surf::vcs::git::{Branch, Browser, Repository};
/// use radicle_surf::file_system::{Label, Path, SystemType};
/// use radicle_surf::file_system::unsound;
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let mut browser = Browser::new(&repo, Branch::local("master"))?;
/// browser.branch(Branch::remote("dev", "origin"))?;
///
/// let directory = browser.get_directory()?;
/// let mut directory_contents = directory.list_directory();
/// directory_contents.sort();
///
/// assert!(directory_contents.contains(
/// &SystemType::file(unsound::label::new("here-we-are-on-a-dev-branch.lol"))
/// ));
/// #
/// # Ok(())
/// # }
/// ```
pub fn branch(&mut self, branch: Branch) -> Result<(), Error> {
let name = BranchName(branch.name());
self.set(self.repository.reference(branch, |reference| {
let is_branch = ext::is_branch(reference) || reference.is_remote();
if !is_branch {
Some(Error::NotBranch(name))
} else {
None
}
})?);
Ok(())
}
/// Set the current `Browser`'s [`History`] to the [`TagName`] provided.
///
/// # Errors
///
/// * [`error::Error::Git`]
/// * [`error::Error::NotTag`]
///
/// # Examples
///
/// ```
/// use nonempty::NonEmpty;
/// use radicle_surf::vcs::History;
/// use radicle_surf::vcs::git::{TagName, Branch, Browser, Oid, Repository};
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let mut browser = Browser::new(&repo, Branch::local("master"))?;
///
/// // Switch to "v0.3.0"
/// browser.tag(TagName::new("v0.3.0"))?;
///
/// let expected_history = History(NonEmpty::from((
/// Oid::from_str("19bec071db6474af89c866a1bd0e4b1ff76e2b97")?,
/// vec![
/// Oid::from_str("f3a089488f4cfd1a240a9c01b3fcc4c34a4e97b2")?,
/// Oid::from_str("2429f097664f9af0c5b7b389ab998b2199ffa977")?,
/// Oid::from_str("d3464e33d75c75c99bfb90fa2e9d16efc0b7d0e3")?,
/// ]
/// )));
///
/// let history_ids = browser.get().map(|commit| commit.id);
///
/// // We are able to render the directory
/// assert_eq!(history_ids, expected_history);
/// #
/// # Ok(())
/// # }
/// ```
pub fn tag(&mut self, tag_name: TagName) -> Result<(), Error> {
let name = tag_name.clone();
self.set(self.repository.reference(tag_name, |reference| {
if !ext::is_tag(reference) {
Some(Error::NotTag(name))
} else {
None
}
})?);
Ok(())
}
/// Set the current `Browser`'s [`History`] to the [`Oid`] (SHA digest)
/// provided.
///
/// # Errors
///
/// * [`error::Error::Git`]
///
/// # Examples
///
/// ```
/// use radicle_surf::file_system::{Label, SystemType};
/// use radicle_surf::file_system::unsound;
/// use radicle_surf::vcs::git::{Branch, Browser, Oid, Repository};
/// use std::str::FromStr;
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let mut browser = Browser::new(&repo, Branch::local("master"))?;
///
/// // Set to the initial commit
/// let commit = Oid::from_str("e24124b7538658220b5aaf3b6ef53758f0a106dc")?;
///
/// browser.commit(commit)?;
///
/// let directory = browser.get_directory()?;
/// let mut directory_contents = directory.list_directory();
///
/// assert_eq!(
/// directory_contents,
/// vec![
/// SystemType::file(unsound::label::new("README.md")),
/// SystemType::directory(unsound::label::new("bin")),
/// SystemType::directory(unsound::label::new("src")),
/// SystemType::directory(unsound::label::new("this")),
/// ]
/// );
/// #
/// # Ok(())
/// # }
/// ```
pub fn commit(&mut self, oid: Oid) -> Result<(), Error> {
self.set(self.get_history(Rev::Oid(oid))?);
Ok(())
}
/// Set a `Browser`'s [`History`] based on a [revspec](https://git-scm.com/docs/git-rev-parse.html#_specifying_revisions).
///
/// # Errors
///
/// * [`error::Error::Git`]
/// * [`error::Error::RevParseFailure`]
///
/// # Examples
///
/// ```
/// use radicle_surf::file_system::{Label, SystemType};
/// use radicle_surf::file_system::unsound;
/// use radicle_surf::vcs::git::{Browser, Branch, Oid, Repository};
/// use std::str::FromStr;
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let mut browser = Browser::new(&repo, Branch::local("master"))?;
///
/// browser.rev(Branch::remote("dev", "origin"))?;
///
/// let directory = browser.get_directory()?;
/// let mut directory_contents = directory.list_directory();
/// directory_contents.sort();
///
/// assert!(directory_contents.contains(
/// &SystemType::file(unsound::label::new("here-we-are-on-a-dev-branch.lol"))
/// ));
/// #
/// # Ok(())
/// # }
/// ```
pub fn rev(&mut self, rev: impl Into<Rev>) -> Result<(), Error> {
let history = self.get_history(rev.into())?;
self.set(history);
Ok(())
}
/// Parse an [`Oid`] from the given string. This is useful if we have a
/// shorthand version of the `Oid`, as opposed to the full one.
///
/// # Examples
///
/// ```
/// use radicle_surf::vcs::git::{Branch, Browser, Oid, Repository};
/// use std::str::FromStr;
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let mut browser = Browser::new(&repo, Branch::local("master"))?;
///
/// // Set to the initial commit
/// let commit = Oid::from_str("e24124b7538658220b5aaf3b6ef53758f0a106dc")?;
///
/// assert_eq!(
/// commit,
/// browser.oid("e24124b")?,
/// );
/// #
/// # Ok(())
/// # }
/// ```
pub fn oid(&self, oid: &str) -> Result<Oid, Error> {
self.repository.oid(oid)
}
/// Get the [`Diff`] between two commits.
pub fn diff(&self, from: Oid, to: Oid) -> Result<Diff, Error> {
self.repository.diff(from, to)
}
/// Get the [`Diff`] of a commit with no parents.
pub fn initial_diff(&self, oid: Oid) -> Result<Diff, Error> {
self.repository.initial_diff(oid)
}
/// List the names of the _branches_ that are contained in the underlying
/// [`Repository`].
///
/// # Errors
///
/// * [`error::Error::Git`]
///
/// # Examples
///
/// ```
/// use radicle_surf::vcs::git::{Branch, RefScope, BranchName, Browser, Namespace, Repository};
/// use std::convert::TryFrom;
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let mut browser = Browser::new(&repo, Branch::local("master"))?;
///
/// let branches = browser.list_branches(RefScope::All)?;
///
/// // 'master' exists in the list of branches
/// assert!(branches.contains(&Branch::local("master")));
///
/// // Filter the branches by `Remote` 'origin'.
/// let mut branches = browser.list_branches(RefScope::Remote {
/// name: Some("origin".to_string())
/// })?;
/// branches.sort();
///
/// assert_eq!(branches, vec![
/// Branch::remote("HEAD", "origin"),
/// Branch::remote("dev", "origin"),
/// Branch::remote("master", "origin"),
/// ]);
///
/// // Filter the branches by all `Remote`s.
/// let mut branches = browser.list_branches(RefScope::Remote {
/// name: None
/// })?;
/// branches.sort();
///
/// assert_eq!(branches, vec![
/// Branch::remote("HEAD", "origin"),
/// Branch::remote("dev", "origin"),
/// Branch::remote("master", "origin"),
/// Branch::remote("orange/pineapple", "banana"),
/// Branch::remote("pineapple", "banana"),
/// ]);
///
/// // We can also switch namespaces and list the branches in that namespace.
/// let golden = browser.switch_namespace(&Namespace::try_from("golden")?, Branch::local("master"))?;
///
/// let mut branches = golden.list_branches(RefScope::Local)?;
/// branches.sort();
///
/// assert_eq!(branches, vec![
/// Branch::local("banana"),
/// Branch::local("master"),
/// ]);
/// #
/// # Ok(())
/// # }
/// ```
pub fn list_branches(&self, filter: RefScope) -> Result<Vec<Branch>, Error> {
self.repository.list_branches(filter)
}
/// List the names of the _tags_ that are contained in the underlying
/// [`Repository`].
///
/// # Errors
///
/// * [`error::Error::Git`]
///
/// # Examples
///
/// ```
/// use radicle_surf::vcs::git::{Branch, RefScope, Browser, Namespace, Oid, Repository, Tag, TagName, Author, Time};
/// use std::convert::TryFrom;
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let mut browser = Browser::new(&repo, Branch::local("master"))?;
///
/// let tags = browser.list_tags(RefScope::Local)?;
///
/// assert_eq!(
/// tags,
/// vec![
/// Tag::Light {
/// id: Oid::from_str("d3464e33d75c75c99bfb90fa2e9d16efc0b7d0e3")?,
/// name: TagName::new("v0.1.0"),
/// remote: None,
/// },
/// Tag::Light {
/// id: Oid::from_str("2429f097664f9af0c5b7b389ab998b2199ffa977")?,
/// name: TagName::new("v0.2.0"),
/// remote: None,
/// },
/// Tag::Light {
/// id: Oid::from_str("19bec071db6474af89c866a1bd0e4b1ff76e2b97")?,
/// name: TagName::new("v0.3.0"),
/// remote: None,
/// },
/// Tag::Light {
/// id: Oid::from_str("91b69e00cd8e5a07e20942e9e4457d83ce7a3ff1")?,
/// name: TagName::new("v0.4.0"),
/// remote: None,
/// },
/// Tag::Light {
/// id: Oid::from_str("80ded66281a4de2889cc07293a8f10947c6d57fe")?,
/// name: TagName::new("v0.5.0"),
/// remote: None,
/// },
/// Tag::Annotated {
/// id: Oid::from_str("4d1f4af2703074d37cb877f4fdbe36322c8e541d")?,
/// target_id: Oid::from_str("d6880352fc7fda8f521ae9b7357668b17bb5bad5")?,
/// name: TagName::new("v0.6.0"),
/// remote: None,
/// tagger: Some(Author {
/// name: "Thomas Scholtes".to_string(),
/// email: "thomas@monadic.xyz".to_string(),
/// time: Time::new(1620740737, 120),
/// }),
/// message: Some("An annotated tag message for v0.6.0\n".to_string())
/// },
/// ]
/// );
///
/// // We can also switch namespaces and list the branches in that namespace.
/// let golden = browser.switch_namespace(&Namespace::try_from("golden")?, Branch::local("master"))?;
///
/// let branches = golden.list_tags(RefScope::Local)?;
/// assert_eq!(branches, vec![
/// Tag::Light {
/// id: Oid::from_str("d3464e33d75c75c99bfb90fa2e9d16efc0b7d0e3")?,
/// name: TagName::new("v0.1.0"),
/// remote: None,
/// },
/// Tag::Light {
/// id: Oid::from_str("2429f097664f9af0c5b7b389ab998b2199ffa977")?,
/// name: TagName::new("v0.2.0"),
/// remote: None,
/// },
/// ]);
/// let golden = golden.switch_namespace(&Namespace::try_from("golden")?, Branch::local("master"))?;
///
/// let branches = golden.list_tags(RefScope::Remote { name: Some("kickflip".to_string()) })?;
/// assert_eq!(branches, vec![
/// Tag::Light {
/// id: Oid::from_str("d3464e33d75c75c99bfb90fa2e9d16efc0b7d0e3")?,
/// name: TagName::new("v0.1.0"),
/// remote: Some("kickflip".to_string()),
/// },
/// ]);
/// #
/// # Ok(())
/// # }
/// ```
pub fn list_tags(&self, scope: RefScope) -> Result<Vec<Tag>, Error> {
self.repository.list_tags(scope)
}
/// List the namespaces within a `Browser`, filtering out ones that do not
/// parse correctly.
///
/// # Errors
///
/// * [`Error::Git`]
///
/// # Examples
///
/// ```
/// use radicle_surf::vcs::git::{Branch, BranchType, BranchName, Browser, Namespace, Repository};
/// use std::convert::TryFrom;
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let mut browser = Browser::new(&repo, Branch::local("master"))?;
///
/// let mut namespaces = browser.list_namespaces()?;
/// namespaces.sort();
///
/// assert_eq!(namespaces, vec![
/// Namespace::try_from("golden")?,
/// Namespace::try_from("golden/silver")?,
/// Namespace::try_from("me")?,
/// ]);
///
///
/// #
/// # Ok(())
/// # }
/// ```
pub fn list_namespaces(&self) -> Result<Vec<Namespace>, Error> {
self.repository.list_namespaces()
}
/// Given a [`crate::file_system::Path`] to a file, return the last
/// [`Commit`] that touched that file or directory.
///
/// # Errors
///
/// * [`error::Error::Git`]
/// * [`error::Error::LastCommitException`]
///
/// # Examples
///
/// ```
/// use radicle_surf::vcs::git::{Branch, Browser, Oid, Repository};
/// use radicle_surf::file_system::{Label, Path, SystemType};
/// use radicle_surf::file_system::unsound;
/// use std::str::FromStr;
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let mut browser = Browser::new(&repo, Branch::local("master"))?;
///
/// // Clamp the Browser to a particular commit
/// let commit = Oid::from_str("d6880352fc7fda8f521ae9b7357668b17bb5bad5")?;
/// browser.commit(commit)?;
///
/// let head_commit = browser.get().first().clone();
/// let expected_commit = Oid::from_str("d3464e33d75c75c99bfb90fa2e9d16efc0b7d0e3")?;
///
/// let readme_last_commit = browser
/// .last_commit(Path::with_root(&[unsound::label::new("README.md")]))?
/// .map(|commit| commit.id);
///
/// assert_eq!(readme_last_commit, Some(expected_commit));
///
/// let expected_commit = Oid::from_str("e24124b7538658220b5aaf3b6ef53758f0a106dc")?;
///
/// let memory_last_commit = browser
/// .last_commit(Path::with_root(&[unsound::label::new("src"), unsound::label::new("memory.rs")]))?
/// .map(|commit| commit.id);
///
/// assert_eq!(memory_last_commit, Some(expected_commit));
/// #
/// # Ok(())
/// # }
/// ```
pub fn last_commit(&self, path: file_system::Path) -> Result<Option<Commit>, Error> {
let file_history = self.repository.file_history(
&path,
repo::CommitHistory::Last,
self.get().first().clone(),
)?;
Ok(file_history.first().cloned())
}
/// Get the commit history for a file _or_ directory.
///
/// # Examples
///
/// ```
/// use nonempty::NonEmpty;
/// use radicle_surf::vcs::git::{Branch, Browser, Oid, Repository};
/// use radicle_surf::file_system::{Label, Path, SystemType};
/// use radicle_surf::file_system::unsound;
/// use std::str::FromStr;
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let mut browser = Browser::new(&repo, Branch::local("master"))?;
///
/// // Clamp the Browser to a particular commit
/// let commit = Oid::from_str("223aaf87d6ea62eef0014857640fd7c8dd0f80b5")?;
/// browser.commit(commit)?;
///
/// let root_commits: Vec<Oid> = browser
/// .file_history(unsound::path::new("~"))?
/// .into_iter()
/// .map(|commit| commit.id)
/// .collect();
///
/// assert_eq!(root_commits,
/// vec![
/// Oid::from_str("223aaf87d6ea62eef0014857640fd7c8dd0f80b5")?,
/// Oid::from_str("80bacafba303bf0cdf6142921f430ff265f25095")?,
/// Oid::from_str("a57846bbc8ced6587bf8329fc4bce970eb7b757e")?,
/// Oid::from_str("3873745c8f6ffb45c990eb23b491d4b4b6182f95")?,
/// Oid::from_str("80ded66281a4de2889cc07293a8f10947c6d57fe")?,
/// Oid::from_str("91b69e00cd8e5a07e20942e9e4457d83ce7a3ff1")?,
/// Oid::from_str("1820cb07c1a890016ca5578aa652fd4d4c38967e")?,
/// Oid::from_str("1e0206da8571ca71c51c91154e2fee376e09b4e7")?,
/// Oid::from_str("e24124b7538658220b5aaf3b6ef53758f0a106dc")?,
/// Oid::from_str("19bec071db6474af89c866a1bd0e4b1ff76e2b97")?,
/// Oid::from_str("f3a089488f4cfd1a240a9c01b3fcc4c34a4e97b2")?,
/// Oid::from_str("2429f097664f9af0c5b7b389ab998b2199ffa977")?,
/// Oid::from_str("d3464e33d75c75c99bfb90fa2e9d16efc0b7d0e3")?,
/// ]
/// );
///
/// let eval_commits: Vec<Oid> = browser
/// .file_history(unsound::path::new("~/src/Eval.hs"))?
/// .into_iter()
/// .map(|commit| commit.id)
/// .collect();
///
/// assert_eq!(eval_commits,
/// vec![
/// Oid::from_str("3873745c8f6ffb45c990eb23b491d4b4b6182f95")?,
/// Oid::from_str("e24124b7538658220b5aaf3b6ef53758f0a106dc")?,
/// ]
/// );
/// #
/// # Ok(())
/// # }
/// ```
pub fn file_history(&self, path: file_system::Path) -> Result<Vec<Commit>, Error> {
self.repository
.file_history(&path, repo::CommitHistory::Full, self.get().first().clone())
}
/// Extract the signature for a commit
///
/// # Arguments
///
/// * `commit` - The commit to extract the signature for
/// * `field` - the name of the header field containing the signature block;
/// pass `None` to extract the default 'gpgsig'
///
/// # Examples
///
/// ```
/// use radicle_surf::vcs::git::{Branch, Browser, Repository, Oid, error};
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let mut browser = Browser::new(&repo, Branch::local("master"))?;
///
/// let commit_with_signature_oid = Oid::from_str(
/// "e24124b7538658220b5aaf3b6ef53758f0a106dc"
/// )?;
///
/// browser.commit(commit_with_signature_oid)?;
/// let history = browser.get();
/// let commit_with_signature = history.first();
/// let signature = browser.extract_signature(commit_with_signature, None)?;
///
/// // We have a signature
/// assert!(signature.is_some());
///
/// let commit_without_signature_oid = Oid::from_str(
/// "80bacafba303bf0cdf6142921f430ff265f25095"
/// )?;
///
/// browser.commit(commit_without_signature_oid)?;
/// let history = browser.get();
/// let commit_without_signature = history.first();
/// let signature = browser.extract_signature(commit_without_signature, None)?;
///
/// // There is no signature
/// assert!(signature.is_none());
/// #
/// # Ok(())
/// # }
/// ```
pub fn extract_signature(
&self,
commit: &Commit,
field: Option<&str>,
) -> Result<Option<Signature>, Error> {
self.repository.extract_signature(&commit.id, field)
}
/// List the [`Branch`]es, which contain the provided [`Commit`].
///
/// # Errors
///
/// * [`error::Error::Git`]
///
/// # Examples
///
/// ```
/// use radicle_surf::vcs::git::{Browser, Repository, Branch, BranchName, Namespace, Oid};
/// use std::convert::TryFrom;
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let browser = Browser::new(&repo, Branch::local("master"))?;
///
///
/// let branches = browser.revision_branches(Oid::from_str("27acd68c7504755aa11023300890bb85bbd69d45")?)?;
/// assert_eq!(
/// branches,
/// vec![
/// Branch::local("dev"),
/// Branch::remote("dev", "origin"),
/// ]
/// );
///
/// // TODO(finto): I worry that this test will fail as other branches get added
/// let mut branches = browser.revision_branches(Oid::from_str("1820cb07c1a890016ca5578aa652fd4d4c38967e")?)?;
/// branches.sort();
/// assert_eq!(
/// branches,
/// vec![
/// Branch::remote("HEAD", "origin"),
/// Branch::local("dev"),
/// Branch::remote("dev", "origin"),
/// Branch::local("master"),
/// Branch::remote("master", "origin"),
/// Branch::remote("orange/pineapple", "banana"),
/// Branch::remote("pineapple", "banana"),
/// ]
/// );
///
/// let golden_browser = browser.switch_namespace(&Namespace::try_from("golden")?,
/// Branch::local("master"))?;
///
/// let branches = golden_browser.revision_branches(Oid::from_str("27acd68c7504755aa11023300890bb85bbd69d45")?)?;
/// assert_eq!(
/// branches,
/// vec![
/// Branch::local("banana"),
/// Branch::remote("fakie/bigspin", "kickflip"),
/// Branch::remote("heelflip", "kickflip"),
/// ]
/// );
/// #
/// # Ok(())
/// # }
/// ```
pub fn revision_branches(&self, rev: impl Into<Rev>) -> Result<Vec<Branch>, Error> {
let commit = self.repository.rev_to_commit(&rev.into())?;
self.repository.revision_branches(&commit.id())
}
/// Get the [`Stats`] of the underlying [`Repository`].
///
/// # Errors
///
/// * [`error::Error::Git`]
///
/// # Examples
///
/// ```
/// use radicle_surf::vcs::git::{Branch, Browser, Repository};
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let repo = Repository::new("./data/git-platinum")?;
/// let mut browser = Browser::new(&repo, Branch::local("master"))?;
///
/// let stats = browser.get_stats()?;
///
/// assert_eq!(stats.branches, 2);
///
/// assert_eq!(stats.commits, 15);
///
/// assert_eq!(stats.contributors, 4);
///
/// # Ok(())
/// # }
/// ```
pub fn get_stats(&self) -> Result<Stats, Error> {
let branches = self.list_branches(RefScope::Local)?.len();
let commits = self.history.len();
let contributors = self
.history
.iter()
.cloned()
.map(|commit| (commit.author.name, commit.author.email))
.collect::<BTreeSet<_>>();
Ok(Stats {
branches,
commits,
contributors: contributors.len(),
})
}
/// Do a pre-order TreeWalk of the given commit. This turns a Tree
/// into a HashMap of Paths and a list of Files. We can then turn that
/// into a Directory.
fn get_tree(
repo: &git2::Repository,
commit: &Commit,
) -> Result<HashMap<file_system::Path, NonEmpty<(file_system::Label, directory::File)>>, Error>
{
let mut file_paths_or_error: Result<
HashMap<file_system::Path, NonEmpty<(file_system::Label, directory::File)>>,
Error,
> = Ok(HashMap::new());
let commit = repo.find_commit(commit.id)?;
let tree = commit.as_object().peel_to_tree()?;
tree.walk(
git2::TreeWalkMode::PreOrder,
|s, entry| match Self::tree_entry_to_file_and_path(repo, s, entry) {
Ok((path, name, file)) => {
match file_paths_or_error.as_mut() {
Ok(files) => Self::update_file_map(path, name, file, files),
// We don't need to update, we want to keep the error.
Err(_err) => {},
}
git2::TreeWalkResult::Ok
},
Err(err) => match err {
// We want to continue if the entry was not a Blob.
TreeWalkError::NotBlob => git2::TreeWalkResult::Ok,
// We found a ObjectType::Commit (likely a submodule) and
// so we can skip it.
TreeWalkError::Commit => git2::TreeWalkResult::Ok,
// But we want to keep the error and abort otherwise.
TreeWalkError::Git(err) => {
file_paths_or_error = Err(err);
git2::TreeWalkResult::Abort
},
},
},
)?;
file_paths_or_error
}
/// Find the best common ancestor between two commits if it exists.
///
/// See [`git2::Repository::merge_base`] for details.
pub fn merge_base(&self, one: Oid, two: Oid) -> Result<Option<Oid>, Error> {
match self.repository.repo_ref.merge_base(one, two) {
Ok(merge_base) => Ok(Some(merge_base)),
Err(err) => {
if err.code() == git2::ErrorCode::NotFound {
Ok(None)
} else {
Err(Error::Git(err))
}
},
}
}
fn update_file_map(
path: file_system::Path,
name: file_system::Label,
file: directory::File,
files: &mut HashMap<file_system::Path, NonEmpty<(file_system::Label, directory::File)>>,
) {
files
.entry(path)
.and_modify(|entries| entries.push((name.clone(), file.clone())))
.or_insert_with(|| NonEmpty::new((name, file)));
}
fn tree_entry_to_file_and_path(
repo: &git2::Repository,
tree_path: &str,
entry: &git2::TreeEntry,
) -> Result<(file_system::Path, file_system::Label, directory::File), TreeWalkError> {
// Account for the "root" of git being the empty string
let path = if tree_path.is_empty() {
Ok(file_system::Path::root())
} else {
file_system::Path::try_from(tree_path)
}?;
// We found a Commit object in the Tree, likely a submodule.
// We will skip this entry.
if let Some(git2::ObjectType::Commit) = entry.kind() {
return Err(TreeWalkError::Commit);
}
let object = entry.to_object(repo)?;
let blob = object.as_blob().ok_or(TreeWalkError::NotBlob)?;
let name = str::from_utf8(entry.name_bytes())?;
let name = file_system::Label::try_from(name).map_err(Error::FileSystem)?;
Ok((
path,
name,
directory::File {
contents: blob.content().to_owned(),
size: blob.size(),
},
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(not(feature = "gh-actions"))]
#[test]
// An issue with submodules, see: https://github.com/radicle-dev/radicle-surf/issues/54
fn test_submodule_failure() {
let repo = Repository::new("..").unwrap();
let browser = Browser::new(&repo, Branch::local("master")).unwrap();
browser.get_directory().unwrap();
}
#[cfg(test)]
mod namespace {
use super::*;
use pretty_assertions::{assert_eq, assert_ne};
#[test]
fn switch_to_banana() -> Result<(), Error> {
let repo = Repository::new("./data/git-platinum")?;
let mut browser = Browser::new_with_namespace(
&repo,
&Namespace::try_from("golden")?,
Branch::local("master"),
)?;
let history = browser.history.clone();
browser.branch(Branch::local("banana"))?;
assert_ne!(history, browser.history);
Ok(())
}
#[test]
fn me_namespace() -> Result<(), Error> {
let repo = Repository::new("./data/git-platinum")?;
let browser = Browser::new(&repo, Branch::local("master"))?;
let history = browser.history.clone();
assert_eq!(browser.which_namespace(), Ok(None));
let browser = browser
.switch_namespace(&Namespace::try_from("me")?, Branch::local("feature/#1194"))?;
assert_eq!(
browser.which_namespace(),
Ok(Some(Namespace::try_from("me")?))
);
assert_eq!(history, browser.history);
let expected_branches: Vec<Branch> = vec![Branch::local("feature/#1194")];
let mut branches = browser.list_branches(RefScope::Local)?;
branches.sort();
assert_eq!(expected_branches, branches);
let expected_branches: Vec<Branch> = vec![Branch::remote("feature/#1194", "fein")];
let mut branches = browser.list_branches(RefScope::Remote {
name: Some("fein".to_string()),
})?;
branches.sort();
assert_eq!(expected_branches, branches);
Ok(())
}
#[test]
fn golden_namespace() -> Result<(), Error> {
let repo = Repository::new("./data/git-platinum")?;
let browser = Browser::new(&repo, Branch::local("master"))?;
let history = browser.history.clone();
assert_eq!(browser.which_namespace(), Ok(None));
let golden_browser = browser
.switch_namespace(&Namespace::try_from("golden")?, Branch::local("master"))?;
assert_eq!(
golden_browser.which_namespace(),
Ok(Some(Namespace::try_from("golden")?))
);
assert_eq!(history, golden_browser.history);
let expected_branches: Vec<Branch> =
vec![Branch::local("banana"), Branch::local("master")];
let mut branches = golden_browser.list_branches(RefScope::Local)?;
branches.sort();
assert_eq!(expected_branches, branches);
let expected_branches: Vec<Branch> = vec![
Branch::remote("fakie/bigspin", "kickflip"),
Branch::remote("heelflip", "kickflip"),
Branch::remote("v0.1.0", "kickflip"),
];
let mut branches = golden_browser.list_branches(RefScope::Remote {
name: Some("kickflip".to_string()),
})?;
branches.sort();
assert_eq!(expected_branches, branches);
Ok(())
}
#[test]
fn silver_namespace() -> Result<(), Error> {
let repo = Repository::new("./data/git-platinum")?;
let browser = Browser::new(&repo, Branch::local("master"))?;
let history = browser.history.clone();
assert_eq!(browser.which_namespace(), Ok(None));
let silver_browser = browser.switch_namespace(
&Namespace::try_from("golden/silver")?,
Branch::local("master"),
)?;
assert_eq!(
silver_browser.which_namespace(),
Ok(Some(Namespace::try_from("golden/silver")?))
);
assert_ne!(history, silver_browser.history);
let expected_branches: Vec<Branch> = vec![Branch::local("master")];
let mut branches = silver_browser.list_branches(RefScope::All)?;
branches.sort();
assert_eq!(expected_branches, branches);
Ok(())
}
}
#[cfg(test)]
mod rev {
use super::{Branch, Browser, Error, Oid, Repository, TagName};
// **FIXME**: This seems to break occasionally on
// buildkite. For some reason the commit
// 3873745c8f6ffb45c990eb23b491d4b4b6182f95, which is on master
// (currently HEAD), is not found. It seems to load the history
// with d6880352fc7fda8f521ae9b7357668b17bb5bad5 as the HEAD.
//
// To temporarily fix this, we need to select "New Build" from the build kite
// build page that's failing.
// * Under "Message" put whatever you want.
// * Under "Branch" put in the branch you're working on.
// * Expand "Options" and select "clean checkout".
#[test]
fn _master() -> Result<(), Error> {
let repo = Repository::new("./data/git-platinum")?;
let mut browser = Browser::new(&repo, Branch::local("master"))?;
browser.rev(Branch::remote("master", "origin"))?;
let commit1 = Oid::from_str("3873745c8f6ffb45c990eb23b491d4b4b6182f95")?;
assert!(
browser
.history
.find(|commit| if commit.id == commit1 {
Some(commit.clone())
} else {
None
})
.is_some(),
"commit_id={}, history =\n{:#?}",
commit1,
browser.history
);
let commit2 = Oid::from_str("d6880352fc7fda8f521ae9b7357668b17bb5bad5")?;
assert!(
browser
.history
.find(|commit| if commit.id == commit2 {
Some(commit.clone())
} else {
None
})
.is_some(),
"commit_id={}, history =\n{:#?}",
commit2,
browser.history
);
Ok(())
}
#[test]
fn commit() -> Result<(), Error> {
let repo = Repository::new("./data/git-platinum")?;
let mut browser = Browser::new(&repo, Branch::local("master"))?;
browser.rev(Oid::from_str("3873745c8f6ffb45c990eb23b491d4b4b6182f95")?)?;
let commit1 = Oid::from_str("3873745c8f6ffb45c990eb23b491d4b4b6182f95")?;
assert!(browser
.history
.find(|commit| if commit.id == commit1 {
Some(commit.clone())
} else {
None
})
.is_some());
Ok(())
}
#[test]
fn commit_parents() -> Result<(), Error> {
let repo = Repository::new("./data/git-platinum")?;
let mut browser = Browser::new(&repo, Branch::local("master"))?;
browser.rev(Oid::from_str("3873745c8f6ffb45c990eb23b491d4b4b6182f95")?)?;
let commit = browser.history.first();
assert_eq!(
commit.parents,
vec![Oid::from_str("d6880352fc7fda8f521ae9b7357668b17bb5bad5")?]
);
Ok(())
}
#[test]
fn commit_short() -> Result<(), Error> {
let repo = Repository::new("./data/git-platinum")?;
let mut browser = Browser::new(&repo, Branch::local("master"))?;
browser.rev(browser.oid("3873745c8")?)?;
let commit1 = Oid::from_str("3873745c8f6ffb45c990eb23b491d4b4b6182f95")?;
assert!(browser
.history
.find(|commit| if commit.id == commit1 {
Some(commit.clone())
} else {
None
})
.is_some());
Ok(())
}
#[test]
fn tag() -> Result<(), Error> {
let repo = Repository::new("./data/git-platinum")?;
let mut browser = Browser::new(&repo, Branch::local("master"))?;
browser.rev(TagName::new("v0.2.0"))?;
let commit1 = Oid::from_str("2429f097664f9af0c5b7b389ab998b2199ffa977")?;
assert_eq!(browser.history.first().id, commit1);
Ok(())
}
}
#[cfg(test)]
mod last_commit {
use crate::{
file_system::{unsound, Path},
vcs::git::{Branch, Browser, Oid, Repository},
};
#[test]
fn readme_missing_and_memory() {
let repo = Repository::new("./data/git-platinum")
.expect("Could not retrieve ./data/git-platinum as git repository");
let mut browser =
Browser::new(&repo, Branch::local("master")).expect("Could not initialise Browser");
// Set the browser history to the initial commit
let commit = Oid::from_str("d3464e33d75c75c99bfb90fa2e9d16efc0b7d0e3")
.expect("Failed to parse SHA");
browser.commit(commit).unwrap();
let head_commit = browser.get().0.first().clone();
// memory.rs is commited later so it should not exist here.
let memory_last_commit = browser
.last_commit(Path::with_root(&[
unsound::label::new("src"),
unsound::label::new("memory.rs"),
]))
.expect("Failed to get last commit")
.map(|commit| commit.id);
assert_eq!(memory_last_commit, None);
// README.md exists in this commit.
let readme_last_commit = browser
.last_commit(Path::with_root(&[unsound::label::new("README.md")]))
.expect("Failed to get last commit")
.map(|commit| commit.id);
assert_eq!(readme_last_commit, Some(head_commit.id));
}
#[test]
fn folder_svelte() {
let repo = Repository::new("./data/git-platinum")
.expect("Could not retrieve ./data/git-platinum as git repository");
let mut browser =
Browser::new(&repo, Branch::local("master")).expect("Could not initialise Browser");
// Check that last commit is the actual last commit even if head commit differs.
let commit = Oid::from_str("19bec071db6474af89c866a1bd0e4b1ff76e2b97")
.expect("Could not parse SHA");
browser.commit(commit).unwrap();
let expected_commit_id =
Oid::from_str("f3a089488f4cfd1a240a9c01b3fcc4c34a4e97b2").unwrap();
let folder_svelte = browser
.last_commit(unsound::path::new("~/examples/Folder.svelte"))
.expect("Failed to get last commit")
.map(|commit| commit.id);
assert_eq!(folder_svelte, Some(expected_commit_id));
}
#[test]
fn nest_directory() {
let repo = Repository::new("./data/git-platinum")
.expect("Could not retrieve ./data/git-platinum as git repository");
let mut browser =
Browser::new(&repo, Branch::local("master")).expect("Could not initialise Browser");
// Check that last commit is the actual last commit even if head commit differs.
let commit = Oid::from_str("19bec071db6474af89c866a1bd0e4b1ff76e2b97")
.expect("Failed to parse SHA");
browser.commit(commit).unwrap();
let expected_commit_id =
Oid::from_str("2429f097664f9af0c5b7b389ab998b2199ffa977").unwrap();
let nested_directory_tree_commit_id = browser
.last_commit(unsound::path::new(
"~/this/is/a/really/deeply/nested/directory/tree",
))
.expect("Failed to get last commit")
.map(|commit| commit.id);
assert_eq!(nested_directory_tree_commit_id, Some(expected_commit_id));
}
#[test]
#[cfg(not(windows))]
fn can_get_last_commit_for_special_filenames() {
let repo = Repository::new("./data/git-platinum")
.expect("Could not retrieve ./data/git-platinum as git repository");
let mut browser =
Browser::new(&repo, Branch::local("master")).expect("Could not initialise Browser");
// Check that last commit is the actual last commit even if head commit differs.
let commit = Oid::from_str("a0dd9122d33dff2a35f564d564db127152c88e02")
.expect("Failed to parse SHA");
browser.commit(commit).unwrap();
let expected_commit_id =
Oid::from_str("a0dd9122d33dff2a35f564d564db127152c88e02").unwrap();
let backslash_commit_id = browser
.last_commit(unsound::path::new("~/special/faux\\path"))
.expect("Failed to get last commit")
.map(|commit| commit.id);
assert_eq!(backslash_commit_id, Some(expected_commit_id));
let ogre_commit_id = browser
.last_commit(unsound::path::new("~/special/👹👹👹"))
.expect("Failed to get last commit")
.map(|commit| commit.id);
assert_eq!(ogre_commit_id, Some(expected_commit_id));
}
#[test]
fn root() {
let repo = Repository::new("./data/git-platinum")
.expect("Could not retrieve ./data/git-platinum as git repository");
let browser =
Browser::new(&repo, Branch::local("master")).expect("Could not initialise Browser");
let root_last_commit_id = browser
.last_commit(Path::root())
.expect("Failed to get last commit")
.map(|commit| commit.id);
assert_eq!(root_last_commit_id, Some(browser.get().first().id));
}
}
#[cfg(test)]
mod diff {
use crate::{diff::*, vcs::git::*};
#[test]
fn test_initial_diff() -> Result<(), Error> {
use file_system::*;
use pretty_assertions::assert_eq;
let oid = Oid::from_str("d3464e33d75c75c99bfb90fa2e9d16efc0b7d0e3")?;
let repo = Repository::new("./data/git-platinum")?;
let commit = repo.0.find_commit(oid).unwrap();
assert!(commit.parents().count() == 0);
assert!(commit.parent(0).is_err());
let bro = Browser::new(&repo, Branch::local("master"))?;
let diff = bro.initial_diff(oid)?;
let expected_diff = Diff {
created: vec![CreateFile {
path: Path::with_root(&[unsound::label::new("README.md")]),
diff: FileDiff::Plain {
hunks: vec![Hunk {
header: Line(b"@@ -0,0 +1 @@\n".to_vec()),
lines: vec![
LineDiff::addition(b"This repository is a data source for the Upstream front-end tests.\n".to_vec(), 1),
]
}].into()
},
}],
deleted: vec![],
moved: vec![],
copied: vec![],
modified: vec![],
};
assert_eq!(expected_diff, diff);
Ok(())
}
#[test]
fn test_diff() -> Result<(), Error> {
use file_system::*;
use pretty_assertions::assert_eq;
let repo = Repository::new("./data/git-platinum")?;
let commit = repo
.0
.find_commit(Oid::from_str("80bacafba303bf0cdf6142921f430ff265f25095")?)
.unwrap();
let parent = commit.parent(0)?;
let bro = Browser::new(&repo, Branch::local("master"))?;
let diff = bro.diff(parent.id(), commit.id())?;
let expected_diff = Diff {
created: vec![],
deleted: vec![],
moved: vec![],
copied: vec![],
modified: vec![ModifiedFile {
path: Path::with_root(&[unsound::label::new("README.md")]),
diff: FileDiff::Plain {
hunks: vec unit tests.\n".to_vec(), 2),
]
}].into()
},
eof: None,
}]
};
assert_eq!(expected_diff, diff);
Ok(())
}
#[cfg(feature = "serialize")]
#[test]
fn test_diff_serde() -> Result<(), Error> {
use file_system::*;
let diff = Diff {
created: vec![CreateFile{path: unsound::path::new("LICENSE"), diff: FileDiff::Plain { hunks: Hunks::default() }}],
deleted: vec![],
moved: vec![
MoveFile {
old_path: unsound::path::new("CONTRIBUTING"),
new_path: unsound::path::new("CONTRIBUTING.md")
}
],
copied: vec![],
modified: vec![ModifiedFile {
path: Path::with_root(&[unsound::label::new("README.md")]),
diff: FileDiff::Plain {
hunks: vec unit tests.\n".to_vec(), 2),
LineDiff::context(b"\n".to_vec(), 3, 4),
]
}].into()
},
eof: None,
}]
};
let eof: Option<u8> = None;
let json = serde_json::json!({
"created": [{"path": "LICENSE", "diff": {
"type": "plain",
"hunks": []
},
}],
"deleted": [],
"moved": [{ "oldPath": "CONTRIBUTING", "newPath": "CONTRIBUTING.md" }],
"copied": [],
"modified": [{
"path": "README.md",
"diff": {
"type": "plain",
"hunks": [{
"header": "@@ -1 +1,2 @@\n",
"lines": [
{ "lineNum": 1,
"line": "This repository is a data source for the Upstream front-end tests.\n",
"type": "deletion"
},
{ "lineNum": 1,
"line": "This repository is a data source for the Upstream front-end tests and the\n",
"type": "addition"
},
{ "lineNum": 2,
"line": "[`radicle-surf`](https://github.com/radicle-dev/git-platinum) unit tests.\n",
"type": "addition"
},
{ "lineNumOld": 3, "lineNumNew": 4,
"line": "\n",
"type": "context"
}
]
}]
},
"eof" : eof,
}]
});
assert_eq!(serde_json::to_value(&diff).unwrap(), json);
Ok(())
}
}
#[cfg(test)]
mod threading {
use crate::vcs::git::*;
use pretty_assertions::assert_eq;
use std::sync::{Mutex, MutexGuard};
#[test]
fn basic_test() -> Result<(), Error> {
let shared_repo = Mutex::new(Repository::new("./data/git-platinum")?);
let locked_repo: MutexGuard<Repository> = shared_repo.lock().unwrap();
let bro = Browser::new(&*locked_repo, Branch::local("master"))?;
let mut branches = bro.list_branches(RefScope::All)?;
branches.sort();
assert_eq!(
branches,
vec![
Branch::remote("HEAD", "origin"),
Branch::remote("dev", "origin"),
Branch::local("dev"),
Branch::remote("master", "origin"),
Branch::local("master"),
Branch::remote("orange/pineapple", "banana"),
Branch::remote("pineapple", "banana"),
]
);
Ok(())
}
}
}