diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-12-20 09:59:14 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-12-20 10:17:27 +0000 |
| commit | 316f858f223162408cfd52183ef7645828c2f480 (patch) | |
| tree | 4f275320ad776ed4fcc85a1d75b2ac2c46c63609 /src/bin/ngit | |
| parent | 8cc4a21061433c9a29e36c4f33e4d84bea1defee (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/ngit')
| -rw-r--r-- | src/bin/ngit/sub_commands/list.rs | 53 |
1 files changed, 35 insertions, 18 deletions
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, |