upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/bin/ngit/sub_commands/sync.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/ngit/sub_commands/sync.rs')
-rw-r--r--src/bin/ngit/sub_commands/sync.rs43
1 files changed, 42 insertions, 1 deletions
diff --git a/src/bin/ngit/sub_commands/sync.rs b/src/bin/ngit/sub_commands/sync.rs
index 00dfe75..61c13a8 100644
--- a/src/bin/ngit/sub_commands/sync.rs
+++ b/src/bin/ngit/sub_commands/sync.rs
@@ -1,4 +1,4 @@
1use anyhow::{Context, Result}; 1use anyhow::{Context, Result, bail};
2use ngit::{ 2use ngit::{
3 client::{ 3 client::{
4 Client, Connect, Params, fetching_with_report, get_repo_ref_from_cache, 4 Client, Connect, Params, fetching_with_report, get_repo_ref_from_cache,
@@ -13,6 +13,9 @@ use ngit::{
13 13
14#[derive(Debug, clap::Args)] 14#[derive(Debug, clap::Args)]
15pub struct SubCommandArgs { 15pub struct SubCommandArgs {
16 /// optionally just sync a specific reference. eg main or v1.5.2
17 #[clap(short, long)]
18 pub(crate) ref_name: Option<String>,
16 /// force push updates and delete refs from non-grasp git servers 19 /// force push updates and delete refs from non-grasp git servers
17 #[arg(long, action)] 20 #[arg(long, action)]
18 force: bool, 21 force: bool,
@@ -23,6 +26,32 @@ pub async fn launch(args: &SubCommandArgs) -> Result<()> {
23 let git_repo = Repo::discover().context("failed to find a git repository")?; 26 let git_repo = Repo::discover().context("failed to find a git repository")?;
24 let git_repo_path = git_repo.get_path()?; 27 let git_repo_path = git_repo.get_path()?;
25 28
29 let full_ref_name = if let Some(ref_name) = &args.ref_name {
30 if ref_name.starts_with("refs/") {
31 if git_repo.git_repo.find_reference(ref_name).is_ok() {
32 Some(ref_name.clone())
33 } else {
34 bail!("could not find reference {ref_name}");
35 }
36 } else if git_repo
37 .git_repo
38 .find_reference(&format!("refs/tags/{ref_name}"))
39 .is_ok()
40 {
41 Some(format!("refs/tags/{ref_name}"))
42 } else if git_repo
43 .git_repo
44 .find_reference(&format!("refs/heads/{ref_name}"))
45 .is_ok()
46 {
47 Some(format!("refs/heads/{ref_name}"))
48 } else {
49 bail!("could not find reference {ref_name}");
50 }
51 } else {
52 None
53 };
54
26 let client = Client::new(Params::with_git_config_relay_defaults(&Some(&git_repo))); 55 let client = Client::new(Params::with_git_config_relay_defaults(&Some(&git_repo)));
27 56
28 let (nostr_remote_name, decoded_nostr_url) = git_repo 57 let (nostr_remote_name, decoded_nostr_url) = git_repo
@@ -56,6 +85,12 @@ pub async fn launch(args: &SubCommandArgs) -> Result<()> {
56 // delete ref from remote 85 // delete ref from remote
57 let mut not_deleted = vec![]; 86 let mut not_deleted = vec![];
58 for remote_ref_name in remote_state.keys() { 87 for remote_ref_name in remote_state.keys() {
88 // skip unspecified refs
89 if let Some(full_ref_name) = &full_ref_name {
90 if remote_ref_name != full_ref_name {
91 continue;
92 }
93 }
59 if (!remote_ref_name.starts_with("refs/heads/pr/") 94 if (!remote_ref_name.starts_with("refs/heads/pr/")
60 && (remote_ref_name.starts_with("refs/heads/") 95 && (remote_ref_name.starts_with("refs/heads/")
61 || remote_ref_name.starts_with("refs/tags/"))) 96 || remote_ref_name.starts_with("refs/tags/")))
@@ -75,6 +110,12 @@ pub async fn launch(args: &SubCommandArgs) -> Result<()> {
75 // add or update ref on remote 110 // add or update ref on remote
76 let mut not_updated = vec![]; 111 let mut not_updated = vec![];
77 for nostr_ref_name in nostr_state.state.keys() { 112 for nostr_ref_name in nostr_state.state.keys() {
113 // skip unspecified refs
114 if let Some(full_ref_name) = &full_ref_name {
115 if nostr_ref_name != full_ref_name {
116 continue;
117 }
118 }
78 if nostr_ref_name.starts_with("refs/heads/") 119 if nostr_ref_name.starts_with("refs/heads/")
79 || nostr_ref_name.starts_with("refs/tags/") 120 || nostr_ref_name.starts_with("refs/tags/")
80 || !nostr_ref_name.starts_with("refs/heads/pr/") 121 || !nostr_ref_name.starts_with("refs/heads/pr/")