Skip to content

Commit 21dde80

Browse files
committed
fix(but): print normalized name after creating branch
`but branch new` always created the new branch with a normalized name, but name reported to users was the original non-normalized input, making it a li'l bit confusing.
1 parent 87dbb89 commit 21dde80

2 files changed

Lines changed: 64 additions & 5 deletions

File tree

  • crates/but
    • src/command/legacy/branch
    • tests/but/command/branch

crates/but/src/command/legacy/branch/mod.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anyhow::bail;
2+
use bstr::ByteSlice;
23
use but_api::json::HexHash;
34
use but_core::{ref_metadata::StackId, sync::RepoExclusive};
45
use colored::Colorize;
@@ -120,7 +121,7 @@ pub fn new(
120121
})
121122
};
122123

123-
but_api::legacy::stack::create_reference_with_perm(
124+
let (_, normalized_branch_name) = but_api::legacy::stack::create_reference_with_perm(
124125
ctx,
125126
but_api::legacy::stack::create_reference::Request {
126127
new_name: branch_name.clone(),
@@ -129,28 +130,38 @@ pub fn new(
129130
guard.write_permission(),
130131
)?;
131132

133+
let normalized_branch_name = normalized_branch_name.shorten();
132134
if let Some(out) = out.for_human() {
135+
if normalized_branch_name != branch_name {
136+
writeln!(
137+
out,
138+
"Branch name normalized: {} -> {}",
139+
branch_name.dimmed(),
140+
normalized_branch_name.to_str_lossy().yellow(),
141+
)?;
142+
}
143+
133144
if let Some(anchor_name) = anchor_display {
134145
writeln!(
135146
out,
136147
"{} {} stacked on {}",
137148
"✓ Created branch".green(),
138-
branch_name.yellow(),
149+
normalized_branch_name.to_str_lossy().yellow(),
139150
anchor_name.dimmed()
140151
)?;
141152
} else {
142153
writeln!(
143154
out,
144155
"{} {}",
145156
"✓ Created branch".green(),
146-
branch_name.yellow()
157+
normalized_branch_name.to_str_lossy().yellow()
147158
)?;
148159
}
149160
} else if let Some(out) = out.for_shell() {
150-
writeln!(out, "{branch_name}")?;
161+
writeln!(out, "{}", normalized_branch_name.to_str_lossy())?;
151162
} else if let Some(out) = out.for_json() {
152163
let value = json::BranchNewOutput {
153-
branch: branch_name.clone(),
164+
branch: normalized_branch_name.to_str_lossy().into(),
154165
anchor: anchor_for_json,
155166
};
156167
out.write_value(value)?;

crates/but/tests/but/command/branch/new.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,54 @@ fn outputs_branch_name() -> anyhow::Result<()> {
3333
Ok(())
3434
}
3535

36+
#[test]
37+
fn creates_branch_with_normalized_name() -> anyhow::Result<()> {
38+
let env = Sandbox::init_scenario_with_target_and_default_settings("one-stack")?;
39+
env.setup_metadata(&[])?;
40+
41+
env.but("branch new trailing-hyphen-stripped-")
42+
.assert()
43+
.success()
44+
.stderr_eq(str![])
45+
.stdout_eq(str![[r#"
46+
Branch name normalized: trailing-hyphen-stripped- -> trailing-hyphen-stripped
47+
✓ Created branch trailing-hyphen-stripped
48+
49+
"#]]);
50+
51+
env.but("show trailing-hyphen-stripped")
52+
.assert()
53+
.success()
54+
.stderr_eq(str![])
55+
.stdout_eq(str![[r#"
56+
Branch: trailing-hyphen-stripped
57+
58+
No commits on this branch.
59+
60+
"#]]);
61+
62+
Ok(())
63+
}
64+
65+
#[test]
66+
fn json_outputs_normalized_branch_name() -> anyhow::Result<()> {
67+
let env = Sandbox::init_scenario_with_target_and_default_settings("one-stack")?;
68+
env.setup_metadata(&[])?;
69+
70+
env.but("branch new --json trailing-hyphen-stripped-")
71+
.assert()
72+
.success()
73+
.stderr_eq(str![])
74+
.stdout_eq(str![[r#"
75+
{
76+
"branch": "trailing-hyphen-stripped"
77+
}
78+
79+
"#]]);
80+
81+
Ok(())
82+
}
83+
3684
#[test]
3785
fn with_json_output() -> anyhow::Result<()> {
3886
let env = Sandbox::init_scenario_with_target_and_default_settings("one-stack")?;

0 commit comments

Comments
 (0)