Skip to content

Commit 77ade0f

Browse files
committed
fix(but): print normalized name after branch rename
`but reword <branch>` always did normalize the new branch name, but the original input was what was printed afterward. This made it seem like you had created an invalid branch name, whereas in reality the name was normalized. This commit just makes the output consistent with reality.
1 parent 87dbb89 commit 77ade0f

3 files changed

Lines changed: 47 additions & 5 deletions

File tree

crates/but-api/src/legacy/stack.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,14 @@ pub fn update_branch_name_with_perm(
287287
branch_name: String,
288288
new_name: String,
289289
perm: &mut RepoExclusive,
290-
) -> Result<()> {
290+
) -> Result<String> {
291291
gitbutler_branch_actions::stack::update_branch_name_with_perm(
292292
ctx,
293293
stack_id,
294294
branch_name,
295295
new_name,
296296
perm,
297-
)?;
298-
Ok(())
297+
)
299298
}
300299

301300
#[but_api]

crates/but/src/command/legacy/reword.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use bstr::{BString, ByteSlice};
33
use but_api::diff::ComputeLineStats;
44
use but_core::sync::RepoExclusive;
55
use but_ctx::Context;
6+
use colored::Colorize;
67
use gix::prelude::ObjectIdExt;
78

89
use super::{
@@ -95,15 +96,23 @@ fn edit_branch_name(
9596
if let Some(sid) = stack_entry.id {
9697
let new_name = prepare_provided_message(message, "branch name")
9798
.unwrap_or_else(|| get_branch_name_from_editor(branch_name))?;
98-
but_api::legacy::stack::update_branch_name_with_perm(
99+
let normalized_name = but_api::legacy::stack::update_branch_name_with_perm(
99100
ctx,
100101
sid,
101102
branch_name.to_owned(),
102103
new_name.clone(),
103104
perm,
104105
)?;
105106
if let Some(out) = out.for_human() {
106-
writeln!(out, "Renamed branch '{branch_name}' to '{new_name}'")?;
107+
if normalized_name != new_name {
108+
writeln!(
109+
out,
110+
"New branch name normalized: {} -> {}",
111+
new_name.dimmed(),
112+
normalized_name.yellow()
113+
)?;
114+
}
115+
writeln!(out, "Renamed branch '{branch_name}' to '{normalized_name}'")?;
107116
}
108117
return Ok(());
109118
}

crates/but/tests/but/command/reword.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,40 @@ Renamed branch 'branch-to-rename-123' to 'renamed-branch'
100100
Ok(())
101101
}
102102

103+
#[test]
104+
fn reword_branch_normalizes_name() -> anyhow::Result<()> {
105+
let env = Sandbox::init_scenario_with_target_and_default_settings("one-stack")?;
106+
107+
env.setup_metadata(&["A"])?;
108+
env.but("branch new branch-to-rename").assert().success();
109+
110+
env.file(
111+
"editor.sh",
112+
"#!/usr/bin/env bash\nprintf 'renamed-branch\\n\\n' > \"$1\"\n",
113+
);
114+
env.but("reword branch-to-rename -m trailing-hyphen-stripped-")
115+
.assert()
116+
.success()
117+
.stdout_eq(str![[r#"
118+
New branch name normalized: trailing-hyphen-stripped- -> trailing-hyphen-stripped
119+
Renamed branch 'branch-to-rename' to 'trailing-hyphen-stripped'
120+
121+
"#]]);
122+
123+
env.but("show trailing-hyphen-stripped")
124+
.assert()
125+
.success()
126+
.stderr_eq(str![])
127+
.stdout_eq(str![[r#"
128+
Branch: trailing-hyphen-stripped
129+
130+
No commits on this branch.
131+
132+
"#]]);
133+
134+
Ok(())
135+
}
136+
103137
#[test]
104138
fn reword_commit_with_same_message_succeeds_as_noop() -> anyhow::Result<()> {
105139
let env = Sandbox::init_scenario_with_target_and_default_settings("one-stack")?;

0 commit comments

Comments
 (0)