Skip to content

Commit

Permalink
Add forceAuthor configuration option to always generate fixup commits…
Browse files Browse the repository at this point in the history
… for foreign authors
  • Loading branch information
blairconrad committed Feb 2, 2025
1 parent 9dbf35d commit fe28ea9
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 8 deletions.
14 changes: 14 additions & 0 deletions Documentation/git-absorb.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions Documentation/git-absorb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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()
Expand Down
50 changes: 42 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit fe28ea9

Please sign in to comment.