Skip to content

Commit

Permalink
Merge pull request #416 from epage/fix
Browse files Browse the repository at this point in the history
fix: Skip allowed prefixes
  • Loading branch information
epage authored Dec 19, 2024
2 parents 79254d6 + c1e805b commit 0f05e16
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 15 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ jobs:
toolchain: ${{ matrix.rust }}
- uses: Swatinem/rust-cache@v2
- uses: taiki-e/install-action@cargo-hack
- name: Configure git
run: |
git config --global user.name "Test User"
git config --global user.email "[email protected]"
- name: Build
run: cargo test --workspace --no-run
- name: Test
Expand Down Expand Up @@ -149,6 +153,10 @@ jobs:
with:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- name: Configure git
run: |
git config --global user.name "Test User"
git config --global user.email "[email protected]"
- name: Install cargo-tarpaulin
run: cargo install cargo-tarpaulin
- name: Gather coverage
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/rust-next.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ jobs:
with:
toolchain: ${{ matrix.rust }}
- uses: Swatinem/rust-cache@v2
- name: Configure git
run: |
git config --global user.name "Test User"
git config --global user.email "[email protected]"
- uses: taiki-e/install-action@cargo-hack
- name: Build
run: cargo test --workspace --no-run
Expand All @@ -52,6 +56,10 @@ jobs:
with:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- name: Configure git
run: |
git config --global user.name "Test User"
git config --global user.email "[email protected]"
- uses: taiki-e/install-action@cargo-hack
- name: Update dependencies
run: cargo update
Expand Down
12 changes: 11 additions & 1 deletion crates/committed/src/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ pub(crate) fn check_message(
if config.no_wip() {
failed |= check_wip(source, message, report)?;
}
message = strip_wip(message);
if config.no_fixup() {
failed |= check_fixup(source, message, report)?;
message = strip_fixup(message);
}
message = strip_fixup(message);
// Bail out due to above checks
if failed {
return Ok(failed);
Expand Down Expand Up @@ -344,6 +345,15 @@ pub(crate) fn check_wip(
}
}

pub(crate) fn strip_wip(message: &str) -> &str {
let Some(c) = WIP_RE.captures(message) else {
return message;
};

let matched = c.get(0).unwrap();
message[matched.len()..].trim_start()
}

const FIXUP_PREFIXES: [&str; 3] = ["fixup! ", "squash! ", "amend! "];

pub(crate) fn check_fixup(
Expand Down
17 changes: 11 additions & 6 deletions crates/committed/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,19 @@ fn run() -> proc_exit::ExitResult {

init_logging(options.verbose.log_level());

let repo = options
.work_tree
.canonicalize()
.with_code(proc_exit::sysexits::USAGE_ERR)?;
let repo = || {
let repo = options
.work_tree
.canonicalize()
.with_code(proc_exit::sysexits::USAGE_ERR)?;

git2::Repository::discover(repo).with_code(proc_exit::sysexits::USAGE_ERR)
};

let repo = git2::Repository::discover(repo).with_code(proc_exit::sysexits::USAGE_ERR)?;
let mut config = if let Some(config_path) = options.config.as_ref() {
load_toml(config_path).with_code(proc_exit::sysexits::CONFIG_ERR)?
} else {
let config_path = repo
let config_path = repo()?
.workdir()
.ok_or_else(|| anyhow::anyhow!("Cannot work on bare repo"))
.with_code(proc_exit::sysexits::USAGE_ERR)?
Expand Down Expand Up @@ -232,6 +235,7 @@ fn run() -> proc_exit::ExitResult {
failed |= checks::check_message(path.as_path().into(), text, &config, report)
.with_code(UNKNOWN_ERR)?;
} else if let Some(commits) = options.commits.as_ref() {
let repo = repo()?;
let revspec =
git::RevSpec::parse(&repo, commits).with_code(proc_exit::sysexits::USAGE_ERR)?;
for commit in revspec.iter() {
Expand Down Expand Up @@ -266,6 +270,7 @@ fn run() -> proc_exit::ExitResult {
.with_code(UNKNOWN_ERR)?;
} else {
debug_assert_eq!(options.commits, None);
let repo = repo()?;
let commit = repo
.head()
.with_code(proc_exit::sysexits::USAGE_ERR)?
Expand Down
129 changes: 121 additions & 8 deletions crates/committed/tests/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,138 @@
use snapbox::str;

#[test]
fn wip() {
fn has_message_fails() {
run_committed("", "")
.code(1)
.stdout_eq(str![[r#"
-: error Empty commits are disallowed
"#]])
.stderr_eq(str![]);
}

#[test]
fn wip_fails() {
run_committed("wip bad times ahead", "")
.code(1)
.stdout_eq(str![[r#"
-: error Work-in-progress commits must be cleaned up
"#]])
.stderr_eq(str![]);
}

#[test]
fn wip_config_override() {
run_committed("wip bad times ahead", "no_wip = false")
.code(1)
.stdout_eq(str![[r#"
-: error Subject should be capitalized but found `bad`
"#]])
.stderr_eq(str![]);
}

#[test]
fn wip_stops_checks() {
run_committed("wip bad times", "")
.code(1)
.stdout_eq(str![[r#"
-: error Work-in-progress commits must be cleaned up
"#]])
.stderr_eq(str![]);
}

#[test]
fn fixup_fails() {
run_committed("fixup! bad times ahead", "")
.code(1)
.stdout_eq(str![[r#"
-: error Fixup commits must be squashed
"#]])
.stderr_eq(str![]);
}

#[test]
fn fixup_config_override() {
run_committed("fixup! bad times ahead", "no_fixup = false")
.code(1)
.stdout_eq(str![[r#"
-: error Subject should be capitalized but found `bad`
"#]])
.stderr_eq(str![]);
}

#[test]
fn fixup_stops_checks() {
run_committed("fixup! bad times", "")
.code(1)
.stdout_eq(str![[r#"
-: error Fixup commits must be squashed
"#]])
.stderr_eq(str![]);
}

#[track_caller]
fn run_committed(message: &str, config: &str) -> snapbox::cmd::OutputAssert {
let root = snapbox::dir::DirRoot::mutable_temp().unwrap();
let root_dir = root.path().unwrap();
std::fs::write(root_dir.join("committed.toml"), "").unwrap();

git2::Repository::init(root_dir).unwrap();
let config_path = root_dir.join("committed.toml");
std::fs::write(&config_path, config).unwrap();

let message = "WIP: bad times ahead";
snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("committed"))
let assert = snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("committed"))
.arg("--commit-file=-")
.arg("--config")
.arg(&config_path)
.current_dir(root_dir)
.stdin(message)
.assert()
.assert();

root.close().unwrap();

assert
}

#[test]
fn in_repo() {
run_committed_repo("WIP: bad times ahead", "")
.code(1)
.stdout_eq(str![[r#"
-: error Work-in-progress commits must be cleaned up
[..]: error Work-in-progress commits must be cleaned up
"#]])
.stderr_eq(str![]);
}

fn run_committed_repo(message: &str, config: &str) -> snapbox::cmd::OutputAssert {
let root = snapbox::dir::DirRoot::mutable_temp().unwrap();
let root_dir = root.path().unwrap();
let config_filename = "committed.toml";
let config_path = root_dir.join(config_filename);
std::fs::write(&config_path, config).unwrap();

let repo = git2::Repository::init(root_dir).unwrap();
let mut index = repo.index().unwrap();
index
.add_path(std::path::Path::new(config_filename))
.unwrap();
index.write().unwrap();
let id = index.write_tree().unwrap();
let tree = repo.find_tree(id).unwrap();
let sig = repo.signature().unwrap();
repo.commit(Some("HEAD"), &sig, &sig, message, &tree, &[])
.unwrap();

let assert = snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("committed"))
.arg("HEAD")
.current_dir(root_dir)
.assert();

root.close().unwrap();

assert
}

0 comments on commit 0f05e16

Please sign in to comment.