Skip to content

Commit 3296387

Browse files
committed
refactor(branch): use gix to rename config section
1 parent e181d58 commit 3296387

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

src/cmd/branch/rename.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
55
use std::str::FromStr;
66

7-
use anyhow::Result;
7+
use anyhow::{Context, Result};
8+
use bstr::{BString, ByteSlice};
89

910
use crate::{
1011
ext::RepositoryExtended,
@@ -77,12 +78,33 @@ pub(super) fn dispatch(repo: &gix::Repository, matches: &clap::ArgMatches) -> Re
7778
))?,
7879
deref: false,
7980
})?;
80-
stupid
81-
.config_rename_section(
82-
&format!("branch.{old_branchname}.stgit"),
83-
&format!("branch.{new_branchname}.stgit"),
84-
)
85-
.ok();
81+
82+
let mut local_config_file = repo.local_config_file().context("opening local config")?;
83+
let old_section_name = format!("{old_branchname}.stgit");
84+
let old_section_name = old_section_name.as_bytes().as_bstr();
85+
if local_config_file
86+
.section("branch", Some(old_section_name))
87+
.is_ok()
88+
{
89+
let new_section_name =
90+
std::borrow::Cow::Owned(BString::from(format!("{new_branchname}.stgit")));
91+
local_config_file
92+
.rename_section(
93+
"branch",
94+
Some(old_section_name),
95+
"branch",
96+
Some(new_section_name),
97+
)
98+
.with_context(|| {
99+
format!(
100+
"renaming config section `branch.{old_branchname}.stgit` \
101+
to `branch.{new_branchname}.stgit`"
102+
)
103+
})?;
104+
repo.write_local_config(local_config_file)
105+
.context("writing local config")?;
106+
}
107+
86108
stupid.branch_move(Some(old_branchname.as_ref()), new_branchname.as_ref())?;
87109
stack.deinitialize()?;
88110
} else {

src/stupid/context.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -423,16 +423,6 @@ impl<'repo, 'index> StupidContext<'repo, 'index> {
423423
parse_oid(&output.stdout)
424424
}
425425

426-
pub(crate) fn config_rename_section(&self, old_name: &str, new_name: &str) -> Result<()> {
427-
self.git()
428-
.args(["config", "--local", "--rename-section"])
429-
.args([old_name, new_name])
430-
.stdout(Stdio::null())
431-
.output_git()?
432-
.require_success("config --rename-section")?;
433-
Ok(())
434-
}
435-
436426
/// Interactive diff
437427
pub(crate) fn diff<SpecIter, SpecArg, OptIter, OptArg>(
438428
&self,

0 commit comments

Comments
 (0)