Skip to content

Commit

Permalink
feat(NostrUrlDecode): add nip05 address support
Browse files Browse the repository at this point in the history
enable  nostr git url format

alongside  and

 format

Merge branch 'pr/nip05-lez(ff1845c0)'
  • Loading branch information
DanConwayDev committed Dec 12, 2024
2 parents d8f4f76 + 2c6e281 commit 2b49a54
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 72 deletions.
12 changes: 6 additions & 6 deletions src/bin/git_remote_nostr/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::{
collections::HashSet,
env, io,
path::{Path, PathBuf},
str::FromStr,
};

use anyhow::{bail, Context, Result};
Expand All @@ -28,7 +27,7 @@ mod utils;

#[tokio::main]
async fn main() -> Result<()> {
let Some((decoded_nostr_url, git_repo)) = process_args()? else {
let Some((decoded_nostr_url, git_repo)) = process_args().await? else {
return Ok(());
};

Expand Down Expand Up @@ -118,7 +117,7 @@ async fn main() -> Result<()> {
}
}

fn process_args() -> Result<Option<(NostrUrlDecoded, Repo)>> {
async fn process_args() -> Result<Option<(NostrUrlDecoded, Repo)>> {
let args = env::args();
let args = args.skip(1).take(2).collect::<Vec<_>>();

Expand All @@ -144,13 +143,14 @@ fn process_args() -> Result<Option<(NostrUrlDecoded, Repo)>> {
return Ok(None);
};

let decoded_nostr_url =
NostrUrlDecoded::from_str(nostr_remote_url).context("invalid nostr url")?;

let git_repo = Repo::from_path(&PathBuf::from(
std::env::var("GIT_DIR").context("git should set GIT_DIR when remote helper is called")?,
))?;

let decoded_nostr_url = NostrUrlDecoded::parse_and_resolve(nostr_remote_url, &Some(&git_repo))
.await
.context("invalid nostr url")?;

Ok(Some((decoded_nostr_url, git_repo)))
}

Expand Down
16 changes: 9 additions & 7 deletions src/bin/ngit/sub_commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, str::FromStr};
use std::collections::HashMap;

use anyhow::{Context, Result};
use console::Style;
Expand Down Expand Up @@ -443,7 +443,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
.map(std::string::ToString::to_string)
.collect::<Vec<String>>();

prompt_to_set_nostr_url_as_origin(&repo_ref, &git_repo)?;
prompt_to_set_nostr_url_as_origin(&repo_ref, &git_repo).await?;

// TODO: if no state event exists and there is currently a remote called
// "origin", automtically push rather than waiting for the next commit
Expand Down Expand Up @@ -484,7 +484,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
Ok(())
}

fn prompt_to_set_nostr_url_as_origin(repo_ref: &RepoRef, git_repo: &Repo) -> Result<()> {
async fn prompt_to_set_nostr_url_as_origin(repo_ref: &RepoRef, git_repo: &Repo) -> Result<()> {
println!(
"starting from your next commit, when you `git push` to a remote that uses your nostr url, it will store your repository state on nostr and update the state of the git server(s) you just listed."
);
Expand All @@ -494,7 +494,9 @@ fn prompt_to_set_nostr_url_as_origin(repo_ref: &RepoRef, git_repo: &Repo) -> Res

if let Ok(origin_remote) = git_repo.git_repo.find_remote("origin") {
if let Some(origin_url) = origin_remote.url() {
if let Ok(nostr_url) = NostrUrlDecoded::from_str(origin_url) {
if let Ok(nostr_url) =
NostrUrlDecoded::parse_and_resolve(origin_url, &Some(git_repo)).await
{
if nostr_url.coordinate.identifier == repo_ref.identifier {
if nostr_url.coordinate.public_key == repo_ref.trusted_maintainer {
return Ok(());
Expand All @@ -521,7 +523,7 @@ fn prompt_to_set_nostr_url_as_origin(repo_ref: &RepoRef, git_repo: &Repo) -> Res
}
}
println!("contributors can clone your repository by installing ngit and using this clone url:");
println!("{}", repo_ref.to_nostr_git_url());
println!("{}", repo_ref.to_nostr_git_url(&Some(git_repo)));

Ok(())
}
Expand All @@ -534,7 +536,7 @@ fn ask_to_set_origin_remote(repo_ref: &RepoRef, git_repo: &Repo) -> Result<()> {
)? {
git_repo
.git_repo
.remote_set_url("origin", &repo_ref.to_nostr_git_url())?;
.remote_set_url("origin", &repo_ref.to_nostr_git_url(&Some(git_repo)))?;
}
Ok(())
}
Expand All @@ -547,7 +549,7 @@ fn ask_to_create_new_origin_remote(repo_ref: &RepoRef, git_repo: &Repo) -> Resul
)? {
git_repo
.git_repo
.remote("origin", &repo_ref.to_nostr_git_url())?;
.remote("origin", &repo_ref.to_nostr_git_url(&Some(git_repo)))?;
}
Ok(())
}
Loading

0 comments on commit 2b49a54

Please sign in to comment.