Skip to content

Commit

Permalink
fix: assure branch creation respects fetch-specs of remotes. (#522)
Browse files Browse the repository at this point in the history
Previously assumptions were made about how shortened tracking branches
would relate to remote names, and partial names would be set as `merge` field
of local branch configuration. The latter could lead to Git being unable
to perform certain operations.

Now the correct full reference name is set.
  • Loading branch information
Byron authored and jpgrayson committed Jan 18, 2025
1 parent 285b3c3 commit 20fbdcc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/cmd/branch/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::rc::Rc;

use anyhow::{anyhow, Result};
use anyhow::{anyhow, Context, Result};
use bstr::ByteSlice;

use crate::{
Expand Down Expand Up @@ -222,20 +222,25 @@ fn set_upstream(

Category::RemoteBranch => {
let mut local_config_file = repo.local_config_file()?;
let (remote_name, remote_branch_name) = from_short_name
.split_once_str(b"/")
.expect("remote branch short name has form <remote>/<branch>");
let (upstream_branch, remote) = repo
.upstream_branch_and_remote_for_tracking_branch(from_branch.get_reference_name())?
.with_context(|| {
format!(
"No refspec of any remote matched '{}'",
from_branch.get_reference_name().as_bstr()
)
})?;
local_config_file.set_raw_value_by(
"branch",
Some(to_short_name),
"remote",
remote_name,
remote.name().expect("only named remotes").as_bstr(),
)?;
local_config_file.set_raw_value_by(
"branch",
Some(to_short_name),
"merge",
remote_branch_name,
upstream_branch.as_bstr(),
)?;
repo.write_local_config(local_config_file)?;
Ok(Some(from_short_name.to_string()))
Expand Down
2 changes: 1 addition & 1 deletion t/t1000-branch-create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test_expect_success 'Create branch based on remote ref' '
stg branch --create foo origin/master &&
test "$(stg branch)" = "foo" &&
test "$(git config --get branch.foo.remote)" = "origin" &&
test "$(git config --get branch.foo.merge)" = "master" &&
test "$(git config --get branch.foo.merge)" = "refs/heads/master" &&
test "$(git config --get branch.foo.stgit.parentbranch)" = "origin/master" &&
stg branch master &&
stg branch --delete foo
Expand Down

0 comments on commit 20fbdcc

Please sign in to comment.