Skip to content

Commit

Permalink
refactor: use nostr url from repo_ref
Browse files Browse the repository at this point in the history
simplify to allow the removal of warning:
`#[allow(clippy::too_many_arguments)]`
  • Loading branch information
DanConwayDev committed Dec 20, 2024
1 parent cc87576 commit 8cc4a21
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 96 deletions.
3 changes: 1 addition & 2 deletions src/bin/git_remote_nostr/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use crate::utils::{
pub async fn run_fetch(
git_repo: &Repo,
repo_ref: &RepoRef,
decoded_nostr_url: &NostrUrlDecoded,
stdin: &Stdin,
oid: &str,
refstr: &str,
Expand All @@ -54,7 +53,7 @@ pub async fn run_fetch(
git_repo,
&oids_from_git_servers,
git_server_url,
decoded_nostr_url,
&repo_ref.to_nostr_git_url(&None),
&term,
) {
errors.push(error);
Expand Down
14 changes: 8 additions & 6 deletions src/bin/git_remote_nostr/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use crate::{
pub async fn run_list(
git_repo: &Repo,
repo_ref: &RepoRef,
decoded_nostr_url: &NostrUrlDecoded,
for_push: bool,
) -> Result<HashMap<String, HashMap<String, String>>> {
let nostr_state =
Expand All @@ -43,7 +42,12 @@ pub async fn run_list(

let term = console::Term::stderr();

let remote_states = list_from_remotes(&term, git_repo, &repo_ref.git_server, decoded_nostr_url);
let remote_states = list_from_remotes(
&term,
git_repo,
&repo_ref.git_server,
&repo_ref.to_nostr_git_url(&None),
);

let mut state = if let Some(nostr_state) = nostr_state {
for (name, value) in &nostr_state.state {
Expand Down Expand Up @@ -89,8 +93,7 @@ pub async fn run_list(
state.retain(|k, _| !k.starts_with("refs/heads/pr/"));

let proposals_state =
get_open_proposals_state(&term, git_repo, repo_ref, decoded_nostr_url, &remote_states)
.await?;
get_open_proposals_state(&term, git_repo, repo_ref, &remote_states).await?;

state.extend(proposals_state);

Expand All @@ -114,7 +117,6 @@ async fn get_open_proposals_state(
term: &console::Term,
git_repo: &Repo,
repo_ref: &RepoRef,
decoded_nostr_url: &NostrUrlDecoded,
remote_states: &HashMap<String, HashMap<String, String>>,
) -> Result<HashMap<String, String>> {
// we cannot use commit_id in the latest patch in a proposal because:
Expand All @@ -133,7 +135,7 @@ async fn get_open_proposals_state(
.cloned()
.collect::<Vec<String>>(),
git_server_url,
decoded_nostr_url,
&repo_ref.to_nostr_git_url(&None),
term,
)
.is_ok()
Expand Down
21 changes: 6 additions & 15 deletions src/bin/git_remote_nostr/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ async fn main() -> Result<()> {

fetching_with_report_for_helper(git_repo_path, &client, &decoded_nostr_url.coordinate).await?;

let repo_ref =
let mut repo_ref =
get_repo_ref_from_cache(Some(git_repo_path), &decoded_nostr_url.coordinate).await?;

repo_ref.set_nostr_git_url(decoded_nostr_url.clone());

let stdin = io::stdin();
let mut line = String::new();

Expand All @@ -77,21 +79,12 @@ async fn main() -> Result<()> {
println!("unsupported");
}
["fetch", oid, refstr] => {
fetch::run_fetch(
&git_repo,
&repo_ref,
&decoded_nostr_url,
&stdin,
oid,
refstr,
)
.await?;
fetch::run_fetch(&git_repo, &repo_ref, &stdin, oid, refstr).await?;
}
["push", refspec] => {
push::run_push(
&git_repo,
&repo_ref,
&decoded_nostr_url,
&stdin,
refspec,
&client,
Expand All @@ -100,12 +93,10 @@ async fn main() -> Result<()> {
.await?;
}
["list"] => {
list_outputs =
Some(list::run_list(&git_repo, &repo_ref, &decoded_nostr_url, false).await?);
list_outputs = Some(list::run_list(&git_repo, &repo_ref, false).await?);
}
["list", "for-push"] => {
list_outputs =
Some(list::run_list(&git_repo, &repo_ref, &decoded_nostr_url, true).await?);
list_outputs = Some(list::run_list(&git_repo, &repo_ref, true).await?);
}
[] => {
return Ok(());
Expand Down
109 changes: 52 additions & 57 deletions src/bin/git_remote_nostr/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ use crate::{
pub async fn run_push(
git_repo: &Repo,
repo_ref: &RepoRef,
decoded_nostr_url: &NostrUrlDecoded,
stdin: &Stdin,
initial_refspec: &str,
client: &Client,
Expand All @@ -75,16 +74,18 @@ pub async fn run_push(

let term = console::Term::stderr();

let list_outputs = match list_outputs {
Some(outputs) => outputs,
_ => list_from_remotes(&term, git_repo, &repo_ref.git_server, decoded_nostr_url),
};

let nostr_state = get_state_from_cache(Some(git_repo.get_path()?), repo_ref).await;
let list_outputs = list_outputs.unwrap_or_else(|| {
list_from_remotes(
&term,
git_repo,
&repo_ref.git_server,
&repo_ref.to_nostr_git_url(&None),
)
});

let existing_state = {
// if no state events - create from first git server listed
if let Ok(nostr_state) = &nostr_state {
if let Ok(nostr_state) = &get_state_from_cache(Some(git_repo.get_path()?), repo_ref).await {
nostr_state.state.clone()
} else if let Some(url) = repo_ref
.git_server
Expand Down Expand Up @@ -118,55 +119,51 @@ pub async fn run_push(
}
});

if git_server_refspecs.is_empty() && proposal_refspecs.is_empty() {
// all refspecs rejected
println!();
return Ok(());
}

let (rejected_proposal_refspecs, rejected) = create_and_publish_events(
git_repo,
repo_ref,
decoded_nostr_url,
&git_server_refspecs,
&proposal_refspecs,
client,
existing_state,
&term,
)
.await?;
// all refspecs aren't rejected
if !(git_server_refspecs.is_empty() && proposal_refspecs.is_empty()) {
let (rejected_proposal_refspecs, rejected) = create_and_publish_events(
git_repo,
repo_ref,
&git_server_refspecs,
&proposal_refspecs,
client,
existing_state,
&term,
)
.await?;

if !rejected {
for refspec in &[git_server_refspecs.clone(), proposal_refspecs.clone()].concat() {
if rejected_proposal_refspecs.contains(refspec) {
continue;
if !rejected {
for refspec in git_server_refspecs.iter().chain(proposal_refspecs.iter()) {
if rejected_proposal_refspecs.contains(refspec) {
continue;
}
let (_, to) = refspec_to_from_to(refspec)?;
println!("ok {to}");
update_remote_refs_pushed(
&git_repo.git_repo,
refspec,
&repo_ref.to_nostr_git_url(&None).to_string(),
)
.context("could not update remote_ref locally")?;
}
let (_, to) = refspec_to_from_to(refspec)?;
println!("ok {to}");
update_remote_refs_pushed(
&git_repo.git_repo,
refspec,
&decoded_nostr_url.original_string,
)
.context("could not update remote_ref locally")?;
}

// TODO make async - check gitlib2 callbacks work async
// TODO make async - check gitlib2 callbacks work async

for (git_server_url, remote_refspecs) in remote_refspecs {
let remote_refspecs = remote_refspecs
.iter()
.filter(|refspec| git_server_refspecs.contains(refspec))
.cloned()
.collect::<Vec<String>>();
if !refspecs.is_empty() {
let _ = push_to_remote(
git_repo,
&git_server_url,
decoded_nostr_url,
&remote_refspecs,
&term,
);
for (git_server_url, remote_refspecs) in remote_refspecs {
let remote_refspecs = remote_refspecs
.iter()
.filter(|refspec| git_server_refspecs.contains(refspec))
.cloned()
.collect::<Vec<String>>();
if !refspecs.is_empty() {
let _ = push_to_remote(
git_repo,
&git_server_url,
&repo_ref.to_nostr_git_url(&None),
&remote_refspecs,
&term,
);
}
}
}
}
Expand All @@ -175,11 +172,9 @@ pub async fn run_push(
Ok(())
}

#[allow(clippy::too_many_arguments)]
async fn create_and_publish_events(
git_repo: &Repo,
repo_ref: &RepoRef,
decoded_nostr_url: &NostrUrlDecoded,
git_server_refspecs: &Vec<String>,
proposal_refspecs: &Vec<String>,
client: &Client,
Expand Down Expand Up @@ -222,7 +217,7 @@ async fn create_and_publish_events(

for event in get_merged_status_events(
term,
decoded_nostr_url,
&repo_ref.to_nostr_git_url(&None),
repo_ref,
git_repo,
&signer,
Expand All @@ -235,7 +230,7 @@ async fn create_and_publish_events(

if let Ok(Some(repo_ref_event)) = get_maintainers_yaml_update(
term,
decoded_nostr_url,
&repo_ref.to_nostr_git_url(&None),
repo_ref,
git_repo,
&signer,
Expand Down
14 changes: 8 additions & 6 deletions src/bin/ngit/sub_commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,10 @@ fn ask_to_set_origin_remote(repo_ref: &RepoRef, git_repo: &Repo) -> Result<()> {
.with_default(true)
.with_prompt("set remote \"origin\" to the nostr url of your repository?"),
)? {
git_repo
.git_repo
.remote_set_url("origin", &repo_ref.to_nostr_git_url(&Some(git_repo)))?;
git_repo.git_repo.remote_set_url(
"origin",
&repo_ref.to_nostr_git_url(&Some(git_repo)).to_string(),
)?;
}
Ok(())
}
Expand All @@ -609,9 +610,10 @@ fn ask_to_create_new_origin_remote(repo_ref: &RepoRef, git_repo: &Repo) -> Resul
.with_default(true)
.with_prompt("set remote \"origin\" to the nostr url of your repository?"),
)? {
git_repo
.git_repo
.remote("origin", &repo_ref.to_nostr_git_url(&Some(git_repo)))?;
git_repo.git_repo.remote(
"origin",
&repo_ref.to_nostr_git_url(&Some(git_repo)).to_string(),
)?;
}
Ok(())
}
3 changes: 3 additions & 0 deletions src/lib/git/nostr_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ pub struct NostrUrlDecoded {

impl fmt::Display for NostrUrlDecoded {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if !self.original_string.is_empty() {
return write!(f, "{}", self.original_string);
}
write!(f, "nostr://")?;
if let Some(user) = &self.user {
write!(f, "{}@", user)?;
Expand Down
16 changes: 6 additions & 10 deletions src/lib/repo_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ impl RepoRef {
self.nostr_git_url = Some(nostr_git_url)
}

pub fn to_nostr_git_url(&self, git_repo: &Option<&Repo>) -> String {
pub fn to_nostr_git_url(&self, git_repo: &Option<&Repo>) -> NostrUrlDecoded {
if let Some(nostr_git_url) = &self.nostr_git_url {
return nostr_git_url.original_string.clone();
return nostr_git_url.clone();
}
let c = self.coordinate_with_hint();
format!("{}", NostrUrlDecoded {
NostrUrlDecoded {
original_string: String::new(),
nip05: use_nip05_git_config_cache_to_find_nip05_from_public_key(
&c.public_key,
Expand All @@ -256,7 +256,7 @@ impl RepoRef {
coordinate: c,
protocol: None,
user: None,
})
}
}
}

Expand Down Expand Up @@ -460,12 +460,8 @@ fn set_or_create_git_remote_with_nostr_url(
repo_ref: &RepoRef,
git_repo: &Repo,
) -> Result<()> {
let url = repo_ref.to_nostr_git_url(&Some(git_repo));
if git_repo
.git_repo
.remote_set_url(name, &repo_ref.to_nostr_git_url(&Some(git_repo)))
.is_err()
{
let url = repo_ref.to_nostr_git_url(&Some(git_repo)).to_string();
if git_repo.git_repo.remote_set_url(name, &url).is_err() {
git_repo.git_repo.remote(name, &url)?;
}
eprintln!("set git remote \"{name}\" to {url}");
Expand Down

0 comments on commit 8cc4a21

Please sign in to comment.