upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-12-20 09:59:14 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2024-12-20 10:17:27 +0000
commit316f858f223162408cfd52183ef7645828c2f480 (patch)
tree4f275320ad776ed4fcc85a1d75b2ac2c46c63609 /src/bin
parent8cc4a21061433c9a29e36c4f33e4d84bea1defee (diff)
refactor: branch_name handling
improve clarity by renaming variables and methods defend against `branch-name` tag with an unsafe name
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/git_remote_nostr/list.rs4
-rw-r--r--src/bin/git_remote_nostr/push.rs6
-rw-r--r--src/bin/git_remote_nostr/utils.rs4
-rw-r--r--src/bin/ngit/sub_commands/list.rs53
4 files changed, 43 insertions, 24 deletions
diff --git a/src/bin/git_remote_nostr/list.rs b/src/bin/git_remote_nostr/list.rs
index 07c6f59..f361272 100644
--- a/src/bin/git_remote_nostr/list.rs
+++ b/src/bin/git_remote_nostr/list.rs
@@ -149,10 +149,10 @@ async fn get_open_proposals_state(
149 let current_user = get_curent_user(git_repo)?; 149 let current_user = get_curent_user(git_repo)?;
150 for (_, (proposal, patches)) in open_proposals { 150 for (_, (proposal, patches)) in open_proposals {
151 if let Ok(cl) = event_to_cover_letter(&proposal) { 151 if let Ok(cl) = event_to_cover_letter(&proposal) {
152 if let Ok(mut branch_name) = cl.get_branch_name() { 152 if let Ok(mut branch_name) = cl.get_branch_name_with_pr_prefix_and_shorthand_id() {
153 branch_name = if let Some(public_key) = current_user { 153 branch_name = if let Some(public_key) = current_user {
154 if proposal.pubkey.eq(&public_key) { 154 if proposal.pubkey.eq(&public_key) {
155 format!("pr/{}", cl.branch_name) 155 format!("pr/{}", cl.branch_name_without_id_or_prefix)
156 } else { 156 } else {
157 branch_name 157 branch_name
158 } 158 }
diff --git a/src/bin/git_remote_nostr/push.rs b/src/bin/git_remote_nostr/push.rs
index 05c8fc2..922808c 100644
--- a/src/bin/git_remote_nostr/push.rs
+++ b/src/bin/git_remote_nostr/push.rs
@@ -1184,7 +1184,8 @@ async fn create_merge_events(
1184 term.write_line( 1184 term.write_line(
1185 format!( 1185 format!(
1186 "applied commits from proposal: create nostr proposal status event for {}", 1186 "applied commits from proposal: create nostr proposal status event for {}",
1187 event_to_cover_letter(&proposal)?.get_branch_name()?, 1187 event_to_cover_letter(&proposal)?
1188 .get_branch_name_with_pr_prefix_and_shorthand_id()?,
1188 ) 1189 )
1189 .as_str(), 1190 .as_str(),
1190 )?; 1191 )?;
@@ -1192,7 +1193,8 @@ async fn create_merge_events(
1192 term.write_line( 1193 term.write_line(
1193 format!( 1194 format!(
1194 "fast-forward merge: create nostr proposal status event for {}", 1195 "fast-forward merge: create nostr proposal status event for {}",
1195 event_to_cover_letter(&proposal)?.get_branch_name()?, 1196 event_to_cover_letter(&proposal)?
1197 .get_branch_name_with_pr_prefix_and_shorthand_id()?,
1196 ) 1198 )
1197 .as_str(), 1199 .as_str(),
1198 )?; 1200 )?;
diff --git a/src/bin/git_remote_nostr/utils.rs b/src/bin/git_remote_nostr/utils.rs
index 15ebb10..316fedb 100644
--- a/src/bin/git_remote_nostr/utils.rs
+++ b/src/bin/git_remote_nostr/utils.rs
@@ -184,10 +184,10 @@ pub async fn get_all_proposals(
184 184
185pub fn find_proposal_and_patches_by_branch_name<'a>( 185pub fn find_proposal_and_patches_by_branch_name<'a>(
186 refstr: &'a str, 186 refstr: &'a str,
187 open_proposals: &'a HashMap<EventId, (Event, Vec<Event>)>, 187 proposals: &'a HashMap<EventId, (Event, Vec<Event>)>,
188 current_user: Option<&PublicKey>, 188 current_user: Option<&PublicKey>,
189) -> Option<(&'a EventId, &'a (Event, Vec<Event>))> { 189) -> Option<(&'a EventId, &'a (Event, Vec<Event>))> {
190 open_proposals.iter().find(|(_, (proposal, _))| { 190 proposals.iter().find(|(_, (proposal, _))| {
191 is_event_proposal_root_for_branch(proposal, refstr, current_user).unwrap_or(false) 191 is_event_proposal_root_for_branch(proposal, refstr, current_user).unwrap_or(false)
192 }) 192 })
193} 193}
diff --git a/src/bin/ngit/sub_commands/list.rs b/src/bin/ngit/sub_commands/list.rs
index 9b84c1b..e8d2e97 100644
--- a/src/bin/ngit/sub_commands/list.rs
+++ b/src/bin/ngit/sub_commands/list.rs
@@ -261,11 +261,15 @@ pub async fn launch() -> Result<()> {
261 .get_local_branch_names() 261 .get_local_branch_names()
262 .context("gitlib2 will not show a list of local branch names")? 262 .context("gitlib2 will not show a list of local branch names")?
263 .iter() 263 .iter()
264 .any(|n| n.eq(&cover_letter.get_branch_name().unwrap())); 264 .any(|n| {
265 n.eq(&cover_letter
266 .get_branch_name_with_pr_prefix_and_shorthand_id()
267 .unwrap())
268 });
265 269
266 let checked_out_proposal_branch = git_repo 270 let checked_out_proposal_branch = git_repo
267 .get_checked_out_branch_name()? 271 .get_checked_out_branch_name()?
268 .eq(&cover_letter.get_branch_name()?); 272 .eq(&cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?);
269 273
270 let proposal_base_commit = str_to_sha1(&tag_value( 274 let proposal_base_commit = str_to_sha1(&tag_value(
271 most_recent_proposal_patch_chain.last().context( 275 most_recent_proposal_patch_chain.last().context(
@@ -327,14 +331,14 @@ pub async fn launch() -> Result<()> {
327 check_clean(&git_repo)?; 331 check_clean(&git_repo)?;
328 let _ = git_repo 332 let _ = git_repo
329 .apply_patch_chain( 333 .apply_patch_chain(
330 &cover_letter.get_branch_name()?, 334 &cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?,
331 most_recent_proposal_patch_chain, 335 most_recent_proposal_patch_chain,
332 ) 336 )
333 .context("failed to apply patch chain")?; 337 .context("failed to apply patch chain")?;
334 338
335 println!( 339 println!(
336 "checked out proposal as '{}' branch", 340 "checked out proposal as '{}' branch",
337 cover_letter.get_branch_name()? 341 cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?
338 ); 342 );
339 Ok(()) 343 Ok(())
340 } 344 }
@@ -347,7 +351,8 @@ pub async fn launch() -> Result<()> {
347 }; 351 };
348 } 352 }
349 353
350 let local_branch_tip = git_repo.get_tip_of_branch(&cover_letter.get_branch_name()?)?; 354 let local_branch_tip = git_repo
355 .get_tip_of_branch(&cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?)?;
351 356
352 // up-to-date 357 // up-to-date
353 if proposal_tip.eq(&local_branch_tip) { 358 if proposal_tip.eq(&local_branch_tip) {
@@ -382,10 +387,12 @@ pub async fn launch() -> Result<()> {
382 )? { 387 )? {
383 0 => { 388 0 => {
384 check_clean(&git_repo)?; 389 check_clean(&git_repo)?;
385 git_repo.checkout(&cover_letter.get_branch_name()?)?; 390 git_repo.checkout(
391 &cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?,
392 )?;
386 println!( 393 println!(
387 "checked out proposal as '{}' branch", 394 "checked out proposal as '{}' branch",
388 cover_letter.get_branch_name()? 395 cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?
389 ); 396 );
390 Ok(()) 397 Ok(())
391 } 398 }
@@ -419,10 +426,12 @@ pub async fn launch() -> Result<()> {
419 )? { 426 )? {
420 0 => { 427 0 => {
421 check_clean(&git_repo)?; 428 check_clean(&git_repo)?;
422 git_repo.checkout(&cover_letter.get_branch_name()?)?; 429 git_repo.checkout(
430 &cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?,
431 )?;
423 let _ = git_repo 432 let _ = git_repo
424 .apply_patch_chain( 433 .apply_patch_chain(
425 &cover_letter.get_branch_name()?, 434 &cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?,
426 most_recent_proposal_patch_chain, 435 most_recent_proposal_patch_chain,
427 ) 436 )
428 .context("failed to apply patch chain")?; 437 .context("failed to apply patch chain")?;
@@ -472,14 +481,16 @@ pub async fn launch() -> Result<()> {
472 0 => { 481 0 => {
473 check_clean(&git_repo)?; 482 check_clean(&git_repo)?;
474 git_repo.create_branch_at_commit( 483 git_repo.create_branch_at_commit(
475 &cover_letter.get_branch_name()?, 484 &cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?,
476 &proposal_base_commit.to_string(), 485 &proposal_base_commit.to_string(),
477 )?; 486 )?;
478 git_repo.checkout(&cover_letter.get_branch_name()?)?; 487 git_repo.checkout(
488 &cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?,
489 )?;
479 let chain_length = most_recent_proposal_patch_chain.len(); 490 let chain_length = most_recent_proposal_patch_chain.len();
480 let _ = git_repo 491 let _ = git_repo
481 .apply_patch_chain( 492 .apply_patch_chain(
482 &cover_letter.get_branch_name()?, 493 &cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?,
483 most_recent_proposal_patch_chain, 494 most_recent_proposal_patch_chain,
484 ) 495 )
485 .context("failed to apply patch chain")?; 496 .context("failed to apply patch chain")?;
@@ -494,7 +505,9 @@ pub async fn launch() -> Result<()> {
494 } 505 }
495 1 => { 506 1 => {
496 check_clean(&git_repo)?; 507 check_clean(&git_repo)?;
497 git_repo.checkout(&cover_letter.get_branch_name()?)?; 508 git_repo.checkout(
509 &cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?,
510 )?;
498 println!( 511 println!(
499 "checked out old proposal in existing branch ({} ahead {} behind '{main_branch_name}')", 512 "checked out old proposal in existing branch ({} ahead {} behind '{main_branch_name}')",
500 local_ahead_of_main.len(), 513 local_ahead_of_main.len(),
@@ -537,7 +550,9 @@ pub async fn launch() -> Result<()> {
537 ]), 550 ]),
538 )? { 551 )? {
539 0 => { 552 0 => {
540 git_repo.checkout(&cover_letter.get_branch_name()?)?; 553 git_repo.checkout(
554 &cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?,
555 )?;
541 println!( 556 println!(
542 "checked out proposal branch with {} unpublished commits ({} ahead {} behind '{main_branch_name}')", 557 "checked out proposal branch with {} unpublished commits ({} ahead {} behind '{main_branch_name}')",
543 local_ahead_of_proposal.len(), 558 local_ahead_of_proposal.len(),
@@ -604,7 +619,8 @@ pub async fn launch() -> Result<()> {
604 )? { 619 )? {
605 0 => { 620 0 => {
606 check_clean(&git_repo)?; 621 check_clean(&git_repo)?;
607 git_repo.checkout(&cover_letter.get_branch_name()?)?; 622 git_repo
623 .checkout(&cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?)?;
608 println!( 624 println!(
609 "checked out old proposal in existing branch ({} ahead {} behind '{main_branch_name}')", 625 "checked out old proposal in existing branch ({} ahead {} behind '{main_branch_name}')",
610 local_ahead_of_main.len(), 626 local_ahead_of_main.len(),
@@ -615,18 +631,19 @@ pub async fn launch() -> Result<()> {
615 1 => { 631 1 => {
616 check_clean(&git_repo)?; 632 check_clean(&git_repo)?;
617 git_repo.create_branch_at_commit( 633 git_repo.create_branch_at_commit(
618 &cover_letter.get_branch_name()?, 634 &cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?,
619 &proposal_base_commit.to_string(), 635 &proposal_base_commit.to_string(),
620 )?; 636 )?;
621 let chain_length = most_recent_proposal_patch_chain.len(); 637 let chain_length = most_recent_proposal_patch_chain.len();
622 let _ = git_repo 638 let _ = git_repo
623 .apply_patch_chain( 639 .apply_patch_chain(
624 &cover_letter.get_branch_name()?, 640 &cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?,
625 most_recent_proposal_patch_chain, 641 most_recent_proposal_patch_chain,
626 ) 642 )
627 .context("failed to apply patch chain")?; 643 .context("failed to apply patch chain")?;
628 644
629 git_repo.checkout(&cover_letter.get_branch_name()?)?; 645 git_repo
646 .checkout(&cover_letter.get_branch_name_with_pr_prefix_and_shorthand_id()?)?;
630 println!( 647 println!(
631 "checked out latest version of proposal ({} ahead {} behind '{main_branch_name}'), replacing unpublished version ({} ahead {} behind '{main_branch_name}')", 648 "checked out latest version of proposal ({} ahead {} behind '{main_branch_name}'), replacing unpublished version ({} ahead {} behind '{main_branch_name}')",
632 chain_length, 649 chain_length,