diff --git a/Documentation/git-absorb.1 b/Documentation/git-absorb.1 index 36e7950..7047ca0 100644 --- a/Documentation/git-absorb.1 +++ b/Documentation/git-absorb.1 @@ -199,6 +199,20 @@ edit your local or global \&.gitconfig and add the following section: .if n \{\ .RE .\} +.SS "GENERATE FIXUPS FOR COMMITS NOT AUTHORED BY YOU" +.sp +By default, git\-absorb will only generate fixup commits for commits that were authored by you\&. To always generate fixups for any author\(cqs commits, edit your local or global \&.gitconfig and add the following section: +.sp +.if n \{\ +.RS 4 +.\} +.nf +[absorb] + forceAuthor = true +.fi +.if n \{\ +.RE +.\} .SH "GITHUB PROJECT" .sp https://github\&.com/tummychow/git\-absorb diff --git a/Documentation/git-absorb.txt b/Documentation/git-absorb.txt index 066f25f..fbb2234 100644 --- a/Documentation/git-absorb.txt +++ b/Documentation/git-absorb.txt @@ -128,6 +128,18 @@ edit your local or global `.gitconfig` and add the following section: maxStack=50 # Or any other reasonable value for your project ............................................................................. +GENERATE FIXUPS FOR COMMITS NOT AUTHORED BY YOU +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +By default, git-absorb will only generate fixup commits for commits that were +authored by you. To always generate fixups for any author's commits, +edit your local or global `.gitconfig` and add the following section: + +............................................................................. +[absorb] + forceAuthor = true +............................................................................. + GITHUB PROJECT -------------- diff --git a/README.md b/README.md index cf2c1ac..fa98206 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,17 @@ edit your local or global `.gitconfig` and add the following section maxStack=50 # Or any other reasonable value for your project ``` +### Generate fixups for commits not authored by you + +By default, git-absorb will only generate fixup commits for commits that were authored by you. +Instead, use the `--force-author` flag to generate fixup commits for commits written by any author. +To always have this behavior, set + +```ini +[absorb] + forceAuthor = true +``` + ### One fixup per fixable commit By default, git-absorb will generate separate fixup commits for every absorbable hunk. Instead, can use the `-F` flag to create only 1 fixup commit for all hunks that absorb into the same commit. diff --git a/src/config.rs b/src/config.rs index 726fac6..d476e68 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,9 @@ pub const MAX_STACK_CONFIG_NAME: &str = "absorb.maxStack"; pub const MAX_STACK: usize = 10; +pub const FORCE_AUTHOR_CONFIG_NAME: &str = "absorb.forceAuthor"; +pub const FORCE_AUTHOR_DEFAULT: bool = false; + pub const ONE_FIXUP_PER_COMMIT_CONFIG_NAME: &str = "absorb.oneFixupPerCommit"; pub const ONE_FIXUP_PER_COMMIT_DEFAULT: bool = false; @@ -20,6 +23,16 @@ pub fn max_stack(repo: &git2::Repository) -> usize { } } +pub fn force_author(repo: &git2::Repository) -> bool { + match repo + .config() + .and_then(|config| config.get_bool(FORCE_AUTHOR_CONFIG_NAME)) + { + Ok(force_author) => force_author, + _ => FORCE_AUTHOR_DEFAULT, + } +} + pub fn one_fixup_per_commit(repo: &git2::Repository) -> bool { match repo .config() diff --git a/src/lib.rs b/src/lib.rs index 49ad43d..f2c7089 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,13 @@ pub fn run(config: &mut Config) -> Result<()> { let repo = git2::Repository::open_from_env()?; debug!(config.logger, "repository found"; "path" => repo.path().to_str()); + run_with_repo(config, &repo) +} + +fn run_with_repo(config: &mut Config, repo: &git2::Repository) -> Result<()> { + // have force flag enable all force* flags + config.force_author |= config.force; + // here, we default to the git config value, // if the flag was not provided in the CLI. // @@ -35,13 +42,7 @@ pub fn run(config: &mut Config) -> Result<()> { // like we do here is no longer sufficient. but until then, this is fine. // config.one_fixup_per_commit |= config::one_fixup_per_commit(&repo); - - run_with_repo(config, &repo) -} - -fn run_with_repo(config: &mut Config, repo: &git2::Repository) -> Result<()> { - // have force flag enable all force* flags - config.force_author |= config.force; + config.force_author |= config::force_author(&repo); let stack = stack::working_stack( repo, @@ -584,7 +585,7 @@ lines fn become_new_author(ctx: &Context) { ctx.repo.config() .unwrap() - .set_str("user.name", "nobody2") + .set_str("user.name", "nobody2") .unwrap(); } @@ -724,6 +725,39 @@ lines assert!(nothing_left_in_index(&ctx.repo).unwrap()); } + #[test] + fn foreign_author_with_force_author_config() { + let ctx = prepare_and_stage(); + + become_new_author(&ctx); + + ctx.repo.config() + .unwrap() + .set_str("absorb.forceAuthor", "true") + .unwrap(); + + // run 'git-absorb' + let drain = slog::Discard; + let logger = slog::Logger::root(drain, o!()); + let mut config = Config { + dry_run: false, + force_author: false, + force: false, + base: None, + and_rebase: false, + whole_file: false, + one_fixup_per_commit: true, + logger: &logger, + }; + run_with_repo(&mut config, &ctx.repo).unwrap(); + + let mut revwalk = ctx.repo.revwalk().unwrap(); + revwalk.push_head().unwrap(); + assert_eq!(revwalk.count(), 2); + + assert!(nothing_left_in_index(&ctx.repo).unwrap()); + } + fn autostage_common(ctx: &Context, file_path: &PathBuf) -> (PathBuf, PathBuf) { // 1 modification w/o staging let path = ctx.join(&file_path);