Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a flag to fix past merges. #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ pub struct Config<'a> {
pub base: Option<&'a str>,
pub and_rebase: bool,
pub whole_file: bool,
pub fix_past_merges: bool,
pub logger: &'a slog::Logger,
}

pub fn run(config: &Config) -> Result<()> {
let repo = git2::Repository::open_from_env()?;
debug!(config.logger, "repository found"; "path" => repo.path().to_str());

let stack = stack::working_stack(&repo, config.base, config.force, config.logger)?;
let stack = stack::working_stack(
&repo,
config.base,
config.force,
config.fix_past_merges,
config.logger,
)?;
if stack.is_empty() {
crit!(config.logger, "No commits available to fix up, exiting");
return Ok(());
Expand Down Expand Up @@ -174,7 +181,10 @@ pub fn run(config: &Config) -> Result<()> {
// cases, might be helpful to just match the first commit touching the same
// file as the current hunk. Use this option with care!
if config.whole_file {
debug!(c_logger, "Commit touches the hunk file and match whole file is enabled");
debug!(
c_logger,
"Commit touches the hunk file and match whole file is enabled"
);
dest_commit = Some(commit);
break 'commit;
}
Expand Down
15 changes: 15 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ fn main() {
.short("w")
.long("whole-file")
.takes_value(false),
)
.arg(
clap::Arg::with_name("fix-past-merges")
.help("Attempt to fixup past the merge commit")
.short("m")
.long("fix-past-merges")
// If you have remote/main -> {first, second} -> merged, then:
// * What does the "max stack" even mean?
// * first and second are likely branches, and the algorithm used in working_stack
// explicitly disallows that.
// It could technically work without base, but in most realistic cases it would just
// silently fail.
.requires("base")
.takes_value(false),
);
let mut args_clone = args.clone();
let args = args.get_matches();
Expand Down Expand Up @@ -112,6 +126,7 @@ fn main() {
base: args.value_of("base"),
and_rebase: args.is_present("and-rebase"),
whole_file: args.is_present("whole-file"),
fix_past_merges: args.is_present("fix-past-merges"),
logger: &logger,
}) {
crit!(logger, "absorb failed"; "err" => e.to_string());
Expand Down
56 changes: 47 additions & 9 deletions src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn working_stack<'repo>(
repo: &'repo git2::Repository,
user_provided_base: Option<&str>,
force: bool,
fix_past_merges: bool,
logger: &slog::Logger,
) -> Result<Vec<git2::Commit<'repo>>> {
let head = repo.head()?;
Expand All @@ -38,7 +39,9 @@ pub fn working_stack<'repo>(
let mut revwalk = repo.revwalk()?;
revwalk.set_sorting(git2::Sort::TOPOLOGICAL)?;
revwalk.push_head()?;
revwalk.simplify_first_parent()?;
if !fix_past_merges {
revwalk.simplify_first_parent()?;
}
debug!(logger, "head pushed"; "head" => head.name());

let base_commit = match user_provided_base {
Expand Down Expand Up @@ -73,7 +76,9 @@ pub fn working_stack<'repo>(
for rev in revwalk {
commits_considered += 1;
let commit = repo.find_commit(rev?)?;
if commit.parents().len() > 1 {
let is_merge = commit.parent_count() > 1;

if !fix_past_merges && is_merge {
warn!(logger, "Will not fix up past the merge commit"; "commit" => commit.id().to_string());
break;
}
Expand All @@ -92,8 +97,11 @@ pub fn working_stack<'repo>(
"limit" => ret.len());
break;
}
debug!(logger, "commit pushed onto stack"; "commit" => commit.id().to_string());
ret.push(commit);
// Ensure that we don't commit a fixup to the merge commit itself.
if !is_merge {
debug!(logger, "commit pushed onto stack"; "commit" => commit.id().to_string());
ret.push(commit);
}
}
if commits_considered == 0 {
if user_provided_base.is_none() {
Expand Down Expand Up @@ -203,7 +211,7 @@ mod tests {

assert_stack_matches_chain(
1,
&working_stack(&repo, None, false, &empty_slog()).unwrap(),
&working_stack(&repo, None, false, false, &empty_slog()).unwrap(),
&commits,
);
}
Expand All @@ -220,6 +228,7 @@ mod tests {
&repo,
Some(&commits[0].id().to_string()),
false,
false,
&empty_slog(),
)
.unwrap(),
Expand All @@ -238,7 +247,7 @@ mod tests {

assert_stack_matches_chain(
MAX_STACK + 1,
&working_stack(&repo, None, false, &empty_slog()).unwrap(),
&working_stack(&repo, None, false, false, &empty_slog()).unwrap(),
&commits,
);
}
Expand All @@ -255,13 +264,13 @@ mod tests {

assert_stack_matches_chain(
2,
&working_stack(&repo, None, false, &empty_slog()).unwrap(),
&working_stack(&repo, None, false, false, &empty_slog()).unwrap(),
&new_commits,
);
}

#[test]
fn test_stack_stops_at_merges() {
fn test_stack_stops_at_merges_without_fix_past_merges() {
let (_dir, repo) = init_repo();
let first = empty_commit(&repo, "HEAD", "first", &[]);
// equivalent to checkout --orphan
Expand All @@ -273,8 +282,37 @@ mod tests {

assert_stack_matches_chain(
2,
&working_stack(&repo, None, false, &empty_slog()).unwrap(),
&working_stack(&repo, None, false, false, &empty_slog()).unwrap(),
&commits,
);
}

#[test]
fn test_stack_continues_at_merges_with_fix_past_merges() {
let (_dir, repo) = init_repo();
let base = empty_commit(&repo, "HEAD", "base", &[]);
let first = empty_commit(&repo, "HEAD", "first", &[&base]);
// equivalent to checkout --orphan
repo.set_head("refs/heads/new").unwrap();
let second = empty_commit(&repo, "HEAD", "second", &[&base]);
// the current commit must be the first parent
let merge = empty_commit(&repo, "HEAD", "merge", &[&second, &first]);
let commits = empty_commit_chain(&repo, "HEAD", &[&merge], 2);

let mut expect = vec![second, first];
expect.extend(commits);

assert_stack_matches_chain(
4,
&working_stack(
&repo,
Some(&base.id().to_string()),
false,
true,
&empty_slog(),
)
.unwrap(),
&expect,
);
}
}