Skip to content

Commit 6fcb00d

Browse files
committed
Have git run rebase from repository root
From [a suggestion made in tummychow#156](tummychow#156 (comment)). In addition to ensuring we're running from the right directory in production, makes it easier to write tests that rebase.
1 parent bc179c5 commit 6fcb00d

File tree

4 files changed

+22
-25
lines changed

4 files changed

+22
-25
lines changed

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,4 @@ memchr = "2.3"
3535
anyhow = "1.0"
3636

3737
[dev-dependencies]
38-
current_dir = "0.1.0"
3938
tempfile = "3.1"

src/lib.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod owned;
88
mod stack;
99

1010
use std::io::Write;
11+
use std::path::Path;
1112

1213
pub struct Config<'a> {
1314
pub dry_run: bool,
@@ -372,6 +373,25 @@ fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository
372373
assert!(number_of_parents <= 1);
373374

374375
let mut command = Command::new("git");
376+
377+
// We'd generally expect to be run from within the repository, but just in case,
378+
// try to have git run rebase from the repository root.
379+
// This simplifies writing tests that execute from within git-absorb's source directory
380+
// but operate on temporary repositories created elsewhere.
381+
// (The tests could explicitly change directories, but then must be serialized.)
382+
let repo_path = repo.path().parent().map(Path::to_str).flatten();
383+
match repo_path {
384+
Some(path) => {
385+
command.args(["-C", path]);
386+
}
387+
_ => {
388+
warn!(
389+
logger,
390+
"Could not determine repository path for rebase. Running in current directory."
391+
);
392+
}
393+
}
394+
375395
command.args(["rebase", "--interactive", "--autosquash", "--autostash"]);
376396

377397
for arg in config.rebase_options {
@@ -687,7 +707,7 @@ mod tests {
687707
and_rebase: true,
688708
..DEFAULT_CONFIG
689709
};
690-
repo_utils::run_in_repo(&ctx, || run_with_repo(&logger, &config, &ctx.repo)).unwrap();
710+
run_with_repo(&logger, &config, &ctx.repo).unwrap();
691711

692712
let mut revwalk = ctx.repo.revwalk().unwrap();
693713
revwalk.push_head().unwrap();
@@ -709,7 +729,7 @@ mod tests {
709729
rebase_options: &vec!["--signoff"],
710730
..DEFAULT_CONFIG
711731
};
712-
repo_utils::run_in_repo(&ctx, || run_with_repo(&logger, &config, &ctx.repo)).unwrap();
732+
run_with_repo(&logger, &config, &ctx.repo).unwrap();
713733

714734
let mut revwalk = ctx.repo.revwalk().unwrap();
715735
revwalk.push_head().unwrap();

src/tests/repo_utils.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#[cfg(test)]
2-
use anyhow::Result;
3-
use current_dir::Cwd;
42
use std::path::{Path, PathBuf};
53
pub struct Context {
64
pub repo: git2::Repository,
@@ -81,19 +79,6 @@ pub fn set_config_option(repo: &git2::Repository, name: &str, value: &str) {
8179
repo.config().unwrap().set_str(name, value).unwrap();
8280
}
8381

84-
/// Run a function while in the working directory of the repository.
85-
///
86-
/// Can be used to ensure that at most one test changes the working
87-
/// directory at a time, preventing clashes.
88-
pub fn run_in_repo<F>(ctx: &Context, f: F) -> Result<()>
89-
where
90-
F: FnOnce() -> Result<()>,
91-
{
92-
let mut locked_cwd = Cwd::mutex().lock().unwrap();
93-
locked_cwd.set(ctx.dir.path()).unwrap();
94-
f()
95-
}
96-
9782
/// Become a new author - set the user.name and user.email config options.
9883
pub fn become_author(repo: &git2::Repository, name: &str, email: &str) {
9984
let mut config = repo.config().unwrap();

0 commit comments

Comments
 (0)