Skip to content

Commit

Permalink
Set the default target branch based on init.defaultBranch
Browse files Browse the repository at this point in the history
Fixes #6.
  • Loading branch information
Timmmm committed Nov 24, 2023
1 parent 8778dc7 commit 0b88bac
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
26 changes: 26 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::path::Path;

use anyhow::Result;
use git_commands::git;

use crate::trim::TrimAsciiWhitespace;

/// Get the default branch name from Git config's `init.defaultBranch` setting,
/// falling back to 'master' if it isn't set. This should help handle default
/// branch names that are more awoke.
pub fn default_branch_name(for_path: &Path) -> Result<String> {
let output = git(
&[
"--no-pager",
"config",
"--default",
"master",
"--get",
"init.defaultBranch",
],
for_path,
)?
.stdout;
let output = std::str::from_utf8(output.trim_ascii_whitespace())?;
Ok(output.to_owned())
}
19 changes: 16 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::{
time::{SystemTime, UNIX_EPOCH},
};

mod config;
use config::*;
mod conflicts;
use conflicts::*;
mod glob;
Expand Down Expand Up @@ -42,7 +44,7 @@ fn set_committer_date_to_now() {
///
pub fn autorebase(
path: &Path,
onto_branch: &str,
onto_branch: Option<&str>,
slow_conflict_detection: bool,
include_non_local: bool,
match_branches: Option<&str>,
Expand All @@ -52,6 +54,16 @@ pub fn autorebase(
bail!("Your Git installation is too old - version 2.23 or later is required");
}

// Get the target branch name in this priority order:
//
// 1. Set explicitly via `--onto`
// 2. The `init.defaultBranch` git config setting.
// 3. "master"
let onto_branch = match onto_branch {
Some(b) => b.to_owned(),
None => default_branch_name(path)?,
};

// The first thing we do is set the commiter date to now. If we don't do this
// then when we have two branch labels on the same commit, when they get
// rebased they will be given different commiter dates which will mean they
Expand Down Expand Up @@ -91,7 +103,8 @@ pub fn autorebase(
let onto_branch_info = all_branches
.iter()
.find(|b| b.branch == onto_branch)
.ok_or_else(|| anyhow!("Couldn't find target branch '{}'", onto_branch))?;
.ok_or_else(|| anyhow!("Couldn't find target branch '{}'. You can set the default target \
branch via 'git config init.defaultBranch' or use the --onto flag.", onto_branch))?;
eprintln!("\r{}", "• Getting branches...".green());

// Print a summary of the branches, and simultaneously filter them.
Expand Down Expand Up @@ -140,7 +153,7 @@ pub fn autorebase(
&git_common_dir,
&mut conflicts,
&conflicts_path,
onto_branch,
&onto_branch,
&autorebase_worktree_path,
slow_conflict_detection,
)?;
Expand Down
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use std::env::current_dir;
/// Automatically pull the master branch and rebase all branches without
/// upstreams onto it.
struct CliOptions {
/// the target branch to pull and rebase onto (typically "master" or "develop")
#[argh(option, default = "String::from(\"master\")")]
onto: String,
/// the target branch to pull and rebase onto;
/// defaults to `git config --get init.defaultBranch` or `master` if unset
#[argh(option)]
onto: Option<String>,

/// if there are conflicts, try rebasing commit by commit backwards from the
/// target, instead of trying to determind the conflicting commit on the
Expand Down Expand Up @@ -53,7 +54,7 @@ fn run() -> Result<()> {

autorebase(
&current_dir()?,
&options.onto,
options.onto.as_deref(),
options.slow,
options.include_non_local,
options.match_branches.as_deref(),
Expand Down

0 comments on commit 0b88bac

Please sign in to comment.