diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-05-23 21:53:35 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-05-23 21:53:35 +0100 |
| commit | a75a1441b7c1cec93ebc0cb796c21360abbc5573 (patch) | |
| tree | 79242e269749fdca294ace312e259c86595ba072 /src/bin/git_remote_nostr/push.rs | |
| parent | de095c6993ed6e99dd22972519e1cdcf4014f7d0 (diff) | |
feat(push): avoid out of sync issues for ngit relay
we need to be careful with git servers with their own permissions so
a ngit user doesn't inadvertantly push changes on top of a another
user who pushed directly to the git server without using the
force flag.
We dont have this problem with ngit-relay so we can always force
push, even if the user didnt as nostr is the authority of state.
Diffstat (limited to 'src/bin/git_remote_nostr/push.rs')
| -rw-r--r-- | src/bin/git_remote_nostr/push.rs | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/src/bin/git_remote_nostr/push.rs b/src/bin/git_remote_nostr/push.rs index a34d729..c4953f8 100644 --- a/src/bin/git_remote_nostr/push.rs +++ b/src/bin/git_remote_nostr/push.rs | |||
| @@ -50,13 +50,14 @@ use crate::{ | |||
| 50 | }; | 50 | }; |
| 51 | 51 | ||
| 52 | #[allow(clippy::too_many_lines)] | 52 | #[allow(clippy::too_many_lines)] |
| 53 | #[allow(clippy::type_complexity)] | ||
| 53 | pub async fn run_push( | 54 | pub async fn run_push( |
| 54 | git_repo: &Repo, | 55 | git_repo: &Repo, |
| 55 | repo_ref: &RepoRef, | 56 | repo_ref: &RepoRef, |
| 56 | stdin: &Stdin, | 57 | stdin: &Stdin, |
| 57 | initial_refspec: &str, | 58 | initial_refspec: &str, |
| 58 | client: &Client, | 59 | client: &Client, |
| 59 | list_outputs: Option<HashMap<String, HashMap<String, String>>>, | 60 | list_outputs: Option<HashMap<String, (HashMap<String, String>, bool)>>, |
| 60 | ) -> Result<()> { | 61 | ) -> Result<()> { |
| 61 | let refspecs = get_refspecs_from_push_batch(stdin, initial_refspec)?; | 62 | let refspecs = get_refspecs_from_push_batch(stdin, initial_refspec)?; |
| 62 | 63 | ||
| @@ -93,7 +94,8 @@ pub async fn run_push( | |||
| 93 | .iter() | 94 | .iter() |
| 94 | .find(|&url| list_outputs.contains_key(url)) | 95 | .find(|&url| list_outputs.contains_key(url)) |
| 95 | { | 96 | { |
| 96 | list_outputs.get(url).unwrap().to_owned() | 97 | let (state, _is_ngit_relay) = list_outputs.get(url).unwrap().to_owned(); |
| 98 | state | ||
| 97 | } else { | 99 | } else { |
| 98 | bail!( | 100 | bail!( |
| 99 | "failed to connect to git servers: {}", | 101 | "failed to connect to git servers: {}", |
| @@ -706,13 +708,14 @@ fn create_rejected_refspecs_and_remotes_refspecs( | |||
| 706 | git_repo: &Repo, | 708 | git_repo: &Repo, |
| 707 | refspecs: &Vec<String>, | 709 | refspecs: &Vec<String>, |
| 708 | nostr_state: &HashMap<String, String>, | 710 | nostr_state: &HashMap<String, String>, |
| 709 | list_outputs: &HashMap<String, HashMap<String, String>>, | 711 | list_outputs: &HashMap<String, (HashMap<String, String>, bool)>, |
| 710 | ) -> Result<(HashMapUrlRefspecs, HashMapUrlRefspecs)> { | 712 | ) -> Result<(HashMapUrlRefspecs, HashMapUrlRefspecs)> { |
| 711 | let mut refspecs_for_remotes = HashMap::new(); | 713 | let mut refspecs_for_remotes = HashMap::new(); |
| 712 | 714 | ||
| 713 | let mut rejected_refspecs: HashMapUrlRefspecs = HashMap::new(); | 715 | let mut rejected_refspecs: HashMapUrlRefspecs = HashMap::new(); |
| 714 | 716 | ||
| 715 | for (url, remote_state) in list_outputs { | 717 | for (url, (remote_state, is_ngit_relay)) in list_outputs { |
| 718 | let is_ngit_relay = is_ngit_relay.to_owned(); | ||
| 716 | let short_name = get_short_git_server_name(git_repo, url); | 719 | let short_name = get_short_git_server_name(git_repo, url); |
| 717 | let mut refspecs_for_remote = vec![]; | 720 | let mut refspecs_for_remote = vec![]; |
| 718 | for refspec in refspecs { | 721 | for refspec in refspecs { |
| @@ -747,12 +750,7 @@ fn create_rejected_refspecs_and_remotes_refspecs( | |||
| 747 | if is_remote_tip_ancestor_of_commit { | 750 | if is_remote_tip_ancestor_of_commit { |
| 748 | refspecs_for_remote.push(refspec.clone()); | 751 | refspecs_for_remote.push(refspec.clone()); |
| 749 | } else { | 752 | } else { |
| 750 | // this is a force push so we need to force push to git server too | 753 | refspecs_for_remote.push(ensure_force_push_refspec(refspec)); |
| 751 | if refspec.starts_with('+') { | ||
| 752 | refspecs_for_remote.push(refspec.clone()); | ||
| 753 | } else { | ||
| 754 | refspecs_for_remote.push(format!("+{refspec}")); | ||
| 755 | } | ||
| 756 | } | 754 | } |
| 757 | } else if let Ok(remote_value_tip) = | 755 | } else if let Ok(remote_value_tip) = |
| 758 | git_repo.get_commit_or_tip_of_reference(remote_value) | 756 | git_repo.get_commit_or_tip_of_reference(remote_value) |
| @@ -778,6 +776,9 @@ fn create_rejected_refspecs_and_remotes_refspecs( | |||
| 778 | if ahead_of_nostr.is_empty() { | 776 | if ahead_of_nostr.is_empty() { |
| 779 | // ancestor of nostr and we are force pushing anyway... | 777 | // ancestor of nostr and we are force pushing anyway... |
| 780 | refspecs_for_remote.push(refspec.clone()); | 778 | refspecs_for_remote.push(refspec.clone()); |
| 779 | } else if is_ngit_relay { | ||
| 780 | // an ngit-relay can only be pushed to via nostr so can force push | ||
| 781 | refspecs_for_remote.push(ensure_force_push_refspec(refspec)); | ||
| 781 | } else { | 782 | } else { |
| 782 | rejected_refspecs | 783 | rejected_refspecs |
| 783 | .entry(refspec.to_string()) | 784 | .entry(refspec.to_string()) |
| @@ -794,6 +795,8 @@ fn create_rejected_refspecs_and_remotes_refspecs( | |||
| 794 | )?; | 795 | )?; |
| 795 | } | 796 | } |
| 796 | } | 797 | } |
| 798 | } else if is_ngit_relay { | ||
| 799 | refspecs_for_remote.push(ensure_force_push_refspec(refspec)); | ||
| 797 | } else { | 800 | } else { |
| 798 | // remote_value oid is not present locally | 801 | // remote_value oid is not present locally |
| 799 | // TODO can we download the remote reference? | 802 | // TODO can we download the remote reference? |
| @@ -827,6 +830,8 @@ fn create_rejected_refspecs_and_remotes_refspecs( | |||
| 827 | if ahead.is_empty() { | 830 | if ahead.is_empty() { |
| 828 | // can soft push | 831 | // can soft push |
| 829 | refspecs_for_remote.push(refspec.clone()); | 832 | refspecs_for_remote.push(refspec.clone()); |
| 833 | } else if is_ngit_relay { | ||
| 834 | refspecs_for_remote.push(ensure_force_push_refspec(refspec)); | ||
| 830 | } else { | 835 | } else { |
| 831 | // cant soft push | 836 | // cant soft push |
| 832 | rejected_refspecs | 837 | rejected_refspecs |
| @@ -841,6 +846,8 @@ fn create_rejected_refspecs_and_remotes_refspecs( | |||
| 841 | ).as_str(), | 846 | ).as_str(), |
| 842 | )?; | 847 | )?; |
| 843 | } | 848 | } |
| 849 | } else if is_ngit_relay { | ||
| 850 | refspecs_for_remote.push(ensure_force_push_refspec(refspec)); | ||
| 844 | } else { | 851 | } else { |
| 845 | // havn't fetched oid from remote | 852 | // havn't fetched oid from remote |
| 846 | // TODO fetch oid from remote | 853 | // TODO fetch oid from remote |
| @@ -878,6 +885,15 @@ fn create_rejected_refspecs_and_remotes_refspecs( | |||
| 878 | Ok((rejected_refspecs, remotes_refspecs_without_rejected)) | 885 | Ok((rejected_refspecs, remotes_refspecs_without_rejected)) |
| 879 | } | 886 | } |
| 880 | 887 | ||
| 888 | fn ensure_force_push_refspec(refspec: &str) -> String { | ||
| 889 | // Check if the refspec starts with '+' or ':' | ||
| 890 | if refspec.starts_with('+') || refspec.starts_with(':') { | ||
| 891 | refspec.to_string() // Return as is | ||
| 892 | } else { | ||
| 893 | format!("+{refspec}") // Add '+' prefix | ||
| 894 | } | ||
| 895 | } | ||
| 896 | |||
| 881 | fn generate_updated_state( | 897 | fn generate_updated_state( |
| 882 | git_repo: &Repo, | 898 | git_repo: &Repo, |
| 883 | existing_state: &HashMap<String, String>, | 899 | existing_state: &HashMap<String, String>, |