Skip to content

Commit 1999cfd

Browse files
authored
Merge pull request #147 from blairconrad/refactor-config
Refactor config-merging
2 parents acfde39 + fc20309 commit 1999cfd

File tree

3 files changed

+70
-69
lines changed

3 files changed

+70
-69
lines changed

src/config.rs

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use crate::Config;
2+
use git2::Repository;
3+
14
pub const MAX_STACK_CONFIG_NAME: &str = "absorb.maxStack";
25
pub const MAX_STACK: usize = 10;
36

@@ -13,6 +16,30 @@ pub const AUTO_STAGE_IF_NOTHING_STAGED_DEFAULT: bool = false;
1316
pub const FIXUP_TARGET_ALWAYS_SHA_CONFIG_NAME: &str = "absorb.fixupTargetAlwaysSHA";
1417
pub const FIXUP_TARGET_ALWAYS_SHA_DEFAULT: bool = false;
1518

19+
pub fn unify<'config>(config: &'config Config, repo: &Repository) -> Config<'config> {
20+
Config {
21+
// here, we default to the git config value,
22+
// if the flag was not provided in the CLI.
23+
//
24+
// in the future, we'd likely want to differentiate between
25+
// a "non-provided" option, vs an explicit --no-<option>
26+
// that disables a behavior, much like git does.
27+
// e.g. user may want to overwrite a config value with
28+
// --no-one-fixup-per-commit -- then, defaulting to the config value
29+
// like we do here is no longer sufficient. but until then, this is fine.
30+
one_fixup_per_commit: config.one_fixup_per_commit
31+
|| bool_value(
32+
&repo,
33+
ONE_FIXUP_PER_COMMIT_CONFIG_NAME,
34+
ONE_FIXUP_PER_COMMIT_DEFAULT,
35+
),
36+
force_author: config.force_author
37+
|| config.force
38+
|| bool_value(&repo, FORCE_AUTHOR_CONFIG_NAME, FORCE_AUTHOR_DEFAULT),
39+
..*config
40+
}
41+
}
42+
1643
pub fn max_stack(repo: &git2::Repository) -> usize {
1744
match repo
1845
.config()
@@ -23,42 +50,28 @@ pub fn max_stack(repo: &git2::Repository) -> usize {
2350
}
2451
}
2552

26-
pub fn force_author(repo: &git2::Repository) -> bool {
27-
match repo
28-
.config()
29-
.and_then(|config| config.get_bool(FORCE_AUTHOR_CONFIG_NAME))
30-
{
31-
Ok(force_author) => force_author,
32-
_ => FORCE_AUTHOR_DEFAULT,
33-
}
34-
}
35-
36-
pub fn one_fixup_per_commit(repo: &git2::Repository) -> bool {
37-
match repo
38-
.config()
39-
.and_then(|config| config.get_bool(ONE_FIXUP_PER_COMMIT_CONFIG_NAME))
40-
{
41-
Ok(one_commit_per_fixup) => one_commit_per_fixup,
42-
_ => ONE_FIXUP_PER_COMMIT_DEFAULT,
43-
}
44-
}
45-
4653
pub fn auto_stage_if_nothing_staged(repo: &git2::Repository) -> bool {
47-
match repo
48-
.config()
49-
.and_then(|config| config.get_bool(AUTO_STAGE_IF_NOTHING_STAGED_CONFIG_NAME))
50-
{
51-
Ok(val) => val,
52-
_ => AUTO_STAGE_IF_NOTHING_STAGED_DEFAULT,
53-
}
54+
bool_value(
55+
&repo,
56+
AUTO_STAGE_IF_NOTHING_STAGED_CONFIG_NAME,
57+
AUTO_STAGE_IF_NOTHING_STAGED_DEFAULT,
58+
)
5459
}
5560

5661
pub fn fixup_target_always_sha(repo: &git2::Repository) -> bool {
62+
bool_value(
63+
&repo,
64+
FIXUP_TARGET_ALWAYS_SHA_CONFIG_NAME,
65+
FIXUP_TARGET_ALWAYS_SHA_DEFAULT,
66+
)
67+
}
68+
69+
fn bool_value(repo: &Repository, setting_name: &str, default_value: bool) -> bool {
5770
match repo
5871
.config()
59-
.and_then(|config| config.get_bool(FIXUP_TARGET_ALWAYS_SHA_CONFIG_NAME))
72+
.and_then(|config| config.get_bool(setting_name))
6073
{
61-
Ok(val) => val,
62-
_ => FIXUP_TARGET_ALWAYS_SHA_DEFAULT,
74+
Ok(value) => value,
75+
_ => default_value,
6376
}
6477
}

