Skip to content

Commit 1e3b883

Browse files
committed
fix: assure branch creation respects fetch-specs of remotes. (#522)
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.
1 parent 505ced9 commit 1e3b883

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/cmd/branch/create.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use std::rc::Rc;
66

7-
use anyhow::{anyhow, Result};
7+
use anyhow::{anyhow, Context, Result};
88
use bstr::ByteSlice;
99

1010
use crate::{
@@ -222,20 +222,25 @@ fn set_upstream(
222222

223223
Category::RemoteBranch => {
224224
let mut local_config_file = repo.local_config_file()?;
225-
let (remote_name, remote_branch_name) = from_short_name
226-
.split_once_str(b"/")
227-
.expect("remote branch short name has form <remote>/<branch>");
225+
let (upstream_branch, remote) = repo
226+
.upstream_branch_and_remote_for_tracking_branch(from_branch.get_reference_name())?
227+
.with_context(|| {
228+
format!(
229+
"No refspec of any remote matched '{}'",
230+
from_branch.get_reference_name().as_bstr()
231+
)
232+
})?;
228233
local_config_file.set_raw_value_by(
229234
"branch",
230235
Some(to_short_name),
231236
"remote",
232-
remote_name,
237+
remote.name().expect("only named remotes").as_bstr(),
233238
)?;
234239
local_config_file.set_raw_value_by(
235240
"branch",
236241
Some(to_short_name),
237242
"merge",
238-
remote_branch_name,
243+
upstream_branch.as_bstr(),
239244
)?;
240245
repo.write_local_config(local_config_file)?;
241246
Ok(Some(from_short_name.to_string()))

0 commit comments

Comments
 (0)