Skip to content

Commit 6feaa49

Browse files
committed
Remove logger from Config
1 parent b391bd0 commit 6feaa49

File tree

2 files changed

+51
-66
lines changed

2 files changed

+51
-66
lines changed

src/lib.rs

Lines changed: 39 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,22 @@ pub struct Config<'a> {
1717
pub and_rebase: bool,
1818
pub whole_file: bool,
1919
pub one_fixup_per_commit: bool,
20-
pub logger: &'a slog::Logger,
2120
}
2221

23-
pub fn run(config: &Config) -> Result<()> {
22+
pub fn run(logger: &slog::Logger, config: &Config) -> Result<()> {
2423
let repo = git2::Repository::open_from_env()?;
25-
debug!(config.logger, "repository found"; "path" => repo.path().to_str());
24+
debug!(logger, "repository found"; "path" => repo.path().to_str());
2625

27-
run_with_repo(&config, &repo)
26+
run_with_repo(&logger, &config, &repo)
2827
}
2928

30-
fn run_with_repo(config: &Config, repo: &git2::Repository) -> Result<()> {
29+
fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository) -> Result<()> {
3130
let config = config::unify(&config, repo);
3231
// have force flag enable all force* flags
3332

34-
let stack = stack::working_stack(
35-
repo,
36-
config.base,
37-
config.force_author,
38-
config.force,
39-
config.logger,
40-
)?;
33+
let stack = stack::working_stack(repo, config.base, config.force_author, config.force, logger)?;
4134
if stack.is_empty() {
42-
crit!(config.logger, "No commits available to fix up, exiting");
35+
crit!(logger, "No commits available to fix up, exiting");
4336
return Ok(());
4437
}
4538

@@ -81,7 +74,7 @@ fn run_with_repo(config: &Config, repo: &git2::Repository) -> Result<()> {
8174
diff_options.as_mut(),
8275
)?,
8376
)?;
84-
trace!(config.logger, "parsed commit diff";
77+
trace!(logger, "parsed commit diff";
8578
"commit" => commit.id().to_string(),
8679
"diff" => format!("{:?}", diff),
8780
);
@@ -98,7 +91,7 @@ fn run_with_repo(config: &Config, repo: &git2::Repository) -> Result<()> {
9891
None,
9992
diff_options.as_mut(),
10093
)?)?;
101-
trace!(config.logger, "parsed index";
94+
trace!(logger, "parsed index";
10295
"index" => format!("{:?}", index),
10396
);
10497

@@ -113,7 +106,7 @@ fn run_with_repo(config: &Config, repo: &git2::Repository) -> Result<()> {
113106
'patch: for index_patch in index.iter() {
114107
let old_path = index_patch.new_path.as_slice();
115108
if index_patch.status != git2::Delta::Modified {
116-
debug!(config.logger, "skipped non-modified hunk";
109+
debug!(logger, "skipped non-modified hunk";
117110
"path" => String::from_utf8_lossy(old_path).into_owned(),
118111
"status" => format!("{:?}", index_patch.status),
119112
);
@@ -125,7 +118,7 @@ fn run_with_repo(config: &Config, repo: &git2::Repository) -> Result<()> {
125118
let mut preceding_hunks_offset = 0isize;
126119
let mut applied_hunks_offset = 0isize;
127120
'hunk: for index_hunk in &index_patch.hunks {
128-
debug!(config.logger, "next hunk";
121+
debug!(logger, "next hunk";
129122
"header" => index_hunk.header(),
130123
"path" => String::from_utf8_lossy(old_path).into_owned(),
131124
);
@@ -170,7 +163,7 @@ fn run_with_repo(config: &Config, repo: &git2::Repository) -> Result<()> {
170163
// | -5,1 +3,0 | -5,1 +4,0 | -4,1 +3,0 |
171164
// |----------------|-----------|------------------|
172165

173-
debug!(config.logger, "";
166+
debug!(logger, "";
174167
"to apply" => hunk_to_apply.header(),
175168
"to commute" => isolated_hunk.header(),
176169
"preceding hunks" => format!("{}/{}", applied_hunks_offset, preceding_hunks_offset),
@@ -184,7 +177,7 @@ fn run_with_repo(config: &Config, repo: &git2::Repository) -> Result<()> {
184177
let mut commuted_index_hunk = isolated_hunk;
185178

186179
'commit: for (commit, diff) in &stack {
187-
let c_logger = config.logger.new(o!(
180+
let c_logger = logger.new(o!(
188181
"commit" => commit.id().to_string(),
189182
));
190183
let next_patch = match diff.by_new(commuted_old_path) {
@@ -247,7 +240,7 @@ fn run_with_repo(config: &Config, repo: &git2::Repository) -> Result<()> {
247240
// so there is no commit to absorb it into
248241
None => {
249242
warn!(
250-
config.logger,
243+
logger,
251244
"Could not find a commit to fix up, use \
252245
--base to increase the search range."
253246
);
@@ -319,12 +312,12 @@ fn run_with_repo(config: &Config, repo: &git2::Repository) -> Result<()> {
319312
&head_tree,
320313
&[&head_commit],
321314
)?)?;
322-
info!(config.logger, "committed";
315+
info!(logger, "committed";
323316
"commit" => head_commit.id().to_string(),
324317
"header" => format!("+{},-{}", diff.insertions(), diff.deletions()),
325318
);
326319
} else {
327-
info!(config.logger, "would have committed";
320+
info!(logger, "would have committed";
328321
"fixup" => dest_commit_locator,
329322
"header" => format!("+{},-{}", diff.insertions(), diff.deletions()),
330323
);
@@ -347,14 +340,14 @@ fn run_with_repo(config: &Config, repo: &git2::Repository) -> Result<()> {
347340
if patches_considered == 0 {
348341
if index_was_empty && !we_added_everything_to_index {
349342
warn!(
350-
config.logger,
343+
logger,
351344
"No changes staged, try adding something \
352345
to the index or set {} = true",
353346
config::AUTO_STAGE_IF_NOTHING_STAGED_CONFIG_NAME
354347
);
355348
} else {
356349
warn!(
357-
config.logger,
350+
logger,
358351
"Could not find a commit to fix up, use \
359352
--base to increase the search range."
360353
)
@@ -495,8 +488,7 @@ mod tests {
495488
// run 'git-absorb'
496489
let drain = slog::Discard;
497490
let logger = slog::Logger::root(drain, o!());
498-
let config = default_config(&logger);
499-
run_with_repo(&config, &ctx.repo).unwrap();
491+
run_with_repo(&logger, &DEFAULT_CONFIG, &ctx.repo).unwrap();
500492

501493
let mut revwalk = ctx.repo.revwalk().unwrap();
502494
revwalk.push_head().unwrap();
@@ -514,9 +506,9 @@ mod tests {
514506
let logger = slog::Logger::root(drain, o!());
515507
let config = Config {
516508
one_fixup_per_commit: true,
517-
..default_config(&logger)
509+
..DEFAULT_CONFIG
518510
};
519-
run_with_repo(&config, &ctx.repo).unwrap();
511+
run_with_repo(&logger, &config, &ctx.repo).unwrap();
520512

521513
let mut revwalk = ctx.repo.revwalk().unwrap();
522514
revwalk.push_head().unwrap();
@@ -534,8 +526,7 @@ mod tests {
534526
// run 'git-absorb'
535527
let drain = slog::Discard;
536528
let logger = slog::Logger::root(drain, o!());
537-
let config = default_config(&logger);
538-
run_with_repo(&config, &ctx.repo).unwrap();
529+
run_with_repo(&logger, &DEFAULT_CONFIG, &ctx.repo).unwrap();
539530

540531
let mut revwalk = ctx.repo.revwalk().unwrap();
541532
revwalk.push_head().unwrap();
@@ -555,9 +546,9 @@ mod tests {
555546
let logger = slog::Logger::root(drain, o!());
556547
let config = Config {
557548
force_author: true,
558-
..default_config(&logger)
549+
..DEFAULT_CONFIG
559550
};
560-
run_with_repo(&config, &ctx.repo).unwrap();
551+
run_with_repo(&logger, &config, &ctx.repo).unwrap();
561552

562553
let mut revwalk = ctx.repo.revwalk().unwrap();
563554
revwalk.push_head().unwrap();
@@ -577,9 +568,9 @@ mod tests {
577568
let logger = slog::Logger::root(drain, o!());
578569
let config = Config {
579570
force: true,
580-
..default_config(&logger)
571+
..DEFAULT_CONFIG
581572
};
582-
run_with_repo(&config, &ctx.repo).unwrap();
573+
run_with_repo(&logger, &config, &ctx.repo).unwrap();
583574

584575
let mut revwalk = ctx.repo.revwalk().unwrap();
585576
revwalk.push_head().unwrap();
@@ -603,8 +594,7 @@ mod tests {
603594
// run 'git-absorb'
604595
let drain = slog::Discard;
605596
let logger = slog::Logger::root(drain, o!());
606-
let config = default_config(&logger);
607-
run_with_repo(&config, &ctx.repo).unwrap();
597+
run_with_repo(&logger, &DEFAULT_CONFIG, &ctx.repo).unwrap();
608598

609599
let mut revwalk = ctx.repo.revwalk().unwrap();
610600
revwalk.push_head().unwrap();
@@ -643,8 +633,7 @@ mod tests {
643633
// run 'git-absorb'
644634
let drain = slog::Discard;
645635
let logger = slog::Logger::root(drain, o!());
646-
let config = default_config(&logger);
647-
run_with_repo(&config, &ctx.repo).unwrap();
636+
run_with_repo(&logger, &DEFAULT_CONFIG, &ctx.repo).unwrap();
648637

649638
let mut revwalk = ctx.repo.revwalk().unwrap();
650639
revwalk.push_head().unwrap();
@@ -671,8 +660,7 @@ mod tests {
671660
// run 'git-absorb'
672661
let drain = slog::Discard;
673662
let logger = slog::Logger::root(drain, o!());
674-
let config = default_config(&logger);
675-
run_with_repo(&config, &ctx.repo).unwrap();
663+
run_with_repo(&logger, &DEFAULT_CONFIG, &ctx.repo).unwrap();
676664

677665
let mut revwalk = ctx.repo.revwalk().unwrap();
678666
revwalk.push_head().unwrap();
@@ -697,8 +685,7 @@ mod tests {
697685
// run 'git-absorb'
698686
let drain = slog::Discard;
699687
let logger = slog::Logger::root(drain, o!());
700-
let config = default_config(&logger);
701-
run_with_repo(&config, &ctx.repo).unwrap();
688+
run_with_repo(&logger, &DEFAULT_CONFIG, &ctx.repo).unwrap();
702689

703690
let mut revwalk = ctx.repo.revwalk().unwrap();
704691
revwalk.push_head().unwrap();
@@ -720,8 +707,7 @@ mod tests {
720707
// run 'git-absorb'
721708
let drain = slog::Discard;
722709
let logger = slog::Logger::root(drain, o!());
723-
let config = default_config(&logger);
724-
run_with_repo(&config, &ctx.repo).unwrap();
710+
run_with_repo(&logger, &DEFAULT_CONFIG, &ctx.repo).unwrap();
725711
assert!(nothing_left_in_index(&ctx.repo).unwrap());
726712

727713
let mut revwalk = ctx.repo.revwalk().unwrap();
@@ -736,16 +722,13 @@ mod tests {
736722
assert_eq!(actual_msg, expected_msg);
737723
}
738724

739-
pub fn default_config(logger: &slog::Logger) -> Config {
740-
Config {
741-
dry_run: false,
742-
force_author: false,
743-
force: false,
744-
base: None,
745-
and_rebase: false,
746-
whole_file: false,
747-
one_fixup_per_commit: false,
748-
logger,
749-
}
750-
}
725+
const DEFAULT_CONFIG: Config = Config {
726+
dry_run: false,
727+
force_author: false,
728+
force: false,
729+
base: None,
730+
and_rebase: false,
731+
whole_file: false,
732+
one_fixup_per_commit: false,
733+
};
751734
}

src/main.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,18 @@ fn main() {
8989
));
9090
}
9191

92-
if let Err(e) = git_absorb::run(&git_absorb::Config {
93-
dry_run,
94-
force_author,
95-
force,
96-
base: base.as_deref(),
97-
and_rebase,
98-
whole_file,
99-
one_fixup_per_commit,
100-
logger: &logger,
101-
}) {
92+
if let Err(e) = git_absorb::run(
93+
&logger,
94+
&git_absorb::Config {
95+
dry_run,
96+
force_author,
97+
force,
98+
base: base.as_deref(),
99+
and_rebase,
100+
whole_file,
101+
one_fixup_per_commit,
102+
},
103+
) {
102104
crit!(logger, "absorb failed"; "err" => e.to_string());
103105
// wait for async logger to finish writing messages
104106
drop(logger);

0 commit comments

Comments
 (0)