upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/bin/git_remote_nostr
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/git_remote_nostr')
-rw-r--r--src/bin/git_remote_nostr/fetch.rs10
-rw-r--r--src/bin/git_remote_nostr/list.rs163
-rw-r--r--src/bin/git_remote_nostr/main.rs3
-rw-r--r--src/bin/git_remote_nostr/push.rs319
-rw-r--r--src/bin/git_remote_nostr/utils.rs448
5 files changed, 21 insertions, 922 deletions
diff --git a/src/bin/git_remote_nostr/fetch.rs b/src/bin/git_remote_nostr/fetch.rs
index f3d4362..221d964 100644
--- a/src/bin/git_remote_nostr/fetch.rs
+++ b/src/bin/git_remote_nostr/fetch.rs
@@ -19,15 +19,15 @@ use ngit::{
19 git_events::{KIND_PULL_REQUEST, KIND_PULL_REQUEST_UPDATE, tag_value}, 19 git_events::{KIND_PULL_REQUEST, KIND_PULL_REQUEST_UPDATE, tag_value},
20 login::get_curent_user, 20 login::get_curent_user,
21 repo_ref::{RepoRef, is_grasp_server}, 21 repo_ref::{RepoRef, is_grasp_server},
22 utils::{
23 Direction, find_proposal_and_patches_by_branch_name, get_oids_from_fetch_batch,
24 get_open_or_draft_proposals, get_read_protocols_to_try, join_with_and,
25 set_protocol_preference,
26 },
22}; 27};
23use nostr::nips::nip19; 28use nostr::nips::nip19;
24use nostr_sdk::{Event, ToBech32}; 29use nostr_sdk::{Event, ToBech32};
25 30
26use crate::utils::{
27 Direction, find_proposal_and_patches_by_branch_name, get_oids_from_fetch_batch,
28 get_open_or_draft_proposals, get_read_protocols_to_try, join_with_and, set_protocol_preference,
29};
30
31pub async fn run_fetch( 31pub async fn run_fetch(
32 git_repo: &Repo, 32 git_repo: &Repo,
33 repo_ref: &RepoRef, 33 repo_ref: &RepoRef,
diff --git a/src/bin/git_remote_nostr/list.rs b/src/bin/git_remote_nostr/list.rs
index 7bdf170..df98c12 100644
--- a/src/bin/git_remote_nostr/list.rs
+++ b/src/bin/git_remote_nostr/list.rs
@@ -1,30 +1,22 @@
1use core::str;
2use std::collections::HashMap; 1use std::collections::HashMap;
3 2
4use anyhow::{Context, Result, anyhow}; 3use anyhow::{Context, Result};
5use auth_git2::GitAuthenticator;
6use client::get_state_from_cache; 4use client::get_state_from_cache;
7use git::RepoActions; 5use git::RepoActions;
8use ngit::{ 6use ngit::{
9 client, 7 client,
10 git::{ 8 git::{self},
11 self,
12 nostr_url::{CloneUrl, NostrUrlDecoded, ServerProtocol},
13 },
14 git_events::{KIND_PULL_REQUEST, KIND_PULL_REQUEST_UPDATE, event_to_cover_letter, tag_value}, 9 git_events::{KIND_PULL_REQUEST, KIND_PULL_REQUEST_UPDATE, event_to_cover_letter, tag_value},
10 list::{get_ahead_behind, list_from_remotes},
15 login::get_curent_user, 11 login::get_curent_user,
16 repo_ref::{self, is_grasp_server}, 12 repo_ref::{self},
13 utils::{get_open_or_draft_proposals, get_short_git_server_name},
17}; 14};
18use nostr_sdk::hashes::sha1::Hash as Sha1Hash;
19use repo_ref::RepoRef; 15use repo_ref::RepoRef;
20 16
21use crate::{ 17use crate::{
22 fetch::{fetch_from_git_server, make_commits_for_proposal}, 18 fetch::{fetch_from_git_server, make_commits_for_proposal},
23 git::Repo, 19 git::Repo,
24 utils::{
25 Direction, get_open_or_draft_proposals, get_read_protocols_to_try,
26 get_short_git_server_name, join_with_and, set_protocol_preference,
27 },
28}; 20};
29 21
30pub async fn run_list( 22pub async fn run_list(
@@ -210,148 +202,3 @@ async fn get_open_and_draft_proposals_state(
210 } 202 }
211 Ok(state) 203 Ok(state)
212} 204}
213
214pub fn list_from_remotes(
215 term: &console::Term,
216 git_repo: &Repo,
217 git_servers: &Vec<String>,
218 decoded_nostr_url: &NostrUrlDecoded,
219 grasp_servers: &[String],
220) -> HashMap<String, (HashMap<String, String>, bool)> {
221 let mut remote_states = HashMap::new();
222 let mut errors = HashMap::new();
223 for url in git_servers {
224 let is_grasp_server = is_grasp_server(url, grasp_servers);
225 match list_from_remote(term, git_repo, url, decoded_nostr_url, is_grasp_server) {
226 Err(error) => {
227 errors.insert(url, error);
228 }
229 Ok(state) => {
230 remote_states.insert(url.to_string(), (state, is_grasp_server));
231 }
232 }
233 }
234 remote_states
235}
236
237pub fn list_from_remote(
238 term: &console::Term,
239 git_repo: &Repo,
240 git_server_url: &str,
241 decoded_nostr_url: &NostrUrlDecoded,
242 is_grasp_server: bool,
243) -> Result<HashMap<String, String>> {
244 let server_url = git_server_url.parse::<CloneUrl>()?;
245 let protocols_to_attempt =
246 get_read_protocols_to_try(git_repo, &server_url, decoded_nostr_url, is_grasp_server);
247
248 let mut failed_protocols = vec![];
249 let mut remote_state: Option<HashMap<String, String>> = None;
250
251 for protocol in &protocols_to_attempt {
252 term.write_line(
253 format!(
254 "fetching {} ref list over {protocol}...",
255 server_url.short_name(),
256 )
257 .as_str(),
258 )?;
259
260 let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?;
261 let res = list_from_remote_url(
262 git_repo,
263 &formatted_url,
264 [ServerProtocol::UnauthHttps, ServerProtocol::UnauthHttp].contains(protocol),
265 term,
266 );
267
268 match res {
269 Ok(state) => {
270 remote_state = Some(state);
271 term.clear_last_lines(1)?;
272 if !failed_protocols.is_empty() {
273 term.write_line(
274 format!(
275 "list: succeeded over {protocol} from {}",
276 server_url.short_name(),
277 )
278 .as_str(),
279 )?;
280 let _ =
281 set_protocol_preference(git_repo, protocol, &server_url, &Direction::Fetch);
282 }
283 break;
284 }
285 Err(error) => {
286 term.clear_last_lines(1)?;
287 term.write_line(
288 format!("list: {formatted_url} failed over {protocol}: {error}").as_str(),
289 )?;
290 failed_protocols.push(protocol);
291 }
292 }
293 }
294 if let Some(remote_state) = remote_state {
295 if failed_protocols.is_empty() {
296 term.clear_last_lines(1)?;
297 }
298 Ok(remote_state)
299 } else {
300 let error = anyhow!(
301 "{} failed over {}{}",
302 server_url.short_name(),
303 join_with_and(&failed_protocols),
304 if decoded_nostr_url.protocol.is_some() {
305 " and nostr url contains protocol override so no other protocols were attempted"
306 } else {
307 ""
308 },
309 );
310 term.write_line(format!("list: {error}").as_str())?;
311 Err(error)
312 }
313}
314
315fn list_from_remote_url(
316 git_repo: &Repo,
317 git_server_remote_url: &str,
318 dont_authenticate: bool,
319 term: &console::Term,
320) -> Result<HashMap<String, String>> {
321 let git_config = git_repo.git_repo.config()?;
322
323 let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_remote_url)?;
324 // authentication may be required
325 let auth = GitAuthenticator::default();
326 let mut remote_callbacks = git2::RemoteCallbacks::new();
327 if !dont_authenticate {
328 remote_callbacks.credentials(auth.credentials(&git_config));
329 }
330 term.write_line("list: connecting...")?;
331 git_server_remote.connect_auth(git2::Direction::Fetch, Some(remote_callbacks), None)?;
332 term.clear_last_lines(1)?;
333 let mut state = HashMap::new();
334 for head in git_server_remote.list()? {
335 if let Some(symbolic_reference) = head.symref_target() {
336 state.insert(
337 head.name().to_string(),
338 format!("ref: {symbolic_reference}"),
339 );
340 // ignore dereferenced tags
341 } else if !head.name().to_string().ends_with("^{}") {
342 state.insert(head.name().to_string(), head.oid().to_string());
343 }
344 }
345 git_server_remote.disconnect()?;
346 Ok(state)
347}
348
349fn get_ahead_behind(
350 git_repo: &Repo,
351 base_ref_or_oid: &str,
352 latest_ref_or_oid: &str,
353) -> Result<(Vec<Sha1Hash>, Vec<Sha1Hash>)> {
354 let base = git_repo.get_commit_or_tip_of_reference(base_ref_or_oid)?;
355 let latest = git_repo.get_commit_or_tip_of_reference(latest_ref_or_oid)?;
356 git_repo.get_commits_ahead_behind(&base, &latest)
357}
diff --git a/src/bin/git_remote_nostr/main.rs b/src/bin/git_remote_nostr/main.rs
index cf47a30..aac626b 100644
--- a/src/bin/git_remote_nostr/main.rs
+++ b/src/bin/git_remote_nostr/main.rs
@@ -18,16 +18,15 @@ use ngit::{
18 client::{self, Params}, 18 client::{self, Params},
19 git::{self, utils::set_git_timeout}, 19 git::{self, utils::set_git_timeout},
20 login::existing::load_existing_login, 20 login::existing::load_existing_login,
21 utils::read_line,
21}; 22};
22use nostr::nips::nip19::Nip19Coordinate; 23use nostr::nips::nip19::Nip19Coordinate;
23use utils::read_line;
24 24
25use crate::{client::Client, git::Repo}; 25use crate::{client::Client, git::Repo};
26 26
27mod fetch; 27mod fetch;
28mod list; 28mod list;
29mod push; 29mod push;
30mod utils;
31 30
32#[tokio::main] 31#[tokio::main]
33async fn main() -> Result<()> { 32async fn main() -> Result<()> {
diff --git a/src/bin/git_remote_nostr/push.rs b/src/bin/git_remote_nostr/push.rs
index 909a0ab..4e0d760 100644
--- a/src/bin/git_remote_nostr/push.rs
+++ b/src/bin/git_remote_nostr/push.rs
@@ -2,12 +2,10 @@ use core::str;
2use std::{ 2use std::{
3 collections::{HashMap, HashSet}, 3 collections::{HashMap, HashSet},
4 io::Stdin, 4 io::Stdin,
5 sync::{Arc, Mutex}, 5 sync::Arc,
6 time::Instant,
7}; 6};
8 7
9use anyhow::{Context, Result, anyhow, bail}; 8use anyhow::{Context, Result, bail};
10use auth_git2::GitAuthenticator;
11use client::{get_events_from_local_cache, get_state_from_cache, send_events, sign_event}; 9use client::{get_events_from_local_cache, get_state_from_cache, send_events, sign_event};
12use console::Term; 10use console::Term;
13use git::{RepoActions, sha1_to_oid}; 11use git::{RepoActions, sha1_to_oid};
@@ -17,17 +15,18 @@ use git_events::{
17}; 15};
18use git2::{Oid, Repository}; 16use git2::{Oid, Repository};
19use ngit::{ 17use ngit::{
20 cli_interactor::count_lines_per_msg_vec,
21 client::{self, get_event_from_cache_by_id, sign_draft_event}, 18 client::{self, get_event_from_cache_by_id, sign_draft_event},
22 git::{ 19 git::{self, nostr_url::NostrUrlDecoded},
23 self,
24 nostr_url::{CloneUrl, NostrUrlDecoded},
25 oid_to_shorthand_string,
26 },
27 git_events::{self, KIND_PULL_REQUEST, event_to_cover_letter, get_event_root}, 20 git_events::{self, KIND_PULL_REQUEST, event_to_cover_letter, get_event_root},
21 list::list_from_remotes,
28 login::{self, user::UserRef}, 22 login::{self, user::UserRef},
23 push::{push_to_remote, push_to_remote_url},
29 repo_ref::{self, get_repo_config_from_yaml, is_grasp_server, normalize_grasp_server_url}, 24 repo_ref::{self, get_repo_config_from_yaml, is_grasp_server, normalize_grasp_server_url},
30 repo_state, 25 repo_state,
26 utils::{
27 find_proposal_and_patches_by_branch_name, get_all_proposals, get_remote_name_by_url,
28 get_short_git_server_name, read_line,
29 },
31}; 30};
32use nostr::{event::UnsignedEvent, nips::nip10::Marker}; 31use nostr::{event::UnsignedEvent, nips::nip10::Marker};
33use nostr_sdk::{ 32use nostr_sdk::{
@@ -37,16 +36,7 @@ use nostr_sdk::{
37use repo_ref::RepoRef; 36use repo_ref::RepoRef;
38use repo_state::RepoState; 37use repo_state::RepoState;
39 38
40use crate::{ 39use crate::{client::Client, git::Repo};
41 client::Client,
42 git::Repo,
43 list::list_from_remotes,
44 utils::{
45 Direction, find_proposal_and_patches_by_branch_name, get_all_proposals,
46 get_remote_name_by_url, get_short_git_server_name, get_write_protocols_to_try,
47 join_with_and, read_line, set_protocol_preference,
48 },
49};
50 40
51#[allow(clippy::too_many_lines)] 41#[allow(clippy::too_many_lines)]
52#[allow(clippy::type_complexity)] 42#[allow(clippy::type_complexity)]
@@ -590,295 +580,6 @@ async fn generate_patches_or_pr_event_or_pr_updates(
590 Ok(events) 580 Ok(events)
591} 581}
592 582
593fn push_to_remote(
594 git_repo: &Repo,
595 git_server_url: &str,
596 decoded_nostr_url: &NostrUrlDecoded,
597 remote_refspecs: &[String],
598 term: &Term,
599 is_grasp_server: bool,
600) -> Result<()> {
601 let server_url = git_server_url.parse::<CloneUrl>()?;
602 let protocols_to_attempt =
603 get_write_protocols_to_try(git_repo, &server_url, decoded_nostr_url, is_grasp_server);
604
605 let mut failed_protocols = vec![];
606 let mut success = false;
607
608 for protocol in &protocols_to_attempt {
609 term.write_line(format!("push: {} over {protocol}...", server_url.short_name(),).as_str())?;
610
611 let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?;
612
613 if let Err(error) = push_to_remote_url(git_repo, &formatted_url, remote_refspecs, term) {
614 term.write_line(
615 format!("push: {formatted_url} failed over {protocol}: {error}").as_str(),
616 )?;
617 failed_protocols.push(protocol);
618 } else {
619 success = true;
620 if !failed_protocols.is_empty() {
621 term.write_line(format!("push: succeeded over {protocol}").as_str())?;
622 let _ = set_protocol_preference(git_repo, protocol, &server_url, &Direction::Push);
623 }
624 break;
625 }
626 }
627 if success {
628 Ok(())
629 } else {
630 let error = anyhow!(
631 "{} failed over {}{}",
632 server_url.short_name(),
633 join_with_and(&failed_protocols),
634 if decoded_nostr_url.protocol.is_some() {
635 " and nostr url contains protocol override so no other protocols were attempted"
636 } else {
637 ""
638 },
639 );
640 term.write_line(format!("push: {error}").as_str())?;
641 Err(error)
642 }
643}
644
645fn push_to_remote_url(
646 git_repo: &Repo,
647 git_server_url: &str,
648 remote_refspecs: &[String],
649 term: &Term,
650) -> Result<()> {
651 let git_config = git_repo.git_repo.config()?;
652 let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_url)?;
653 let auth = GitAuthenticator::default();
654 let mut push_options = git2::PushOptions::new();
655 let mut remote_callbacks = git2::RemoteCallbacks::new();
656 let push_reporter = Arc::new(Mutex::new(PushReporter::new(term)));
657
658 remote_callbacks.credentials(auth.credentials(&git_config));
659
660 remote_callbacks.push_update_reference({
661 let push_reporter = Arc::clone(&push_reporter);
662 move |name, error| {
663 let mut reporter = push_reporter.lock().unwrap();
664 if let Some(error) = error {
665 let existing_lines = reporter.count_all_existing_lines();
666 reporter.update_reference_errors.push(format!(
667 "WARNING: {} failed to push {name} error: {error}",
668 get_short_git_server_name(git_repo, git_server_url),
669 ));
670 reporter.write_all(existing_lines);
671 }
672 Ok(())
673 }
674 });
675
676 remote_callbacks.push_negotiation({
677 let push_reporter = Arc::clone(&push_reporter);
678 move |updates| {
679 let mut reporter = push_reporter.lock().unwrap();
680 let existing_lines = reporter.count_all_existing_lines();
681
682 for update in updates {
683 let dst_refname = update
684 .dst_refname()
685 .unwrap_or("")
686 .replace("refs/heads/", "")
687 .replace("refs/tags/", "tags/");
688 let msg = if update.dst().is_zero() {
689 format!("push: - [delete] {dst_refname}")
690 } else if update.src().is_zero() {
691 if update.dst_refname().unwrap_or("").contains("refs/tags") {
692 format!("push: * [new tag] {dst_refname}")
693 } else {
694 format!("push: * [new branch] {dst_refname}")
695 }
696 } else {
697 let force = remote_refspecs
698 .iter()
699 .any(|r| r.contains(&dst_refname) && r.contains('+'));
700 format!(
701 "push: {} {}..{} {} -> {dst_refname}",
702 if force { "+" } else { " " },
703 oid_to_shorthand_string(update.src()).unwrap(),
704 oid_to_shorthand_string(update.dst()).unwrap(),
705 update
706 .src_refname()
707 .unwrap_or("")
708 .replace("refs/heads/", "")
709 .replace("refs/tags/", "tags/"),
710 )
711 };
712 // other possibilities will result in push to fail but better reporting is
713 // needed:
714 // deleting a non-existant branch:
715 // ! [remote rejected] <old-branch-name> -> <old-branch-name> (not found)
716 // adding a branch that already exists:
717 // ! [remote rejected] <new-branch-name> -> <new-branch-name> (already exists)
718 // pushing without non-fast-forward without force:
719 // ! [rejected] <branch-name> -> <branch-name> (non-fast-forward)
720 reporter.negotiation.push(msg);
721 }
722 reporter.write_all(existing_lines);
723 Ok(())
724 }
725 });
726
727 remote_callbacks.push_transfer_progress({
728 let push_reporter = Arc::clone(&push_reporter);
729 #[allow(clippy::cast_precision_loss)]
730 move |current, total, bytes| {
731 let mut reporter = push_reporter.lock().unwrap();
732 reporter.process_transfer_progress_update(current, total, bytes);
733 }
734 });
735
736 remote_callbacks.sideband_progress({
737 let push_reporter = Arc::clone(&push_reporter);
738 move |data| {
739 let mut reporter = push_reporter.lock().unwrap();
740 reporter.process_remote_msg(data);
741 true
742 }
743 });
744 push_options.remote_callbacks(remote_callbacks);
745 git_server_remote.push(remote_refspecs, Some(&mut push_options))?;
746 let _ = git_server_remote.disconnect();
747 Ok(())
748}
749
750#[allow(clippy::cast_precision_loss)]
751#[allow(clippy::float_cmp)]
752#[allow(clippy::needless_pass_by_value)]
753fn report_on_transfer_progress(
754 current: usize,
755 total: usize,
756 bytes: usize,
757 start_time: &Instant,
758 end_time: Option<&Instant>,
759) -> Option<String> {
760 if total == 0 {
761 return None;
762 }
763 let percentage = ((current as f64 / total as f64) * 100.0)
764 // always round down because 100% complete is misleading when its not complete
765 .floor();
766 let (size, unit) = if bytes as f64 >= (1024.0 * 1024.0) {
767 (bytes as f64 / (1024.0 * 1024.0), "MiB")
768 } else {
769 (bytes as f64 / 1024.0, "KiB")
770 };
771 let speed = {
772 let duration = if let Some(end_time) = end_time {
773 (*end_time - *start_time).as_millis() as f64
774 } else {
775 start_time.elapsed().as_millis() as f64
776 };
777
778 if duration > 0.0 {
779 (bytes as f64 / (1024.0 * 1024.0)) / (duration / 1000.0) // Convert bytes to MiB and milliseconds to seconds
780 } else {
781 0.0
782 }
783 };
784
785 Some(format!(
786 "push: Writing objects: {percentage}% ({current}/{total}) {size:.2} {unit} | {speed:.2} MiB/s{}",
787 if current == total { ", done." } else { "" },
788 ))
789}
790
791struct PushReporter<'a> {
792 remote_msgs: Vec<String>,
793 negotiation: Vec<String>,
794 transfer_progress_msgs: Vec<String>,
795 update_reference_errors: Vec<String>,
796 term: &'a console::Term,
797 start_time: Option<Instant>,
798 end_time: Option<Instant>,
799}
800impl<'a> PushReporter<'a> {
801 fn new(term: &'a console::Term) -> Self {
802 Self {
803 remote_msgs: vec![],
804 negotiation: vec![],
805 transfer_progress_msgs: vec![],
806 update_reference_errors: vec![],
807 term,
808 start_time: None,
809 end_time: None,
810 }
811 }
812 fn write_all(&self, lines_to_clear: usize) {
813 let _ = self.term.clear_last_lines(lines_to_clear);
814 for msg in &self.remote_msgs {
815 let _ = self.term.write_line(format!("remote: {msg}").as_str());
816 }
817 for msg in &self.negotiation {
818 let _ = self.term.write_line(msg);
819 }
820 for msg in &self.transfer_progress_msgs {
821 let _ = self.term.write_line(msg);
822 }
823 for msg in &self.update_reference_errors {
824 let _ = self.term.write_line(msg);
825 }
826 }
827
828 fn count_all_existing_lines(&self) -> usize {
829 let width = self.term.size().1;
830 count_lines_per_msg_vec(width, &self.remote_msgs, "remote: ".len())
831 + count_lines_per_msg_vec(width, &self.negotiation, 0)
832 + count_lines_per_msg_vec(width, &self.transfer_progress_msgs, 0)
833 + count_lines_per_msg_vec(width, &self.update_reference_errors, 0)
834 }
835 fn process_remote_msg(&mut self, data: &[u8]) {
836 if let Ok(data) = str::from_utf8(data) {
837 let data = data
838 .split(['\n', '\r'])
839 .map(str::trim)
840 .filter(|line| !line.trim().is_empty())
841 .collect::<Vec<&str>>();
842 for data in data {
843 let existing_lines = self.count_all_existing_lines();
844 let msg = data.to_string();
845 if let Some(last) = self.remote_msgs.last() {
846 if (last.contains('%') && !last.contains("100%"))
847 || last == &msg.replace(", done.", "")
848 {
849 self.remote_msgs.pop();
850 self.remote_msgs.push(msg);
851 } else {
852 self.remote_msgs.push(msg);
853 }
854 } else {
855 self.remote_msgs.push(msg);
856 }
857 self.write_all(existing_lines);
858 }
859 }
860 }
861 fn process_transfer_progress_update(&mut self, current: usize, total: usize, bytes: usize) {
862 if self.start_time.is_none() {
863 self.start_time = Some(Instant::now());
864 }
865 if let Some(report) = report_on_transfer_progress(
866 current,
867 total,
868 bytes,
869 &self.start_time.unwrap(),
870 self.end_time.as_ref(),
871 ) {
872 let existing_lines = self.count_all_existing_lines();
873 if report.contains("100%") {
874 self.end_time = Some(Instant::now());
875 }
876 self.transfer_progress_msgs = vec![report];
877 self.write_all(existing_lines);
878 }
879 }
880}
881
882type HashMapUrlRefspecs = HashMap<String, Vec<String>>; 583type HashMapUrlRefspecs = HashMap<String, Vec<String>>;
883 584
884#[allow(clippy::too_many_lines)] 585#[allow(clippy::too_many_lines)]
diff --git a/src/bin/git_remote_nostr/utils.rs b/src/bin/git_remote_nostr/utils.rs
deleted file mode 100644
index 2cb85bf..0000000
--- a/src/bin/git_remote_nostr/utils.rs
+++ /dev/null
@@ -1,448 +0,0 @@
1use core::str;
2use std::{
3 collections::HashMap,
4 fmt,
5 io::{self, Stdin},
6 str::FromStr,
7};
8
9use anyhow::{Context, Result, bail};
10use git2::Repository;
11use ngit::{
12 client::{
13 get_all_proposal_patch_pr_pr_update_events_from_cache, get_events_from_local_cache,
14 get_proposals_and_revisions_from_cache,
15 },
16 git::{
17 Repo, RepoActions,
18 nostr_url::{CloneUrl, NostrUrlDecoded, ServerProtocol},
19 },
20 git_events::{
21 event_is_revision_root, get_pr_tip_event_or_most_recent_patch_with_ancestors, get_status,
22 is_event_proposal_root_for_branch, status_kinds,
23 },
24 repo_ref::RepoRef,
25};
26use nostr_sdk::{Event, EventId, Kind, PublicKey, Url};
27
28pub fn get_short_git_server_name(git_repo: &Repo, url: &str) -> std::string::String {
29 if let Ok(name) = get_remote_name_by_url(&git_repo.git_repo, url) {
30 return name;
31 }
32 if let Ok(url) = Url::parse(url) {
33 if let Some(domain) = url.domain() {
34 return domain.to_string();
35 }
36 }
37 url.to_string()
38}
39
40pub fn get_remote_name_by_url(git_repo: &Repository, url: &str) -> Result<String> {
41 let remotes = git_repo.remotes()?;
42 Ok(remotes
43 .iter()
44 .find(|r| {
45 if let Some(name) = r {
46 if let Some(remote_url) = git_repo.find_remote(name).unwrap().url() {
47 url == remote_url
48 } else {
49 false
50 }
51 } else {
52 false
53 }
54 })
55 .context("could not find remote with matching url")?
56 .context("remote with matching url must be named")?
57 .to_string())
58}
59
60pub fn get_oids_from_fetch_batch(
61 stdin: &Stdin,
62 initial_oid: &str,
63 initial_refstr: &str,
64) -> Result<HashMap<String, String>> {
65 let mut line = String::new();
66 let mut batch = HashMap::new();
67 batch.insert(initial_refstr.to_string(), initial_oid.to_string());
68 loop {
69 let tokens = read_line(stdin, &mut line)?;
70 match tokens.as_slice() {
71 ["fetch", oid, refstr] => {
72 batch.insert((*refstr).to_string(), (*oid).to_string());
73 }
74 [] => break,
75 _ => bail!(
76 "after a `fetch` command we are only expecting another fetch or an empty line"
77 ),
78 }
79 }
80 Ok(batch)
81}
82
83/// Read one line from stdin, and split it into tokens.
84pub fn read_line<'a>(stdin: &io::Stdin, line: &'a mut String) -> io::Result<Vec<&'a str>> {
85 line.clear();
86
87 let read = stdin.read_line(line)?;
88 if read == 0 {
89 return Ok(vec![]);
90 }
91 let line = line.trim();
92 let tokens = line.split(' ').filter(|t| !t.is_empty()).collect();
93
94 Ok(tokens)
95}
96
97pub async fn get_open_or_draft_proposals(
98 git_repo: &Repo,
99 repo_ref: &RepoRef,
100) -> Result<HashMap<EventId, (Event, Vec<Event>)>> {
101 let git_repo_path = git_repo.get_path()?;
102 let proposals: Vec<nostr::Event> =
103 get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates())
104 .await?
105 .iter()
106 .filter(|e|
107 // If we wanted to treat to list Pull Requests that revise a Patch we would do this:
108 // e.kind.eq(&KIND_PULL_REQUEST) ||
109 !event_is_revision_root(e))
110 .cloned()
111 .collect();
112
113 let statuses: Vec<nostr::Event> = {
114 let mut statuses = get_events_from_local_cache(
115 git_repo_path,
116 vec![
117 nostr::Filter::default()
118 .kinds(status_kinds().clone())
119 .events(proposals.iter().map(|e| e.id)),
120 ],
121 )
122 .await?;
123 statuses.sort_by_key(|e| e.created_at);
124 statuses.reverse();
125 statuses
126 };
127 let mut open_or_draft_proposals = HashMap::new();
128
129 for proposal in &proposals {
130 let status = get_status(proposal, repo_ref, &statuses, &proposals);
131 if [Kind::GitStatusOpen, Kind::GitStatusDraft].contains(&status) {
132 if let Ok(commits_events) = get_all_proposal_patch_pr_pr_update_events_from_cache(
133 git_repo_path,
134 repo_ref,
135 &proposal.id,
136 )
137 .await
138 {
139 if let Ok(most_recent_proposal_patch_chain) =
140 get_pr_tip_event_or_most_recent_patch_with_ancestors(commits_events.clone())
141 {
142 open_or_draft_proposals.insert(
143 proposal.id,
144 (proposal.clone(), most_recent_proposal_patch_chain),
145 );
146 }
147 }
148 }
149 }
150 Ok(open_or_draft_proposals)
151}
152
153pub async fn get_all_proposals(
154 git_repo: &Repo,
155 repo_ref: &RepoRef,
156) -> Result<HashMap<EventId, (Event, Vec<Event>)>> {
157 let git_repo_path = git_repo.get_path()?;
158 let proposals: Vec<nostr::Event> =
159 get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates())
160 .await?
161 .iter()
162 .filter(|e|
163 // If we wanted to treat to list Pull Requests that revise a Patch we would do this:
164 // e.kind.eq(&KIND_PULL_REQUEST) ||
165 !event_is_revision_root(e))
166 .cloned()
167 .collect();
168
169 let mut all_proposals = HashMap::new();
170
171 for proposal in proposals {
172 if let Ok(commits_events) = get_all_proposal_patch_pr_pr_update_events_from_cache(
173 git_repo_path,
174 repo_ref,
175 &proposal.id,
176 )
177 .await
178 {
179 if let Ok(most_recent_proposal_patch_chain) =
180 get_pr_tip_event_or_most_recent_patch_with_ancestors(commits_events.clone())
181 {
182 all_proposals.insert(proposal.id, (proposal, most_recent_proposal_patch_chain));
183 }
184 }
185 }
186 Ok(all_proposals)
187}
188
189pub fn find_proposal_and_patches_by_branch_name<'a>(
190 refstr: &'a str,
191 proposals: &'a HashMap<EventId, (Event, Vec<Event>)>,
192 current_user: Option<&PublicKey>,
193) -> Option<(&'a EventId, &'a (Event, Vec<Event>))> {
194 proposals.iter().find(|(_, (proposal, _))| {
195 is_event_proposal_root_for_branch(proposal, refstr, current_user).unwrap_or(false)
196 })
197}
198
199pub fn join_with_and<T: ToString>(items: &[T]) -> String {
200 match items.len() {
201 0 => String::new(),
202 1 => items[0].to_string(),
203 _ => {
204 let last_item = items.last().unwrap().to_string();
205 let rest = &items[..items.len() - 1];
206 format!(
207 "{} and {}",
208 rest.iter()
209 .map(std::string::ToString::to_string)
210 .collect::<Vec<_>>()
211 .join(", "),
212 last_item
213 )
214 }
215 }
216}
217
218/// get an ordered vector of server protocols to attempt
219pub fn get_read_protocols_to_try(
220 git_repo: &Repo,
221 server_url: &CloneUrl,
222 decoded_nostr_url: &NostrUrlDecoded,
223 is_grasp_server: bool,
224) -> Vec<ServerProtocol> {
225 if is_grasp_server {
226 if server_url.protocol() == ServerProtocol::Http {
227 vec![(ServerProtocol::UnauthHttp)]
228 } else {
229 vec![(ServerProtocol::UnauthHttps)]
230 }
231 } else if server_url.protocol() == ServerProtocol::Filesystem {
232 vec![(ServerProtocol::Filesystem)]
233 } else if let Some(protocol) = &decoded_nostr_url.protocol {
234 vec![protocol.clone()]
235 } else {
236 let mut list = if server_url.protocol() == ServerProtocol::Http {
237 vec![
238 ServerProtocol::UnauthHttp,
239 ServerProtocol::Ssh,
240 // note: list and fetch stop here if ssh was authenticated
241 ServerProtocol::Http,
242 ]
243 } else if server_url.protocol() == ServerProtocol::Ftp {
244 vec![ServerProtocol::Ftp, ServerProtocol::Ssh]
245 } else if server_url.protocol() == ServerProtocol::Git {
246 vec![
247 ServerProtocol::Git,
248 ServerProtocol::UnauthHttps,
249 ServerProtocol::Ssh,
250 // note: list and fetch stop here if ssh was authenticated
251 ServerProtocol::Https,
252 ]
253 } else {
254 vec![
255 ServerProtocol::UnauthHttps,
256 ServerProtocol::Ssh,
257 // note: list and fetch stop here if ssh was authenticated
258 ServerProtocol::Https,
259 ]
260 };
261 if let Some(protocol) = get_protocol_preference(git_repo, server_url, &Direction::Fetch) {
262 if let Some(pos) = list.iter().position(|p| *p == protocol) {
263 list.remove(pos);
264 list.insert(0, protocol);
265 }
266 }
267 list
268 }
269}
270
271/// get an ordered vector of server protocols to attempt
272pub fn get_write_protocols_to_try(
273 git_repo: &Repo,
274 server_url: &CloneUrl,
275 decoded_nostr_url: &NostrUrlDecoded,
276 is_grasp_server: bool,
277) -> Vec<ServerProtocol> {
278 if is_grasp_server {
279 if server_url.protocol() == ServerProtocol::Http {
280 vec![(ServerProtocol::UnauthHttp)]
281 } else {
282 vec![(ServerProtocol::UnauthHttps)]
283 }
284 } else if server_url.protocol() == ServerProtocol::Filesystem {
285 vec![(ServerProtocol::Filesystem)]
286 } else if let Some(protocol) = &decoded_nostr_url.protocol {
287 vec![protocol.clone()]
288 } else {
289 let mut list = if server_url.protocol() == ServerProtocol::Http {
290 vec![
291 ServerProtocol::Ssh,
292 // note: list and fetch stop here if ssh was authenticated
293 ServerProtocol::Http,
294 ]
295 } else if server_url.protocol() == ServerProtocol::Ftp {
296 vec![ServerProtocol::Ssh, ServerProtocol::Ftp]
297 } else if server_url.protocol() == ServerProtocol::Git {
298 vec![
299 ServerProtocol::Ssh,
300 ServerProtocol::Git,
301 // note: list and fetch stop here if ssh was authenticated
302 ServerProtocol::Https,
303 ]
304 } else {
305 vec![
306 ServerProtocol::Ssh,
307 // note: list and fetch stop here if ssh was authenticated
308 ServerProtocol::Https,
309 ]
310 };
311 if let Some(protocol) = get_protocol_preference(git_repo, server_url, &Direction::Push) {
312 if let Some(pos) = list.iter().position(|p| *p == protocol) {
313 list.remove(pos);
314 list.insert(0, protocol);
315 }
316 }
317
318 list
319 }
320}
321
322#[derive(Debug, PartialEq)]
323pub enum Direction {
324 Push,
325 Fetch,
326}
327impl fmt::Display for Direction {
328 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
329 match self {
330 Direction::Push => write!(f, "push"),
331 Direction::Fetch => write!(f, "fetch"),
332 }
333 }
334}
335
336pub fn get_protocol_preference(
337 git_repo: &Repo,
338 server_url: &CloneUrl,
339 direction: &Direction,
340) -> Option<ServerProtocol> {
341 let server_short_name = server_url.short_name();
342 if let Ok(Some(list)) =
343 git_repo.get_git_config_item(format!("nostr.protocol-{direction}").as_str(), Some(false))
344 {
345 for item in list.split(';') {
346 let pair = item.split(',').collect::<Vec<&str>>();
347 if let Some(url) = pair.get(1) {
348 if *url == server_short_name {
349 if let Some(protocol) = pair.first() {
350 if let Ok(protocol) = ServerProtocol::from_str(protocol) {
351 return Some(protocol);
352 }
353 }
354 }
355 }
356 }
357 }
358 None
359}
360
361pub fn set_protocol_preference(
362 git_repo: &Repo,
363 protocol: &ServerProtocol,
364 server_url: &CloneUrl,
365 direction: &Direction,
366) -> Result<()> {
367 let server_short_name = server_url.short_name();
368 let mut new = String::new();
369 if let Some(list) =
370 git_repo.get_git_config_item(format!("nostr.protocol-{direction}").as_str(), Some(false))?
371 {
372 for item in list.split(';') {
373 let pair = item.split(',').collect::<Vec<&str>>();
374 if let Some(url) = pair.get(1) {
375 if *url != server_short_name && !item.is_empty() {
376 new.push_str(format!("{item};").as_str());
377 }
378 }
379 }
380 }
381 new.push_str(format!("{protocol},{server_short_name};").as_str());
382
383 git_repo.save_git_config_item(
384 format!("nostr.protocol-{direction}").as_str(),
385 new.as_str(),
386 false,
387 )
388}
389
390pub fn error_might_be_authentication_related(error: &anyhow::Error) -> bool {
391 let error_str = error.to_string();
392 for s in [
393 "no ssh keys found",
394 "invalid or unknown remote ssh hostkey",
395 "authentication",
396 "Permission",
397 "permission",
398 "not found",
399 ] {
400 if error_str.contains(s) {
401 return true;
402 }
403 }
404 false
405}
406
407#[cfg(test)]
408mod tests {
409 use super::*;
410 mod join_with_and {
411 use super::*;
412 #[test]
413 fn test_empty() {
414 let items: Vec<&str> = vec![];
415 assert_eq!(join_with_and(&items), "");
416 }
417
418 #[test]
419 fn test_single_item() {
420 let items = vec!["apple"];
421 assert_eq!(join_with_and(&items), "apple");
422 }
423
424 #[test]
425 fn test_two_items() {
426 let items = vec!["apple", "banana"];
427 assert_eq!(join_with_and(&items), "apple and banana");
428 }
429
430 #[test]
431 fn test_three_items() {
432 let items = vec!["apple", "banana", "cherry"];
433 assert_eq!(join_with_and(&items), "apple, banana and cherry");
434 }
435
436 #[test]
437 fn test_four_items() {
438 let items = vec!["apple", "banana", "cherry", "date"];
439 assert_eq!(join_with_and(&items), "apple, banana, cherry and date");
440 }
441
442 #[test]
443 fn test_multiple_items() {
444 let items = vec!["one", "two", "three", "four", "five"];
445 assert_eq!(join_with_and(&items), "one, two, three, four and five");
446 }
447 }
448}