rad-patch: create tracking branch on push

If not tracking branch is set, create one when pushing.  This is a common
default behavior for git and avoids failing over on some installations.
This commit is contained in:
Slack Coder 2023-01-13 12:00:26 -05:00 committed by Alexis Sellier
parent 8f8eacc078
commit 592b0047a4
No known key found for this signature in database
1 changed files with 22 additions and 5 deletions

View File

@ -40,6 +40,14 @@ fn confirm<D: fmt::Display>(prompt: D, options: &Options) -> bool {
!options.confirm || term::confirm(prompt)
}
/// Give the name of the branch or an appropriate error.
#[inline]
fn branch_name<'a>(branch: &'a git::raw::Branch) -> anyhow::Result<&'a str> {
branch
.name()?
.ok_or(anyhow!("head branch must be valid UTF-8"))
}
/// Run patch creation.
pub fn run(
storage: &Repository,
@ -66,9 +74,11 @@ pub fn run(
let head = workdir.head()?;
let head_oid = head.target().ok_or(anyhow!("invalid HEAD ref; aborting"))?;
let head_commit = workdir.find_commit(head_oid)?;
let head_branch = head
.shorthand()
.ok_or(anyhow!("cannot create patch from detached head; aborting"))?;
let head_branch = if head.is_branch() {
git::raw::Branch::wrap(head)
} else {
anyhow::bail!("cannot create patch from detached head; aborting")
};
// Make sure the `HEAD` commit can be found in the monorepo. Otherwise there
// is no way for anyone to merge this patch.
@ -89,7 +99,14 @@ pub fn run(
}
spinner.message("Pushing HEAD to storage...");
let output = git::run::<_, _, &str, &str>(Path::new("."), ["push", "rad"], [])?;
let output = match head_branch.upstream() {
Ok(_) => git::run::<_, _, &str, &str>(Path::new("."), ["push", "rad"], [])?,
Err(_) => git::run::<_, _, &str, &str>(
Path::new("."),
["push", "--set-upstream", "rad", branch_name(&head_branch)?],
[],
)?,
};
if options.verbose {
spinner.finish();
term::blob(output);
@ -191,7 +208,7 @@ pub fn run(
term::format::highlight(project.default_branch().to_string()),
term::format::secondary(term::format::oid(*target_oid)),
term::format::dim(term::format::node(patches.public_key())),
term::format::highlight(head_branch.to_string()),
term::format::highlight(branch_name(&head_branch)?),
term::format::secondary(term::format::oid(head_oid)),
);