src/lib.rs

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,16 @@ pub struct Config<'a> {
2020
pub logger: &'a slog::Logger,
2121
}
2222

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

27-
run_with_repo(config, &repo)
27+
run_with_repo(&config, &repo)
2828
}
2929

30-
fn run_with_repo(config: &mut Config, repo: &git2::Repository) -> Result<()> {
30+
fn run_with_repo(config: &Config, repo: &git2::Repository) -> Result<()> {
31+
let config = config::unify(&config, repo);
3132
// have force flag enable all force* flags
32-
config.force_author |= config.force;
33-
34-
// here, we default to the git config value,
35-
// if the flag was not provided in the CLI.
36-
//
37-
// in the future, we'd likely want to differentiate between
38-
// a "non-provided" option, vs an explicit --no-<option>
39-
// that disables a behavior, much like git does.
40-
// e.g. user may want to overwrite a config value with
41-
// --no-one-fixup-per-commit -- then, defaulting to the config value
42-
// like we do here is no longer sufficient. but until then, this is fine.
43-
//
44-
config.one_fixup_per_commit |= config::one_fixup_per_commit(&repo);
45-
config.force_author |= config::force_author(&repo);
4633

4734
let stack = stack::working_stack(
4835
repo,
@@ -595,7 +582,7 @@ lines
595582
// run 'git-absorb'
596583
let drain = slog::Discard;
597584
let logger = slog::Logger::root(drain, o!());
598-
let mut config = Config {
585+
let config = Config {
599586
dry_run: false,
600587
force_author: false,
601588
force: false,
@@ -605,7 +592,7 @@ lines
605592
one_fixup_per_commit: false,
606593
logger: &logger,
607594
};
608-
run_with_repo(&mut config, &ctx.repo).unwrap();
595+
run_with_repo(&config, &ctx.repo).unwrap();
609596

610597
let mut revwalk = ctx.repo.revwalk().unwrap();
611598
revwalk.push_head().unwrap();
@@ -621,7 +608,7 @@ lines
621608
// run 'git-absorb'
622609
let drain = slog::Discard;
623610
let logger = slog::Logger::root(drain, o!());
624-
let mut config = Config {
611+
let config = Config {
625612
dry_run: false,
626613
force_author: false,
627614
force: false,
@@ -631,7 +618,7 @@ lines
631618
one_fixup_per_commit: true,
632619
logger: &logger,
633620
};
634-
run_with_repo(&mut config, &ctx.repo).unwrap();
621+
run_with_repo(&config, &ctx.repo).unwrap();
635622

636623
let mut revwalk = ctx.repo.revwalk().unwrap();
637624
revwalk.push_head().unwrap();
@@ -649,7 +636,7 @@ lines
649636
// run 'git-absorb'
650637
let drain = slog::Discard;
651638
let logger = slog::Logger::root(drain, o!());
652-
let mut config = Config {
639+
let config = Config {
653640
dry_run: false,
654641
force_author: false,
655642
force: false,
@@ -659,7 +646,7 @@ lines
659646
one_fixup_per_commit: true,
660647
logger: &logger,
661648
};
662-
run_with_repo(&mut config, &ctx.repo).unwrap();
649+
run_with_repo(&config, &ctx.repo).unwrap();
663650

664651
let mut revwalk = ctx.repo.revwalk().unwrap();
665652
revwalk.push_head().unwrap();
@@ -677,7 +664,7 @@ lines
677664
// run 'git-absorb'
678665
let drain = slog::Discard;
679666
let logger = slog::Logger::root(drain, o!());
680-
let mut config = Config {
667+
let config = Config {
681668
dry_run: false,
682669
force_author: true,
683670
force: false,
@@ -687,7 +674,7 @@ lines
687674
one_fixup_per_commit: true,
688675
logger: &logger,
689676
};
690-
run_with_repo(&mut config, &ctx.repo).unwrap();
677+
run_with_repo(&config, &ctx.repo).unwrap();
691678

692679
let mut revwalk = ctx.repo.revwalk().unwrap();
693680
revwalk.push_head().unwrap();
@@ -705,7 +692,7 @@ lines
705692
// run 'git-absorb'
706693
let drain = slog::Discard;
707694
let logger = slog::Logger::root(drain, o!());
708-
let mut config = Config {
695+
let config = Config {
709696
dry_run: false,
710697
force_author: false,
711698
force: true,
@@ -715,7 +702,7 @@ lines
715702
one_fixup_per_commit: true,
716703
logger: &logger,
717704
};
718-
run_with_repo(&mut config, &ctx.repo).unwrap();
705+
run_with_repo(&config, &ctx.repo).unwrap();
719706

720707
let mut revwalk = ctx.repo.revwalk().unwrap();
721708
revwalk.push_head().unwrap();
@@ -730,15 +717,16 @@ lines
730717

731718
become_new_author(&ctx);
732719

733-
ctx.repo.config()
720+
ctx.repo
721+
.config()
734722
.unwrap()
735723
.set_str("absorb.forceAuthor", "true")
736724
.unwrap();
737725

738726
// run 'git-absorb'
739727
let drain = slog::Discard;
740728
let logger = slog::Logger::root(drain, o!());
741-
let mut config = Config {
729+
let config = Config {
742730
dry_run: false,
743731
force_author: false,
744732
force: false,
@@ -748,7 +736,7 @@ lines
748736
one_fixup_per_commit: true,
749737
logger: &logger,
750738
};
751-
run_with_repo(&mut config, &ctx.repo).unwrap();
739+
run_with_repo(&config, &ctx.repo).unwrap();
752740

753741
let mut revwalk = ctx.repo.revwalk().unwrap();
754742
revwalk.push_head().unwrap();
@@ -787,7 +775,7 @@ lines
787775
// run 'git-absorb'
788776
let drain = slog::Discard;
789777
let logger = slog::Logger::root(drain, o!());
790-
let mut config = Config {
778+
let config = Config {
791779
dry_run: false,
792780
force_author: false,
793781
force: false,
@@ -797,7 +785,7 @@ lines
797785
one_fixup_per_commit: false,
798786
logger: &logger,
799787
};
800-
run_with_repo(&mut config, &ctx.repo).unwrap();
788+
run_with_repo(&config, &ctx.repo).unwrap();
801789

802790
let mut revwalk = ctx.repo.revwalk().unwrap();
803791
revwalk.push_head().unwrap();
@@ -824,7 +812,7 @@ lines
824812
// run 'git-absorb'
825813
let drain = slog::Discard;
826814
let logger = slog::Logger::root(drain, o!());
827-
let mut config = Config {
815+
let config = Config {
828816
dry_run: false,
829817
force_author: false,
830818
force: false,
@@ -834,7 +822,7 @@ lines
834822
one_fixup_per_commit: false,
835823
logger: &logger,
836824
};
837-
run_with_repo(&mut config, &ctx.repo).unwrap();
825+
run_with_repo(&config, &ctx.repo).unwrap();
838826

839827
let mut revwalk = ctx.repo.revwalk().unwrap();
840828
revwalk.push_head().unwrap();
@@ -859,7 +847,7 @@ lines
859847
// run 'git-absorb'
860848
let drain = slog::Discard;
861849
let logger = slog::Logger::root(drain, o!());
862-
let mut config = Config {
850+
let config = Config {
863851
dry_run: false,
864852
force_author: false,
865853
force: false,
@@ -869,7 +857,7 @@ lines
869857
one_fixup_per_commit: false,
870858
logger: &logger,
871859
};
872-
run_with_repo(&mut config, &ctx.repo).unwrap();
860+
run_with_repo(&config, &ctx.repo).unwrap();
873861

874862
let mut revwalk = ctx.repo.revwalk().unwrap();
875863
revwalk.push_head().unwrap();
@@ -891,7 +879,7 @@ lines
891879
// run 'git-absorb'
892880
let drain = slog::Discard;
893881
let logger = slog::Logger::root(drain, o!());
894-
let mut config = Config {
882+
let config = Config {
895883
dry_run: false,
896884
force_author: false,
897885
force: false,
@@ -901,7 +889,7 @@ lines
901889
one_fixup_per_commit: true,
902890
logger: &logger,
903891
};
904-
run_with_repo(&mut config, &ctx.repo).unwrap();
892+
run_with_repo(&config, &ctx.repo).unwrap();
905893
assert!(nothing_left_in_index(&ctx.repo).unwrap());
906894

907895
let mut revwalk = ctx.repo.revwalk().unwrap();

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ fn main() {
8989
));
9090
}
9191

92-
if let Err(e) = git_absorb::run(&mut git_absorb::Config {
92+
if let Err(e) = git_absorb::run(&git_absorb::Config {
9393
dry_run,
9494
force_author,
9595
force,

0 commit comments

Comments
 (0)