Skip to content

Commit c693736

Browse files
committed
fix: StGit-specific branch config handling
When renaming, deleting, or cleaning-up branches, the branch and StGit specific configuration section, i.e. "branch.<branch>.stgit" was not being handled correctly. For `stg branch --rename`, this config section was not being renamed along with the branch. For `stg branch --delete` and `stg branch --cleanup`, this section was not being entirely removed; the section heading would remain, but with no values.
1 parent 908ec3c commit c693736

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

src/cmd/branch.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,14 @@ fn rename(repo: &git2::Repository, matches: &ArgMatches) -> Result<()> {
573573
false,
574574
&format!("rename {old_branchname} to {new_branchname}"),
575575
)?;
576-
stack.deinitialize()?;
576+
stupid
577+
.config_rename_section(
578+
&format!("branch.{old_branchname}.stgit"),
579+
&format!("branch.{new_branchname}.stgit"),
580+
)
581+
.ok();
577582
stupid.branch_move(Some(old_branchname), new_branchname)?;
583+
stack.deinitialize()?;
578584
} else {
579585
stupid.branch_move(Some(old_branchname), new_branchname)?;
580586
}

src/stack/stack.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::{
1010
state::StackState, transaction::TransactionBuilder, upgrade::stack_upgrade, PatchState,
1111
StackStateAccess,
1212
};
13-
use crate::{patchname::PatchName, repo::RepositoryExtended};
13+
use crate::{patchname::PatchName, repo::RepositoryExtended, stupid::Stupid};
1414

1515
/// StGit stack
1616
///
@@ -75,29 +75,19 @@ impl<'repo> Stack<'repo> {
7575
refname,
7676
..
7777
} = self;
78-
let mut config = repo.config()?;
7978
let mut state_ref = repo.find_reference(&refname)?;
8079
let patch_ref_glob = get_patch_refname(&branch_name, "*");
8180
for patch_reference in repo.references_glob(&patch_ref_glob)? {
8281
let mut patch_reference = patch_reference?;
8382
patch_reference.delete()?;
8483
}
8584
state_ref.delete()?;
86-
let mut config_to_delete: Vec<_> = Vec::new();
87-
88-
{
89-
let mut entries = config.entries(Some(&format!("branch.{branch_name}.stgit")))?;
90-
while let Some(entry) = entries.next() {
91-
let entry = entry?;
92-
if let Some(name) = entry.name() {
93-
config_to_delete.push(name.to_string());
94-
}
95-
}
96-
}
9785

98-
for name_to_delete in config_to_delete {
99-
config.remove(&name_to_delete)?;
100-
}
86+
// It is ok if the StGit-specific config section does not exist.
87+
repo.stupid()
88+
.config_remove_section(&format!("branch.{branch_name}.stgit"))
89+
.ok();
90+
10191
Ok(())
10292
}
10393

src/stupid/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,26 @@ impl<'repo, 'index> StupidContext<'repo, 'index> {
420420
parse_oid(&output.stdout)
421421
}
422422

423+
pub(crate) fn config_remove_section(&self, section_name: &str) -> Result<()> {
424+
self.git()
425+
.args(["config", "--local", "--remove-section"])
426+
.arg(section_name)
427+
.stdout(Stdio::null())
428+
.output_git()?
429+
.require_success("config --remove-section")?;
430+
Ok(())
431+
}
432+
433+
pub(crate) fn config_rename_section(&self, old_name: &str, new_name: &str) -> Result<()> {
434+
self.git()
435+
.args(["config", "--local", "--rename-section"])
436+
.args([old_name, new_name])
437+
.stdout(Stdio::null())
438+
.output_git()?
439+
.require_success("config --rename-section")?;
440+
Ok(())
441+
}
442+
423443
/// Interactive diff
424444
pub(crate) fn diff<SpecIter, SpecArg, OptIter, OptArg>(
425445
&self,

0 commit comments

Comments
 (0)