diff options
Diffstat (limited to 'src/git_remote_helper.rs')
| -rw-r--r-- | src/git_remote_helper.rs | 267 |
1 files changed, 206 insertions, 61 deletions
diff --git a/src/git_remote_helper.rs b/src/git_remote_helper.rs index d00ad0e..a5244bf 100644 --- a/src/git_remote_helper.rs +++ b/src/git_remote_helper.rs | |||
| @@ -19,7 +19,7 @@ use client::{ | |||
| 19 | get_state_from_cache, sign_event, Connect, STATE_KIND, | 19 | get_state_from_cache, sign_event, Connect, STATE_KIND, |
| 20 | }; | 20 | }; |
| 21 | use console::Term; | 21 | use console::Term; |
| 22 | use git::{nostr_git_url_to_repo_coordinates, sha1_to_oid, RepoActions}; | 22 | use git::{sha1_to_oid, NostrUrlDecoded, RepoActions}; |
| 23 | use git2::{Oid, Repository}; | 23 | use git2::{Oid, Repository}; |
| 24 | use nostr::nips::{nip01::Coordinate, nip10::Marker}; | 24 | use nostr::nips::{nip01::Coordinate, nip10::Marker}; |
| 25 | use nostr_sdk::{ | 25 | use nostr_sdk::{ |
| @@ -81,12 +81,12 @@ async fn main() -> Result<()> { | |||
| 81 | #[cfg(test)] | 81 | #[cfg(test)] |
| 82 | let client = <MockConnect as std::default::Default>::default(); | 82 | let client = <MockConnect as std::default::Default>::default(); |
| 83 | 83 | ||
| 84 | let repo_coordinates = | 84 | let decoded_nostr_url = |
| 85 | nostr_git_url_to_repo_coordinates(nostr_remote_url).context("invalid nostr url")?; | 85 | NostrUrlDecoded::from_str(nostr_remote_url).context("invalid nostr url")?; |
| 86 | 86 | ||
| 87 | fetching_with_report_for_helper(git_repo_path, &client, &repo_coordinates).await?; | 87 | fetching_with_report_for_helper(git_repo_path, &client, &decoded_nostr_url.coordinates).await?; |
| 88 | 88 | ||
| 89 | let repo_ref = get_repo_ref_from_cache(git_repo_path, &repo_coordinates).await?; | 89 | let repo_ref = get_repo_ref_from_cache(git_repo_path, &decoded_nostr_url.coordinates).await?; |
| 90 | 90 | ||
| 91 | let stdin = io::stdin(); | 91 | let stdin = io::stdin(); |
| 92 | let mut line = String::new(); | 92 | let mut line = String::new(); |
| @@ -1639,7 +1639,8 @@ impl RepoState { | |||
| 1639 | mod tests { | 1639 | mod tests { |
| 1640 | use super::*; | 1640 | use super::*; |
| 1641 | 1641 | ||
| 1642 | mod nostr_git_url_to_repo_coordinates { | 1642 | mod nostr_git_url_paramemters_from_str { |
| 1643 | use git::ServerProtocol; | ||
| 1643 | use nostr_sdk::PublicKey; | 1644 | use nostr_sdk::PublicKey; |
| 1644 | 1645 | ||
| 1645 | use super::*; | 1646 | use super::*; |
| @@ -1663,79 +1664,223 @@ mod tests { | |||
| 1663 | #[test] | 1664 | #[test] |
| 1664 | fn from_naddr() -> Result<()> { | 1665 | fn from_naddr() -> Result<()> { |
| 1665 | assert_eq!( | 1666 | assert_eq!( |
| 1666 | nostr_git_url_to_repo_coordinates( | 1667 | NostrUrlDecoded::from_str( |
| 1667 | "nostr://naddr1qqzxuemfwsqs6amnwvaz7tmwdaejumr0dspzpgqgmmc409hm4xsdd74sf68a2uyf9pwel4g9mfdg8l5244t6x4jdqvzqqqrhnym0k2qj" | 1668 | "nostr://naddr1qqzxuemfwsqs6amnwvaz7tmwdaejumr0dspzpgqgmmc409hm4xsdd74sf68a2uyf9pwel4g9mfdg8l5244t6x4jdqvzqqqrhnym0k2qj" |
| 1668 | )?, | 1669 | )?, |
| 1669 | HashSet::from([Coordinate { | 1670 | NostrUrlDecoded { |
| 1670 | identifier: "ngit".to_string(), | 1671 | coordinates: HashSet::from([Coordinate { |
| 1671 | public_key: PublicKey::parse( | 1672 | identifier: "ngit".to_string(), |
| 1672 | "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", | 1673 | public_key: PublicKey::parse( |
| 1673 | ) | 1674 | "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", |
| 1674 | .unwrap(), | 1675 | ) |
| 1675 | kind: nostr_sdk::Kind::GitRepoAnnouncement, | 1676 | .unwrap(), |
| 1676 | relays: vec!["wss://nos.lol".to_string()], // wont add the slash | 1677 | kind: nostr_sdk::Kind::GitRepoAnnouncement, |
| 1677 | }]), | 1678 | relays: vec!["wss://nos.lol".to_string()], // wont add the slash |
| 1679 | }]), | ||
| 1680 | protocol: None, | ||
| 1681 | user: None, | ||
| 1682 | }, | ||
| 1678 | ); | 1683 | ); |
| 1679 | Ok(()) | 1684 | Ok(()) |
| 1680 | } | 1685 | } |
| 1681 | mod from_npub_slah_identifier { | 1686 | mod from_npub_slash_identifier { |
| 1682 | use super::*; | 1687 | use super::*; |
| 1683 | 1688 | ||
| 1684 | #[test] | 1689 | #[test] |
| 1685 | fn without_relay() -> Result<()> { | 1690 | fn without_relay() -> Result<()> { |
| 1686 | assert_eq!( | 1691 | assert_eq!( |
| 1687 | nostr_git_url_to_repo_coordinates( | 1692 | NostrUrlDecoded::from_str( |
| 1688 | "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit" | 1693 | "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit" |
| 1689 | )?, | 1694 | )?, |
| 1690 | HashSet::from([get_model_coordinate(false)]), | 1695 | NostrUrlDecoded { |
| 1696 | coordinates: HashSet::from([get_model_coordinate(false)]), | ||
| 1697 | protocol: None, | ||
| 1698 | user: None, | ||
| 1699 | }, | ||
| 1691 | ); | 1700 | ); |
| 1692 | Ok(()) | 1701 | Ok(()) |
| 1693 | } | 1702 | } |
| 1694 | 1703 | ||
| 1695 | #[test] | 1704 | mod with_url_parameters { |
| 1696 | fn with_relay_without_scheme_defaults_to_wss() -> Result<()> { | 1705 | |
| 1697 | assert_eq!( | 1706 | use super::*; |
| 1698 | nostr_git_url_to_repo_coordinates( | 1707 | |
| 1699 | "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay=nos.lol" | 1708 | #[test] |
| 1700 | )?, | 1709 | fn with_relay_without_scheme_defaults_to_wss() -> Result<()> { |
| 1701 | HashSet::from([get_model_coordinate(true)]), | 1710 | assert_eq!( |
| 1702 | ); | 1711 | NostrUrlDecoded::from_str( |
| 1703 | Ok(()) | 1712 | "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay=nos.lol" |
| 1704 | } | 1713 | )?, |
| 1714 | NostrUrlDecoded { | ||
| 1715 | coordinates: HashSet::from([get_model_coordinate(true)]), | ||
| 1716 | protocol: None, | ||
| 1717 | user: None, | ||
| 1718 | }, | ||
| 1719 | ); | ||
| 1720 | Ok(()) | ||
| 1721 | } | ||
| 1705 | 1722 | ||
| 1706 | #[test] | 1723 | #[test] |
| 1707 | fn with_encoded_relay() -> Result<()> { | 1724 | fn with_encoded_relay() -> Result<()> { |
| 1708 | assert_eq!( | 1725 | assert_eq!( |
| 1709 | nostr_git_url_to_repo_coordinates(&format!( | 1726 | NostrUrlDecoded::from_str(&format!( |
| 1710 | "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay={}", | 1727 | "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay={}", |
| 1711 | urlencoding::encode("wss://nos.lol") | 1728 | urlencoding::encode("wss://nos.lol") |
| 1712 | ))?, | 1729 | ))?, |
| 1713 | HashSet::from([get_model_coordinate(true)]), | 1730 | NostrUrlDecoded { |
| 1714 | ); | 1731 | coordinates: HashSet::from([get_model_coordinate(true)]), |
| 1715 | Ok(()) | 1732 | protocol: None, |
| 1733 | user: None, | ||
| 1734 | }, | ||
| 1735 | ); | ||
| 1736 | Ok(()) | ||
| 1737 | } | ||
| 1738 | #[test] | ||
| 1739 | fn with_multiple_encoded_relays() -> Result<()> { | ||
| 1740 | assert_eq!( | ||
| 1741 | NostrUrlDecoded::from_str(&format!( | ||
| 1742 | "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay={}&relay1={}", | ||
| 1743 | urlencoding::encode("wss://nos.lol"), | ||
| 1744 | urlencoding::encode("wss://relay.damus.io"), | ||
| 1745 | ))?, | ||
| 1746 | NostrUrlDecoded { | ||
| 1747 | coordinates: HashSet::from([Coordinate { | ||
| 1748 | identifier: "ngit".to_string(), | ||
| 1749 | public_key: PublicKey::parse( | ||
| 1750 | "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", | ||
| 1751 | ) | ||
| 1752 | .unwrap(), | ||
| 1753 | kind: nostr_sdk::Kind::GitRepoAnnouncement, | ||
| 1754 | relays: vec![ | ||
| 1755 | "wss://nos.lol/".to_string(), | ||
| 1756 | "wss://relay.damus.io/".to_string(), | ||
| 1757 | ], | ||
| 1758 | }]), | ||
| 1759 | protocol: None, | ||
| 1760 | user: None, | ||
| 1761 | }, | ||
| 1762 | ); | ||
| 1763 | Ok(()) | ||
| 1764 | } | ||
| 1765 | |||
| 1766 | #[test] | ||
| 1767 | fn with_server_protocol() -> Result<()> { | ||
| 1768 | assert_eq!( | ||
| 1769 | NostrUrlDecoded::from_str( | ||
| 1770 | "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?protocol=ssh" | ||
| 1771 | )?, | ||
| 1772 | NostrUrlDecoded { | ||
| 1773 | coordinates: HashSet::from([get_model_coordinate(false)]), | ||
| 1774 | protocol: Some(ServerProtocol::Ssh), | ||
| 1775 | user: None, | ||
| 1776 | }, | ||
| 1777 | ); | ||
| 1778 | Ok(()) | ||
| 1779 | } | ||
| 1780 | #[test] | ||
| 1781 | fn with_server_protocol_and_user() -> Result<()> { | ||
| 1782 | assert_eq!( | ||
| 1783 | NostrUrlDecoded::from_str( | ||
| 1784 | "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?protocol=ssh&user=fred" | ||
| 1785 | )?, | ||
| 1786 | NostrUrlDecoded { | ||
| 1787 | coordinates: HashSet::from([get_model_coordinate(false)]), | ||
| 1788 | protocol: Some(ServerProtocol::Ssh), | ||
| 1789 | user: Some("fred".to_string()), | ||
| 1790 | }, | ||
| 1791 | ); | ||
| 1792 | Ok(()) | ||
| 1793 | } | ||
| 1716 | } | 1794 | } |
| 1717 | #[test] | 1795 | mod with_parameters_embedded_with_slashes { |
| 1718 | fn with_multiple_encoded_relays() -> Result<()> { | 1796 | use super::*; |
| 1719 | assert_eq!( | 1797 | |
| 1720 | nostr_git_url_to_repo_coordinates(&format!( | 1798 | #[test] |
| 1721 | "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay={}&relay1={}", | 1799 | fn with_relay_without_scheme_defaults_to_wss() -> Result<()> { |
| 1722 | urlencoding::encode("wss://nos.lol"), | 1800 | assert_eq!( |
| 1723 | urlencoding::encode("wss://relay.damus.io"), | 1801 | NostrUrlDecoded::from_str( |
| 1724 | ))?, | 1802 | "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/nos.lol/ngit" |
| 1725 | HashSet::from([Coordinate { | 1803 | )?, |
| 1726 | identifier: "ngit".to_string(), | 1804 | NostrUrlDecoded { |
| 1727 | public_key: PublicKey::parse( | 1805 | coordinates: HashSet::from([get_model_coordinate(true)]), |
| 1728 | "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", | 1806 | protocol: None, |
| 1729 | ) | 1807 | user: None, |
| 1730 | .unwrap(), | 1808 | }, |
| 1731 | kind: nostr_sdk::Kind::GitRepoAnnouncement, | 1809 | ); |
| 1732 | relays: vec![ | 1810 | Ok(()) |
| 1733 | "wss://nos.lol/".to_string(), | 1811 | } |
| 1734 | "wss://relay.damus.io/".to_string(), | 1812 | |
| 1735 | ], | 1813 | #[test] |
| 1736 | }]), | 1814 | fn with_encoded_relay() -> Result<()> { |
| 1737 | ); | 1815 | assert_eq!( |
| 1738 | Ok(()) | 1816 | NostrUrlDecoded::from_str(&format!( |
| 1817 | "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/{}/ngit", | ||
| 1818 | urlencoding::encode("wss://nos.lol") | ||
| 1819 | ))?, | ||
| 1820 | NostrUrlDecoded { | ||
| 1821 | coordinates: HashSet::from([get_model_coordinate(true)]), | ||
| 1822 | protocol: None, | ||
| 1823 | user: None, | ||
| 1824 | }, | ||
| 1825 | ); | ||
| 1826 | Ok(()) | ||
| 1827 | } | ||
| 1828 | #[test] | ||
| 1829 | fn with_multiple_encoded_relays() -> Result<()> { | ||
| 1830 | assert_eq!( | ||
| 1831 | NostrUrlDecoded::from_str(&format!( | ||
| 1832 | "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/{}/{}/ngit", | ||
| 1833 | urlencoding::encode("wss://nos.lol"), | ||
| 1834 | urlencoding::encode("wss://relay.damus.io"), | ||
| 1835 | ))?, | ||
| 1836 | NostrUrlDecoded { | ||
| 1837 | coordinates: HashSet::from([Coordinate { | ||
| 1838 | identifier: "ngit".to_string(), | ||
| 1839 | public_key: PublicKey::parse( | ||
| 1840 | "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", | ||
| 1841 | ) | ||
| 1842 | .unwrap(), | ||
| 1843 | kind: nostr_sdk::Kind::GitRepoAnnouncement, | ||
| 1844 | relays: vec![ | ||
| 1845 | "wss://nos.lol/".to_string(), | ||
| 1846 | "wss://relay.damus.io/".to_string(), | ||
| 1847 | ], | ||
| 1848 | }]), | ||
| 1849 | protocol: None, | ||
| 1850 | user: None, | ||
| 1851 | }, | ||
| 1852 | ); | ||
| 1853 | Ok(()) | ||
| 1854 | } | ||
| 1855 | |||
| 1856 | #[test] | ||
| 1857 | fn with_server_protocol() -> Result<()> { | ||
| 1858 | assert_eq!( | ||
| 1859 | NostrUrlDecoded::from_str( | ||
| 1860 | "nostr://ssh/npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit" | ||
| 1861 | )?, | ||
| 1862 | NostrUrlDecoded { | ||
| 1863 | coordinates: HashSet::from([get_model_coordinate(false)]), | ||
| 1864 | protocol: Some(ServerProtocol::Ssh), | ||
| 1865 | user: None, | ||
| 1866 | }, | ||
| 1867 | ); | ||
| 1868 | Ok(()) | ||
| 1869 | } | ||
| 1870 | #[test] | ||
| 1871 | fn with_server_protocol_and_user() -> Result<()> { | ||
| 1872 | assert_eq!( | ||
| 1873 | NostrUrlDecoded::from_str( | ||
| 1874 | "nostr://fred@ssh/npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit" | ||
| 1875 | )?, | ||
| 1876 | NostrUrlDecoded { | ||
| 1877 | coordinates: HashSet::from([get_model_coordinate(false)]), | ||
| 1878 | protocol: Some(ServerProtocol::Ssh), | ||
| 1879 | user: Some("fred".to_string()), | ||
| 1880 | }, | ||
| 1881 | ); | ||
| 1882 | Ok(()) | ||
| 1883 | } | ||
| 1739 | } | 1884 | } |
| 1740 | } | 1885 | } |
| 1741 | } | 1886 | } |